From 83d77f88651c32aad576ac996c25c68684a17805 Mon Sep 17 00:00:00 2001 From: Quad Date: Wed, 13 Apr 2022 22:22:52 +0200 Subject: [PATCH] Implement logging/formatting --- src/modules/blog.nim | 2 ++ src/modules/db.nim | 3 ++- src/modules/formatter.nim | 52 +++++++++++++++++++++++++++++++++++++++ src/modules/views.nim | 2 ++ src/nimblog.nim | 14 +++++++---- 5 files changed, 67 insertions(+), 6 deletions(-) create mode 100644 src/modules/formatter.nim diff --git a/src/modules/blog.nim b/src/modules/blog.nim index 67699da..50e729c 100644 --- a/src/modules/blog.nim +++ b/src/modules/blog.nim @@ -1,6 +1,7 @@ import prologue import prologue/middlewares/staticfile import views +import formatter proc runWebsite(settings: Table[string, bool], dbSettings: Table[string, string]) = let prologueSettings = newSettings( @@ -13,6 +14,7 @@ proc runWebsite(settings: Table[string, bool], dbSettings: Table[string, string] website.get("/about", getAbout) website.use(staticFileMiddleware("static")) + logPrint("Press Ctrl-C to stop site", "Info") website.run() export runWebsite diff --git a/src/modules/db.nim b/src/modules/db.nim index 8b01886..161c1c6 100644 --- a/src/modules/db.nim +++ b/src/modules/db.nim @@ -1,5 +1,6 @@ import tables import std/[os, db_sqlite, strutils, streams] +import formatter proc createDb(dbSettings: Table): bool = if not fileExists(dbSettings["path"]): @@ -25,7 +26,7 @@ proc createDb(dbSettings: Table): bool = if command == "\c\n" or command == "\n": continue db.exec(sql(command.strip)) - echo "Ran command: " & command.strip + logPrint("Ran SQL command from schema", "Info") db.close() return true diff --git a/src/modules/formatter.nim b/src/modules/formatter.nim new file mode 100644 index 0000000..d556fba --- /dev/null +++ b/src/modules/formatter.nim @@ -0,0 +1,52 @@ +import tables +import strutils +import math + +proc colorEsc(color: string): string {.gcsafe.} = + # Stored in proc to be gcsafe + let colors = { + "black": 30, + "red": 31, + "green": 32, + "orange": 33, + "blue": 34, + "purple": 35, + "cyan": 36, + "reset": 0 + }.toTable() + + return "\e[" & intToStr(colors[color]) & "m" + +proc colorize(text: string, color: string): string {.gcsafe.} = + return colorEsc(color) & text & colorEsc("reset") + +proc strCenter(text: string, filler: string = "-", length: int = 10): string {.gcsafe.} = + # Simply truncate and return if it fills the entire length + if text.len() > length: + return text[0..length-1] + + # Find remaining characters and how much to pad each side + var remainHalf: float = (length - text.len()) / 2 + var remainLeft: int = remainHalf.floor().int + var remainRight: int = remainHalf.ceil().int + + return filler.repeat(remainLeft) & text & filler.repeat(remainRight) + +proc logFormat(text: string, level: string = "undef"): string {.gcsafe.} = + # Stored in proc to be gcsafe + let colorMapping = { + "Main": "green", + "Error": "red", + "Info": "purple", + "Warning": "orange", + "undef": "cyan" + }.toTable() + + var prefix = "[" & colorize(text=strCenter(level), color=colorMapping[level]) & "]" + return prefix & " " & text + +proc logPrint(text: string = "Undefined error message", level: string = "undef") {.gcsafe.} = + # In separate function in case I need to for example also write it to a file in the future. + echo logFormat(text=text, level=level) + +export logPrint diff --git a/src/modules/views.nim b/src/modules/views.nim index 7b04d7a..ffc508c 100644 --- a/src/modules/views.nim +++ b/src/modules/views.nim @@ -1,7 +1,9 @@ import prologue import nimja/parser +import formatter proc renderTemplate(templateName: static[string]): string = + logPrint("Served template " & templateName, "Main") compileTemplateFile(getScriptDir() & "/templates/" & templateName) proc getIndex*(ctx: Context) {.async.} = diff --git a/src/nimblog.nim b/src/nimblog.nim index 57ea233..2691d3f 100644 --- a/src/nimblog.nim +++ b/src/nimblog.nim @@ -1,7 +1,7 @@ when isMainModule: import tables import os - import modules/[blog, env, db] + import modules/[blog, env, db, formatter] let dataDir: string = "data" @@ -10,21 +10,25 @@ when isMainModule: "logging": boolEnvOrDefault("NIMBLOG_LOG", true) }.toTable() + if settings["debug"]: + logPrint("Debug mode is enabled", "Warning") + let dbSettings = { "path": dataDir & "/" & "blog.db", "schema_path": "schema.sql" }.toTable() + logPrint("Starting website", "Main") + if not dirExists(dataDir): - echo "Created data directory" + logPrint("Created data directory", "Info") createDir(dataDir) if not createDb(dbSettings): - echo "Failed to prepare database" + logPrint("Failed to prepare database", "Error") quit(QuitFailure) try: - echo "Starting website" runWebsite(settings=settings, dbSettings=dbSettings) except: - echo "Could not run website" + logPrint("Could not run website", "Error")