-
Notifications
You must be signed in to change notification settings - Fork 71
/
class_Logger.ahk
192 lines (176 loc) · 4.48 KB
/
class_Logger.ahk
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
/**
* Logger class (singleton) - Tailored to FeatShare.ahk
*
* FeatShare v0.2 - Text integration tool
* Copyright (C) 2016 szapp <http://github.com/szapp>
*
* This file is part of FeatShare.
* <http://github.com/szapp/FeatShare>
*
* FeatShare 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.
*
* FeatShare 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 FeatShare. If not, see <http://www.gnu.org/licenses/>.
*
*
* Third-party software:
*
* MPRESS v2.19, Copyright (C) 2007-2012 MATCODE Software,
* for license information see: /mpress/LICENSE
*
* AutoHotkey-JSON v2.1.1, 2013-2016 cocobelgica, WTFPL <http://wtfpl.net>
*
* Class_RichEdit v0.1.05.00, 2013-2015 just me,
* Unlicense <http://unlicense.org>
*
*
* Info: Comments are set to C++ style. Escape character is ` e.g.: `n
*/
#CommentFlag, //
#Include, funcStrRegEx.ahk
/**
* Logger class (singleton)
*/
class Logger {
// Static singleton instance
static instance = 0
// Small hack (no static methods in AHK)
static getInstance := Func("Logger_getInstance")
// Instance variables
path := ""
instantFlush := False
buffer := ""
showDebug := False
showWarn := True
timeformat := "yyyy-MM-dd HH:mm:ss"
history := {}
/**
* Create new Logger
*
* Don't allow creating a logger if there already exists one
* There is no private methods in AHK
*/
__New() {
if this.instance
return False
}
/**
* Append string to buffer (internal method)
*
* str String to add to buffer
* type Type of message (warn, debug,..)
*/
add(str, type) {
// Only log, if this message was not added so far
if this.history.HasKey(str) && (this.history[str] == type)
return True
this.buffer .= this.prefix(type) str "`n"
this.history[str] := type
if this.instantFlush
return this.flush()
return True
}
/**
* Logs event
*
* str String to add to buffer
*/
event(str) {
return this.add(str, "")
}
/**
* Logs debug
*
* str String to add to buffer
*/
debug(str) {
if this.showDebug
return this.add(str, "DEBUG")
return False
}
/**
* Logs warning
*
* str String to add to buffer
*/
warn(str) {
if this.showWarn
return this.add(str, "WARNING")
return False
}
/**
* Logs error
*
* str String to add to buffer
*/
error(str) {
return this.add(str, "ERROR")
}
/**
* Logs critical error
*
* str String to add to buffer
*/
critical(str) {
return this.add(str, "CRITICAL")
}
/**
* Wrapper for error and warning logging
*
* err True logs an error, False logs a warning
* str String to add to buffer
*/
issue(err, str) {
if err
return this.error(str)
else
return this.warn(str)
}
/**
* Write buffer to file (append)
*
* returns True on success, False otherwise
*/
flush() {
if !this.path
return False
if !(file := FileOpen(this.path, "a"))
return False
if !file.Write(this.buffer) {
file.Close()
return False
}
file.Close()
this.buffer := "" // Clear buffer if writing was successful
return True
}
/**
* Prefix from timestamp
*
* type Type of message
*
* returns String with timestamp
*/
prefix(type) {
FormatTime, timestamp, , % this.timeformat
return timestamp " :: " type fill(" ", 8-StrLen(type)) " :: "
}
}
/**
* Singleton method returning the only Logger instance
*
* returns Logger instance
*/
Logger_getInstance(this) {
if !this.instance
this.instance := new Logger()
return this.instance
}