mirror of
https://github.com/tobspr/shapez.io.git
synced 2025-12-11 09:11:50 +00:00
Introduce new Logger implementation
Remove old Logger class and replace it with a new implementation that keeps the location of the log calls (for display in devtools). Remove internals of the old implementation and their one external usage.
This commit is contained in:
parent
ce1cab3065
commit
2240c00090
@ -1,111 +0,0 @@
|
|||||||
import { globalConfig } from "../core/config";
|
|
||||||
|
|
||||||
/*
|
|
||||||
Logging functions
|
|
||||||
- To be extended
|
|
||||||
*/
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Base logger class
|
|
||||||
*/
|
|
||||||
class Logger {
|
|
||||||
constructor(context) {
|
|
||||||
this.context = context;
|
|
||||||
}
|
|
||||||
|
|
||||||
debug(...args) {
|
|
||||||
globalDebug(this.context, ...args);
|
|
||||||
}
|
|
||||||
|
|
||||||
log(...args) {
|
|
||||||
globalLog(this.context, ...args);
|
|
||||||
}
|
|
||||||
|
|
||||||
warn(...args) {
|
|
||||||
globalWarn(this.context, ...args);
|
|
||||||
}
|
|
||||||
|
|
||||||
error(...args) {
|
|
||||||
globalError(this.context, ...args);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
export function createLogger(context) {
|
|
||||||
return new Logger(context);
|
|
||||||
}
|
|
||||||
|
|
||||||
export function globalDebug(context, ...args) {
|
|
||||||
if (G_IS_DEV) {
|
|
||||||
logInternal(context, console.log, prepareArgsForLogging(args));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
export function globalLog(context, ...args) {
|
|
||||||
logInternal(context, console.log, prepareArgsForLogging(args));
|
|
||||||
}
|
|
||||||
|
|
||||||
export function globalWarn(context, ...args) {
|
|
||||||
logInternal(context, console.warn, prepareArgsForLogging(args));
|
|
||||||
}
|
|
||||||
|
|
||||||
export function globalError(context, ...args) {
|
|
||||||
args = prepareArgsForLogging(args);
|
|
||||||
logInternal(context, console.error, args);
|
|
||||||
}
|
|
||||||
|
|
||||||
function prepareArgsForLogging(args) {
|
|
||||||
let result = [];
|
|
||||||
for (let i = 0; i < args.length; ++i) {
|
|
||||||
result.push(args[i]);
|
|
||||||
}
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
export function logSection(name, color) {
|
|
||||||
while (name.length <= 14) {
|
|
||||||
name = " " + name + " ";
|
|
||||||
}
|
|
||||||
name = name.padEnd(19, " ");
|
|
||||||
|
|
||||||
const lineCss =
|
|
||||||
"letter-spacing: -3px; color: " + color + "; font-size: 6px; background: #eee; color: #eee;";
|
|
||||||
const line = "%c----------------------------";
|
|
||||||
console.log("\n" + line + " %c" + name + " " + line + "\n", lineCss, "color: " + color, lineCss);
|
|
||||||
}
|
|
||||||
|
|
||||||
function extractHandleContext(handle) {
|
|
||||||
let context = handle || "unknown";
|
|
||||||
if (handle && handle.constructor && handle.constructor.name) {
|
|
||||||
context = handle.constructor.name;
|
|
||||||
if (context === "String") {
|
|
||||||
context = handle;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (handle && handle.name) {
|
|
||||||
context = handle.name;
|
|
||||||
}
|
|
||||||
return context + "";
|
|
||||||
}
|
|
||||||
|
|
||||||
function logInternal(handle, consoleMethod, args) {
|
|
||||||
const context = extractHandleContext(handle).padEnd(20, " ");
|
|
||||||
const labelColor = handle && handle.LOG_LABEL_COLOR ? handle.LOG_LABEL_COLOR : "#aaa";
|
|
||||||
|
|
||||||
if (G_IS_DEV && globalConfig.debug.logTimestamps) {
|
|
||||||
const timestamp = "⏱ %c" + (Math.floor(performance.now()) + "").padEnd(6, " ") + "";
|
|
||||||
consoleMethod.call(
|
|
||||||
console,
|
|
||||||
timestamp + " %c" + context,
|
|
||||||
"color: #7f7;",
|
|
||||||
"color: " + labelColor + ";",
|
|
||||||
...args
|
|
||||||
);
|
|
||||||
} else {
|
|
||||||
// if (G_IS_DEV && !globalConfig.debug.disableLoggingLogSources) {
|
|
||||||
consoleMethod.call(console, "%c" + context, "color: " + labelColor, ...args);
|
|
||||||
// } else {
|
|
||||||
// consoleMethod.call(console, ...args);
|
|
||||||
// }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
60
src/js/core/logging.ts
Normal file
60
src/js/core/logging.ts
Normal file
@ -0,0 +1,60 @@
|
|||||||
|
export class Logger {
|
||||||
|
/**
|
||||||
|
* A simple {@link console} wrapper that retains the location of log calls.
|
||||||
|
* @param context Label to be displayed in each log message
|
||||||
|
* @param color Optional label color override
|
||||||
|
* @param debug Whether to log {@link Logger.debug} messages
|
||||||
|
*/
|
||||||
|
constructor(context: string, color = "#aaa", debug = G_IS_DEV) {
|
||||||
|
const label = "%c" + context.padEnd(20, " ");
|
||||||
|
const style = `color: ${color}`;
|
||||||
|
|
||||||
|
if (debug) {
|
||||||
|
this.debug = console.debug.bind(console, label, style);
|
||||||
|
}
|
||||||
|
|
||||||
|
this.log = console.log.bind(console, label, style);
|
||||||
|
this.warn = console.warn.bind(console, label, style);
|
||||||
|
this.error = console.error.bind(console, label, style);
|
||||||
|
}
|
||||||
|
|
||||||
|
// @ts-expect-error parameters are actually used
|
||||||
|
debug(...args: unknown[]) {}
|
||||||
|
// @ts-expect-error same
|
||||||
|
log(...args: unknown[]) {}
|
||||||
|
// @ts-expect-error same
|
||||||
|
warn(...args: unknown[]) {}
|
||||||
|
// @ts-expect-error same
|
||||||
|
error(...args: unknown[]) {}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @deprecated Use the {@link Logger} constructor instead
|
||||||
|
* @param handle Object to be used as the logger label
|
||||||
|
* @returns A {@link Logger} instance
|
||||||
|
*/
|
||||||
|
export function createLogger(handle: unknown) {
|
||||||
|
const context = extractHandleContext(handle);
|
||||||
|
return new Logger(context);
|
||||||
|
}
|
||||||
|
|
||||||
|
export function logSection(name, color) {
|
||||||
|
while (name.length <= 14) {
|
||||||
|
name = " " + name + " ";
|
||||||
|
}
|
||||||
|
name = name.padEnd(19, " ");
|
||||||
|
|
||||||
|
const lineCss =
|
||||||
|
"letter-spacing: -3px; color: " + color + "; font-size: 6px; background: #eee; color: #eee;";
|
||||||
|
const line = "%c----------------------------";
|
||||||
|
console.log("\n" + line + " %c" + name + " " + line + "\n", lineCss, "color: " + color, lineCss);
|
||||||
|
}
|
||||||
|
|
||||||
|
function extractHandleContext(handle: unknown) {
|
||||||
|
handle ??= "unknown";
|
||||||
|
if (typeof handle === "string") {
|
||||||
|
return handle;
|
||||||
|
}
|
||||||
|
|
||||||
|
return handle.constructor.name;
|
||||||
|
}
|
||||||
@ -1,5 +1,5 @@
|
|||||||
import { gMetaBuildingRegistry } from "../../../core/global_registries";
|
import { gMetaBuildingRegistry } from "../../../core/global_registries";
|
||||||
import { globalWarn } from "../../../core/logging";
|
import { Logger } from "../../../core/logging";
|
||||||
import { STOP_PROPAGATION } from "../../../core/signal";
|
import { STOP_PROPAGATION } from "../../../core/signal";
|
||||||
import { makeDiv, safeModulo } from "../../../core/utils";
|
import { makeDiv, safeModulo } from "../../../core/utils";
|
||||||
import { MetaBlockBuilding } from "../../buildings/block";
|
import { MetaBlockBuilding } from "../../buildings/block";
|
||||||
@ -12,6 +12,8 @@ import { GameRoot } from "../../root";
|
|||||||
import { BaseHUDPart } from "../base_hud_part";
|
import { BaseHUDPart } from "../base_hud_part";
|
||||||
import { DynamicDomAttach } from "../dynamic_dom_attach";
|
import { DynamicDomAttach } from "../dynamic_dom_attach";
|
||||||
|
|
||||||
|
const logger = new Logger("hud/base_toolbar");
|
||||||
|
|
||||||
export class HUDBaseToolbar extends BaseHUDPart {
|
export class HUDBaseToolbar extends BaseHUDPart {
|
||||||
/**
|
/**
|
||||||
* @param {GameRoot} root
|
* @param {GameRoot} root
|
||||||
@ -106,7 +108,9 @@ export class HUDBaseToolbar extends BaseHUDPart {
|
|||||||
const binding = actionMapper.getBinding(rawBinding);
|
const binding = actionMapper.getBinding(rawBinding);
|
||||||
binding.add(() => this.selectBuildingForPlacement(metaBuilding));
|
binding.add(() => this.selectBuildingForPlacement(metaBuilding));
|
||||||
} else {
|
} else {
|
||||||
globalWarn("Building has no keybinding:", metaBuilding.getId());
|
// FIXME: This check shouldn't be here. Once registries rework is done,
|
||||||
|
// check for keybindings while finalizing the buildings registry
|
||||||
|
logger.warn("Building has no keybinding:", metaBuilding.getId());
|
||||||
}
|
}
|
||||||
|
|
||||||
const itemContainer = makeDiv(
|
const itemContainer = makeDiv(
|
||||||
|
|||||||
@ -1,7 +1,7 @@
|
|||||||
import { SavegameInterface_V1000 } from "./1000.js";
|
import { createLogger } from "../../core/logging";
|
||||||
import { createLogger } from "../../core/logging.js";
|
|
||||||
import { T } from "../../translations.js";
|
import { T } from "../../translations.js";
|
||||||
import { TypeVector, TypeNumber, TypeString, TypeNullable } from "../serialization_data_types.js";
|
import { TypeNumber, TypeVector } from "../serialization_data_types.js";
|
||||||
|
import { SavegameInterface_V1000 } from "./1000.js";
|
||||||
|
|
||||||
import schema from "./1001.json";
|
import schema from "./1001.json";
|
||||||
|
|
||||||
|
|||||||
@ -1,5 +1,4 @@
|
|||||||
import { createLogger } from "../../core/logging.js";
|
import { createLogger } from "../../core/logging";
|
||||||
import { T } from "../../translations.js";
|
|
||||||
import { SavegameInterface_V1001 } from "./1001.js";
|
import { SavegameInterface_V1001 } from "./1001.js";
|
||||||
|
|
||||||
import schema from "./1002.json";
|
import schema from "./1002.json";
|
||||||
|
|||||||
@ -1,4 +1,4 @@
|
|||||||
import { createLogger } from "../../core/logging.js";
|
import { createLogger } from "../../core/logging";
|
||||||
import { SavegameInterface_V1002 } from "./1002.js";
|
import { SavegameInterface_V1002 } from "./1002.js";
|
||||||
|
|
||||||
import schema from "./1003.json";
|
import schema from "./1003.json";
|
||||||
|
|||||||
@ -1,4 +1,4 @@
|
|||||||
import { createLogger } from "../../core/logging.js";
|
import { createLogger } from "../../core/logging";
|
||||||
import { SavegameInterface_V1003 } from "./1003.js";
|
import { SavegameInterface_V1003 } from "./1003.js";
|
||||||
|
|
||||||
import schema from "./1004.json";
|
import schema from "./1004.json";
|
||||||
|
|||||||
@ -1,4 +1,4 @@
|
|||||||
import { createLogger } from "../../core/logging.js";
|
import { createLogger } from "../../core/logging";
|
||||||
import { SavegameInterface_V1004 } from "./1004.js";
|
import { SavegameInterface_V1004 } from "./1004.js";
|
||||||
|
|
||||||
import schema from "./1005.json";
|
import schema from "./1005.json";
|
||||||
|
|||||||
@ -1,5 +1,5 @@
|
|||||||
import { gMetaBuildingRegistry } from "../../core/global_registries";
|
import { gMetaBuildingRegistry } from "../../core/global_registries";
|
||||||
import { createLogger } from "../../core/logging.js";
|
import { createLogger } from "../../core/logging";
|
||||||
import { getCodeFromBuildingData } from "../../game/building_codes.js";
|
import { getCodeFromBuildingData } from "../../game/building_codes.js";
|
||||||
import { enumBalancerVariants, MetaBalancerBuilding } from "../../game/buildings/balancer.js";
|
import { enumBalancerVariants, MetaBalancerBuilding } from "../../game/buildings/balancer.js";
|
||||||
import { MetaBeltBuilding } from "../../game/buildings/belt.js";
|
import { MetaBeltBuilding } from "../../game/buildings/belt.js";
|
||||||
|
|||||||
@ -1,4 +1,4 @@
|
|||||||
import { createLogger } from "../../core/logging.js";
|
import { createLogger } from "../../core/logging";
|
||||||
import { SavegameInterface_V1006 } from "./1006.js";
|
import { SavegameInterface_V1006 } from "./1006.js";
|
||||||
|
|
||||||
import schema from "./1007.json";
|
import schema from "./1007.json";
|
||||||
|
|||||||
@ -1,4 +1,4 @@
|
|||||||
import { createLogger } from "../../core/logging.js";
|
import { createLogger } from "../../core/logging";
|
||||||
import { SavegameInterface_V1007 } from "./1007.js";
|
import { SavegameInterface_V1007 } from "./1007.js";
|
||||||
|
|
||||||
import schema from "./1008.json";
|
import schema from "./1008.json";
|
||||||
|
|||||||
@ -1,4 +1,4 @@
|
|||||||
import { createLogger } from "../../core/logging.js";
|
import { createLogger } from "../../core/logging";
|
||||||
import { RegularGameMode } from "../../game/modes/regular.js";
|
import { RegularGameMode } from "../../game/modes/regular.js";
|
||||||
import { SavegameInterface_V1008 } from "./1008.js";
|
import { SavegameInterface_V1008 } from "./1008.js";
|
||||||
|
|
||||||
|
|||||||
@ -1,4 +1,4 @@
|
|||||||
import { createLogger } from "../../core/logging.js";
|
import { createLogger } from "../../core/logging";
|
||||||
import { SavegameInterface_V1009 } from "./1009.js";
|
import { SavegameInterface_V1009 } from "./1009.js";
|
||||||
|
|
||||||
import schema from "./1010.json";
|
import schema from "./1010.json";
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user