-
-
Notifications
You must be signed in to change notification settings - Fork 13
/
logging.lua
340 lines (299 loc) · 9.33 KB
/
logging.lua
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
-- Logging
-- Part of Live Simulator: 2, can be used as standalone library
--[[---------------------------------------------------------------------------
-- Copyright (c) 2020 Miku AuahDark
--
-- Permission is hereby granted, free of charge, to any person obtaining a
-- copy of this software and associated documentation files (the "Software"),
-- to deal in the Software without restriction, including without limitation
-- the rights to use, copy, modify, merge, publish, distribute, sublicense,
-- and/or sell copies of the Software, and to permit persons to whom the
-- Software is furnished to do so, subject to the following conditions:
--
-- The above copyright notice and this permission notice shall be included in
-- all copies or substantial portions of the Software.
--
-- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
-- OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
-- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
-- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
-- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
-- FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
-- DEALINGS IN THE SOFTWARE.
--]]---------------------------------------------------------------------------
local love = require("love")
local log = {}
-- Modify this variable to anything you want
local ENVIRONMENT_VARIABLE = "LIVESIM2_LOGLEVEL"
-- loglevel
-- 0 = no log message
-- 1 = error
-- 2 = warn (default)
-- 3 = info
-- 4 = debug
local level = tonumber(os.getenv(ENVIRONMENT_VARIABLE))
if not(level) or (level < 0 or level > 4) then
if love.filesystem then
level = tonumber((love.filesystem.read(ENVIRONMENT_VARIABLE)))
if not(level) or (level < 0 or level > 4) then
level = 2
end
else
level = 2
end
end
level = math.floor(level)
-- Default implementation
local function infoImpl(tag, text)
io.stderr:write("I[", tag, "] ", text, "\n")
end
local function warnImpl(tag, text)
io.stderr:write("W[", tag, "] ", text, "\n")
end
local function errorImpl(tag, text)
io.stderr:write("E[", tag, "] ", text, "\n")
end
local function debugImpl(tag, text)
io.stderr:write("D[", tag, "] ", text, "\n")
end
-- Codepath used if ANSI color code is supported
local function setupANSICode()
local function m(n)
return string.format("\27[%dm", n)
end
function warnImpl(tag, text)
io.stderr:write(m(1), m(33), "W[", tag, "] ", text, m(0), "\n")
end
function errorImpl(tag, text)
io.stderr:write(m(31), "E[", tag, "] ", text, m(0), "\n")
end
function debugImpl(tag, text)
io.stderr:write(m(1), m(37), "D[", tag, "] ", text, m(0), "\n")
end
end
if love._os == "Windows" then
-- Windows can have many options depending on Windows version and terminal used.
-- If one of these requirement is met, ANSI color code is used:
-- * if "WT_PROFILE_ID" environment variable is present (Windows Terminal)
-- * if "ANSICON" environment variable is present (ANSICON)
-- * if "ConEmuANSI" environment variable is "ON" (ConEmu)
-- * if it's possible to set VT100 mode to console (Windows 10 Anniv+)
-- Otherwise, NLog will fallback to Windows Console API for setting color (Windows 10 RTM or older)
if os.getenv("WT_PROFILE_ID") or os.getenv("ANSICON") or os.getenv("ConEmuANSI") == "ON" then
setupANSICode()
else
local hasFFI, ffi = pcall(require, "ffi")
if hasFFI then
local bit = require("bit")
local Kernel32 = ffi.C -- cache namespace
ffi.cdef [[
// coord structure
typedef struct logging_Coord {
int16_t x, y;
} logging_Coord;
// small rect structure
typedef struct logging_SmallRect {
int16_t l, t, r, b;
} logging_SmallRect;
// CSBI structure
typedef struct logging_CSBI {
logging_Coord csbiSize;
logging_Coord cursorPos;
int16_t attributes;
logging_SmallRect windowRect;
logging_Coord maxWindowSize;
} logging_CSBI;
void * __stdcall GetStdHandle(uint32_t );
int __stdcall SetConsoleMode(void *, uint32_t );
int __stdcall GetConsoleMode(void *, uint32_t *);
int __stdcall GetConsoleScreenBufferInfo(void *, logging_CSBI *);
int __stdcall SetConsoleTextAttribute(void *, int16_t );
]]
local stderr = Kernel32.GetStdHandle(-12)
-- Try to use VT100 processing if it's available
-- Reference: https://bugs.php.net/bug.php?id=72768
local cmode = ffi.new("uint32_t[1]")
Kernel32.GetConsoleMode(stderr, cmode);
-- Try to enable ENABLE_VIRTUAL_TERMINAL_PROCESSING (0x4)
if Kernel32.SetConsoleMode(stderr, bit.bor(cmode[0], 4)) > 0 then
-- ENABLE_VIRTUAL_TERMINAL_PROCESSING is supported. Use ANSI color codes
setupANSICode()
else
-- ENABLE_VIRTUAL_TERMINAL_PROCESSING is not supported. Fallback to Console APIs
local csbi = ffi.new("logging_CSBI[1]")
local function pushMode(mode)
Kernel32.GetConsoleScreenBufferInfo(stderr, csbi)
local m = csbi[0].attributes
Kernel32.SetConsoleTextAttribute(stderr, mode)
return m
end
local function popMode(mode)
Kernel32.SetConsoleTextAttribute(stderr, mode)
ffi.fill(csbi[0], ffi.sizeof("logging_CSBI"), 0)
end
function warnImpl(tag, text)
local m = pushMode(0x0004+0x0002+0x0008) -- bright yellow
io.stderr:write("W[", tag, "] ", text, "\n")
io.stderr:flush()
popMode(m)
end
function errorImpl(tag, text)
local m = pushMode(0x0004) -- red
io.stderr:write("E[", tag, "] ", text, "\n")
io.stderr:flush()
popMode(m)
end
function debugImpl(tag, text)
local m = pushMode(0x0004+0x0002+0x0001+0x0008) -- bright white
io.stderr:write("D[", tag, "] ", text, "\n")
io.stderr:flush()
popMode(m)
end
end
end
end
elseif love._os == "Linux" or love._os == "OS X" then
-- Well does macOS support this?
setupANSICode()
elseif love._os == "Android" then
local hasFFI, ffi = pcall(require, "ffi")
if hasFFI then
-- Use native Android logging library
local androidLog = ffi.load("log")
ffi.cdef[[
enum AndroidLogPriority {
unknown,
default,
verbose,
debug,
info,
warning, warn = 6,
error,
fatal,
silent
};
int __android_log_write(enum AndroidLogPriority, const char *tag, const char *text);
]]
function infoImpl(tag, text)
androidLog.__android_log_write("info", tag, text)
end
function warnImpl(tag, text)
androidLog.__android_log_write("warning", tag, text)
end
function errorImpl(tag, text)
androidLog.__android_log_write("error", tag, text)
end
function debugImpl(tag, text)
androidLog.__android_log_write("debug", tag, text)
end
else
-- Screw this, use print and hope for the best
function infoImpl(tag, text)
print("I["..tag.."] "..text.."\n")
end
function warnImpl(tag, text)
print("W["..tag.."] "..text.."\n")
end
function errorImpl(tag, text)
print("E["..tag.."] "..text.."\n")
end
function debugImpl(tag, text)
print("D["..tag.."] "..text.."\n")
end
end
end
local function infoImplMutex(_, tag, text)
return infoImpl(tag, text)
end
local function warnImplMutex(_, tag, text)
return warnImpl(tag, text)
end
local function errorImplMutex(_, tag, text)
return errorImpl(tag, text)
end
local function debugImplMutex(_, tag, text)
return debugImpl(tag, text)
end
local function initMutex()
if not(log.mutex) and love.thread then
-- Lock
log.mutex = love.thread.getChannel("logging.lock")
end
return not(not(log.mutex))
end
---@param tag string
---@param text string
function log.info(tag, text)
if level >= 3 then
if initMutex() then
return log.mutex:performAtomic(infoImplMutex, tag, text)
else
return infoImpl(tag, text)
end
end
end
---@param tag string
---@param text string
function log.infof(tag, text, ...)
return log.info(tag, string.format(text, ...))
end
---@param tag string
---@param text string
function log.warning(tag, text)
if level >= 2 then
if level >= 4 then
text = debug.traceback(text, 2)
end
if initMutex() then
return log.mutex:performAtomic(warnImplMutex, tag, text)
else
return warnImpl(tag, text)
end
end
end
log.warn = log.warning
---@param tag string
---@param text string
function log.warningf(tag, text, ...)
return log.warning(tag, string.format(text, ...))
end
log.warnf = log.warningf
---@param tag string
---@param text string
function log.error(tag, text)
if level >= 1 then
if level >= 4 then
text = debug.traceback(text, 2)
end
if initMutex() then
return log.mutex:performAtomic(errorImplMutex, tag, text)
else
return errorImpl(tag, text)
end
end
end
---@param tag string
---@param text string
function log.errorf(tag, text, ...)
return log.error(tag, string.format(text, ...))
end
---@param tag string
---@param text string
function log.debug(tag, text)
if level >= 4 then
if initMutex() then
return log.mutex:performAtomic(debugImplMutex, tag, text)
else
return debugImpl(tag, text)
end
end
end
---@param tag string
---@param text string
function log.debugf(tag, text, ...)
return log.debug(tag, string.format(text, ...))
end
function log.getLevel()
return level
end
return log