-
Notifications
You must be signed in to change notification settings - Fork 0
/
debug_logging.nim
64 lines (55 loc) · 1.74 KB
/
debug_logging.nim
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
#****h* debug/logging
## PURPOSE
## Provides debug logging.
#******
import std/[macros, sets, logging, strutils, os]
import debug_tags
export debug_tags
export
logging.log, logging.info, logging.notice, logging.warn, logging.error
var consoleLog {.threadvar.}: Logger
template moduleName*: string =
os.splitFile(instantiationInfo().filename).name
#****if* logging/lineInfo
proc lineInfo(info: tuple[filename: string, line: int, column: int]): string =
## PURPOSE
## Return formatted lineinfo as a string.
## DESCRIPTION
## Used for logging.
## Formatted with a leading relative directory so link is clickable in VSCode.
format($CurDir/"$1($2, $3)", info.filename, info.line, info.column)
#******
#****f* logging/initLogging
proc initLogging*() =
## PURPOSE
## Initialize logging for the current thread.
## DESCRIPTION
## Different global logging variables such as log filter level are thread
## local variables and need initialized before use in each thread.
consoleLog = newConsoleLogger(fmtStr="$levelid: ", useStderr = true)
consoleLog.addHandler
when defined debug:
setLogFilter(lvlDebug)
#******
#****f* logging/debug(string,seq[string])
template debug*(msg: string, tags = @[moduleName()]) =
## PURPOSE
## Write formatted debug `msg` to `stderr`.
when debug_tags.inDebugTags(tags):
{.cast(noSideEffect).}:
logging.debug msg
#******
#****f* logging/fatal
template fatal*(msg: string) =
## PURPOSE
## Write formatted fatal `msg` to `stderr` and quit with error return code.
logging.fatal msg
quit 1
#******
when isMainModule:
when defined test:
import std/[tempfiles, unittest]
suite "debug logging":
test "init":
initLogging()
debug "init", tags = @["test"]