Skip to content

Commit

Permalink
Change internal logging format to JSON
Browse files Browse the repository at this point in the history
  • Loading branch information
PMunch committed Jun 2, 2020
1 parent ab0e3cc commit e2442ae
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/irclog.nim
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ proc `$`(s: seq[string]): string =
result = "[" & join(escaped, ",") & "]"

proc writeLog(logger: PLogger, msg: IRCEvent) =
logger.logFile.writeFlush($$(time: getTime(), msg: msg) & "\n")
logger.logFile.writeFlush($(%*{"time": getTime(), "msg": msg}) & "\n")

proc log*(logger: PLogger, msg: IRCEvent) =
if msg.origin != "#nim" and msg.cmd notin {MQuit, MNick}: return
Expand Down
7 changes: 4 additions & 3 deletions src/irclogrender.nim
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import irc, htmlgen, times, strutils, marshal, os, xmltree, re
import irc, htmlgen, times, strutils, marshal, os, xmltree, re, json
from jester import Request, makeUri
import irclog

type
Entry = tuple[time: Time, msg: IRCEvent]
TLogRenderer = object of TLogger
items*: seq[tuple[time: Time, msg: IRCEvent]] ## Only used for HTML gen
items*: seq[Entry] ## Only used for HTML gen
PLogRenderer* = ref TLogRenderer

proc loadRenderer*(f: string): PLogRenderer =
Expand All @@ -19,7 +20,7 @@ proc loadRenderer*(f: string): PLogRenderer =
result.logFilepath = f.splitFile.dir
while i < lines.len:
if lines[i] != "":
result.items.add(to[tuple[time: Time, msg: IRCEvent]](lines[i]))
result.items.add(json.to(lines[i].parseJson, Entry))
inc i

proc renderMessage(msg: string): string =
Expand Down
2 changes: 1 addition & 1 deletion src/nimbot.nim
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ asyncCheck state.ircClient.run()

var settings = newSettings(port = Port(5001))
routes:
get "/?":
get "/":
let curTime = getTime().utc()
let path = state.irclogsFilename / curTime.format("dd'-'MM'-'yyyy'.logs'")
var logs = loadRenderer(path)
Expand Down

0 comments on commit e2442ae

Please sign in to comment.