Skip to content

Commit 971d205

Browse files
committedSep 19, 2024·
feat: Write logs to a file (only in debug)
1 parent 6c1682e commit 971d205

File tree

3 files changed

+36
-3
lines changed

3 files changed

+36
-3
lines changed
 

‎.gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,5 @@ package-lock.json
88
config.ini
99
tosu.exe
1010
_version.js
11-
**/tosu/gameOverlay/
11+
**/tosu/gameOverlay/
12+
logs/

‎packages/common/utils/config.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,8 @@ export const config = {
7777
enableGosuOverlay: (process.env.ENABLE_GOSU_OVERLAY || '') === 'true',
7878
timestamp: 0,
7979
currentVersion: '',
80-
updateVersion: ''
80+
updateVersion: '',
81+
logsPath: ''
8182
};
8283

8384
export const updateConfigFile = () => {

‎packages/common/utils/logger.ts

+32-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1+
import fs from 'fs';
2+
import path from 'path';
3+
14
import { config } from './config';
5+
import { getProgramPath } from './directories';
26

37
const colors = {
48
info: '\x1b[1m\x1b[40m\x1b[42m',
@@ -15,32 +19,59 @@ export function colorText(status: string) {
1519
const timestamp = new Date().toISOString().split('T')[1].replace('Z', '');
1620

1721
const time = `${colors.grey}${timestamp}${colors.reset}`;
18-
return `${time} ${colorCode} ${status.toUpperCase()} ${colors.reset}`;
22+
const version = `${colors.grey}v${config.currentVersion}${colors.reset}`;
23+
return `${time} ${version} ${colorCode} ${status.toUpperCase()} ${colors.reset}`;
1924
}
2025

2126
export const wLogger = {
2227
info: (...args: any) => {
2328
const coloredText = colorText('info');
2429
console.log(coloredText, ...args);
30+
31+
if (config.debugLogging === true) writeLog('info', args);
2532
},
2633
debug: (...args: any) => {
2734
if (config.debugLogging !== true) return;
2835

2936
const coloredText = colorText('debug');
3037
console.log(coloredText, ...args);
38+
39+
writeLog('debug', args);
3140
},
3241
debugError: (...args: any) => {
3342
if (config.debugLogging !== true) return;
3443

3544
const coloredText = colorText('debugError');
3645
console.log(coloredText, ...args);
46+
47+
if (config.debugLogging === true) writeLog('debugError', args);
3748
},
3849
error: (...args: any) => {
3950
const coloredText = colorText('error');
4051
console.log(coloredText, ...args);
52+
53+
if (config.debugLogging === true) writeLog('error', args);
4154
},
4255
warn: (...args: any) => {
4356
const coloredText = colorText('warn');
4457
console.log(coloredText, ...args);
58+
59+
if (config.debugLogging === true) writeLog('error', args);
4560
}
4661
};
62+
63+
function writeLog(type: string, ...args) {
64+
if (config.logsPath === '') {
65+
const logsPath = path.join(getProgramPath(), 'logs');
66+
if (!fs.existsSync(logsPath))
67+
fs.mkdirSync(logsPath, { recursive: true });
68+
69+
config.logsPath = path.join(logsPath, `${Date.now()}.txt`);
70+
}
71+
72+
fs.appendFileSync(
73+
config.logsPath,
74+
`${new Date().toISOString()} ${type} ${args.join(' ')}\n`,
75+
'utf8'
76+
);
77+
}

0 commit comments

Comments
 (0)
Please sign in to comment.