Skip to content

Commit

Permalink
Code cleanup.
Browse files Browse the repository at this point in the history
  • Loading branch information
regorxxx committed Dec 20, 2023
1 parent a3f5a6f commit 34af1b2
Showing 1 changed file with 42 additions and 39 deletions.
81 changes: 42 additions & 39 deletions helpers/helpers_xxx_console.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
'use strict';
//15/08/23
//20/12/23

include(fb.ComponentPath + 'docs\\Codepages.js');
/* global convertCharsetToCodepage:readable */

// Console log file
// Edit here to change logging file. Replace with '' or null to disable logging
Object.defineProperty(console, 'File', {enumerable: false, configurable: false, writable: true, value: fb.ProfilePath + 'console.log'});
Object.defineProperty(console, 'File', { enumerable: false, configurable: false, writable: true, value: fb.ProfilePath + 'console.log' });
// File size, in bytes. Setting to zero or null disables logging too
Object.defineProperty(console, 'MaxSize', {enumerable: false, configurable: false, writable: true, value: 5000000});
Object.defineProperty(console, 'MaxSize', { enumerable: false, configurable: false, writable: true, value: 5000000 });
// Interval to flush to console, in ms. Setting to zero or null writes to console file on every call (not recommended)
Object.defineProperty(console, 'Throttling', {enumerable: false, configurable: false, writable: true, value: 100});
Object.defineProperty(console, 'Throttling', { enumerable: false, configurable: false, writable: true, value: 100 });
// Interval use
Object.defineProperty(console, 'Timer', {enumerable: false, configurable: false, writable: true});
Object.defineProperty(console, 'Cache', {enumerable: false, configurable: false, writable: true, value: []});
Object.defineProperty(console, 'Timer', { enumerable: false, configurable: false, writable: true });
Object.defineProperty(console, 'Cache', { enumerable: false, configurable: false, writable: true, value: [] });

/* global fso:readable */
const fsoCL = typeof fso !== 'undefined' ? fso : new ActiveXObject('Scripting.FileSystemObject'); // Reuse fso if possible

// Override logging
Expand All @@ -25,7 +27,7 @@ function consoleLog() {
// Load previous log
console.checkSize();
if (utils.IsFile(console.File)) {
try {log += utils.ReadTextFile(console.File, convertCharsetToCodepage('UTF-8'));} catch (e) {/* continue regardless of error */}
try { log += utils.ReadTextFile(console.File, convertCharsetToCodepage('UTF-8')); } catch (e) {/* continue regardless of error */ }
lastMod = new Date(fsoCL.GetFile(console.File).DateLastModified).toLocaleDateString();
}
// Add dd/mm/yyyy
Expand All @@ -37,15 +39,15 @@ function consoleLog() {
log += (log && log.length ? '\n' : '') + (bCache ? '' : stamp);
// Unpack args
const args = bCache ? console.Cache : [[...arguments]];
if (bCache) {console.Cache = [];}
if (bCache) { console.Cache = []; }
args.forEach((call, j) => {
if (bCache && j !== 0) {log += '\n';}
if (bCache && j !== 0) { log += '\n'; }
call.forEach((arg, i) => {
const type = typeof arg;
let val = null;
switch (type) {
case 'undefined': {
val = void(0);
val = void (0);
break;
}
case 'function':
Expand All @@ -56,55 +58,56 @@ function consoleLog() {
break;
}
case 'object':
default : {
default: {
if (arg !== null) {
let instance = null;
switch (true) { // Get object types
case arg instanceof Set: {instance = {name: 'Set', type: 'array'}; break;}
case arg instanceof Map: {instance = {name: 'Map', type: 'array'}; break;}
case arg instanceof WeakMap: {instance = {name: 'WeakMap', type: 'array'}; break;}
case arg instanceof WeakSet: {instance = {name: 'WeakSet', type: 'array'}; break;}
case arg instanceof Error: {instance = {name: 'Error', type: 'error'}; break;}
case arg instanceof Set: { instance = { name: 'Set', type: 'array' }; break; }
case arg instanceof Map: { instance = { name: 'Map', type: 'array' }; break; }
case arg instanceof WeakMap: { instance = { name: 'WeakMap', type: 'array' }; break; }
case arg instanceof WeakSet: { instance = { name: 'WeakSet', type: 'array' }; break; }
case arg instanceof Error: { instance = { name: 'Error', type: 'error' }; break; }
}
if (instance) { // Convert to array objects if possible and stringify
switch (instance.type) {
case 'array': {val = [...arg]; break;}
case 'error': {val = arg.toString(); break;}
case 'array': { val = [...arg]; break; }
case 'error': { val = arg.toString(); break; }
}
}
try {
val = (instance ? instance.name + ' ' : 'Object ') + JSON.stringify(val ? val : arg, (k, v) => {
if (typeof v !== 'undefined' && v !== null) {
if (v.RawPath && v.Path) {
return 'FbMetadbHandle ' + JSON.stringify({FileSize: v.FileSize, Length: v.Length, Path: v.Path, RawPath: v.RawPath, SubSong: v.SubSong}, null, ' ').replace(/{\n /,'{').replace(/"|\n/g,'').replace(/\\\\/g, '\\');
}
return 'FbMetadbHandle ' + JSON.stringify({ FileSize: v.FileSize, Length: v.Length, Path: v.Path, RawPath: v.RawPath, SubSong: v.SubSong }, null, ' ').replace(/{\n /, '{').replace(/["\n]/g, '').replace(/\\\\/g, '\\');
}
else if (v instanceof FbMetadbHandleList) {
return 'FbMetadbHandleList ' + JSON.stringify({Count: v.Count}, null, ' ').replace(/{\n /,'{').replace(/"|\n/g,'')
return 'FbMetadbHandleList ' + JSON.stringify({ Count: v.Count }, null, ' ').replace(/{\n /, '{').replace(/["\n]/g, '');
}
else if (v instanceof Set) {
return 'Set ' + JSON.stringify([...v]).replace(/"|\n/g,'');
return 'Set ' + JSON.stringify([...v]).replace(/["\n]/g, '');
}
else if (v instanceof Map) {
return 'Map ' + JSON.stringify([...v]).replace(/"|\n/g,'');
return 'Map ' + JSON.stringify([...v]).replace(/["\n]/g, '');
}
else if (v instanceof WeakMap) {
return 'WeakMap ' + JSON.stringify([...v]).replace(/"|\n/g,'');
return 'WeakMap ' + JSON.stringify([...v]).replace(/["\n]/g, '');
}
else if (v instanceof WeakSet) {
return 'WeakMap ' + JSON.stringify([...v]).replace(/"|\n/g,'');
return 'WeakMap ' + JSON.stringify([...v]).replace(/["\n]/g, '');
}
else if (v instanceof Error) {
return 'Error ' + arg.toString().replace(/"|\n/g,'');
return 'Error ' + arg.toString().replace(/["\n]/g, '');
}
}
return v;
});
} catch (e) {
if (e.message === 'can\'t access dead object') {
console.logUI('Console.log: can\'t access dead object: ', type);
console.logUI('Console.log: ' + e.message + ': ', type);
} else {
try {val = arg.constructor.name || (arg.constructor.toString().match(/function (\w*)/) || [ , ])[1];} catch (e) {}
if (!val) {val = '--unknown type--';}
// eslint-disable-next-line no-sparse-arrays
try { val = arg.constructor.name || (arg.constructor.toString().match(/function (\w*)/) || [,])[1]; } catch (e) { /* empty */ } //NOSONAR
if (!val) { val = '--unknown type--'; }
console.logUI('Console.log: argument type not recognized: ', type, val);
}
}
Expand All @@ -116,13 +119,13 @@ function consoleLog() {
});
});
// Write
try {utils.WriteTextFile(console.File, log, false);} catch (e) {/* continue regardless of error */}
try { utils.WriteTextFile(console.File, log, false); } catch (e) {/* continue regardless of error */ }
}

// Check file size doesn't exceed threshold or reset it
console.checkSize = () => {
if (utils.IsFile(console.File) && utils.GetFileSize(console.File) > console.MaxSize) {
try {utils.WriteTextFile(console.File, '', false);} catch (e) {/* continue regardless of error */}
try { utils.WriteTextFile(console.File, '', false); } catch (e) {/* continue regardless of error */ }
console.log('helpers_xxx: console log file size exceeds ' + (console.MaxSize / 1e7) + ' MB, creating new file: ' + console.File);
return true;
}
Expand All @@ -131,15 +134,15 @@ console.checkSize = () => {

// Force writing cache to file (usually used at on_script_unload)
console.flush = () => {
if (console.Cache.length) {consoleLog();}
if (console.Cache.length) { consoleLog(); }
};

// Send to popup and console
console.popup = (arg, popupName, bPopup = true, bSplit = true) => {
if (bPopup) {fb.ShowPopupMessage(arg, popupName);}
if (bPopup) { fb.ShowPopupMessage(arg, popupName); }
if (bSplit) {
arg.split('\n').forEach((line) => {
if (line && line.length) {console.log(line);}
if (line && line.length) { console.log(line); }
});
} else {
console.log(arg);
Expand All @@ -148,7 +151,7 @@ console.popup = (arg, popupName, bPopup = true, bSplit = true) => {

if (console.File && console.File.length && console.MaxSize && console.log) {
console.logUI = console.log;
console.log = function() {
console.log = function () {
console.logUI(...arguments);
if (console.Throttling) {
clearTimeout(console.Timer);
Expand All @@ -167,7 +170,7 @@ if (console.File && console.File.length && console.MaxSize && console.log) {
if (FbProfiler.prototype) {
const oldProto = FbProfiler.prototype;
const oldFunc = FbProfiler;
FbProfiler = function(name) {
FbProfiler = function (name) { // NOSONAR
const obj = oldFunc(name);
obj.Name = name;
return obj;
Expand All @@ -178,12 +181,12 @@ if (FbProfiler.prototype) {
// Rewrap FbProfiler to also log to file
if (FbProfiler.prototype.Print) {
FbProfiler.prototype.PrintUI = FbProfiler.prototype.Print;
FbProfiler.prototype.Print = function(additionalMsgopt = '', printComponentInfoopt = true) {
FbProfiler.prototype.Print = function (additionalMsgopt = '', printComponentInfoopt = true) {
// Recreate the message format
let message = '';
if (printComponentInfoopt) {message += 'Spider Monkey Panel v' + utils.Version + ': ';}
if (printComponentInfoopt) { message += 'Spider Monkey Panel v' + utils.Version + ': '; }
message += 'profiler (' + this.Name + '): ';
if (additionalMsgopt && additionalMsgopt.length) {message += additionalMsgopt + ' ';}
if (additionalMsgopt && additionalMsgopt.length) { message += additionalMsgopt + ' '; }
message += this.Time + 'ms';
console.log(message); // Instead of using the original method, just use the basic log routine with debounce
// this.PrintUI(additionalMsgopt, printComponentInfoopt);
Expand Down

0 comments on commit 34af1b2

Please sign in to comment.