Skip to content

Instantly share code, notes, and snippets.

@Varriount
Created November 12, 2013 21:41
Show Gist options
  • Save Varriount/7439309 to your computer and use it in GitHub Desktop.
Save Varriount/7439309 to your computer and use it in GitHub Desktop.
## Imports
import marshal
import macros
## Types
type TLog* = ref object
path: string
file: TFile
## Procedures
proc openLog*(path: string, mode: TFileMode = fmReadWrite): TLog =
new(result)
discard result.file.open(path, mode)
proc closeLog*(log: TLog) =
log.file.close()
log.file = nil
proc writeLog*[T](log: TLog, value: T) =
log.file.writeln($$value)
proc readLog*[T](log: TLog): T =
var line = log.file.readLine()
result = to[T](line)
var log = openLog("log2.db")
log.writeLog("hello world")
log.file.setFilePos(0)
echo "reading from log (1)"
echo readLog[string](log) #works
echo log.readLog[string]() #fails
echo "closing log"
log.closeLog()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment