Skip to content

Commit

Permalink
feat: Remove winston, since we only use colored console.log
Browse files Browse the repository at this point in the history
  • Loading branch information
cyperdark committed Mar 4, 2024
1 parent d4f02ef commit dc01f8e
Show file tree
Hide file tree
Showing 11 changed files with 44 additions and 193 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
47 changes: 24 additions & 23 deletions packages/common/utils/logger.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,29 @@
import winston, { format, transports } from 'winston';

import { config } from './config';

const { timestamp, label, printf } = format;
const colors = {
error: '\x1b[31m',
info: '\x1b[32m',
debug: '\x1b[34m',
warn: '\x1b[33m',
reset: '\x1b[0m'
};

const customFormat = printf(({ level, message, label, timestamp }) => {
return `${timestamp} [${label}] ${level}: ${message}`;
});
function colorText(status: string, ...anything: any) {
const colorCode = colors[status] || colors.reset;
const timestamp = new Date().toISOString().replace('T', ' ');
console.log(
`${timestamp} [tosu] ${colorCode}${status}${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: 2 additions & 4 deletions packages/tosu/src/entities/AllTimesData/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -368,8 +368,7 @@ export class AllTimesData extends AbstractEntity {
return true;
});
} catch (exc) {
wLogger.error("can't update config state");
console.error(exc);
wLogger.error("can't update config state", exc);
}
}

Expand All @@ -392,8 +391,7 @@ export class AllTimesData extends AbstractEntity {
return true;
});
} catch (exc) {
wLogger.error("can't update binding state");
console.error(exc);
wLogger.error("can't update binding state", exc);
}
}

Expand Down
6 changes: 3 additions & 3 deletions packages/tosu/src/entities/BeatmapPpData/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -277,10 +277,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"
"Something happend, when we're tried to parse beatmap",
exc
);
return;
}
Expand Down
4 changes: 2 additions & 2 deletions packages/tosu/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ import { autoUpdater } from '@tosu/updater';
import { InstanceManager } from './objects/instanceManager/instanceManager';

(async () => {
wLogger.info('Starting tosu');

const instanceManager = new InstanceManager();
const httpServer = new Server({ instanceManager });

watchConfigFile({ httpServer });

wLogger.info('Starting tosu');

const { update } = argumetsParser(process.argv);

if (
Expand Down
14 changes: 6 additions & 8 deletions packages/tosu/src/objects/instanceManager/osuInstance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,9 +185,9 @@ export class OsuInstance {
);
this.isReady = true;
} catch (exc) {
console.log(exc);
wLogger.error(
'PATTERN SCANNING FAILED, TRYING ONE MORE TIME...'
'PATTERN SCANNING FAILED, TRYING ONE MORE TIME...',
exc
);
this.emitter.emit('onResolveFailed', this.pid);
return;
Expand Down Expand Up @@ -319,8 +319,7 @@ export class OsuInstance {

await userProfile.updateState();
} catch (exc) {
wLogger.error('error happend while another loop executed');
console.error(exc);
wLogger.error('error happend while another loop executed', exc);
}

await sleep(config.pollRate);
Expand Down Expand Up @@ -353,9 +352,9 @@ export class OsuInstance {
await sleep(config.keyOverlayPollRate);
} catch (exc) {
wLogger.error(
'error happend while keyboard overlay attempted to parse'
'error happend while keyboard overlay attempted to parse',
exc
);
console.error(exc);
}
}
}
Expand Down Expand Up @@ -397,8 +396,7 @@ export class OsuInstance {
try {
await beatmapPpData.updateMapMetadata(currentMods);
} catch (exc) {
wLogger.error("can't update beatmap metadata");
console.error(exc);
wLogger.error("can't update beatmap metadata", exc);
}
}

Expand Down
2 changes: 1 addition & 1 deletion packages/updater/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ const deleteNotLocked = async (filePath: string) => {
return;
}

console.log(err.message, err.code);
wLogger.error(err);
}
};

Expand Down
Loading

0 comments on commit dc01f8e

Please sign in to comment.