logutils.nim 1.7 KB
Newer Older
1
#[
Nong Hoang Tu's avatar
Nong Hoang Tu committed
2
3
4
5
6
7
8
9
10
11
12
  Description:
    Module that control file logger and console logger
  Authors: 2020, Nong Hoang Tu <dmknght@parrotsec.org>
  Contributors:
  Module structure:
    - initLogs: Create object of FileLogger and ConsoleLogger with custom format
    - initResultLogs: Call init log with result log as destination path
    - initFullLogs: Call init log with full log as destination path
    - logInfo: Show log info to screen and save it to file. Use color bright cyan for console
    - logErr: Show log error to screen and save it to file with stderr bright magenta for console
  TODO:
13
14
]#

Nong Hoang Tu's avatar
Nong Hoang Tu committed
15
16
17
18
import .. / cores / configs
import logging


19
20
21
22
23
24
25
26
27
28
29
30
31
32
proc initLogs(fullPath: string): LogHandler =
  var
    resultLogFile = newFileLogger(
      fullPath,
      fmtStr = "$levelname [$date $time] "
    )
    resultLogConsole = newConsoleLogger(
      fmtStr = "$levelname [$date $time] "
    )
  var log = LogHandler(
    print: resultLogConsole,
    save: resultLogFile
  )
  return log
Nong Hoang Tu's avatar
Nong Hoang Tu committed
33
34


35
36
37
proc initResultLogs*(path: string): LogHandler =
  let fullPath = path & "result.log"
  return initLogs(fullPath)
Nong Hoang Tu's avatar
Nong Hoang Tu committed
38
39


40
41
42
proc initFullLogs*(path: string): LogHandler =
  let fullPath = path & "full.log"
  return initLogs(fullPath)
Nong Hoang Tu's avatar
Nong Hoang Tu committed
43
44


Nong Hoang Tu's avatar
Nong Hoang Tu committed
45
proc logInfo*(handler: LogHandler, prjName, msg: string) =
Nong Hoang Tu's avatar
Nong Hoang Tu committed
46
  # bright cyan
47
  let fullMsg = "[" & prjName & "] " & msg
Nong Hoang Tu's avatar
Nong Hoang Tu committed
48
49
  handler.print.log(lvlInfo, "\e[96m" & fullMsg & "\e[0m")
  handler.save.log(lvlInfo, "\e[96m" & fullMsg & "\e[0m")
Nong Hoang Tu's avatar
Nong Hoang Tu committed
50
51


52
proc logErr*(handler: LogHandler, prjName, msg, stderr: string) =
Nong Hoang Tu's avatar
Nong Hoang Tu committed
53
  # bright magenta
54
  let fullMsg = "[" & prjName & "] " & msg
Nong Hoang Tu's avatar
Nong Hoang Tu committed
55
  handler.print.log(lvlError, "\e[95m" & fullMsg & "\e[0m")
56
57
58
  handler.save.log(lvlError, "\e[95m" & fullMsg & "\e[0m\n")
  if stdErr != "":
    handler.save.log(lvlError, stderr)