-
-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathlogger.js
106 lines (79 loc) · 4.86 KB
/
logger.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/*
* File: logger.js
* Project: steam-comment-service-bot
* Created Date: 2021-07-09 16:26:00
* Author: 3urobeat
*
* Last Modified: 2025-01-12 17:01:05
* Modified By: 3urobeat
*
* Copyright (c) 2021 - 2025 3urobeat <https://github.com/3urobeat>
*
* This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
* You should have received a copy of the GNU General Public License along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
const logger = require("output-logger"); // Look Mom, it's my own library!
const Controller = require("../controller.js");
// Configure my logging library (https://github.com/3urobeat/output-logger#options-1) (animation speed and printDebug will be changed later in controller.js after advancedconfig import)
logger.options({
required_from_childprocess: true, // eslint-disable-line camelcase
msgstructure: `[${logger.Const.ANIMATION}] [${logger.Const.DATE} | ${logger.Const.TYPE}] ${logger.Const.MESSAGE}`,
paramstructure: [logger.Const.TYPE, logger.Const.MESSAGE, "nodate", "remove", logger.Const.ANIMATION, "cutToWidth", "customTimestamp"],
outputfile: srcdir + "/../output.txt"
});
// Save suppressed logs during startup that get logged by ready event
let logAfterReady = [];
let botIsReady = false;
// Modfied output-logger function to hold back certain messages until ready event fired
/**
* Logs text to the terminal and appends it to the output.txt file.
* @param {string} type String that determines the type of the log message. Can be info, warn, error, debug or an empty string to not use the field.
* @param {string} str The text to log into the terminal
* @param {boolean} nodate Setting to true will hide date and time in the message
* @param {boolean} remove Setting to true will remove this message with the next one
* @param {Array.<string>} animation Array containing animation frames as elements
* @param {boolean} printNow Ignores the readyafterlogs check and force prints the message now
* @param {boolean} cutToWidth Cuts the string to the width of the terminal
*/
Controller.prototype.logger = function(type, str, nodate, remove, animation, printNow, cutToWidth) {
// Push string to _logAfterReady if bot is still starting (Note: We cannot use "this." here as context is missing when global var is called)
if (typeof str == "string" && !nodate && !remove && !printNow // Log instant if msg should have no date, it will be removed or printNow is forced
&& !botIsReady // Log instant if bot is already started, var gets updated by _loggerLogAfterReady
&& !type.toLowerCase().includes("err") // Log errors instantly
&& type.toLowerCase() != "debug" // Log debug messages immediately
&& !str.toLowerCase().includes("error")) { // Log instantly if message contains Error
logAfterReady.push([ type, str, nodate, remove, animation, cutToWidth, Date.now() ]); // Push all arguments this function got to the array, including a customTimestamp
logger("debug", `Controller logger: Pushing "${str}${logger.colors.reset}" to logAfterReady array...`);
} else {
logger(type, str, nodate, remove, animation, cutToWidth);
}
};
// Add all nested functions from output-logger to our modified logger function
Object.assign(Controller.prototype.logger, logger);
// Make our logger public so we can use it everywhere
global.logger = Controller.prototype.logger;
/**
* Internal: Call this function after loading advancedconfig.json to set previously inaccessible options
* @private
* @param {object} advancedconfig The advancedconfig object imported by the DataManager
*/
Controller.prototype._loggerOptionsUpdateAfterConfigLoad = function(advancedconfig) {
logger.options({
animationinterval: advancedconfig.logAnimationSpeed,
printdebug: advancedconfig.printDebug
});
};
/**
* Internal: Logs all held back messages from logAfterReady array
* @private
*/
Controller.prototype._loggerLogAfterReady = function() {
botIsReady = true;
if (logAfterReady.length == 0) return; // Don't do anything if empty to confuse me less when debugging
logger("debug", `Controller logger: Logging ${logAfterReady.length} suppressed log messages...`);
logAfterReady.forEach(e => { logger(e[0], e[1], e[2], e[3], e[4], e[5]); }); // Log suppressed logs
logger("", "", true); // Log a newline to separate held back messages from other ready messages
// Clear content and prevent new entries
logAfterReady = [];
};