Implement logging/formatting

This commit is contained in:
Quad 2022-04-13 22:22:52 +02:00
parent 7c484b18d3
commit 83d77f8865
5 changed files with 67 additions and 6 deletions

View File

@ -1,6 +1,7 @@
import prologue import prologue
import prologue/middlewares/staticfile import prologue/middlewares/staticfile
import views import views
import formatter
proc runWebsite(settings: Table[string, bool], dbSettings: Table[string, string]) = proc runWebsite(settings: Table[string, bool], dbSettings: Table[string, string]) =
let prologueSettings = newSettings( let prologueSettings = newSettings(
@ -13,6 +14,7 @@ proc runWebsite(settings: Table[string, bool], dbSettings: Table[string, string]
website.get("/about", getAbout) website.get("/about", getAbout)
website.use(staticFileMiddleware("static")) website.use(staticFileMiddleware("static"))
logPrint("Press Ctrl-C to stop site", "Info")
website.run() website.run()
export runWebsite export runWebsite

View File

@ -1,5 +1,6 @@
import tables import tables
import std/[os, db_sqlite, strutils, streams] import std/[os, db_sqlite, strutils, streams]
import formatter
proc createDb(dbSettings: Table): bool = proc createDb(dbSettings: Table): bool =
if not fileExists(dbSettings["path"]): if not fileExists(dbSettings["path"]):
@ -25,7 +26,7 @@ proc createDb(dbSettings: Table): bool =
if command == "\c\n" or command == "\n": if command == "\c\n" or command == "\n":
continue continue
db.exec(sql(command.strip)) db.exec(sql(command.strip))
echo "Ran command: " & command.strip logPrint("Ran SQL command from schema", "Info")
db.close() db.close()
return true return true

52
src/modules/formatter.nim Normal file
View File

@ -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

View File

@ -1,7 +1,9 @@
import prologue import prologue
import nimja/parser import nimja/parser
import formatter
proc renderTemplate(templateName: static[string]): string = proc renderTemplate(templateName: static[string]): string =
logPrint("Served template " & templateName, "Main")
compileTemplateFile(getScriptDir() & "/templates/" & templateName) compileTemplateFile(getScriptDir() & "/templates/" & templateName)
proc getIndex*(ctx: Context) {.async.} = proc getIndex*(ctx: Context) {.async.} =

View File

@ -1,7 +1,7 @@
when isMainModule: when isMainModule:
import tables import tables
import os import os
import modules/[blog, env, db] import modules/[blog, env, db, formatter]
let dataDir: string = "data" let dataDir: string = "data"
@ -10,21 +10,25 @@ when isMainModule:
"logging": boolEnvOrDefault("NIMBLOG_LOG", true) "logging": boolEnvOrDefault("NIMBLOG_LOG", true)
}.toTable() }.toTable()
if settings["debug"]:
logPrint("Debug mode is enabled", "Warning")
let dbSettings = { let dbSettings = {
"path": dataDir & "/" & "blog.db", "path": dataDir & "/" & "blog.db",
"schema_path": "schema.sql" "schema_path": "schema.sql"
}.toTable() }.toTable()
logPrint("Starting website", "Main")
if not dirExists(dataDir): if not dirExists(dataDir):
echo "Created data directory" logPrint("Created data directory", "Info")
createDir(dataDir) createDir(dataDir)
if not createDb(dbSettings): if not createDb(dbSettings):
echo "Failed to prepare database" logPrint("Failed to prepare database", "Error")
quit(QuitFailure) quit(QuitFailure)
try: try:
echo "Starting website"
runWebsite(settings=settings, dbSettings=dbSettings) runWebsite(settings=settings, dbSettings=dbSettings)
except: except:
echo "Could not run website" logPrint("Could not run website", "Error")