Skip to content

Commit

Permalink
Merge pull request #102 from CCDirectLink/logging
Browse files Browse the repository at this point in the history
Added logging to file
  • Loading branch information
2767mr committed Dec 17, 2023
2 parents fb0c23c + e7ad948 commit 91839c5
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 19 deletions.
7 changes: 4 additions & 3 deletions ccloader/js/ccloader.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { Plugin } from './plugin.js';
import { Greenworks } from './greenworks.js';
import { Package } from './package.js';

const CCLOADER_VERSION = '2.22.1';
const CCLOADER_VERSION = '2.23.0';
const KNOWN_EXTENSIONS = ["post-game", "manlea", "ninja-skin", "fish-gear", "flying-hedgehag", "scorpion-robo", "snowman-tank"]

export class ModLoader {
Expand Down Expand Up @@ -314,14 +314,15 @@ export class ModLoader {

window.activeMods = Object.freeze(window.activeMods);
window.inactiveMods = Object.freeze(window.inactiveMods);

console.logToFile('Active mods: ', activeMods.map(m => m.name + ' ' + m.version).join(', '));
console.logToFile('Inactive mods: ', inactiveMods.map(m => m.name + ' ' + m.version).join(', '));
}

/**
* Sets up all global objects from ccloader in the game window.
*/
_setupGamewindow() {
this.ui.applyBindings(window.console);

const versions = Object.assign(this.versions, {
ccloader: CCLOADER_VERSION,
crosscode: this.ccVersion
Expand Down
89 changes: 74 additions & 15 deletions ccloader/js/ui.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,12 @@ export class UI {
this.modloader = modloader;
this.nextID = 1;

/** @type {import('fs')} */
this.fs = window.require && window.require('fs');

this._clearLogFile();
this._loadImage();
this.applyBindings(console);
this.applyBindings(window.console);
}

get container() {
Expand All @@ -77,23 +81,29 @@ export class UI {

const logFlags = localStorage.getItem('logFlags') || 3;

if (logFlags & LOG_TYPE.ERROR) {
console.error = (...msg) => {
console.error = (...msg) => {
this._logMessageToFile('error', ...msg);
if (logFlags & LOG_TYPE.ERROR) {
this.error.apply(this, msg);
err.apply(console, msg);
};
}
if (logFlags & LOG_TYPE.WARNING) {
console.warn = (...msg) => {
}
err.apply(console, msg);
};
console.warn = (...msg) => {
this._logMessageToFile('warn', ...msg);
if (logFlags & LOG_TYPE.WARNING) {
this.warn.apply(this, msg);
warn.apply(console, msg);
};
}
if (logFlags & LOG_TYPE.INFO) {
console.log = (...msg) => {
}
warn.apply(console, msg);
};
console.log = (...msg) => {
this._logMessageToFile('log', ...msg);
if (logFlags & LOG_TYPE.INFO) {
this.log.apply(this, msg);
log.apply(console, msg);
};
}
log.apply(console, msg);
};
console.logToFile = (...msg) => {
this._logMessageToFile('file', ...msg);
}
}

Expand Down Expand Up @@ -125,6 +135,55 @@ export class UI {
this._drawMessage(msg.join(' '), buttons.red, 15);
}

/**
*
* @param {string} level
* @param {...unknown} msg
*/
_logMessageToFile(level, ...msg) {
if (this.fs) {
let result = new Date().toISOString() + ' ' + level + ': ' + msg.join(' ') + '\n';

for (const part of msg) {
if (part && part instanceof Error) {
result += part.stack + '\n';
}
}

this.fs.appendFile('log.txt', result, (err) => {
//No error handling since that would cause an endless loop
return;
});
this.fs.appendFile('biglog.txt', result, (err) => {
//No error handling since that would cause an endless loop
return;
});
}
}

/**
*
*/
_clearLogFile() {
if (this.fs) {
//The log shouldn't get that big but just in case we check it and clear it if necessary.
this.fs.stat('biglog.txt', (err, stats) => {
if (err) {
return null;
}

if (stats.size > 10490000) { //10 Mib
this.fs.truncate('biglog.txt', 0, (err) => {
return;
})
}
});
this.fs.truncate('log.txt', 0, (err) => {
return;
});
}
}

/**
*
* @param {string} text
Expand Down
2 changes: 1 addition & 1 deletion ccloader/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
"ccmodHumanName": "CCLoader",
"ccmodType": "base",
"description": "Modloader for CrossCode. This or a similar modloader is needed for most mods.",
"version": "2.22.1"
"version": "2.23.0"
}

0 comments on commit 91839c5

Please sign in to comment.