-
-
Notifications
You must be signed in to change notification settings - Fork 101
/
Log.mqh
344 lines (303 loc) · 9.32 KB
/
Log.mqh
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
//+------------------------------------------------------------------+
//| EA31337 framework |
//| Copyright 2016-2023, EA31337 Ltd |
//| https://github.com/EA31337 |
//+------------------------------------------------------------------+
/*
* This file 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 <http://www.gnu.org/licenses/>.
*
*/
// Includes.
#include "Array.mqh"
#include "DateTime.mqh"
#include "DictStruct.mqh"
#include "Object.mqh"
#include "Storage/Collection.mqh"
// Prevents processing this includes file for the second time.
#ifndef LOG_MQH
#define LOG_MQH
// Define assert macros.
// Alias for function and line macros combined together.
#define __FUNCTION_LINE__ string(__FUNCTION__) + ":" + IntegerToString(__LINE__)
// Log verbosity level.
enum ENUM_LOG_LEVEL {
V_NONE = 0, // None
V_ERROR = 1, // Errors only
V_WARNING = 2, // Errors and warnings
V_INFO = 3, // All (info, errors and warnings)
V_DEBUG = 4, // All with debug!
V_TRACE = 5 // All with debug and trace!
};
/**
* Class to provide logging functionality.
*/
class Log : public Object {
private:
struct log_entry {
datetime timestamp;
ENUM_LOG_LEVEL log_level;
string msg;
};
DictStruct<int, Ref<Log>> logs;
string filename;
ARRAY(log_entry, data);
int last_entry;
datetime last_flush;
ENUM_LOG_LEVEL log_level;
public:
/**
* Class constructor.
*/
Log(ENUM_LOG_LEVEL _log_level = V_INFO, string new_filename = "")
: last_entry(-1), last_flush(0), log_level(_log_level), filename(new_filename != "" ? new_filename : "Log.txt") {}
/**
* Class copy constructor.
*/
Log(const Log &_log) : filename(_log.filename), last_entry(_log.last_entry), log_level(_log.log_level) {
}
/**
* Class deconstructor.
*/
~Log() { Flush(); }
/* Getters */
/**
* Get last message.
*/
string GetLastMsg(ENUM_LOG_LEVEL _level = V_INFO, bool _dt = false) {
int i;
string _output = "";
for (i = last_entry; i == 0; i--) {
if (data[i].log_level <= _level) {
_output += (_dt ? DateTimeStatic::TimeToStr(data[i].timestamp) + ": " : "") + data[i].msg;
break;
}
}
return _output;
}
/**
* Returns log level.
*/
ENUM_LOG_LEVEL GetLevel() { return log_level; }
/**
* Returns level name.
*/
string GetLevelName(ENUM_LOG_LEVEL _log_level) { return StringSubstr(EnumToString(_log_level), 2); }
/* Setters */
/**
* Sets new log level.
*/
void SetLevel(ENUM_LOG_LEVEL _log_level) { log_level = _log_level; }
/* Other methods */
/**
* Adds a log entry.
*/
bool Add(ENUM_LOG_LEVEL _log_level, string msg, string prefix, string suffix) {
if (_log_level > log_level) {
// Ignore entry if verbosity is higher than set.
return false;
}
int _size = ArraySize(data);
if (last_entry > 0 && msg == data[last_entry].msg) {
// Avoid adding duplicate messages.
return false;
}
if (++last_entry >= _size) {
if (!ArrayResize(data, (_size + 100), 100)) {
return false;
}
}
msg = GetLevelName(_log_level) + ": " + (prefix != "" ? prefix + ": " : "") + msg +
(suffix != "" ? "; " + suffix : "");
data[last_entry].timestamp = TimeCurrent();
data[last_entry].log_level = _log_level;
data[last_entry].msg = msg;
return true;
}
/**
* Adds a log entry.
*/
bool Add(string msg, string prefix, string suffix, ENUM_LOG_LEVEL entry_log_level = V_INFO) {
return Add(prefix, msg, suffix, entry_log_level);
}
bool Add(ARRAY_REF(double, arr), string prefix, string suffix, ENUM_LOG_LEVEL entry_log_level = V_INFO) {
return Add(prefix, Array::ArrToString(arr), suffix, entry_log_level);
}
/**
* Reports an last error.
*/
bool AddLastError(string prefix = "", string suffix = "");
bool AddLastError(string prefix, long suffix);
/**
* Reports an error.
*/
bool Error(string msg, string prefix = "", string suffix = "") { return Add(V_ERROR, msg, prefix, suffix); }
/**
* Reports a warning.
*/
bool Warning(string msg, string prefix = "", string suffix = "") { return Add(V_WARNING, msg, prefix, suffix); }
/**
* Reports an info message.
*/
bool Info(string msg, string prefix = "", string suffix = "") { return Add(V_INFO, msg, prefix, suffix); }
/**
* Reports a debug message for debugging purposes.
*/
bool Debug(string msg, string prefix = "", string suffix = "") { return Add(V_DEBUG, msg, prefix, suffix); }
/**
* Reports a debug message for debugging purposes.
*/
bool Trace(string msg, string prefix = "", string suffix = "") { return Add(V_TRACE, msg, prefix, suffix); }
/**
* Link this instance with another log instance.
*/
void Link(Log *_log) {
PTR_ATTRIB(_log, SetLevel(log_level)); // Sets the same level as this instance.
// @todo: Make sure we're not linking the same instance twice.
logs.Push(_log);
}
/**
* Copy logs into another array.
*/
bool Copy(ARRAY_REF(log_entry, _logs)) {
// @fixme
// Error: 'ArrayCopy<log_entry>' - cannot to apply function template
// Array::ArrayCopy(_logs, data, 0, 0, WHOLE_ARRAY);
if (!ArrayResize(_logs, last_entry)) {
return false;
}
for (int i = 0; i < last_entry; i++) {
_logs[i] = data[i];
}
return ArraySize(_logs) > 0;
}
/**
* Append logs into another array.
*/
bool Append(ARRAY_REF(log_entry, _logs)) {
// @fixme
// Error: 'ArrayCopy<log_entry>' - cannot to apply function template
// Array::ArrayCopy(_logs, data, 0, 0, WHOLE_ARRAY);
unsigned int _size = ArraySize(_logs);
if (!ArrayResize(_logs, _size + last_entry)) {
return false;
}
for (int i = 0; i < last_entry; i++) {
_logs[_size + i] = data[i];
}
return ArraySize(_logs) > 0;
}
/**
* Flushes all log entries by printing them to the output.
*/
#ifdef __MQL__
template <>
#endif
void Flush(int _freq = 0, bool _dt = true) {
if (_freq > 0 && last_flush + _freq >= TimeCurrent()) {
// Avoids flushing logs too often.
return;
}
for (int i = 0; i <= last_entry; i++) {
if (data[i].timestamp > last_flush) {
Print((_dt ? DateTimeStatic::TimeToStr(data[i].timestamp) + ": " : ""), data[i].msg);
}
}
// Flush logs from another linked instances.
for (DictStructIterator<int, Ref<Log>> _li = logs.Begin(); _li.IsValid(); ++_li) {
Log *_log = _li.Value().Ptr();
if (Object::IsValid(_log)) {
PTR_ATTRIB(_log, Flush(_freq, _dt));
}
}
last_entry = -1;
last_flush = TimeCurrent();
}
/**
* Flushes all log entries by printing them to the output.
*/
/*
void Flush(bool _dt = true) {
Flush(log_level, _dt);
}
*/
virtual const string ToString() {
string result;
unsigned int lid;
int i;
for (i = 0; i <= last_entry; i++) {
result += DateTimeStatic::TimeToStr(data[i].timestamp) + ": " + data[i].msg + "\n";
}
Log *_log;
// Flush logs from another linked instances.
for (lid = 0; lid < logs.Size(); lid++) {
_log = logs[lid].Ptr();
if (Object::IsValid(_log)) {
result += PTR_ATTRIB(_log, ToString());
}
}
return result;
}
/**
* Save logs to file in CSV format.
*/
bool SaveToFile(string new_filename, ENUM_LOG_LEVEL _log_level) {
string filepath = new_filename != "" ? new_filename : filename;
int handle = FileOpen(filepath, FILE_WRITE | FILE_CSV, ':');
if (handle != INVALID_HANDLE) {
for (int i = 0; i < ArraySize(data); i++) {
if (data[i].log_level <= _log_level) {
FileWrite(handle, TimeToString(data[i].timestamp, TIME_DATE | TIME_MINUTES), ": ", data[i].msg);
}
}
FileClose(handle);
return true;
} else {
FileClose(handle);
return false;
}
}
bool SaveToFile(string new_filename = "") { return SaveToFile(new_filename, log_level); }
template <typename T>
void Erase(ARRAY_REF(T, A), int iPos) {
int iLast = ArraySize(A) - 1;
A[iPos].timestamp = A[iLast].timestamp;
A[iPos].msg = A[iLast].msg;
ArrayResize(A, iLast);
}
bool DeleteByTimestamp(datetime timestamp) {
int size = ArraySize(data);
if (size > 0) {
int offset = 0;
for (int i = 0; i < size; i++) {
if (data[i].timestamp == timestamp) {
Erase(data, i);
return true;
break;
}
}
}
return false;
}
};
#include "Terminal.mqh"
/**
* Reports last error.
*/
bool Log::AddLastError(string prefix, string suffix) {
return Add(V_ERROR, Terminal::GetLastErrorText(), prefix, suffix);
}
bool Log::AddLastError(string prefix, long suffix) {
return Add(V_ERROR, Terminal::GetLastErrorText(), prefix, StringFormat("%d", suffix));
}
#endif