Skip to content

Commit bb9dc90

Browse files
committed
add optional logging (-d:nimibNoLog)
1 parent 02b5253 commit bb9dc90

File tree

7 files changed

+69
-45
lines changed

7 files changed

+69
-45
lines changed

src/nimib.nim

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import std/[os, strutils, sugar, strformat, macros, macrocache, sequtils, jsonutils]
22
export jsonutils
3-
import nimib / [types, blocks, docs, boost, config, options, capture, jsutils]
3+
import nimib / [types, blocks, docs, boost, config, options, capture, jsutils, logging]
44
export types, blocks, docs, boost, sugar, jsutils
55
# types exports mustache, tables, paths
66

@@ -26,25 +26,25 @@ template nbInit*(theme = themes.useDefault, backend = renders.useHtmlBackend, th
2626
nb.thisFile = instantiationInfo(-1, true).filename.AbsoluteFile
2727
else:
2828
nb.thisFile = nb.srcDir / thisFileRel.RelativeFile
29-
echo "[nimib] thisFile: ", nb.thisFile
29+
log "thisFile: " & $nb.thisFile
3030

3131
try:
3232
nb.source = read(nb.thisFile)
3333
except IOError:
34-
echo "[nimib] cannot read source"
34+
log "cannot read source"
3535

3636
if nb.options.filename == "":
3737
nb.filename = nb.thisFile.string.splitFile.name & ".html"
3838
else:
3939
nb.filename = nb.options.filename
4040

4141
if nb.cfg.srcDir != "":
42-
echo "[nimib] srcDir: ", nb.srcDir
42+
log "srcDir: " & $nb.srcDir
4343
nb.filename = (nb.thisDir.relativeTo nb.srcDir).string / nb.filename
44-
echo "[nimib] filename: ", nb.filename
44+
log "filename: " & nb.filename
4545

4646
if nb.cfg.homeDir != "":
47-
echo "[nimib] setting current directory to nb.homeDir: ", nb.homeDir
47+
log "setting current directory to nb.homeDir: " & $nb.homeDir
4848
setCurrentDir nb.homeDir
4949

5050
# can be overriden by theme, but it is better to initialize this anyway

src/nimib/blocks.nim

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import std / [macros, strutils, sugar]
2-
import types, sources
2+
import types, sources, logging
33

44
macro toStr*(body: untyped): string =
55
(body.toStrLit)
@@ -17,17 +17,17 @@ func nbNormalize*(text: string): string =
1717
# note that: '\c' == '\r' and '\l' == '\n'
1818

1919
template newNbBlock*(cmd: string, readCode: static[bool], nbDoc, nbBlock, body, blockImpl: untyped) =
20-
stdout.write "[nimib] ", nbDoc.blocks.len, " ", cmd, ": "
2120
nbBlock = NbBlock(command: cmd, context: newContext(searchDirs = @[], partials = nbDoc.partials))
2221
when readCode:
2322
nbBlock.code = nbNormalize:
2423
when defined(nimibCodeFromAst):
2524
toStr(body)
2625
else:
2726
getCodeAsInSource(nbDoc.source, cmd, body)
28-
echo peekFirstLineOf(nbBlock.code)
27+
log "$1 $2: $3" % [$nbDoc.blocks.len, cmd, peekFirstLineOf(nbBlock.code)]
2928
blockImpl
30-
if len(nbBlock.output) > 0: echo " -> ", peekFirstLineOf(nbBlock.output)
29+
if nimibLog and len(nbBlock.output) > 0:
30+
echo " -> ", peekFirstLineOf(nbBlock.output)
3131
nbBlock.context["code"] = nbBlock.code
3232
nbBlock.context["output"] = nbBlock.output.dup(removeSuffix)
3333
nbDoc.blocks.add nbBlock

src/nimib/capture.nim

+27-24
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,30 @@ import fusion/ioutils
33
import std/tempfiles
44

55
template captureStdout*(ident: untyped, body: untyped) =
6-
## redirect stdout to a temporary file and captures output of body in ident
7-
# Duplicate stdout
8-
let stdoutFileno: FileHandle = stdout.getFileHandle()
9-
let stdoutDupFd: FileHandle = stdoutFileno.duplicate()
10-
11-
# Create a new temporary file or attempt to open it
12-
let (tmpFile, _) = createTempFile("tmp", "")
13-
let tmpFileFd: FileHandle = tmpFile.getFileHandle()
14-
15-
# writing to stdoutFileno now writes to tmpFile
16-
tmpFileFd.duplicateTo(stdoutFileno)
17-
18-
# Execute body code
19-
body
20-
21-
# Flush stdout and tmpFile, read tmpFile from start to ident and then close tmpFile
22-
stdout.flushFile()
23-
tmpFile.flushFile()
24-
tmpFile.setFilePos(0)
25-
ident = tmpFile.readAll()
26-
tmpFile.close()
27-
28-
# Restore stdout
29-
stdoutDupFd.duplicateTo(stdoutFileno)
6+
## redirect stdout to a temporary file and captures output of body in ident
7+
# Duplicate stdout
8+
let stdoutFileno: FileHandle = stdout.getFileHandle()
9+
let stdoutDupFd: FileHandle = stdoutFileno.duplicate()
10+
11+
# Create a new temporary file or attempt to open it
12+
let (tmpFile, _) = createTempFile("tmp", "")
13+
let tmpFileFd: FileHandle = tmpFile.getFileHandle()
14+
15+
# needs to be present when stdout isn't being written to by `newNbBlock` (-d:nimibNoLog)
16+
stdout.flushFile()
17+
18+
# writing to stdoutFileno now writes to tmpFile
19+
tmpFileFd.duplicateTo(stdoutFileno)
20+
21+
# Execute body code
22+
body
23+
24+
# Flush stdout and tmpFile, read tmpFile from start to ident and then close tmpFile
25+
stdout.flushFile()
26+
tmpFile.flushFile()
27+
tmpFile.setFilePos(0)
28+
ident = tmpFile.readAll()
29+
tmpFile.close()
30+
31+
# Restore stdout
32+
stdoutDupFd.duplicateTo(stdoutFileno)

src/nimib/config.nim

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import types, parsetoml, jsony, std / [json, os, osproc, math, sequtils]
1+
import types, logging, parsetoml, jsony, std / [json, os, osproc, math, sequtils]
22

33
proc getNimibVersion*(): string =
44
var dir = currentSourcePath().parentDir().parentDir()
@@ -85,7 +85,7 @@ proc loadNimibCfg*(cfgName: string): tuple[found: bool, dir: AbsoluteDir, raw: s
8585
for dir in parentDirs(getCurrentDir()):
8686
if fileExists(dir / cfgName):
8787
result.dir = dir.AbsoluteDir
88-
echo "[nimib] config file found: ", dir / cfgName
88+
log "config file found: " & dir / cfgName
8989
result.found = true
9090
break
9191
if result.found:
@@ -99,7 +99,7 @@ proc loadCfg*(doc: var NbDoc) =
9999
doc.rawCfg = cfg.raw
100100
doc.cfg = cfg.nb
101101
if not doc.hasCfg:
102-
echo "[nimib] using default config"
102+
log "using default config"
103103
doc.useDefaultCfg
104104
doc.optOverride
105105

src/nimib/docs.nim

+4-5
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
import std/os
22
import browsers
3-
import types
4-
import nimib / renders
3+
import types, logging, renders
54

65
proc write*(doc: var NbDoc) =
7-
echo "[nimib] current directory: ", getCurrentDir()
6+
log "current directory: " & getCurrentDir()
87
let dir = doc.filename.splitFile().dir
98
if not dir.dirExists:
10-
echo "[nimib] creating directory: ", dir
9+
log "creating directory: " & dir
1110
createDir(dir)
12-
echo "[nimib] saving file: ", doc.filename
11+
log "saving file: " & doc.filename
1312
writeFile(doc.filename, render(doc))
1413

1514
proc open*(doc: NbDoc) =

src/nimib/logging.nim

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import std/strformat
2+
3+
const nimibLog* = not defined(nimibNoLog)
4+
5+
proc log*(label: string, message: string) =
6+
when nimibLog:
7+
if label.len > 0:
8+
echo fmt"[nimib.{label}] {message}"
9+
else:
10+
echo fmt"[nimib] {message}"
11+
12+
proc log*(message: string) =
13+
log("", message)
14+
15+
proc info*(message: string) =
16+
log("info", message)
17+
18+
proc error*(message: string) =
19+
log("error", message)
20+
21+
proc warning*(message: string) =
22+
log("warning", message)

src/nimib/renders.nim

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import std / [strutils, tables, sugar, os, strformat, sequtils]
2-
import ./types, ./jsutils, markdown, mustache
2+
import ./types, ./jsutils, ./logging, markdown, mustache
33

44
import highlight
55
import mustachepkg/values
@@ -109,12 +109,12 @@ proc useMdBackend*(doc: var NbDoc) =
109109

110110
template debugRender(message: string) =
111111
when defined(nimibDebugRender):
112-
echo "[nimib.debugRender] ", message
112+
log "debugRender", message
113113

114114
proc render*(nb: var NbDoc, blk: var NbBlock): string =
115115
debugRender "rendering block " & blk.command
116116
if blk.command not_in nb.partials:
117-
echo "[nimib.warning] no partial found for block ", blk.command
117+
warning "no partial found for block " & blk.command
118118
return
119119
else:
120120
if blk.command in nb.renderPlans:

0 commit comments

Comments
 (0)