-
Notifications
You must be signed in to change notification settings - Fork 0
/
logger.lua
150 lines (125 loc) · 4.12 KB
/
logger.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
local addon, dark_addon = ...
dark_addon.console = {
debugLevel = 0,
file = '',
line = ''
}
local fontObject = CreateFont("dark_addon_console")
fontObject:SetFont("Interface\\Addons\\dark_addon\\media\\Consolas.ttf", 12)
local consoleFrame = CreateFrame('ScrollingMessageFrame', 'dark_addon_console', UIParent)
consoleFrame:SetFontObject("dark_addon_console")
-- position and setup
consoleFrame:SetPoint('CENTER', UIParent)
consoleFrame:SetMaxLines(1000)
consoleFrame:SetInsertMode('BOTTOM')
consoleFrame:SetWidth(500)
consoleFrame:SetHeight(145)
consoleFrame:SetJustifyH('LEFT')
consoleFrame:SetFading(false)
consoleFrame:SetClampedToScreen(true)
consoleFrame:Hide()
-- setup background
consoleFrame.background = consoleFrame:CreateTexture('background')
consoleFrame.background:SetPoint('TOPLEFT', consoleFrame, 'TOPLEFT', -5, 5)
consoleFrame.background:SetPoint('BOTTOMRIGHT', consoleFrame, 'BOTTOMRIGHT', 5, -5)
consoleFrame.background:SetColorTexture(0, 0, 0, 0.75)
consoleFrame.background2 = consoleFrame:CreateTexture('background')
consoleFrame.background2:SetPoint('TOPLEFT', consoleFrame, 'TOPLEFT', -7, 7)
consoleFrame.background2:SetPoint('BOTTOMRIGHT', consoleFrame, 'BOTTOMRIGHT', 7, -7)
consoleFrame.background2:SetColorTexture(20/255, 20/255, 20/255, 0.4)
-- make draggable
consoleFrame:SetMovable(true)
consoleFrame:EnableMouse(true)
consoleFrame:RegisterForDrag('LeftButton')
consoleFrame:SetScript('OnDragStart', consoleFrame.StartMoving)
consoleFrame:SetScript('OnDragStop', consoleFrame.StopMovingOrSizing)
-- scrolling
consoleFrame:SetScript('OnMouseWheel', function(self, delta)
if delta > 0 then
if IsShiftKeyDown() then
self:ScrollToTop()
else
self:ScrollUp()
end
else
if IsShiftKeyDown() then
self:ScrollToBottom()
else
self:ScrollDown()
end
end
end)
-- display frame
function dark_addon.console.set_level(level)
level = tonumber(level) or 0
dark_addon.console.debugLevel = level
dark_addon.settings.store('debug_level', level)
end
function dark_addon.console.toggle(show)
show = show
dark_addon.settings.store('debug_show', show)
if show then
consoleFrame:Show()
else
consoleFrame:Hide()
end
end
local function join(...)
local ret = ''
for n = 1, select('#', ...) do
ret = ret .. ', ' .. tostring(select(n, ...))
end
return ret:sub(3)
end
local colorize = dark_addon.interface.colorize
local last = false
function dark_addon.console.log_time(str)
local at = date('%H:%M:%S', time())
local joined = string.format('%s %s', at, str)
if last ~= joined then
dark_addon.console.log(joined)
last = joined
end
end
function dark_addon.console.log(...)
consoleFrame:AddMessage(...)
end
function dark_addon.console.notice(...)
dark_addon.console.log(date('%H:%M:%S', time())..'|cff91FF00[notice]|r ' .. join(...))
end
function dark_addon.console.debug(level, section, color, ...)
if dark_addon.console.debugLevel >= level then
dark_addon.console.log_time(
string.format(
'%s %s',
colorize(color, '[' .. section .. ']'),
join(...)
)
)
end
end
function dark_addon.log(string, ...)
local formatted = string.format(string, ...)
print('|cff' .. dark_addon.color .. '['..dark_addon.name..']|r ' .. formatted)
end
function dark_addon.error(...)
print('|cff' .. dark_addon.color .. '['..dark_addon.name..']|r |cffc32425' .. join(...) .. '|r')
end
local function round(num, numDecimalPlaces)
local mult = 10^(numDecimalPlaces or 0)
return math.floor(num * mult + 0.5) / mult
end
function dark_addon.format(value)
if tonumber(value) then
return round(value, 2)
else
return tostring(value)
end
end
dark_addon.on_ready(function()
local debug_level = dark_addon.settings.fetch('debug_level', nil)
dark_addon.console.set_level(debug_level)
local toggle = dark_addon.settings.fetch('debug_show', false)
dark_addon.console.toggle(toggle)
dark_addon.console.log("Welcome!")
end)