more modules updated

This commit is contained in:
Araq 2014-08-28 02:06:11 +02:00
commit 42f5a838a3
4 changed files with 426 additions and 426 deletions

View file

@ -1,6 +1,6 @@
#
#
# Nimrod's Runtime Library
# Nim's Runtime Library
# (c) Copyright 2013 Dominik Picheta, Andreas Rumpf
#
# See the file "copying.txt", included in this
@ -68,20 +68,20 @@ type
const
GenericSeqSize = (2 * sizeof(int))
proc genericAssign(dest, src: Pointer, mt: PNimType) {.importCompilerProc.}
proc genericShallowAssign(dest, src: Pointer, mt: PNimType) {.
proc genericAssign(dest, src: pointer, mt: PNimType) {.importCompilerProc.}
proc genericShallowAssign(dest, src: pointer, mt: PNimType) {.
importCompilerProc.}
proc incrSeq(seq: PGenSeq, elemSize: int): PGenSeq {.importCompilerProc.}
proc newObj(typ: PNimType, size: int): pointer {.importCompilerProc.}
proc newSeq(typ: PNimType, len: int): pointer {.importCompilerProc.}
proc objectInit(dest: Pointer, typ: PNimType) {.importCompilerProc.}
proc objectInit(dest: pointer, typ: PNimType) {.importCompilerProc.}
template `+!!`(a, b: expr): expr = cast[pointer](cast[TAddress](a) + b)
template `+!!`(a, b: expr): expr = cast[pointer](cast[ByteAddress](a) + b)
proc getDiscriminant(aa: Pointer, n: ptr TNimNode): int =
proc getDiscriminant(aa: pointer, n: ptr TNimNode): int =
assert(n.kind == nkCase)
var d: int
var a = cast[TAddress](aa)
var a = cast[ByteAddress](aa)
case n.typ.size
of 1: d = ze(cast[ptr int8](a +% n.offset)[])
of 2: d = ze(cast[ptr int16](a +% n.offset)[])
@ -89,7 +89,7 @@ proc getDiscriminant(aa: Pointer, n: ptr TNimNode): int =
else: assert(false)
return d
proc selectBranch(aa: Pointer, n: ptr TNimNode): ptr TNimNode =
proc selectBranch(aa: pointer, n: ptr TNimNode): ptr TNimNode =
var discr = getDiscriminant(aa, n)
if discr <% n.len:
result = n.sons[discr]
@ -174,14 +174,14 @@ proc `[]`*(x: TAny, i: int): TAny =
of tyArray:
var bs = x.rawType.base.size
if i >=% x.rawType.size div bs:
raise newException(EInvalidIndex, "index out of bounds")
raise newException(IndexError, "index out of bounds")
return newAny(x.value +!! i*bs, x.rawType.base)
of tySequence:
var s = cast[ppointer](x.value)[]
if s == nil: raise newException(EInvalidValue, "sequence is nil")
if s == nil: raise newException(ValueError, "sequence is nil")
var bs = x.rawType.base.size
if i >=% cast[PGenSeq](s).len:
raise newException(EInvalidIndex, "index out of bounds")
raise newException(IndexError, "index out of bounds")
return newAny(s +!! (GenericSeqSize+i*bs), x.rawType.base)
else: assert false
@ -191,15 +191,15 @@ proc `[]=`*(x: TAny, i: int, y: TAny) =
of tyArray:
var bs = x.rawType.base.size
if i >=% x.rawType.size div bs:
raise newException(EInvalidIndex, "index out of bounds")
raise newException(IndexError, "index out of bounds")
assert y.rawType == x.rawType.base
genericAssign(x.value +!! i*bs, y.value, y.rawType)
of tySequence:
var s = cast[ppointer](x.value)[]
if s == nil: raise newException(EInvalidValue, "sequence is nil")
if s == nil: raise newException(ValueError, "sequence is nil")
var bs = x.rawType.base.size
if i >=% cast[PGenSeq](s).len:
raise newException(EInvalidIndex, "index out of bounds")
raise newException(IndexError, "index out of bounds")
assert y.rawType == x.rawType.base
genericAssign(s +!! (GenericSeqSize+i*bs), y.value, y.rawType)
else: assert false
@ -276,7 +276,7 @@ proc cmpIgnoreStyle(a, b: cstring): int {.noSideEffect.} =
else: result = c
var i = 0
var j = 0
while True:
while true:
while a[i] == '_': inc(i)
while b[j] == '_': inc(j) # BUGFIX: typo
var aa = toLower(a[i])
@ -311,12 +311,12 @@ proc `[]=`*(x: TAny, fieldName: string, value: TAny) =
when false:
if x.rawType.kind == tyObject: t = cast[ptr PNimType](x.value)[]
assert x.rawType.kind in {tyTuple, tyObject}
var n = getFieldNode(x.value, t.node, fieldname)
var n = getFieldNode(x.value, t.node, fieldName)
if n != nil:
assert n.typ == value.rawType
genericAssign(x.value +!! n.offset, value.value, value.rawType)
else:
raise newException(EInvalidValue, "invalid field name: " & fieldName)
raise newException(ValueError, "invalid field name: " & fieldName)
proc `[]`*(x: TAny, fieldName: string): TAny =
## gets a field of `x`; `x` represents an object or a tuple.
@ -325,80 +325,80 @@ proc `[]`*(x: TAny, fieldName: string): TAny =
when false:
if x.rawType.kind == tyObject: t = cast[ptr PNimType](x.value)[]
assert x.rawType.kind in {tyTuple, tyObject}
var n = getFieldNode(x.value, t.node, fieldname)
var n = getFieldNode(x.value, t.node, fieldName)
if n != nil:
result.value = x.value +!! n.offset
result.rawType = n.typ
else:
raise newException(EInvalidValue, "invalid field name: " & fieldName)
raise newException(ValueError, "invalid field name: " & fieldName)
proc `[]`*(x: TAny): TAny =
## dereference operation for the any `x` that represents a ptr or a ref.
assert x.rawtype.kind in {tyRef, tyPtr}
assert x.rawType.kind in {tyRef, tyPtr}
result.value = cast[ppointer](x.value)[]
result.rawType = x.rawType.base
proc `[]=`*(x, y: TAny) =
## dereference operation for the any `x` that represents a ptr or a ref.
assert x.rawtype.kind in {tyRef, tyPtr}
assert x.rawType.kind in {tyRef, tyPtr}
assert y.rawType == x.rawType.base
genericAssign(cast[ppointer](x.value)[], y.value, y.rawType)
proc getInt*(x: TAny): int =
## retrieve the int value out of `x`. `x` needs to represent an int.
assert skipRange(x.rawtype).kind == tyInt
assert skipRange(x.rawType).kind == tyInt
result = cast[ptr int](x.value)[]
proc getInt8*(x: TAny): int8 =
## retrieve the int8 value out of `x`. `x` needs to represent an int8.
assert skipRange(x.rawtype).kind == tyInt8
assert skipRange(x.rawType).kind == tyInt8
result = cast[ptr int8](x.value)[]
proc getInt16*(x: TAny): int16 =
## retrieve the int16 value out of `x`. `x` needs to represent an int16.
assert skipRange(x.rawtype).kind == tyInt16
assert skipRange(x.rawType).kind == tyInt16
result = cast[ptr int16](x.value)[]
proc getInt32*(x: TAny): int32 =
## retrieve the int32 value out of `x`. `x` needs to represent an int32.
assert skipRange(x.rawtype).kind == tyInt32
assert skipRange(x.rawType).kind == tyInt32
result = cast[ptr int32](x.value)[]
proc getInt64*(x: TAny): int64 =
## retrieve the int64 value out of `x`. `x` needs to represent an int64.
assert skipRange(x.rawtype).kind == tyInt64
assert skipRange(x.rawType).kind == tyInt64
result = cast[ptr int64](x.value)[]
proc getBiggestInt*(x: TAny): biggestInt =
proc getBiggestInt*(x: TAny): BiggestInt =
## retrieve the integer value out of `x`. `x` needs to represent
## some integer, a bool, a char, an enum or a small enough bit set.
## The value might be sign-extended to ``biggestInt``.
var t = skipRange(x.rawtype)
## The value might be sign-extended to ``BiggestInt``.
var t = skipRange(x.rawType)
case t.kind
of tyInt: result = biggestInt(cast[ptr int](x.value)[])
of tyInt8: result = biggestInt(cast[ptr int8](x.value)[])
of tyInt16: result = biggestInt(cast[ptr int16](x.value)[])
of tyInt32: result = biggestInt(cast[ptr int32](x.value)[])
of tyInt64, tyUInt64: result = biggestInt(cast[ptr int64](x.value)[])
of tyBool: result = biggestInt(cast[ptr bool](x.value)[])
of tyChar: result = biggestInt(cast[ptr char](x.value)[])
of tyInt: result = BiggestInt(cast[ptr int](x.value)[])
of tyInt8: result = BiggestInt(cast[ptr int8](x.value)[])
of tyInt16: result = BiggestInt(cast[ptr int16](x.value)[])
of tyInt32: result = BiggestInt(cast[ptr int32](x.value)[])
of tyInt64, tyUInt64: result = BiggestInt(cast[ptr int64](x.value)[])
of tyBool: result = BiggestInt(cast[ptr bool](x.value)[])
of tyChar: result = BiggestInt(cast[ptr char](x.value)[])
of tyEnum, tySet:
case t.size
of 1: result = ze64(cast[ptr int8](x.value)[])
of 2: result = ze64(cast[ptr int16](x.value)[])
of 4: result = biggestInt(cast[ptr int32](x.value)[])
of 8: result = biggestInt(cast[ptr int64](x.value)[])
of 4: result = BiggestInt(cast[ptr int32](x.value)[])
of 8: result = BiggestInt(cast[ptr int64](x.value)[])
else: assert false
of tyUInt: result = biggestInt(cast[ptr uint](x.value)[])
of tyUInt8: result = biggestInt(cast[ptr uint8](x.value)[])
of tyUInt16: result = biggestInt(cast[ptr uint16](x.value)[])
of tyUInt32: result = biggestInt(cast[ptr uint32](x.value)[])
of tyUInt: result = BiggestInt(cast[ptr uint](x.value)[])
of tyUInt8: result = BiggestInt(cast[ptr uint8](x.value)[])
of tyUInt16: result = BiggestInt(cast[ptr uint16](x.value)[])
of tyUInt32: result = BiggestInt(cast[ptr uint32](x.value)[])
else: assert false
proc setBiggestInt*(x: TAny, y: biggestInt) =
proc setBiggestInt*(x: TAny, y: BiggestInt) =
## sets the integer value of `x`. `x` needs to represent
## some integer, a bool, a char, an enum or a small enough bit set.
var t = skipRange(x.rawtype)
var t = skipRange(x.rawType)
case t.kind
of tyInt: cast[ptr int](x.value)[] = int(y)
of tyInt8: cast[ptr int8](x.value)[] = int8(y)
@ -422,37 +422,37 @@ proc setBiggestInt*(x: TAny, y: biggestInt) =
proc getUInt*(x: TAny): uint =
## retrieve the uint value out of `x`, `x` needs to represent an uint.
assert skipRange(x.rawtype).kind == tyUInt
assert skipRange(x.rawType).kind == tyUInt
result = cast[ptr uint](x.value)[]
proc getUInt8*(x: TAny): uint8 =
## retrieve the uint8 value out of `x`, `x` needs to represent an
## uint8.
assert skipRange(x.rawtype).kind == tyUInt8
assert skipRange(x.rawType).kind == tyUInt8
result = cast[ptr uint8](x.value)[]
proc getUInt16*(x: TAny): uint16 =
## retrieve the uint16 value out of `x`, `x` needs to represent an
## uint16.
assert skipRange(x.rawtype).kind == tyUInt16
assert skipRange(x.rawType).kind == tyUInt16
result = cast[ptr uint16](x.value)[]
proc getUInt32*(x: TAny): uint32 =
## retrieve the uint32 value out of `x`, `x` needs to represent an
## uint32.
assert skipRange(x.rawtype).kind == tyUInt32
assert skipRange(x.rawType).kind == tyUInt32
result = cast[ptr uint32](x.value)[]
proc getUInt64*(x: TAny): uint64 =
## retrieve the uint64 value out of `x`, `x` needs to represent an
## uint64.
assert skipRange(x.rawtype).kind == tyUInt64
assert skipRange(x.rawType).kind == tyUInt64
result = cast[ptr uint64](x.value)[]
proc getBiggestUint*(x: TAny): uint64 =
## retrieve the unsigned integer value out of `x`. `x` needs to
## represent an unsigned integer.
var t = skipRange(x.rawtype)
var t = skipRange(x.rawType)
case t.kind
of tyUInt: result = uint64(cast[ptr uint](x.value)[])
of tyUInt8: result = uint64(cast[ptr uint8](x.value)[])
@ -464,7 +464,7 @@ proc getBiggestUint*(x: TAny): uint64 =
proc setBiggestUint*(x: TAny; y: uint64) =
## sets the unsigned integer value of `c`. `c` needs to represent an
## unsigned integer.
var t = skipRange(x.rawtype)
var t = skipRange(x.rawType)
case t.kind:
of tyUInt: cast[ptr uint](x.value)[] = uint(y)
of tyUInt8: cast[ptr uint8](x.value)[] = uint8(y)
@ -475,13 +475,13 @@ proc setBiggestUint*(x: TAny; y: uint64) =
proc getChar*(x: TAny): char =
## retrieve the char value out of `x`. `x` needs to represent a char.
var t = skipRange(x.rawtype)
var t = skipRange(x.rawType)
assert t.kind == tyChar
result = cast[ptr char](x.value)[]
proc getBool*(x: TAny): bool =
## retrieve the bool value out of `x`. `x` needs to represent a bool.
var t = skipRange(x.rawtype)
var t = skipRange(x.rawType)
assert t.kind == tyBool
result = cast[ptr bool](x.value)[]
@ -495,7 +495,7 @@ proc getEnumOrdinal*(x: TAny, name: string): int =
## gets the enum field ordinal from `name`. `x` needs to represent an enum
## but is only used to access the type information. In case of an error
## ``low(int)`` is returned.
var typ = skipRange(x.rawtype)
var typ = skipRange(x.rawType)
assert typ.kind == tyEnum
var n = typ.node
var s = n.sons
@ -511,7 +511,7 @@ proc getEnumField*(x: TAny, ordinalValue: int): string =
## gets the enum field name as a string. `x` needs to represent an enum
## but is only used to access the type information. The field name of
## `ordinalValue` is returned.
var typ = skipRange(x.rawtype)
var typ = skipRange(x.rawType)
assert typ.kind == tyEnum
var e = ordinalValue
if ntfEnumHole notin typ.flags:
@ -531,51 +531,51 @@ proc getEnumField*(x: TAny): string =
proc getFloat*(x: TAny): float =
## retrieve the float value out of `x`. `x` needs to represent an float.
assert skipRange(x.rawtype).kind == tyFloat
assert skipRange(x.rawType).kind == tyFloat
result = cast[ptr float](x.value)[]
proc getFloat32*(x: TAny): float32 =
## retrieve the float32 value out of `x`. `x` needs to represent an float32.
assert skipRange(x.rawtype).kind == tyFloat32
assert skipRange(x.rawType).kind == tyFloat32
result = cast[ptr float32](x.value)[]
proc getFloat64*(x: TAny): float64 =
## retrieve the float64 value out of `x`. `x` needs to represent an float64.
assert skipRange(x.rawtype).kind == tyFloat64
assert skipRange(x.rawType).kind == tyFloat64
result = cast[ptr float64](x.value)[]
proc getBiggestFloat*(x: TAny): biggestFloat =
proc getBiggestFloat*(x: TAny): BiggestFloat =
## retrieve the float value out of `x`. `x` needs to represent
## some float. The value is extended to ``biggestFloat``.
case skipRange(x.rawtype).kind
of tyFloat: result = biggestFloat(cast[ptr Float](x.value)[])
of tyFloat32: result = biggestFloat(cast[ptr Float32](x.value)[])
of tyFloat64: result = biggestFloat(cast[ptr Float64](x.value)[])
## some float. The value is extended to ``BiggestFloat``.
case skipRange(x.rawType).kind
of tyFloat: result = BiggestFloat(cast[ptr float](x.value)[])
of tyFloat32: result = BiggestFloat(cast[ptr float32](x.value)[])
of tyFloat64: result = BiggestFloat(cast[ptr float64](x.value)[])
else: assert false
proc setBiggestFloat*(x: TAny, y: biggestFloat) =
proc setBiggestFloat*(x: TAny, y: BiggestFloat) =
## sets the float value of `x`. `x` needs to represent
## some float.
case skipRange(x.rawtype).kind
of tyFloat: cast[ptr Float](x.value)[] = y
of tyFloat32: cast[ptr Float32](x.value)[] = y.float32
of tyFloat64: cast[ptr Float64](x.value)[] = y
case skipRange(x.rawType).kind
of tyFloat: cast[ptr float](x.value)[] = y
of tyFloat32: cast[ptr float32](x.value)[] = y.float32
of tyFloat64: cast[ptr float64](x.value)[] = y
else: assert false
proc getString*(x: TAny): string =
## retrieve the string value out of `x`. `x` needs to represent a string.
assert x.rawtype.kind == tyString
assert x.rawType.kind == tyString
if not isNil(cast[ptr pointer](x.value)[]):
result = cast[ptr string](x.value)[]
proc setString*(x: TAny, y: string) =
## sets the string value of `x`. `x` needs to represent a string.
assert x.rawtype.kind == tyString
assert x.rawType.kind == tyString
cast[ptr string](x.value)[] = y
proc getCString*(x: TAny): cstring =
## retrieve the cstring value out of `x`. `x` needs to represent a cstring.
assert x.rawtype.kind == tyCString
assert x.rawType.kind == tyCString
result = cast[ptr cstring](x.value)[]
proc assign*(x, y: TAny) =
@ -587,7 +587,7 @@ proc assign*(x, y: TAny) =
iterator elements*(x: TAny): int =
## iterates over every element of `x` that represents a Nimrod bitset.
assert x.rawType.kind == tySet
var typ = x.rawtype
var typ = x.rawType
var p = x.value
# "typ.slots.len" field is for sets the "first" field
var u: int64
@ -609,7 +609,7 @@ iterator elements*(x: TAny): int =
proc inclSetElement*(x: TAny, elem: int) =
## includes an element `elem` in `x`. `x` needs to represent a Nimrod bitset.
assert x.rawType.kind == tySet
var typ = x.rawtype
var typ = x.rawType
var p = x.value
# "typ.slots.len" field is for sets the "first" field
var e = elem - typ.node.len

View file

@ -51,7 +51,7 @@ proc mustRehash(length, counter: int): bool {.inline.} =
proc nextTry(h, maxHash: THash): THash {.inline.} =
result = ((5 * h) + 1) and maxHash
proc intSetGet(t: TIntSet, key: int): PTrunk =
proc intSetGet(t: IntSet, key: int): PTrunk =
var h = key and t.max
while t.data[h] != nil:
if t.data[h].key == key:
@ -59,7 +59,7 @@ proc intSetGet(t: TIntSet, key: int): PTrunk =
h = nextTry(h, t.max)
result = nil
proc intSetRawInsert(t: TIntSet, data: var TTrunkSeq, desc: PTrunk) =
proc intSetRawInsert(t: IntSet, data: var TTrunkSeq, desc: PTrunk) =
var h = desc.key and t.max
while data[h] != nil:
assert(data[h] != desc)
@ -67,7 +67,7 @@ proc intSetRawInsert(t: TIntSet, data: var TTrunkSeq, desc: PTrunk) =
assert(data[h] == nil)
data[h] = desc
proc intSetEnlarge(t: var TIntSet) =
proc intSetEnlarge(t: var IntSet) =
var n: TTrunkSeq
var oldMax = t.max
t.max = ((t.max + 1) * 2) - 1
@ -76,7 +76,7 @@ proc intSetEnlarge(t: var TIntSet) =
if t.data[i] != nil: intSetRawInsert(t, n, t.data[i])
swap(t.data, n)
proc intSetPut(t: var TIntSet, key: int): PTrunk =
proc intSetPut(t: var IntSet, key: int): PTrunk =
var h = key and t.max
while t.data[h] != nil:
if t.data[h].key == key:
@ -93,7 +93,7 @@ proc intSetPut(t: var TIntSet, key: int): PTrunk =
t.head = result
t.data[h] = result
proc contains*(s: TIntSet, key: int): bool =
proc contains*(s: IntSet, key: int): bool =
## returns true iff `key` is in `s`.
var t = intSetGet(s, `shr`(key, TrunkShift))
if t != nil:
@ -102,14 +102,14 @@ proc contains*(s: TIntSet, key: int): bool =
else:
result = false
proc incl*(s: var TIntSet, key: int) =
proc incl*(s: var IntSet, key: int) =
## includes an element `key` in `s`.
var t = intSetPut(s, `shr`(key, TrunkShift))
var u = key and TrunkMask
t.bits[`shr`(u, IntShift)] = t.bits[`shr`(u, IntShift)] or
`shl`(1, u and IntMask)
proc excl*(s: var TIntSet, key: int) =
proc excl*(s: var IntSet, key: int) =
## excludes `key` from the set `s`.
var t = intSetGet(s, `shr`(key, TrunkShift))
if t != nil:
@ -117,7 +117,7 @@ proc excl*(s: var TIntSet, key: int) =
t.bits[`shr`(u, IntShift)] = t.bits[`shr`(u, IntShift)] and
not `shl`(1, u and IntMask)
proc containsOrIncl*(s: var TIntSet, key: int): bool =
proc containsOrIncl*(s: var IntSet, key: int): bool =
## returns true if `s` contains `key`, otherwise `key` is included in `s`
## and false is returned.
var t = intSetGet(s, `shr`(key, TrunkShift))
@ -131,14 +131,14 @@ proc containsOrIncl*(s: var TIntSet, key: int): bool =
incl(s, key)
result = false
proc initIntSet*: TIntSet =
proc initIntSet*: IntSet =
## creates a new int set that is empty.
newSeq(result.data, InitIntSetSize)
result.max = InitIntSetSize-1
result.counter = 0
result.head = nil
proc assign*(dest: var TIntSet, src: TIntSet) =
proc assign*(dest: var IntSet, src: IntSet) =
## copies `src` to `dest`. `dest` does not need to be initialized by
## `initIntSet`.
dest.counter = src.counter
@ -162,7 +162,7 @@ proc assign*(dest: var TIntSet, src: TIntSet) =
it = it.next
iterator items*(s: TIntSet): int {.inline.} =
iterator items*(s: IntSet): int {.inline.} =
## iterates over any included element of `s`.
var r = s.head
while r != nil:
@ -187,11 +187,11 @@ template dollarImpl(): stmt =
result.add($key)
result.add("}")
proc `$`*(s: TIntSet): string =
proc `$`*(s: IntSet): string =
## The `$` operator for int sets.
dollarImpl()
proc empty*(s: TIntSet): bool {.inline.} =
proc empty*(s: IntSet): bool {.inline.} =
## returns true if `s` is empty. This is safe to call even before
## the set has been initialized with `initIntSet`.
result = s.counter == 0

View file

@ -61,15 +61,15 @@ const
type
Logger* = ref object of RootObj ## abstract logger; the base type of all loggers
levelThreshold*: Leve l ## only messages of level >= levelThreshold
## should be processed
levelThreshold*: Level ## only messages of level >= levelThreshold
## should be processed
fmtStr: string ## = defaultFmtStr by default, see substituteLog for $date etc.
ConsoleLogger* = ref object of Logger ## logger that writes the messages to the
## console
FileLogger* = ref object of Logger ## logger that writes the messages to a file
f: TFile
f: File
RollingFileLogger* = ref object of FileLogger ## logger that writes the
## messages to a file and
@ -109,19 +109,19 @@ proc substituteLog(frmt: string): string =
of "appname": result.add(app.splitFile.name)
method log*(logger: Logger, level: Level,
frmt: string, args: varargs[string, `$`]) {.raises: [EBase], tags: [FTime, FWriteIO, FReadIO].} =
frmt: string, args: varargs[string, `$`]) {.raises: [Exception], tags: [FTime, FWriteIO, FReadIO].} =
## Override this method in custom loggers. Default implementation does
## nothing.
discard
method log*(logger: PConsoleLogger, level: TLevel,
method log*(logger: ConsoleLogger, level: Level,
frmt: string, args: varargs[string, `$`]) =
## Logs to the console using ``logger`` only.
if level >= logger.levelThreshold:
writeln(stdout, LevelNames[level], " ", substituteLog(logger.fmtStr),
frmt % args)
method log*(logger: PFileLogger, level: TLevel,
method log*(logger: FileLogger, level: Level,
frmt: string, args: varargs[string, `$`]) =
## Logs to a file using ``logger`` only.
if level >= logger.levelThreshold:
@ -133,16 +133,16 @@ proc defaultFilename*(): string =
var (path, name, ext) = splitFile(getAppFilename())
result = changeFileExt(path / name, "log")
proc newConsoleLogger*(levelThreshold = lvlAll, fmtStr = defaultFmtStr): PConsoleLogger =
proc newConsoleLogger*(levelThreshold = lvlAll, fmtStr = defaultFmtStr): ConsoleLogger =
## Creates a new console logger. This logger logs to the console.
new result
result.fmtStr = fmtStr
result.levelThreshold = levelThreshold
proc newFileLogger*(filename = defaultFilename(),
mode: TFileMode = fmAppend,
mode: FileMode = fmAppend,
levelThreshold = lvlAll,
fmtStr = defaultFmtStr): PFileLogger =
fmtStr = defaultFmtStr): FileLogger =
## Creates a new file logger. This logger logs to a file.
new(result)
result.levelThreshold = levelThreshold
@ -151,7 +151,7 @@ proc newFileLogger*(filename = defaultFilename(),
# ------
proc countLogLines(logger: PRollingFileLogger): int =
proc countLogLines(logger: RollingFileLogger): int =
result = 0
for line in logger.f.lines():
result.inc()
@ -169,13 +169,13 @@ proc countFiles(filename: string): int =
let num = parseInt(numS)
if num > result:
result = num
except EInvalidValue: discard
except ValueError: discard
proc newRollingFileLogger*(filename = defaultFilename(),
mode: TFileMode = fmReadWrite,
mode: FileMode = fmReadWrite,
levelThreshold = lvlAll,
fmtStr = defaultFmtStr,
maxLines = 1000): PRollingFileLogger =
maxLines = 1000): RollingFileLogger =
## Creates a new rolling file logger. Once a file reaches ``maxLines`` lines
## a new log file will be started and the old will be renamed.
new(result)
@ -193,14 +193,14 @@ proc newRollingFileLogger*(filename = defaultFilename(),
# We need to get a line count because we will be appending to the file.
result.curLine = countLogLines(result)
proc rotate(logger: PRollingFileLogger) =
proc rotate(logger: RollingFileLogger) =
let (dir, name, ext) = splitFile(logger.baseName)
for i in countdown(logger.logFiles, 0):
let srcSuff = if i != 0: ExtSep & $i else: ""
moveFile(dir / (name & ext & srcSuff),
dir / (name & ext & ExtSep & $(i+1)))
method log*(logger: PRollingFileLogger, level: TLevel,
method log*(logger: RollingFileLogger, level: Level,
frmt: string, args: varargs[string, `$`]) =
## Logs to a file using rolling ``logger`` only.
if level >= logger.levelThreshold:
@ -218,20 +218,20 @@ method log*(logger: PRollingFileLogger, level: TLevel,
var
level* = lvlAll ## global log filter
handlers*: seq[PLogger] = @[] ## handlers with their own log levels
handlers*: seq[Logger] = @[] ## handlers with their own log levels
proc logLoop(level: TLevel, frmt: string, args: varargs[string, `$`]) =
proc logLoop(level: Level, frmt: string, args: varargs[string, `$`]) =
for logger in items(handlers):
if level >= logger.levelThreshold:
log(logger, level, frmt, args)
template log*(level: TLevel, frmt: string, args: varargs[string, `$`]) =
template log*(level: Level, frmt: string, args: varargs[string, `$`]) =
## Logs a message to all registered handlers at the given level.
bind logLoop
bind `%`
bind logging.Level
bind logging.level
if level >= logging.Level:
if level >= logging.level:
logLoop(level, frmt, args)
template debug*(frmt: string, args: varargs[string, `$`]) =

View file

@ -1,319 +1,319 @@
#
#
# Nim's Runtime Library
# (c) Copyright 2014 Andreas Rumpf
#
# See the file "copying.txt", included in this
# distribution, for details about the copyright.
#
## This module contains procs for serialization and deseralization of
## arbitrary Nim data structures. The serialization format uses JSON.
##
## **Restriction**: For objects their type is **not** serialized. This means
## essentially that it does not work if the object has some other runtime
## type than its compiletime type:
##
## .. code-block:: nim
##
## type
## TA = object
## TB = object of TA
## f: int
##
## var
## a: ref TA
## b: ref TB
##
## new(b)
## a = b
## echo($$a[]) # produces "{}", not "{f: 0}"
import streams, typeinfo, json, intsets, tables
proc ptrToInt(x: pointer): int {.inline.} =
result = cast[int](x) # don't skip alignment
proc storeAny(s: PStream, a: TAny, stored: var TIntSet) =
case a.kind
of akNone: assert false
of akBool: s.write($getBool(a))
of akChar: s.write(escapeJson($getChar(a)))
of akArray, akSequence:
if a.kind == akSequence and isNil(a): s.write("null")
else:
s.write("[")
for i in 0 .. a.len-1:
if i > 0: s.write(", ")
storeAny(s, a[i], stored)
s.write("]")
of akObject, akTuple:
s.write("{")
var i = 0
for key, val in fields(a):
if i > 0: s.write(", ")
s.write(escapeJson(key))
s.write(": ")
storeAny(s, val, stored)
inc(i)
s.write("}")
of akSet:
s.write("[")
var i = 0
for e in elements(a):
if i > 0: s.write(", ")
s.write($e)
inc(i)
s.write("]")
of akRange: storeAny(s, skipRange(a), stored)
of akEnum: s.write(getEnumField(a).escapeJson)
of akPtr, akRef:
var x = a.getPointer
if isNil(x): s.write("null")
elif stored.containsOrIncl(x.ptrToInt):
# already stored, so we simply write out the pointer as an int:
s.write($x.ptrToInt)
else:
# else as a [value, key] pair:
# (reversed order for convenient x[0] access!)
s.write("[")
s.write($x.ptrToInt)
s.write(", ")
storeAny(s, a[], stored)
s.write("]")
of akProc, akPointer, akCString: s.write($a.getPointer.ptrToInt)
of akString:
var x = getString(a)
if IsNil(x): s.write("null")
else: s.write(escapeJson(x))
of akInt..akInt64, akUInt..akUInt64: s.write($getBiggestInt(a))
of akFloat..akFloat128: s.write($getBiggestFloat(a))
proc loadAny(p: var TJsonParser, a: TAny, t: var TTable[biggestInt, pointer]) =
case a.kind
of akNone: assert false
of akBool:
case p.kind
of jsonFalse: setBiggestInt(a, 0)
of jsonTrue: setBiggestInt(a, 1)
else: raiseParseErr(p, "'true' or 'false' expected for a bool")
next(p)
of akChar:
if p.kind == jsonString:
var x = p.str
if x.len == 1:
setBiggestInt(a, ord(x[0]))
next(p)
return
raiseParseErr(p, "string of length 1 expected for a char")
of akEnum:
if p.kind == jsonString:
setBiggestInt(a, getEnumOrdinal(a, p.str))
next(p)
return
raiseParseErr(p, "string expected for an enum")
of akArray:
if p.kind != jsonArrayStart: raiseParseErr(p, "'[' expected for an array")
next(p)
var i = 0
while p.kind != jsonArrayEnd and p.kind != jsonEof:
loadAny(p, a[i], t)
inc(i)
if p.kind == jsonArrayEnd: next(p)
else: raiseParseErr(p, "']' end of array expected")
of akSequence:
case p.kind
of jsonNull:
setPointer(a, nil)
next(p)
of jsonArrayStart:
next(p)
invokeNewSeq(a, 0)
var i = 0
while p.kind != jsonArrayEnd and p.kind != jsonEof:
extendSeq(a)
loadAny(p, a[i], t)
inc(i)
if p.kind == jsonArrayEnd: next(p)
else: raiseParseErr(p, "")
else:
raiseParseErr(p, "'[' expected for a seq")
of akObject, akTuple:
if a.kind == akObject: setObjectRuntimeType(a)
if p.kind != jsonObjectStart: raiseParseErr(p, "'{' expected for an object")
next(p)
while p.kind != jsonObjectEnd and p.kind != jsonEof:
if p.kind != jsonString:
raiseParseErr(p, "string expected for a field name")
var fieldName = p.str
next(p)
loadAny(p, a[fieldName], t)
if p.kind == jsonObjectEnd: next(p)
else: raiseParseErr(p, "'}' end of object expected")
of akSet:
if p.kind != jsonArrayStart: raiseParseErr(p, "'[' expected for a set")
next(p)
while p.kind != jsonArrayEnd and p.kind != jsonEof:
if p.kind != jsonInt: raiseParseErr(p, "int expected for a set")
inclSetElement(a, p.getInt.int)
next(p)
if p.kind == jsonArrayEnd: next(p)
else: raiseParseErr(p, "']' end of array expected")
of akPtr, akRef:
case p.kind
of jsonNull:
setPointer(a, nil)
next(p)
of jsonInt:
setPointer(a, t[p.getInt])
next(p)
of jsonArrayStart:
next(p)
if a.kind == akRef: invokeNew(a)
else: setPointer(a, alloc0(a.baseTypeSize))
if p.kind == jsonInt:
t[p.getInt] = getPointer(a)
next(p)
else: raiseParseErr(p, "index for ref type expected")
loadAny(p, a[], t)
if p.kind == jsonArrayEnd: next(p)
else: raiseParseErr(p, "']' end of ref-address pair expected")
else: raiseParseErr(p, "int for pointer type expected")
of akProc, akPointer, akCString:
case p.kind
of jsonNull:
setPointer(a, nil)
next(p)
of jsonInt:
setPointer(a, cast[pointer](p.getInt.int))
next(p)
else: raiseParseErr(p, "int for pointer type expected")
of akString:
case p.kind
of jsonNull:
setPointer(a, nil)
next(p)
of jsonString:
setString(a, p.str)
next(p)
else: raiseParseErr(p, "string expected")
of akInt..akInt64, akUInt..akUInt64:
if p.kind == jsonInt:
setBiggestInt(a, getInt(p))
next(p)
return
raiseParseErr(p, "int expected")
of akFloat..akFloat128:
if p.kind == jsonFloat:
setBiggestFloat(a, getFloat(p))
next(p)
return
raiseParseErr(p, "float expected")
of akRange: loadAny(p, a.skipRange, t)
proc loadAny(s: PStream, a: TAny, t: var TTable[BiggestInt, pointer]) =
var p: TJsonParser
open(p, s, "unknown file")
next(p)
loadAny(p, a, t)
close(p)
proc load*[T](s: PStream, data: var T) =
## loads `data` from the stream `s`. Raises `EIO` in case of an error.
var tab = initTable[biggestInt, pointer]()
loadAny(s, toAny(data), tab)
proc store*[T](s: PStream, data: T) =
## stores `data` into the stream `s`. Raises `EIO` in case of an error.
var stored = initIntSet()
var d: T
shallowCopy(d, data)
storeAny(s, toAny(d), stored)
proc `$$`*[T](x: T): string =
## returns a string representation of `x`.
var stored = initIntSet()
var d: T
shallowCopy(d, x)
var s = newStringStream()
storeAny(s, toAny(d), stored)
result = s.data
proc to*[T](data: string): T =
## reads data and transforms it to a ``T``.
var tab = initTable[biggestInt, pointer]()
loadAny(newStringStream(data), toAny(result), tab)
when isMainModule:
template testit(x: expr) = echo($$to[type(x)]($$x))
var x: array[0..4, array[0..4, string]] = [
["test", "1", "2", "3", "4"], ["test", "1", "2", "3", "4"],
["test", "1", "2", "3", "4"], ["test", "1", "2", "3", "4"],
["test", "1", "2", "3", "4"]]
testit(x)
var test2: tuple[name: string, s: uint] = ("tuple test", 56u)
testit(test2)
type
TE = enum
blah, blah2
TestObj = object
test, asd: int
case test2: TE
of blah:
help: string
else:
nil
PNode = ref TNode
TNode = object
next, prev: PNode
data: string
proc buildList(): PNode =
new(result)
new(result.next)
new(result.prev)
result.data = "middle"
result.next.data = "next"
result.prev.data = "prev"
result.next.next = result.prev
result.next.prev = result
result.prev.next = result
result.prev.prev = result.next
var test3: TestObj
test3.test = 42
test3.test2 = blah
testit(test3)
var test4: ref tuple[a, b: string]
new(test4)
test4.a = "ref string test: A"
test4.b = "ref string test: B"
testit(test4)
var test5 = @[(0,1),(2,3),(4,5)]
testit(test5)
var test6: set[char] = {'A'..'Z', '_'}
testit(test6)
var test7 = buildList()
echo($$test7)
testit(test7)
type
TA {.inheritable.} = object
TB = object of TA
f: int
var
a: ref TA
b: ref TB
new(b)
a = b
echo($$a[]) # produces "{}", not "{f: 0}"
#
#
# Nim's Runtime Library
# (c) Copyright 2014 Andreas Rumpf
#
# See the file "copying.txt", included in this
# distribution, for details about the copyright.
#
## This module contains procs for serialization and deseralization of
## arbitrary Nim data structures. The serialization format uses JSON.
##
## **Restriction**: For objects their type is **not** serialized. This means
## essentially that it does not work if the object has some other runtime
## type than its compiletime type:
##
## .. code-block:: nim
##
## type
## TA = object
## TB = object of TA
## f: int
##
## var
## a: ref TA
## b: ref TB
##
## new(b)
## a = b
## echo($$a[]) # produces "{}", not "{f: 0}"
import streams, typeinfo, json, intsets, tables
proc ptrToInt(x: pointer): int {.inline.} =
result = cast[int](x) # don't skip alignment
proc storeAny(s: Stream, a: TAny, stored: var IntSet) =
case a.kind
of akNone: assert false
of akBool: s.write($getBool(a))
of akChar: s.write(escapeJson($getChar(a)))
of akArray, akSequence:
if a.kind == akSequence and isNil(a): s.write("null")
else:
s.write("[")
for i in 0 .. a.len-1:
if i > 0: s.write(", ")
storeAny(s, a[i], stored)
s.write("]")
of akObject, akTuple:
s.write("{")
var i = 0
for key, val in fields(a):
if i > 0: s.write(", ")
s.write(escapeJson(key))
s.write(": ")
storeAny(s, val, stored)
inc(i)
s.write("}")
of akSet:
s.write("[")
var i = 0
for e in elements(a):
if i > 0: s.write(", ")
s.write($e)
inc(i)
s.write("]")
of akRange: storeAny(s, skipRange(a), stored)
of akEnum: s.write(getEnumField(a).escapeJson)
of akPtr, akRef:
var x = a.getPointer
if isNil(x): s.write("null")
elif stored.containsOrIncl(x.ptrToInt):
# already stored, so we simply write out the pointer as an int:
s.write($x.ptrToInt)
else:
# else as a [value, key] pair:
# (reversed order for convenient x[0] access!)
s.write("[")
s.write($x.ptrToInt)
s.write(", ")
storeAny(s, a[], stored)
s.write("]")
of akProc, akPointer, akCString: s.write($a.getPointer.ptrToInt)
of akString:
var x = getString(a)
if isNil(x): s.write("null")
else: s.write(escapeJson(x))
of akInt..akInt64, akUInt..akUInt64: s.write($getBiggestInt(a))
of akFloat..akFloat128: s.write($getBiggestFloat(a))
proc loadAny(p: var JsonParser, a: TAny, t: var Table[BiggestInt, pointer]) =
case a.kind
of akNone: assert false
of akBool:
case p.kind
of jsonFalse: setBiggestInt(a, 0)
of jsonTrue: setBiggestInt(a, 1)
else: raiseParseErr(p, "'true' or 'false' expected for a bool")
next(p)
of akChar:
if p.kind == jsonString:
var x = p.str
if x.len == 1:
setBiggestInt(a, ord(x[0]))
next(p)
return
raiseParseErr(p, "string of length 1 expected for a char")
of akEnum:
if p.kind == jsonString:
setBiggestInt(a, getEnumOrdinal(a, p.str))
next(p)
return
raiseParseErr(p, "string expected for an enum")
of akArray:
if p.kind != jsonArrayStart: raiseParseErr(p, "'[' expected for an array")
next(p)
var i = 0
while p.kind != jsonArrayEnd and p.kind != jsonEof:
loadAny(p, a[i], t)
inc(i)
if p.kind == jsonArrayEnd: next(p)
else: raiseParseErr(p, "']' end of array expected")
of akSequence:
case p.kind
of jsonNull:
setPointer(a, nil)
next(p)
of jsonArrayStart:
next(p)
invokeNewSeq(a, 0)
var i = 0
while p.kind != jsonArrayEnd and p.kind != jsonEof:
extendSeq(a)
loadAny(p, a[i], t)
inc(i)
if p.kind == jsonArrayEnd: next(p)
else: raiseParseErr(p, "")
else:
raiseParseErr(p, "'[' expected for a seq")
of akObject, akTuple:
if a.kind == akObject: setObjectRuntimeType(a)
if p.kind != jsonObjectStart: raiseParseErr(p, "'{' expected for an object")
next(p)
while p.kind != jsonObjectEnd and p.kind != jsonEof:
if p.kind != jsonString:
raiseParseErr(p, "string expected for a field name")
var fieldName = p.str
next(p)
loadAny(p, a[fieldName], t)
if p.kind == jsonObjectEnd: next(p)
else: raiseParseErr(p, "'}' end of object expected")
of akSet:
if p.kind != jsonArrayStart: raiseParseErr(p, "'[' expected for a set")
next(p)
while p.kind != jsonArrayEnd and p.kind != jsonEof:
if p.kind != jsonInt: raiseParseErr(p, "int expected for a set")
inclSetElement(a, p.getInt.int)
next(p)
if p.kind == jsonArrayEnd: next(p)
else: raiseParseErr(p, "']' end of array expected")
of akPtr, akRef:
case p.kind
of jsonNull:
setPointer(a, nil)
next(p)
of jsonInt:
setPointer(a, t[p.getInt])
next(p)
of jsonArrayStart:
next(p)
if a.kind == akRef: invokeNew(a)
else: setPointer(a, alloc0(a.baseTypeSize))
if p.kind == jsonInt:
t[p.getInt] = getPointer(a)
next(p)
else: raiseParseErr(p, "index for ref type expected")
loadAny(p, a[], t)
if p.kind == jsonArrayEnd: next(p)
else: raiseParseErr(p, "']' end of ref-address pair expected")
else: raiseParseErr(p, "int for pointer type expected")
of akProc, akPointer, akCString:
case p.kind
of jsonNull:
setPointer(a, nil)
next(p)
of jsonInt:
setPointer(a, cast[pointer](p.getInt.int))
next(p)
else: raiseParseErr(p, "int for pointer type expected")
of akString:
case p.kind
of jsonNull:
setPointer(a, nil)
next(p)
of jsonString:
setString(a, p.str)
next(p)
else: raiseParseErr(p, "string expected")
of akInt..akInt64, akUInt..akUInt64:
if p.kind == jsonInt:
setBiggestInt(a, getInt(p))
next(p)
return
raiseParseErr(p, "int expected")
of akFloat..akFloat128:
if p.kind == jsonFloat:
setBiggestFloat(a, getFloat(p))
next(p)
return
raiseParseErr(p, "float expected")
of akRange: loadAny(p, a.skipRange, t)
proc loadAny(s: Stream, a: TAny, t: var Table[BiggestInt, pointer]) =
var p: JsonParser
open(p, s, "unknown file")
next(p)
loadAny(p, a, t)
close(p)
proc load*[T](s: Stream, data: var T) =
## loads `data` from the stream `s`. Raises `EIO` in case of an error.
var tab = initTable[BiggestInt, pointer]()
loadAny(s, toAny(data), tab)
proc store*[T](s: Stream, data: T) =
## stores `data` into the stream `s`. Raises `EIO` in case of an error.
var stored = initIntSet()
var d: T
shallowCopy(d, data)
storeAny(s, toAny(d), stored)
proc `$$`*[T](x: T): string =
## returns a string representation of `x`.
var stored = initIntSet()
var d: T
shallowCopy(d, x)
var s = newStringStream()
storeAny(s, toAny(d), stored)
result = s.data
proc to*[T](data: string): T =
## reads data and transforms it to a ``T``.
var tab = initTable[BiggestInt, pointer]()
loadAny(newStringStream(data), toAny(result), tab)
when isMainModule:
template testit(x: expr) = echo($$to[type(x)]($$x))
var x: array[0..4, array[0..4, string]] = [
["test", "1", "2", "3", "4"], ["test", "1", "2", "3", "4"],
["test", "1", "2", "3", "4"], ["test", "1", "2", "3", "4"],
["test", "1", "2", "3", "4"]]
testit(x)
var test2: tuple[name: string, s: uint] = ("tuple test", 56u)
testit(test2)
type
TE = enum
blah, blah2
TestObj = object
test, asd: int
case test2: TE
of blah:
help: string
else:
nil
PNode = ref TNode
TNode = object
next, prev: PNode
data: string
proc buildList(): PNode =
new(result)
new(result.next)
new(result.prev)
result.data = "middle"
result.next.data = "next"
result.prev.data = "prev"
result.next.next = result.prev
result.next.prev = result
result.prev.next = result
result.prev.prev = result.next
var test3: TestObj
test3.test = 42
test3.test2 = blah
testit(test3)
var test4: ref tuple[a, b: string]
new(test4)
test4.a = "ref string test: A"
test4.b = "ref string test: B"
testit(test4)
var test5 = @[(0,1),(2,3),(4,5)]
testit(test5)
var test6: set[char] = {'A'..'Z', '_'}
testit(test6)
var test7 = buildList()
echo($$test7)
testit(test7)
type
TA {.inheritable.} = object
TB = object of TA
f: int
var
a: ref TA
b: ref TB
new(b)
a = b
echo($$a[]) # produces "{}", not "{f: 0}"