forked from arcojuana/AACloud
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DebugLog.js
188 lines (143 loc) · 7.02 KB
/
DebugLog.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
exports.newDebugLog = function newDebugLog() {
const MODULE_NAME = "DebugLog";
let executionDatetime = "D." + global.EXECUTION_DATETIME.getUTCFullYear() +
"." + pad(global.EXECUTION_DATETIME.getUTCMonth() + 1, 2) +
"." + pad(global.EXECUTION_DATETIME.getUTCDate(), 2) +
".T." + pad(global.EXECUTION_DATETIME.getUTCHours(), 2) +
"." + pad(global.EXECUTION_DATETIME.getUTCMinutes(), 2) +
"." + pad(global.EXECUTION_DATETIME.getUTCSeconds(), 2);
let messageId = 0;
let thisObject = {
bot: undefined,
write: write,
newInternalLoop: newInternalLoop,
persist: persist, // This method is executed at the end of each Main Loop.
initialize: initialize,
finalize: finalize
};
let accumulatedLog = "[";
let disableLogging;
let internalLoopCounter = -1;
return thisObject;
function initialize(pdisableLogging) {
try {
disableLogging = pdisableLogging
} catch (err) {
console.log("[ERROR] Debug Log -> initialize -> err = " + err.stack);
}
}
function finalize() {
persist()
}
function newInternalLoop(pBot, pProcess, date, percentage) {
if (percentage === undefined) {
percentage = ""
} else {
percentage = percentage.toFixed(2) + " %"
}
if (date === undefined) { date = thisObject.bot.processDatetime }
date = date.getUTCFullYear() + '-' + strPad(date.getUTCMonth() + 1, 2, "0") + '-' + strPad(date.getUTCDate(), 2, "0");
console.log(new Date().toISOString() + " " + strPad(thisObject.bot.exchange, 20) + " " + strPad(thisObject.bot.market.baseAsset + '/' + thisObject.bot.market.quotedAsset, 10) + " " + strPad(pBot, 30) + " " + strPad(pProcess, 30)
+ " Internal Loop # " + strPad(internalLoopCounter + 1, 8) + " " + strPad(date, 30) + " " + strPad(percentage, 10))
persist();
function strPad(str, max, fill) {
if (fill === undefined) { fill = " " }
str = str.toString();
return str.length < max ? strPad(fill + str, max) : str;
}
}
function persist() {
/* Here we actually write the content of the in-memory log to a file */
try {
if (accumulatedLog === "[") { return } // nothing to persist at the moment.
internalLoopCounter++;
let contentToPersist = accumulatedLog;
accumulatedLog = "[";
const FILE_STORAGE = require('./FileStorage.js');
let fileStorage = FILE_STORAGE.newFileStorage();
let filePath = thisObject.bot.filePathRoot + "/Logs/" + thisObject.bot.process + "/"
if (thisObject.bot.TRADING_SESSION !== undefined) {
filePath = filePath + thisObject.bot.TRADING_SESSION.folderName + "/" + executionDatetime;
} else {
if (thisObject.bot.LEARNING_SESSION !== undefined) {
filePath = filePath + thisObject.bot.LEARNING_SESSION.folderName + "/" + executionDatetime;
} else {
filePath = filePath + executionDatetime;
}
}
if (thisObject.bot.debug.year !== undefined) {
filePath = filePath + "/" + thisObject.bot.debug.year + "/" + thisObject.bot.debug.month;
}
let fileName;
if (internalLoopCounter >= 0) {
fileName = "Loop." + pad(thisObject.bot.loopCounter, 8) + "." + pad(internalLoopCounter, 4) + ".json";
} else {
fileName = "Loop." + pad(thisObject.bot.loopCounter, 8) + ".json";
}
writeLog();
/* This is the implementation of the mechanism to auto-mantain logs. */
if (contentToPersist.indexOf('[ERROR]') < 0 && contentToPersist.indexOf('[PERSIST]') < 0 && contentToPersist.indexOf('[IMPORTANT]') < 0) {
thisObject.bot.LOGS_TO_DELETE_QUEUE.push(filePath + '/' + fileName)
}
if (thisObject.bot.LOGS_TO_DELETE_QUEUE.length > thisObject.bot.DELETE_QUEUE_SIZE) {
let fileToDelete = thisObject.bot.LOGS_TO_DELETE_QUEUE[0]
thisObject.bot.LOGS_TO_DELETE_QUEUE.splice(0, 1)
/* Will delete this file only if it does not contains ERROR inside. */
let fileContent = fileStorage.getTextFile(fileToDelete, onGetFile, true)
function onGetFile(err, fileContent) {
if (err.result === global.DEFAULT_OK_RESPONSE.result) {
if (fileContent.indexOf("ERROR") < 0) {
/* Logs will only be deleted when they contain no ERROR in them. */
fileStorage.deleteTextFile(fileToDelete);
}
} else {
fileStorage.deleteTextFile(fileToDelete);
}
}
}
function writeLog() {
fileStorage.createTextFile(filePath + '/' + fileName, contentToPersist + '\r\n' + "]", onFileCreated, undefined, true);
function onFileCreated(err) {
if (err.result !== global.DEFAULT_OK_RESPONSE.result) {
console.log("[ERROR] DebugLog -> persist -> onInizialized -> onFileCreated -> err = " + err.message);
setTimeout(writeLog, 10000); // Lets retry until we make it.
return;
}
contentToPersist = "";
}
}
} catch (err) {
console.log("[ERROR] DebugLog -> persist -> err = " + err.stack);
console.log("[ERROR] DebugLog -> persist -> onInizialized -> contentToPersist = " + contentToPersist);
}
}
function write(pModule, pMessage) {
try {
if (disableLogging === true) { return; }
if (thisObject.bot === undefined) { return; }
if (pModule === "") { // For debugging purposes
console.log(pMessage)
}
let newDate = new Date();
newDate = newDate.toISOString();
messageId++;
/* When writting one file for all modules we use this. */
let message = "['" + newDate + "'," + messageId + ",'" + pModule + "','" + pMessage + "']"
let logLine = '\r\n' + message;
if (process.env.CONSOLE_LOG === "true" || message.indexOf("ERROR") > 0) {
let key = ''
if (thisObject.bot) {
key = thisObject.bot.dataMine + '-' + thisObject.bot.codeName + '-' + thisObject.bot.process
}
console.log('*********** ' + message + ' @ ' + key)
}
accumulatedLog = accumulatedLog + logLine;
} catch (err) {
console.log("[ERROR] DebugLog -> write -> err = " + err.stack);
}
}
function pad(str, max) {
str = str.toString();
return str.length < max ? pad("0" + str, max) : str;
}
};