You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

105 lines
2.7 KiB

2 years ago
import {Room} from "./room"
import {Client} from "./client"
import fs = require('fs');
import util = require('util');
let logFolder = "./logs";
if (!fs.existsSync(logFolder)) {
fs.mkdirSync(logFolder);
}
let logFile = fs.createWriteStream(logFolder + '/' + new Date().getTime() + '.log', {flags: 'a'});
let logStdout = process.stdout;
console.log = function () {
logFile.write(util.format.apply(null, arguments) + '\n');
logStdout.write(util.format.apply(null, arguments) + '\n');
};
console.error = console.log;
process.on('uncaughtException', err => {
console.error('Uncaught error: ', err);
process.exit(1);
});
process.stdin.pipe(logFile);
export function log(type: string, client: Client, lobby?: Room, msg?: string) {
let now = new Date(Date.now()).toString(), message, name, game;
let date = '[' + now.substring(0, now.indexOf('GMT') - 1) + ']';
if (client) {
game = '[' + client.game + ']';
let short = client.id.substring(0, Math.round(client.id.length / 3));
name = '"' + client.name + '(' + short + '...)"';
} else {
if (type === 'lobby-deleted') {
game = '[' + lobby.gameName + ']';
} else {
game = '[undefined]';
}
name = 'UNKNOWN';
}
if (lobby) {
game = '[' + lobby.gameName + ']';
}
switch (type) {
case 'join-non-existent':
message = name + ' tried to join non-existent lobby "' + lobby.id + '"';
break;
case 'join-started':
message = name + ' tried to join the started game "' + lobby.id + '"';
break;
case 'lobby-created':
message = name + ' created new lobby: "' + lobby.id + '"';
break;
case 'game-started':
message = name + ' started the game: "' + lobby.id + '"';
break;
case 'game-stopped':
message = name + ' stopped the game: "' + lobby.id + '"';
break;
case 'member-joined':
message = name + ' joined the lobby "' + lobby.id + '"';
break;
case 'member-left':
message = name + ' left the lobby "' + lobby.id + '"';
break;
case 'lobby-deleted':
message = 'Lobby "' + lobby.id + '" was deleted';
break;
case 'save-success':
message = msg;
break;
case 'save-error':
message = 'Failed to save contents to file: ' + msg;
break;
case 'load-success':
message = 'Successfully loaded and parsed file contents';
break;
case 'load-error':
message = 'Failed to load file: ' + msg;
break;
case 'parse-error':
message = 'Failed to parse contents: ' + msg;
break;
case 'feedback':
message = 'Saved feedback to file: ' + msg;
break;
case 'connection':
message = name + ' connected';
break;
case 'disconnection':
message = name + ' disconnected';
break;
case 'startup':
message = msg;
break;
}
console.log(date + game + ' ---> {' + message + '}');
}