Skip to content

Commit

Permalink
Merge pull request #89 from KotRikD/chore/replace-logger
Browse files Browse the repository at this point in the history
Remove `winston`
  • Loading branch information
KotRikD authored Mar 5, 2024
2 parents 654f28f + 2b058d7 commit 5560e10
Show file tree
Hide file tree
Showing 20 changed files with 120 additions and 243 deletions.
3 changes: 1 addition & 2 deletions packages/common/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
},
"dependencies": {
"adm-zip": "^0.5.10",
"dotenv": "^16.0.3",
"winston": "^3.8.2"
"dotenv": "^16.0.3"
}
}
5 changes: 1 addition & 4 deletions packages/common/utils/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as dotenv from 'dotenv';
import fs from 'fs';
import path from 'path';

import { configureLogger, wLogger } from './logger';
import { wLogger } from './logger';

const configPath = path.join(
'pkg' in process ? path.dirname(process.execPath) : process.cwd(),
Expand Down Expand Up @@ -115,8 +115,6 @@ export const updateConfigFile = () => {
};

export const watchConfigFile = ({ httpServer }: { httpServer: any }) => {
configureLogger();

refreshConfig(httpServer, false);
updateConfigFile();

Expand Down Expand Up @@ -181,7 +179,6 @@ export const refreshConfig = (httpServer: any, refresh: boolean) => {
}

if (updated) wLogger.info(`Config ${status}ed`);
configureLogger();
};

export const writeConfig = (httpServer: any, text: string) => {
Expand Down
51 changes: 28 additions & 23 deletions packages/common/utils/logger.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,33 @@
import winston, { format, transports } from 'winston';

import { config } from './config';

const { timestamp, label, printf } = format;
const colors = {
info: '\x1b[1m\x1b[40m\x1b[42m',
error: '\x1b[1m\x1b[37m\x1b[41m',
debug: '\x1b[1m\x1b[37m\x1b[44m',
warn: '\x1b[1m\x1b[40m\x1b[43m',
reset: '\x1b[0m',
grey: '\x1b[90m'
};

function colorText(status: string, ...anything: any) {
const colorCode = colors[status] || colors.reset;
const timestamp = new Date().toISOString().split('T')[1].replace('Z', '');

const customFormat = printf(({ level, message, label, timestamp }) => {
return `${timestamp} [${label}] ${level}: ${message}`;
});
const time = `${colors.grey}${timestamp}${colors.reset}`;
console.log(
time,
`${colorCode} ${status.toUpperCase()} ${colors.reset}`,
...anything
);
}

export const configureLogger = () =>
winston.configure({
level: config.debugLogging ? 'debug' : 'info',
transports: [
//
// - Write to all logs with specified level to console.
new transports.Console({
format: format.combine(
format.colorize(),
label({ label: 'tosu' }),
timestamp(),
customFormat
)
})
]
});
export const wLogger = {
info: (...args: any) => colorText('info', ...args),
debug: (...args: any) => {
if (config.debugLogging != true) return;

export const wLogger = winston;
colorText('debug', ...args);
},
error: (...args: any) => colorText('error', ...args),
warn: (...args: any) => colorText('warn', ...args)
};
6 changes: 3 additions & 3 deletions packages/game-overlay/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { downloadFile } from '@tosu/common';
import { downloadFile, wLogger } from '@tosu/common';
import decompress from 'decompress';
import { execFile } from 'node:child_process';
import { existsSync, writeFileSync } from 'node:fs';
Expand Down Expand Up @@ -53,7 +53,7 @@ export const injectGameOverlay = async (p: Process) => {
path.join(process.cwd(), 'gameOverlay', 'gosumemoryoverlay.dll')
)
) {
console.log('Please delete gameOverlay folder, and restart program!');
wLogger.info('Please delete gameOverlay folder, and restart program!');
process.exit(1);
}

Expand All @@ -72,7 +72,7 @@ export const injectGameOverlay = async (p: Process) => {
reject(err);
});
child.on('exit', () => {
console.log(
wLogger.info(
'[gosu-overlay] initialized successfully, see https://github.com/l3lackShark/gosumemory/wiki/GameOverlay for tutorial'
);
resolve(true);
Expand Down
2 changes: 1 addition & 1 deletion packages/server/router/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ export default function buildBaseApi(app: HttpServer) {

exec(`start "" "${folderPath}"`, (err, stdout, stderr) => {
if (err) {
console.error('Error opening file explorer:', err);
wLogger.error('Error opening file explorer:', err);
return sendJson(res, {
error: `Error opening file explorer: ${err.message}`
});
Expand Down
6 changes: 3 additions & 3 deletions packages/server/utils/socket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,21 +40,21 @@ export class Websocket {
this.socket.on('connection', (ws: ModifiedWebsocket) => {
ws.id = getUniqueID();

wLogger.debug(`[${ws.id}](${this.clients.size}) >>> ws: CONNECTED`);
wLogger.debug(`WS(CONNECTED) >>> ${ws.id}`);

ws.on('close', (reason, description) => {
this.clients.delete(ws.id);

wLogger.debug(
`[${ws.id}](${this.clients.size}) >>> ws: CLOSED ${reason} [${description}]`
`WS(CLOSED) >>> ${ws.id}: ${reason} [${description}]`
);
});

ws.on('error', (reason, description) => {
this.clients.delete(ws.id);

wLogger.debug(
`[${ws.id}](${this.clients.size}) >>> ws: ERROR ${reason} [${description}]`
`WS(ERROR) >>> ${ws.id}: ${reason} [${description}]`
);
});

Expand Down
12 changes: 8 additions & 4 deletions packages/tosu/src/entities/AllTimesData/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -380,8 +380,10 @@ export class AllTimesData extends AbstractEntity {
}
}
} catch (exc) {
wLogger.error("can't update config state");
console.error(exc);
wLogger.error(
"ATD(updateConfigState) Can't update config state",
exc
);
}
}

Expand All @@ -404,8 +406,10 @@ export class AllTimesData extends AbstractEntity {
}
}
} catch (exc) {
wLogger.error("can't update binding state");
console.error(exc);
wLogger.error(
"ATD(updateBindingState) Can't update binding state",
exc
);
}
}

Expand Down
4 changes: 2 additions & 2 deletions packages/tosu/src/entities/BassDensityData/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export class BassDensityData extends AbstractEntity {
osuProcess.readInt(patterns.getPattern('rulesetsAddr') - 0xb) + 0x4
);
if (rulesetAddr === 0) {
wLogger.debug('rulesetAddr is zero');
wLogger.debug('BDD(updateState) rulesetAddr is zero');
return;
}

Expand All @@ -42,7 +42,7 @@ export class BassDensityData extends AbstractEntity {
const bassDensityLength = osuProcess.readInt(audioVelocityBase + 0x4);
if (bassDensityLength < 40) {
wLogger.debug(
'bassDensity length less than 40 (basically it have 1024 values)'
'BDD(updateState) bassDensity length less than 40 (basically it have 1024 values)'
);
return;
}
Expand Down
34 changes: 18 additions & 16 deletions packages/tosu/src/entities/BeatmapPpData/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,9 @@ export class BeatmapPPData extends AbstractEntity {

updateCurrentAttributes(stars: number, pp: number) {
wLogger.debug(
`maxPP -> ${this.currAttributes.maxThisPlayPP} pp -> ${pp} stars -> ${stars}`
`BPPD(updateCurrentAttributes) maxPP -> ${this.currAttributes.maxThisPlayPP.toFixed(
2
)} pp -> ${pp.toFixed(2)} stars -> ${stars.toFixed(2)}`
);
const maxThisPlayPP = Math.max(pp, this.currAttributes.maxThisPlayPP);

Expand Down Expand Up @@ -203,15 +205,15 @@ export class BeatmapPPData extends AbstractEntity {
hp: menuData.HP
});
} catch (_) {
wLogger.debug(`can't get map: ${mapPath}`);
wLogger.debug(`BPPD(updateMapMetadata) Can't get map: ${mapPath}`);
return;
}

const beatmap_check_time = performance.now();
wLogger.debug(
`(updateMapMetadata) Spend:${(
`BPPD(updateMapMetadata) [${(
beatmap_check_time - start_time
).toFixed(2)}ms on opening beatmap`
).toFixed(2)}ms] Spend on opening beatmap`
);

const calc = new Calculator();
Expand All @@ -230,9 +232,9 @@ export class BeatmapPPData extends AbstractEntity {

const calculation_time = performance.now();
wLogger.debug(
`(updateMapMetadata) Spend:${(
`BPPD(updateMapMetadata) [${(
calculation_time - beatmap_check_time
).toFixed(2)}ms on attributes & starins calculation`
).toFixed(2)}ms] Spend on attributes & strains calculation`
);

const resultStrains: BeatmapStrains = {
Expand Down Expand Up @@ -277,10 +279,10 @@ export class BeatmapPPData extends AbstractEntity {
const full = Math.round(lazerBeatmap.totalLength);

this.updateTimings(firstObj, full);
} catch (e) {
console.error(e);
} catch (exc) {
wLogger.error(
"Something happend, when we're tried to parse beatmap"
"BPPD(updateMapMetadata) Something happend, when we're tried to parse beatmap",
exc
);
return;
}
Expand All @@ -293,9 +295,9 @@ export class BeatmapPPData extends AbstractEntity {

const beatmap_parse_time = performance.now();
wLogger.debug(
`(updateMapMetadata) Spend:${(
`BPPD(updateMapMetadata) [${(
beatmap_parse_time - calculation_time
).toFixed(2)}ms on parsing beatmap`
).toFixed(2)}ms] Spend on parsing beatmap`
);

const LEFT_OFFSET = Math.floor(firstObj / offset);
Expand Down Expand Up @@ -367,9 +369,9 @@ export class BeatmapPPData extends AbstractEntity {

const graph_process_time = performance.now();
wLogger.debug(
`(updateMapMetadata) Spend:${(
`BPPD(updateMapMetadata) [${(
graph_process_time - beatmap_parse_time
).toFixed(2)}ms on prcoessing graph strains`
).toFixed(2)}ms] Spend on prcoessing graph strains`
);

for (let i = 0; i < LEFT_OFFSET; i++) {
Expand All @@ -387,9 +389,9 @@ export class BeatmapPPData extends AbstractEntity {

const end_time = performance.now();
wLogger.debug(
`(updateMapMetadata) Total elapsed time: ${(
end_time - start_time
).toFixed(2)}ms`
`BPPD(updateMapMetadata) [${(end_time - start_time).toFixed(
2
)}ms] Total spent time`
);

this.updatePPData(oldStrains, resultStrains, ppAcc as never, {
Expand Down
25 changes: 13 additions & 12 deletions packages/tosu/src/entities/GamePlayData/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ export class GamePlayData extends AbstractEntity {
}

init(isRetry?: boolean) {
wLogger.debug(`[GameplayData:init] reseting (retry:${isRetry})`);
wLogger.debug(`GD(init) Reset (isRetry:${isRetry})`);

this.HitErrors = [];
this.MaxCombo = 0;
Expand Down Expand Up @@ -125,25 +125,25 @@ export class GamePlayData extends AbstractEntity {
process.readInt(rulesetsAddr - 0xb) + 0x4
);
if (rulesetAddr === 0) {
wLogger.debug('RulesetAddr is 0');
wLogger.debug('GD(updateState) RulesetAddr is 0');
return;
}

const gameplayBase = process.readInt(rulesetAddr + 0x68);
if (gameplayBase === 0) {
wLogger.debug('gameplayBase is zero');
wLogger.debug('GD(updateState) gameplayBase is zero');
return;
}

const scoreBase = process.readInt(gameplayBase + 0x38);
if (scoreBase === 0) {
wLogger.debug('scoreBase is zero');
wLogger.debug('GD(updateState) scoreBase is zero');
return;
}

let hpBarBase = process.readInt(gameplayBase + 0x40);
if (hpBarBase === 0) {
wLogger.debug('hpBar is zero');
wLogger.debug('GD(updateState) hpBar is zero');
return;
}

Expand Down Expand Up @@ -242,12 +242,13 @@ export class GamePlayData extends AbstractEntity {
process.readInt(patterns.getPattern('rulesetsAddr') - 0xb) + 0x4
);
if (rulesetAddr === 0) {
wLogger.debug('GD(updateKeyOverlay) rulesetAddr is zero');
return;
}

const keyOverlayPtr = process.readInt(rulesetAddr + 0xb0);
if (keyOverlayPtr === 0) {
wLogger.debug('keyOverlayPtr is zero');
wLogger.debug('GD(updateKeyOverlay) keyOverlayPtr is zero');
return;
}

Expand All @@ -256,7 +257,7 @@ export class GamePlayData extends AbstractEntity {
process.readInt(keyOverlayPtr + 0x10) + 0x4
);
if (keyOverlayArrayAddr === 0) {
wLogger.debug('keyOverlayArrayAddr is zero');
wLogger.debug('GD(updateKeyOverlay) keyOverlayArrayAddr is zero');
return;
}

Expand All @@ -281,7 +282,7 @@ export class GamePlayData extends AbstractEntity {
this.KeyOverlay = keys;

wLogger.debug(
`[GamePlayData:updateKeyOverlay] updated (${rulesetAddr} ${keyOverlayArrayAddr}) ${keys.K1Count}:${keys.K2Count}:${keys.M1Count}:${keys.M2Count}`
`GD(updateKeyOverlay) updated (${rulesetAddr} ${keyOverlayArrayAddr}) ${keys.K1Count}:${keys.K2Count}:${keys.M1Count}:${keys.M2Count}`
);
}

Expand Down Expand Up @@ -430,7 +431,7 @@ export class GamePlayData extends AbstractEntity {
private updateStarsAndPerformance() {
if (!config.calculatePP) {
wLogger.debug(
`[GamePlayData:updateStarsAndPerformance] pp calculation disabled`
`GD(updateStarsAndPerformance) pp calculation disabled`
);
return;
}
Expand All @@ -441,7 +442,7 @@ export class GamePlayData extends AbstractEntity {

if (!settings.gameFolder) {
wLogger.debug(
`[GamePlayData:updateStarsAndPerformance] game folder not found`
`GD(updateStarsAndPerformance) game folder not found`
);
return;
}
Expand All @@ -460,8 +461,8 @@ export class GamePlayData extends AbstractEntity {
cs: menuData.CS,
hp: menuData.HP
});
} catch (e) {
wLogger.debug("can't get map");
} catch (exc) {
wLogger.error("GD(updateStarsAndPerformance) Can't get map", exc);
return;
}

Expand Down
2 changes: 1 addition & 1 deletion packages/tosu/src/entities/MenuData/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export class MenuData extends AbstractEntity {

const beatmapAddr = process.readPointer(baseAddr - 0xc);
if (beatmapAddr === 0) {
wLogger.debug('beatmapAddr is 0');
wLogger.debug('MD(updateState) beatmapAddr is 0');
return;
}
// [[Beatmap] + 0x6C]
Expand Down
Loading

0 comments on commit 5560e10

Please sign in to comment.