/ tests / custom_stream.nim
custom_stream.nim
 1  import
 2    chronicles
 3  
 4  type
 5    MyRecord[Output] = object
 6      output*: Output
 7      first*: bool
 8  
 9  template initLogRecord*(r: var MyRecord, lvl: LogLevel,
10                          topics: string, name: string) =
11    r.output.append "[", $lvl, "] ", name, ": "
12    r.first = true
13  
14  template setProperty*(r: var MyRecord, key: string, val: auto) =
15    if r.first:
16      r.output.append " ("
17      r.first = false
18    else:
19      r.output.append(" ,")
20    r.output.append key, "=", $val
21  
22  template flushRecord*(r: var MyRecord) =
23    if not r.first:
24      r.output.append ")"
25    r.output.append "\n"
26  
27    r.output.flushOutput
28  
29  customLogStream myStream[MyRecord[StdOutOutput]]
30  
31  proc main =
32    logScope:
33      stream = myStream
34      key = "val"
35  
36    info "inside main"
37  
38  info("before main", a = 1, b = 3)
39  
40  main()
41  
42  info "after main"
43  
44  myStream.warn "exiting"
45