From 2215d5b15069874d7c9e5f924591552c491dd742 Mon Sep 17 00:00:00 2001 From: Paul Wilde Date: Wed, 11 Sep 2024 21:08:27 +0100 Subject: [PATCH] adding prelim procs for using a log file --- docs/norgbackupweb | 2 +- norg/model/log_type.nim | 27 +++++++++++++++++++++++++-- norg/norg.nim | 4 ++-- 3 files changed, 28 insertions(+), 5 deletions(-) diff --git a/docs/norgbackupweb b/docs/norgbackupweb index 9b5c6dd..b7d4362 160000 --- a/docs/norgbackupweb +++ b/docs/norgbackupweb @@ -1 +1 @@ -Subproject commit 9b5c6dd04825d33cb27a9c6715764ad7e23182c4 +Subproject commit b7d436287a9ecbe87466babf56d81f3c9fb56959 diff --git a/norg/model/log_type.nim b/norg/model/log_type.nim index dd63b73..29caf2f 100644 --- a/norg/model/log_type.nim +++ b/norg/model/log_type.nim @@ -8,9 +8,15 @@ type file*: string var logger*: ConsoleLogger -proc initLogger*(level: Level = lvlInfo, strfmt: string = "[$levelname] ") = +var filelogger*: FileLogger + +proc initLogger*(level: Level = lvlInfo, strfmt: string = "[$levelname] ", log_file: string = "") = logger = newConsoleLogger(level, strfmt) logger.log(lvlNotice, "Log Level: ", $level) + if log_file != "": + let str = "$time " & strfmt + filelogger = newFileLogger(log_file, levelThreshold=level, fmtStr=str) + filelogger.log(lvlNotice, "Log Level: ", $level) #proc initLoggerThread*(level: Level = lvlInfo, strfmt: string = "[$levelname] ") = # initLogThread(strfmt, level) @@ -36,13 +42,30 @@ proc toLogLevel*(s: string): Level = proc debug*(args: varargs[string,`$`]) = logger.log(lvlDebug, args.join(" ")) + if filelogger != nil: + filelogger.log(lvlDebug, args.join(" ")) + proc fatal*(args: varargs[string,`$`]) = - logger.log(lvlfatal, args.join(" ")) + logger.log(lvlFatal, args.join(" ")) + if filelogger != nil: + filelogger.log(lvlFatal, args.join(" ")) + proc warn*(args: varargs[string,`$`]) = logger.log(lvlwarn, args.join(" ")) + if filelogger != nil: + filelogger.log(lvlWarn, args.join(" ")) + proc notice*(args: varargs[string,`$`]) = logger.log(lvlnotice, args.join(" ")) + if filelogger != nil: + filelogger.log(lvlNotice, args.join(" ")) + proc info*(args: varargs[string,`$`]) = logger.log(lvlinfo, args.join(" ")) + if filelogger != nil: + filelogger.log(lvlInfo, args.join(" ")) + proc error*(args: varargs[string,`$`]) = logger.log(lvlerror, args.join(" ")) + if filelogger != nil: + filelogger.log(lvlError, args.join(" ")) diff --git a/norg/norg.nim b/norg/norg.nim index 9185174..ffd094a 100644 --- a/norg/norg.nim +++ b/norg/norg.nim @@ -11,7 +11,7 @@ proc showVersion() = echo "Norg Version: ", VERSION quit(0) -proc startLogger(log_info: LogInfo) = +proc startLogger(nc: NorgConfig, log_info: LogInfo) = initLogger(log_info.level, "[$levelname] ") proc start() = @@ -22,7 +22,7 @@ proc start() = elif norg_args.config_file != "": norg_config = parseConfigFile(norg_args.config_file) echo norg_config.log_info.level - startLogger(norg_config.log_info) + norg_config.startLogger(norg_config.log_info) if norg_config.source_directories.len > 0 and norg_config.repositories.len > 0: run_actions(norg_config.actions.before_everything) for repo in norg_config.repositories: