-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLogger.js
More file actions
156 lines (138 loc) · 3.94 KB
/
Logger.js
File metadata and controls
156 lines (138 loc) · 3.94 KB
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
/*
QML component for writing log messages from a MuseScore plugin.
Copyright (C) 2024 - 2025 Alessandro Culatti
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 VERSION = "2.1.1";
let loggerId = null;
const TRACE = 0;
const INFO = 1;
const WARNING = 2;
const ERROR = 3;
const FATAL = 4;
const LOG_LEVEL_NAMES = [
"TRACE",
"INFO",
"WARNING",
"ERROR",
"FATAL",
];
// Current log level. Only messages with a level equal or greater than this
// will be logged.
let logLevel = null;
let logMessages = null;
const SEPARATOR = "\t";
/**
* Initialise the logger with input ID, and optionally with the input log level.
* Also initialise the log file path with the current date time, and to be in
* the specified folder. The folder must already exist, this library does not
* create it if it's missing.
*/
function initialise(id, level = ERROR, folderPath = "logs")
{
loggerId = id;
loggerId.source = Qt.resolvedUrl(".").toString() + folderPath + "/" + getFileDateTime() + "_log.txt";
logLevel = level;
logMessages = "";
}
/**
* Log the input message with the specified log level, or INFO if no log level
* is specified.
*/
function log(message, level = INFO)
{
if (level >= logLevel)
{
logMessages += `${getRFC3339DateTime()}${SEPARATOR}${LOG_LEVEL_NAMES[level]}${SEPARATOR}${message}\n`;
}
}
/**
* Log the input message with TRACE level.
*/
function trace(message)
{
log(message, TRACE);
}
/**
* Log the input message with WARNING level.
*/
function warning(message)
{
log(message, WARNING);
}
/**
* Log the input message with ERROR level.
*/
function err(message)
{
log(message, ERROR);
}
/**
* Log the input message with FATAL level.
*/
function fatal(message)
{
log(message, FATAL);
}
/**
* Write the log messages to the log file. This should be called at the end of
* the plugin, to write the log messages, if any, to the log file.
*/
function writeLogs()
{
if (logMessages)
{
loggerId.write(logMessages);
}
}
/**
* Log every property of the input object, with the specified level, or INFO if
* no log level is specified.
*/
function logProperties(obj, level = INFO)
{
let s = "" + obj + ":";
for (let key in obj)
{
s += "\n\t" + key + ": " + obj[key];
}
log(s, level);
}
/**
* Return the current date time in a format compatible with file names.
*/
function getFileDateTime()
{
let currentDate = new Date();
let year = currentDate.getFullYear();
let month = String(currentDate.getMonth() + 1).padStart(2, "0");
let day = String(currentDate.getDate()).padStart(2, "0");
let hours = String(currentDate.getHours()).padStart(2, "0");
let minutes = String(currentDate.getMinutes()).padStart(2, "0");
let seconds = String(currentDate.getSeconds()).padStart(2, "0");
return `${year}-${month}-${day}_${hours}-${minutes}-${seconds}`;
}
/**
* Return the current date time in the RFC3339 format.
*/
function getRFC3339DateTime()
{
let currentDate = new Date();
let year = currentDate.getFullYear();
let month = String(currentDate.getMonth() + 1).padStart(2, "0");
let day = String(currentDate.getDate()).padStart(2, "0");
let hours = String(currentDate.getHours()).padStart(2, "0");
let minutes = String(currentDate.getMinutes()).padStart(2, "0");
let seconds = String(currentDate.getSeconds()).padStart(2, "0");
let milliseconds = String(currentDate.getMilliseconds()).padStart(3, "0");
return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}.${milliseconds}`;
}