Skip to content
This repository
Browse code

Added default format for logging.

  • Loading branch information...
commit f50a81e7304c34f46694281307f717c940bd7751 1 parent 1a8118a
Yury Benesh exhu authored

Showing 1 changed file with 64 additions and 29 deletions. Show diff stats Hide diff stats

  1. +64 29 devel/logging.nim
93 devel/logging.nim
@@ -35,42 +35,31 @@ const
35 35 "DEBUG", "DEBUG", "INFO", "WARN", "ERROR", "FATAL", "NONE"
36 36 ]
37 37
  38 + defaultFmtStr = "" ## default string between log level and message per logger
  39 + verboseFmtStr = "$date $time "
  40 +
38 41 type
39 42 TLogger* = object of TObject ## abstract logger; the base type of all loggers
40 43 levelThreshold*: TLevel ## only messages of level >= levelThreshold
41 44 ## should be processed
  45 + fmtStr: string ## = defaultFmtStr by default, see substituteLog for $date etc.
  46 +
42 47 TConsoleLogger* = object of TLogger ## logger that writes the messages to the
43 48 ## console
44 49
45 50 TFileLogger* = object of TLogger ## logger that writes the messages to a file
46 51 f: TFile
47 52
48   - # TODO: implement rolling log
  53 + # TODO: implement rolling log, will produce filename.1, filename.2 etc.
49 54 TRollingFileLogger* = object of TFileLogger ## logger that writes the
50 55 ## message to a file
51   - maxLines: int # maximum number of lines
52   - lines: seq[string]
  56 + maxLines: int # maximum number of lines
53 57 curLine : int
  58 + baseName: string # initial filename
  59 + logFiles: int # how many log files already created, e.g. basename.1, basename.2...
  60 +
54 61
55 62
56   -method log*(L: ref TLogger, level: TLevel,
57   - frmt: string, args: openArray[string]) =
58   - ## override this method in custom loggers. Default implementation does
59   - ## nothing.
60   - nil
61   -
62   -method log*(L: ref TConsoleLogger, level: TLevel,
63   - frmt: string, args: openArray[string]) =
64   - Writeln(stdout, LevelNames[level], " ", frmt % args)
65   -
66   -method log*(L: ref TFileLogger, level: TLevel,
67   - frmt: string, args: openArray[string]) =
68   - Writeln(L.f, LevelNames[level], " ", frmt % args)
69   -
70   -proc defaultFilename*(): string =
71   - ## returns the default filename for a logger
72   - var (path, name, ext) = splitFile(getAppFilename())
73   - result = changeFileExt(path / name & "_" & getDateStr(), "log")
74 63
75 64 proc substituteLog*(frmt: string): string =
76 65 ## converts $date to the current date
@@ -96,41 +85,75 @@ proc substituteLog*(frmt: string): string =
96 85 of "app": result.add(app)
97 86 of "appdir": result.add(app.splitFile.dir)
98 87 of "appname": result.add(app.splitFile.name)
  88 +
  89 +
  90 +
  91 +method log*(L: ref TLogger, level: TLevel,
  92 + frmt: string, args: openArray[string]) =
  93 + ## override this method in custom loggers. Default implementation does
  94 + ## nothing.
  95 + nil
  96 +
  97 +method log*(L: ref TConsoleLogger, level: TLevel,
  98 + frmt: string, args: openArray[string]) =
  99 + Writeln(stdout, LevelNames[level], " ", substituteLog(L.fmtStr), frmt % args)
  100 +
  101 +method log*(L: ref TFileLogger, level: TLevel,
  102 + frmt: string, args: openArray[string]) =
  103 + Writeln(L.f, LevelNames[level], " ", substituteLog(L.fmtStr), frmt % args)
  104 +
  105 +proc defaultFilename*(): string =
  106 + ## returns the default filename for a logger
  107 + var (path, name, ext) = splitFile(getAppFilename())
  108 + result = changeFileExt(path / name & "_" & getDateStr(), "log")
  109 +
99 110
100 111
  112 +
  113 +proc newConsoleLogger*(levelThreshold = lvlAll) : ref TConsoleLogger =
  114 + new result
  115 + result.fmtStr = defaultFmtStr
  116 + result.levelThreshold = levelThreshold
  117 +
101 118 proc newFileLogger*(filename = defaultFilename(),
102 119 mode: TFileMode = fmAppend,
103 120 levelThreshold = lvlAll): ref TFileLogger =
104 121 new(result)
105 122 result.levelThreshold = levelThreshold
106 123 result.f = open(filename, mode)
  124 + result.fmtStr = defaultFmtStr
107 125
108 126 # ------
109 127
  128 +proc readLogLines(logger : ref TRollingFileLogger) = nil
  129 + #f.readLine # TODO read all lines, update curLine
  130 +
  131 +
110 132 proc newRollingFileLogger*(filename = defaultFilename(),
111 133 mode: TFileMode = fmReadWrite,
112 134 levelThreshold = lvlAll,
113 135 maxLines = 1000): ref TRollingFileLogger =
114 136 new(result)
115 137 result.levelThreshold = levelThreshold
  138 + result.fmtStr = defaultFmtStr
116 139 result.maxLines = maxLines
117 140 result.f = open(filename, mode)
118 141 result.curLine = 0
119   - if mode in {fmReadWrite, fmReadWriteExisting}:
120   - readLogLines(result)
  142 +
  143 + # TODO count all number files
  144 + # count lines in existing filename file
  145 + # if >= maxLines then rename to next numbered file and create new file
  146 +
  147 + #if mode in {fmReadWrite, fmReadWriteExisting}:
  148 + # readLogLines(result)
121 149
122   -proc readLogLines(logger : ref TRollingFileLogger) =
123   - f.readLine # TODO read all lines, update curLine
124 150
125 151
126 152 method log*(L: ref TRollingFileLogger, level: TLevel,
127 153 frmt: string, args: openArray[string]) =
128 154 # TODO
129 155 # if more than maxlines, then set cursor to zero
130   - # otherwise add new line
131   - # update line in the lines
132   - # increment cursor
133   - # store lines into file
  156 +
134 157 Writeln(L.f, LevelNames[level], " ", frmt % args)
135 158
136 159 # --------
@@ -173,3 +196,15 @@ template fatal*(frmt: string, args: openarray[string]) =
173 196 ## logs a fatal error message
174 197 log(lvlFatal, frmt, args)
175 198
  199 +
  200 +# --------------
  201 +
  202 +when isMainModule:
  203 + var L = newConsoleLogger()
  204 + var fL = newFileLogger("test.log")
  205 + fL.fmtStr = verboseFmtStr
  206 + handlers.add(L)
  207 + handlers.add(fL)
  208 + info("hello", [])
  209 +
  210 +

0 comments on commit f50a81e

Please sign in to comment.
Something went wrong with that request. Please try again.