Skip to content

Commit

Permalink
Debug logging
Browse files Browse the repository at this point in the history
  • Loading branch information
AL1L committed Jul 31, 2023
1 parent 5a1a470 commit 386094f
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 12 deletions.
2 changes: 1 addition & 1 deletion default.project.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"Version": {
"$className": "StringValue",
"$properties": {
"Value": "v1.122.5"
"Value": "v1.122.6"
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "bloxadmin-sdk",
"version": "1.122.5",
"version": "1.122.6",
"description": "",
"main": "src/lib.ts",
"scripts": {
Expand Down
6 changes: 4 additions & 2 deletions src/BloxAdmin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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`);

Expand Down Expand Up @@ -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`);
}

Expand Down
12 changes: 8 additions & 4 deletions src/Logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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[]) {
Expand Down Expand Up @@ -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:
Expand All @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion src/consts.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Config } from "types";

export const BLOXADMIN_VERSION = 122;
export const BLOXADMIN_VERSION = 123;

export const DEFAULT_CONFIG: Config = {
api: {
Expand Down
22 changes: 19 additions & 3 deletions src/modules/DebugUI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
Expand All @@ -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()})`;
}
Expand Down

0 comments on commit 386094f

Please sign in to comment.