-
Notifications
You must be signed in to change notification settings - Fork 26
/
Log.ttslua
139 lines (107 loc) · 4.02 KB
/
Log.ttslua
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
local Check = require("Kdm/Util/Check")
local Console = require("Kdm/Console")
local Util = require("Kdm/Util/Util")
---------------------------------------------------------------------------------------------------
local Log = {}
Log.__index = Log
Log.__call = function(self, ...) log(...) end
Log.logsByModule = {}
Log.DEBUG_MODULES = {
--["BattleUi"] = true,
--["Campaign"] = true,
--["Expansion"] = true,
--["Hunt"] = true,
--["Location"] = true,
--["Player"] = true,
--["Rules"] = true,
--["Showdown"] = true,
--["Survivor"] = true,
--["Timeline"] = true,
}
Log.POST_INIT_DEBUG_MODULES = {
--["Ui"] = true,
}
---------------------------------------------------------------------------------------------------
function Log.Create(module)
local log = {
module = module
}
setmetatable(log, Log)
log:EnableDebug(Log.DEBUG_MODULES[module])
return log
end
---------------------------------------------------------------------------------------------------
function Log:Printf(fmt, ...)
fmt = fmt or ""
local msg = Util.SafeFormat(fmt, ...)
log(Util.SafeFormat("[%s] "..fmt, self.module, ...), nil, "print")
printToAll(Util.SafeFormat("[66aaff]"..fmt, ...))
end
---------------------------------------------------------------------------------------------------
function Log:DisabledDebugf(fmt, ...) end
function Log:EnabledDebugf(fmt, ...)
fmt = fmt or ""
local s = Util.SafeFormat("[%s] "..fmt, self.module, ...)
log(s, nil, "debug")
end
---------------------------------------------------------------------------------------------------
function Log:Errorf(fmt, ...)
fmt = fmt or ""
log(Util.SafeFormat("[%s] "..fmt, self.module, ...), nil, "error")
printToAll(Util.SafeFormat("[ff4444]"..fmt, ...))
end
---------------------------------------------------------------------------------------------------
function Log:Broadcastf(fmt, ...)
fmt = fmt or ""
log(Util.SafeFormat("[%s] "..fmt, self.module, ...), nil, "print")
broadcastToAll(Util.SafeFormat(fmt, ...))
end
---------------------------------------------------------------------------------------------------
function Log:EnableDebug(enable)
if enable then
self.Debugf = Log.EnabledDebugf
else
self.Debugf = Log.DisabledDebugf
end
end
---------------------------------------------------------------------------------------------------
function Log.Init()
logStyle("debug", { r = 1.0, g = 1.0, b = 1.0 }, "", "")
logStyle("print", { r = 0.0, g = 1.0, b = 1.0 }, "", "")
logStyle("error", { r = 1.0, g = 0.0, b = 0.0 }, "", "")
Console.AddCommand("debug", function(args)
if #args != 3 or (args[3] != "on" and args[3] != "off") then
return Console.Printf("Usage: debug <module> <on|off>")
end
local module = args[2]
local log = Log.logsByModule[module]
if not log then
return Console.Printf("Unknown module '%s'", module)
end
local enabled = args[3] == "on"
log:EnableDebug(enabled)
Console.Printf("%s debugging for %s", enabled and "Enabled" or "Disabled", log.module)
end, "Toggles debug logging for specific modules")
end
---------------------------------------------------------------------------------------------------
function Log.PostInit()
for module, _ in pairs(Log.POST_INIT_DEBUG_MODULES) do
Log.logsByModule[module]:EnableDebug(true)
end
end
---------------------------------------------------------------------------------------------------
function Log.ForModule(module)
local log = Log.logsByModule[module]
if not log then
log = Log.Create(module)
Log.logsByModule[module] = log
Log.logsByModule[module:lower()] = log -- for console commands, which come in all lowercase
end
return log
end
---------------------------------------------------------------------------------------------------
return {
Init = Log.Init,
PostInit = Log.PostInit,
ForModule = Log.ForModule,
}