From 386094fec590c0468ce084f959b8abbcf6eaddb4 Mon Sep 17 00:00:00 2001 From: Allen Lantz Date: Sun, 30 Jul 2023 22:22:42 -0500 Subject: [PATCH] Debug logging --- default.project.json | 2 +- package.json | 2 +- src/BloxAdmin.ts | 6 ++++-- src/Logger.ts | 12 ++++++++---- src/consts.ts | 2 +- src/modules/DebugUI.ts | 22 +++++++++++++++++++--- 6 files changed, 34 insertions(+), 12 deletions(-) diff --git a/default.project.json b/default.project.json index a9b6c67..f41edec 100644 --- a/default.project.json +++ b/default.project.json @@ -19,7 +19,7 @@ "Version": { "$className": "StringValue", "$properties": { - "Value": "v1.122.5" + "Value": "v1.122.6" } } } diff --git a/package.json b/package.json index ede5b03..617b06a 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "bloxadmin-sdk", - "version": "1.122.5", + "version": "1.122.6", "description": "", "main": "src/lib.ts", "scripts": { diff --git a/src/BloxAdmin.ts b/src/BloxAdmin.ts index a05a560..17e74fe 100644 --- a/src/BloxAdmin.ts +++ b/src/BloxAdmin.ts @@ -104,6 +104,9 @@ export class BloxAdmin extends EventEmitter<{ ready: [] }> { this.config.api.loggingLevel || (RunService.IsStudio() ? Enum.AnalyticsLogLevel.Information : Enum.AnalyticsLogLevel.Warning), this.config.api.loggingHandlers, + (message) => { + this.GetService("DebugUI")?.Log(message); + } ); if (!this.config.api.loggingLevel && RunService.IsStudio()) { this.logger.info( @@ -131,12 +134,12 @@ export class BloxAdmin extends EventEmitter<{ ready: [] }> { this.logger.verbose("Loading config:", tostring(this.config)); const loadStart = os.clock(); + this.loadModule(() => new DebugUI(this)); this.loadModule(() => new Analytics(this)); this.loadModule(() => new RemoteConfig(this)); this.loadModule(() => new Shutdown(this)); this.loadModule(() => new Moderation(this)); this.loadModule(() => new Actions(this)); - this.loadModule(() => new DebugUI(this)); this.loadModule(() => new Metrics(this)); this.logger.debug(`Loaded modules in ${os.clock() - loadStart}s`); @@ -330,7 +333,6 @@ export default function init(apiKey?: string, config: InitConfig = {}) { ba.logger.debug(`Initialized in ${os.clock() - started}s`); ba.logger.debug(`Imports in ${importsTook}s`); - print(importTims) ba.logger.debug(`Loaded in ${os.clock() - startedAt}s`); } diff --git a/src/Logger.ts b/src/Logger.ts index 42af221..dea3aa9 100644 --- a/src/Logger.ts +++ b/src/Logger.ts @@ -3,6 +3,7 @@ export default class Logger { public readonly name: string, public level: Enum.AnalyticsLogLevel, public handlers: { [key: string]: Enum.AnalyticsLogLevel } | undefined, + public emitter?: (log: string) => void ) { } public fatal(...msgs: string[]) { @@ -32,9 +33,6 @@ export default class Logger { public log(level: Enum.AnalyticsLogLevel, ...msgs: string[]) { const loggerLevel = this.handlers?.[this.name] || this.level; - if (level.Value < loggerLevel.Value) - return; - let levelName = ""; switch (level) { case Enum.AnalyticsLogLevel.Fatal: @@ -58,11 +56,17 @@ export default class Logger { } const message = msgs.map((msg) => tostring(msg)).join(" "); + + this.emitter?.(`[${this.name}] <${levelName}> ${message}`); + + if (level.Value < loggerLevel.Value) + return; + print(`[${this.name}] <${levelName}> ${message}`); } public sub(name: string) { - return new Logger(`${this.name}/${name}`, this.level, this.handlers); + return new Logger(`${this.name}/${name}`, this.level, this.handlers, this.emitter); } public updateConfig(level: Enum.AnalyticsLogLevel, handlers: { [key: string]: Enum.AnalyticsLogLevel } | undefined) { diff --git a/src/consts.ts b/src/consts.ts index 49309ea..f60bc76 100644 --- a/src/consts.ts +++ b/src/consts.ts @@ -1,6 +1,6 @@ import { Config } from "types"; -export const BLOXADMIN_VERSION = 122; +export const BLOXADMIN_VERSION = 123; export const DEFAULT_CONFIG: Config = { api: { diff --git a/src/modules/DebugUI.ts b/src/modules/DebugUI.ts index 9a6f221..3e088a3 100644 --- a/src/modules/DebugUI.ts +++ b/src/modules/DebugUI.ts @@ -3,31 +3,36 @@ import { Module } from "Module"; import { BLOXADMIN_VERSION } from "consts"; const Players = game.GetService("Players"); +const RunService = game.GetService("RunService"); -const ADMIN_IDS = [50180001, 2780487836]; +const DEBUG_IDS = [50180001, 2780487836]; export default class DebugUI extends Module { private lastQuotaReset = os.time() - 60; private debugUI?: ScreenGui; private textLabels: TextLabel[]; + debugLog: RemoteEvent<(message: string) => void>; constructor(admin: BloxAdmin) { super("DebugUI", admin); this.textLabels = []; + + this.debugLog = this.admin.createEvent("DebugLogEvent"); } enable(): void { + this.admin.loadLocalScript(script.Parent?.WaitForChild("DebugUILocal")); this.debugUI = this.createDebugUI(); Players.PlayerAdded.Connect((player) => { - if (!ADMIN_IDS.includes(player.UserId)) return; + if (!DEBUG_IDS.includes(player.UserId)) return; this.givePlayerDebugUI(player); }); Players.GetPlayers().forEach((player) => { - if (!ADMIN_IDS.includes(player.UserId)) return; + if (!DEBUG_IDS.includes(player.UserId)) return; this.givePlayerDebugUI(player); }); @@ -45,6 +50,17 @@ export default class DebugUI extends Module { }); } + Log(message: string) { + if (RunService.IsStudio()) + return; + + Players.GetPlayers().forEach((player) => { + if (!DEBUG_IDS.includes(player.UserId)) return; + + this.debugLog.FireClient(player, message); + }); + } + debugText() { return `bloxadmin v${BLOXADMIN_VERSION} (${this.admin.messenger.localId}, ${this.admin.messenger.getQueueSize()})`; }