-
Notifications
You must be signed in to change notification settings - Fork 1
/
Arbor.kt
297 lines (249 loc) · 6.56 KB
/
Arbor.kt
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
package com.toxicbakery.logging
import kotlin.jvm.JvmOverloads
import kotlin.jvm.JvmStatic
import kotlin.native.concurrent.ThreadLocal
/**
* Primary interface to sow, harvest, and perform logging actions against seedlings. The implementation is no op by
* default requiring a seedling to be sown for end user usage.
*/
@Suppress("TooManyFunctions")
@ThreadLocal
object Arbor {
/**
* Debug level message
*/
const val DEBUG = 1
/**
* Verbose level message
*/
const val VERBOSE = 2
/**
* Info level message
*/
const val INFO = 3
/**
* Warning level message
*/
const val WARNING = 4
/**
* Error level message
*/
const val ERROR = 5
/**
* WTF level message
*/
const val WTF = 6
private val taglessBranch: Branch = Branch()
private val perennial: MutableSet<ISeedling> = mutableSetOf()
/**
* Immutable set of all sown seedlings in the forest.
*/
@JvmStatic
val forest: Set<ISeedling>
get() = perennial
/**
* Create a tagged seedling. Tags use a shallow copy snapshot of the current forest for all logging operations.
*/
@JvmStatic
fun tag(tag: String): Branch = Branch(tag)
/**
* Sow a seedling into the forest. Logs will call seedlings in the order they have been sown.
*
* @return true if added, false if previously added
*/
@JvmStatic
fun sow(seedling: ISeedling) = perennial.add(seedling)
/**
* Harvest a seedling from the forest.
*
* @return true if removed
*/
@JvmStatic
fun harvest(seedling: ISeedling) = perennial.remove(seedling)
/**
* Clear the forest of all sown seedlings returning [Arbor] to its natural state.
*/
@JvmStatic
fun reset() = perennial.clear()
/**
* Log a debug message.
*/
@JvmStatic
fun d(msg: String) = taglessBranch.d(msg)
/**
* Log a debug message.
*/
@JvmStatic
fun d(msg: () -> String) = taglessBranch.d(msg)
/**
* Log a debug message.
*/
@JvmStatic
@JvmOverloads
fun d(throwable: Throwable, msg: String = "") = taglessBranch.d(throwable, msg)
/**
* Log a debug message.
*/
@JvmStatic
fun d(msg: String, vararg args: Any?) = taglessBranch.d(msg, args)
/**
* Log a debug message.
*/
@JvmStatic
fun d(throwable: Throwable, msg: String, vararg args: Any?) = taglessBranch.d(throwable, msg, args)
/**
* Log a verbose message.
*/
@JvmStatic
fun v(msg: String) = taglessBranch.v(msg)
/**
* Log a verbose message.
*/
@JvmStatic
fun v(msg: () -> String) = taglessBranch.v(msg)
/**
* Log a verbose message.
*/
@JvmStatic
@JvmOverloads
fun v(throwable: Throwable, msg: String = "") = taglessBranch.v(throwable, msg)
/**
* Log a verbose message.
*/
@JvmStatic
fun v(msg: String, vararg args: Any?) = taglessBranch.v(msg, args)
/**
* Log a verbose message.
*/
@JvmStatic
fun v(throwable: Throwable, msg: String, vararg args: Any?) = taglessBranch.v(throwable, msg, args)
/**
* Log an info message.
*/
@JvmStatic
fun i(msg: String) = taglessBranch.i(msg)
/**
* Log an info message.
*/
@JvmStatic
fun i(msg: () -> String) = taglessBranch.i(msg)
/**
* Log an info message.
*/
@JvmStatic
@JvmOverloads
fun i(throwable: Throwable, msg: String = "") = taglessBranch.i(throwable, msg)
/**
* Log a info message.
*/
@JvmStatic
fun i(msg: String, vararg args: Any?) = taglessBranch.i(msg, args)
/**
* Log a info message.
*/
@JvmStatic
fun i(throwable: Throwable, msg: String, vararg args: Any?) = taglessBranch.i(throwable, msg, args)
/**
* Log a warning message.
*/
@JvmStatic
fun w(msg: String) = taglessBranch.w(msg)
/**
* Log a warning message.
*/
@JvmStatic
fun w(msg: () -> String) = taglessBranch.w(msg)
/**
* Log a warning message.
*/
@JvmStatic
@JvmOverloads
fun w(throwable: Throwable, msg: String = "") = taglessBranch.w(throwable, msg)
/**
* Log a warning message.
*/
@JvmStatic
fun w(msg: String, vararg args: Any?) = taglessBranch.w(msg, args)
/**
* Log a warning message.
*/
@JvmStatic
fun w(throwable: Throwable, msg: String, vararg args: Any?) = taglessBranch.w(throwable, msg, args)
/**
* Log an error message.
*/
@JvmStatic
fun e(msg: String) = taglessBranch.e(msg)
/**
* Log an error message.
*/
@JvmStatic
fun e(msg: () -> String) = taglessBranch.e(msg)
/**
* Log an error message.
*/
@JvmStatic
@JvmOverloads
fun e(throwable: Throwable, msg: String = "") = taglessBranch.e(throwable, msg)
/**
* Log a error message.
*/
@JvmStatic
fun e(msg: String, vararg args: Any?) = taglessBranch.e(msg, args)
/**
* Log a error message.
*/
@JvmStatic
fun e(throwable: Throwable, msg: String, vararg args: Any?) = taglessBranch.e(throwable, msg, args)
/**
* Log a wtf message.
*/
@JvmStatic
fun wtf(msg: String) = taglessBranch.wtf(msg)
/**
* Log a wtf message.
*/
@JvmStatic
fun wtf(msg: () -> String) = taglessBranch.wtf(msg)
/**
* Log a wtf message.
*/
@JvmStatic
@JvmOverloads
fun wtf(throwable: Throwable, msg: String = "") = taglessBranch.wtf(throwable, msg)
/**
* Log a wtf message.
*/
@JvmStatic
fun wtf(msg: String, vararg args: Any?) = taglessBranch.wtf(msg, args)
/**
* Log a wtf message.
*/
@JvmStatic
fun wtf(throwable: Throwable, msg: String, vararg args: Any?) = taglessBranch.wtf(throwable, msg, args)
}
enum class LogLevel { D, V, I, W, E, WTF }
private val defaultBranch: Branch = Arbor.tag("2143fef6-18e0-4341-a1de-b043ba5e32cd")
fun arbor(
logLevel: LogLevel = LogLevel.D,
tag: Branch = defaultBranch,
message: () -> String
) = if (tag == defaultBranch) {
when (logLevel) {
LogLevel.D -> Arbor.d(message)
LogLevel.V -> Arbor.v(message)
LogLevel.I -> Arbor.i(message)
LogLevel.W -> Arbor.w(message)
LogLevel.E -> Arbor.e(message)
LogLevel.WTF -> Arbor.wtf(message)
}
} else {
when (logLevel) {
LogLevel.D -> tag.d(message)
LogLevel.V -> tag.v(message)
LogLevel.I -> tag.i(message)
LogLevel.W -> tag.w(message)
LogLevel.E -> tag.e(message)
LogLevel.WTF -> tag.wtf(message)
}
}