-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathconsole.brs
121 lines (95 loc) · 3.51 KB
/
console.brs
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
'
' console.brs
'
'
function ConsoleUtil() as Object
console = {
_timers: {}
_counters: {}
_groupLevel: 0
_outputIdent: ""
log: function(msg)
m._print(0, msg)
end function
info: function(msg)
m._print(1, msg)
end function
error: function(msg)
m._print(2, msg)
end function
assert: function(condition as Boolean, msg)
if not condition then m._print(3, msg)
end function
count: function(label = "default" as String)
counter = m._counters[label]
if counter = invalid then counter = 0
m._counters[label] = counter + 1
m.log(label + ": " + m._counters[label].toStr())
end function
countReset: function(label = "default" as String)
m._counters[label] = 0
m.log(label + ": " + m._counters[label].toStr())
end function
time: function(eventName as String)
m._startTimer(eventName.trim())
m.log(eventName + ": timer started")
end function
timeEnd: function(eventName)
ellapsedTime = m._endTimer(eventName.trim())
if ellapsedTime <> invalid then m.log(eventName + ": " + ellapsedTime + "ms")
end function
group: function()
m._groupLevel = m._groupLevel + 1
end function
groupEnd: function()
if m._groupLevel > 0 then m._groupLevel = m._groupLevel - 1
end function
_startTimer: function(event as String)
m._timers[event] = createObject("roTimespan")
end function
_endTimer: function(event as String) as Dynamic
if m._timers[event] = invalid then return invalid
eventTime = m._timers[event].totalMilliseconds().toStr()
m._timers.delete(event)
return eventTime
end function
'** Get timestamp with current time
'@return string with the following format: "HH:MM:SS:MMM"
_getTimestamp: function() as String
now = createObject("roDateTime")
now.toLocalTime()
hours = now.getHours()
mins = now.getMinutes()
seconds = now.getSeconds()
millis = now.getMilliseconds()
sHours = hours.toStr()
if sHours.len() = 1 then sHours = "0" + sHours
sMins = mins.toStr()
if sMins.len() = 1 then sMins = "0" + sMins
sSecs = seconds.toStr()
if sSecs.Len() = 1 then sSecs = "0" + sSecs
sMillis = millis.toStr()
if sMillis.len() = 2 then sMillis = "0" + sMillis
if sMillis.len() = 1 then sMillis = "00" + sMillis
return "[" + sHours + ":" + sMins + ":" + sSecs + ":" + sMillis + "] "
end function
_print: function(logLevel as Integer, output)
print m._getTimestamp(); m._getGroupIndent(); m._getLabelFromLogLevel(logLevel); output
end function
_getGroupIndent: function() as String
return string(m._groupLevel, " ")
end function
_getLabelFromLogLevel: function(logLevel as Integer) as String
if logLevel = 1 then
return "[INFO] "
else if logLevel = 2 then
return "[ERROR] "
else if logLevel = 3 then
return "[ASSERT] "
else
return ""
end if
end function
}
return console
end function