This commit is contained in:
Zutatensuppe 2021-06-09 09:52:58 +02:00
parent accd38eb02
commit 4b10fbc01b
2 changed files with 33 additions and 36 deletions

View file

@ -1276,13 +1276,11 @@ const idxname = (gameId) => `${DATA_DIR}/log_${gameId}.idx.log`;
const create = (gameId, ts) => { const create = (gameId, ts) => {
const idxfile = idxname(gameId); const idxfile = idxname(gameId);
if (!fs.existsSync(idxfile)) { if (!fs.existsSync(idxfile)) {
const logfile = filename(gameId, 0);
fs.appendFileSync(logfile, "");
fs.appendFileSync(idxfile, JSON.stringify({ fs.appendFileSync(idxfile, JSON.stringify({
gameId: gameId, gameId: gameId,
total: 0, total: 0,
lastTs: ts, lastTs: ts,
currentFile: logfile, currentFile: '',
perFile: LINES_PER_LOG_FILE, perFile: LINES_PER_LOG_FILE,
})); }));
} }
@ -1296,21 +1294,21 @@ const _log = (gameId, type, ...args) => {
if (!fs.existsSync(idxfile)) { if (!fs.existsSync(idxfile)) {
return; return;
} }
const ts = args[args.length - 1]; const idxObj = JSON.parse(fs.readFileSync(idxfile, 'utf-8'));
const otherArgs = args.slice(0, -1); if (idxObj.total % idxObj.perFile === 0) {
const idx = JSON.parse(fs.readFileSync(idxfile, 'utf-8')); idxObj.currentFile = filename(gameId, idxObj.total);
idx.total++;
const diff = ts - idx.lastTs;
idx.lastTs = ts;
const line = JSON.stringify([type, ...otherArgs, diff]).slice(1, -1);
fs.appendFileSync(idx.currentFile, line + "\n");
// prepare next log file
if (idx.total % idx.perFile === 0) {
const logfile = filename(gameId, idx.total);
fs.appendFileSync(logfile, "");
idx.currentFile = logfile;
} }
fs.writeFileSync(idxfile, JSON.stringify(idx)); const tsIdx = type === Protocol.LOG_HEADER ? 3 : (args.length - 1);
const ts = args[tsIdx];
if (type !== Protocol.LOG_HEADER) {
// for everything but header save the diff to last log entry
args[tsIdx] = ts - idxObj.lastTs;
}
const line = JSON.stringify([type, ...args]).slice(1, -1);
fs.appendFileSync(idxObj.currentFile, line + "\n");
idxObj.total++;
idxObj.lastTs = ts;
fs.writeFileSync(idxfile, JSON.stringify(idxObj));
}; };
const get = (gameId, offset = 0) => { const get = (gameId, offset = 0) => {
const idxfile = idxname(gameId); const idxfile = idxname(gameId);

View file

@ -1,4 +1,5 @@
import fs from 'fs' import fs from 'fs'
import Protocol from '../common/Protocol'
import Time from '../common/Time' import Time from '../common/Time'
import { Timestamp } from '../common/Types' import { Timestamp } from '../common/Types'
import { logger } from './../common/Util' import { logger } from './../common/Util'
@ -27,13 +28,11 @@ export const idxname = (gameId: string) => `${DATA_DIR}/log_${gameId}.idx.log`
const create = (gameId: string, ts: Timestamp): void => { const create = (gameId: string, ts: Timestamp): void => {
const idxfile = idxname(gameId) const idxfile = idxname(gameId)
if (!fs.existsSync(idxfile)) { if (!fs.existsSync(idxfile)) {
const logfile = filename(gameId, 0)
fs.appendFileSync(logfile, "")
fs.appendFileSync(idxfile, JSON.stringify({ fs.appendFileSync(idxfile, JSON.stringify({
gameId: gameId, gameId: gameId,
total: 0, total: 0,
lastTs: ts, lastTs: ts,
currentFile: logfile, currentFile: '',
perFile: LINES_PER_LOG_FILE, perFile: LINES_PER_LOG_FILE,
})) }))
} }
@ -50,23 +49,23 @@ const _log = (gameId: string, type: number, ...args: Array<any>): void => {
return return
} }
const ts: Timestamp = args[args.length - 1] const idxObj = JSON.parse(fs.readFileSync(idxfile, 'utf-8'))
const otherArgs: any[] = args.slice(0, -1) if (idxObj.total % idxObj.perFile === 0) {
idxObj.currentFile = filename(gameId, idxObj.total)
const idx = JSON.parse(fs.readFileSync(idxfile, 'utf-8'))
idx.total++
const diff = ts - idx.lastTs
idx.lastTs = ts
const line = JSON.stringify([type, ...otherArgs, diff]).slice(1, -1)
fs.appendFileSync(idx.currentFile, line + "\n")
// prepare next log file
if (idx.total % idx.perFile === 0) {
const logfile = filename(gameId, idx.total)
fs.appendFileSync(logfile, "")
idx.currentFile = logfile
} }
fs.writeFileSync(idxfile, JSON.stringify(idx))
const tsIdx = type === Protocol.LOG_HEADER ? 3 : (args.length - 1)
const ts: Timestamp = args[tsIdx]
if (type !== Protocol.LOG_HEADER) {
// for everything but header save the diff to last log entry
args[tsIdx] = ts - idxObj.lastTs
}
const line = JSON.stringify([type, ...args]).slice(1, -1)
fs.appendFileSync(idxObj.currentFile, line + "\n")
idxObj.total++
idxObj.lastTs = ts
fs.writeFileSync(idxfile, JSON.stringify(idxObj))
} }
const get = ( const get = (