Skip to content

Commit

Permalink
Merge nbc changes to master (#139)
Browse files Browse the repository at this point in the history
* update readme

* Don't log non-printable characters

* bump mingw from v8.1.0 to v11.2.0

* ci: reenable windows matrix

* CI: test with multiple Nim versions (#106)

* CI: test with multiple Nim versions

- plus Nim-1.6 fix
- and enable Windows

* size.nim: halve the number of log lines

* enable styleCheck:usages (nbc branch) (#113)

* fix chronos v4 compatiblity (#138)

`yield` has never worked correctly in chronos - removing broken
function.

Also, the "official" way of checking the state of the future is via
`failed`

---------

Co-authored-by: Zahary Karadjov <zahary@gmail.com>
Co-authored-by: jangko <jangko128@gmail.com>
Co-authored-by: Ștefan Talpalaru <stefantalpalaru@yahoo.com>
Co-authored-by: Etan Kissling <etan@status.im>
  • Loading branch information
5 people authored Nov 17, 2023
1 parent d1d34b9 commit b879544
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 13 deletions.
9 changes: 1 addition & 8 deletions chronicles/chronos_tools.nim
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,5 @@ proc catchOrQuit*(error: Exception) =

proc traceAsyncErrors*(fut: FutureBase) =
fut.addCallback do (arg: pointer):
if not fut.error.isNil:
if fut.failed():
catchOrQuit fut.error[]

template traceAwaitErrors*(fut: FutureBase) =
let f = fut
yield f
if not f.error.isNil:
catchOrQuit f.error[]

45 changes: 40 additions & 5 deletions chronicles/log_output.nim
Original file line number Diff line number Diff line change
Expand Up @@ -551,6 +551,41 @@ proc initLogRecord*(r: var TextLineRecord,
r.level = lvl
appendHeader(r, lvl, topics, name, true)

const
controlChars = {'\x00'..'\x1f'}
extendedAsciiChars = {'\x7f'..'\xff'}
escapedChars*: set[char] = strutils.Newlines + {'"', '\\'} + controlChars + extendedAsciiChars
quoteChars*: set[char] = {' ', '='}

func containsEscapedChars*(str: string|cstring): bool =
for c in str:
if c in escapedChars:
return true
return false

func needsQuotes*(str: string|cstring): bool =
for c in str:
if c in quoteChars:
return true
return false

proc writeEscapedString*(output: var string, str: string|cstring) =
for c in str:
case c
of '"': output.add "\\\""
of '\\': output.add "\\\\"
of '\r': output.add "\\r"
of '\n': output.add "\\n"
of '\t': output.add "\\t"
else:
const hexChars = "0123456789abcdef"
if c >= char(0x20) and c <= char(0x7e):
output.add c
else:
output.add("\\x")
output.add hexChars[int(c) shr 4 and 0xF]
output.add hexChars[int(c) and 0xF]

proc setProperty*(
r: var TextLineRecord, key: string, val: auto) {.raises: [].} =
append(r.output, " ")
Expand All @@ -566,20 +601,20 @@ proc setProperty*(
# This is similar to how it's done in logfmt:
# https://github.com/csquared/node-logfmt/blob/master/lib/stringify.js#L13
let
needsEscape = valText.find(Newlines + {'"', '\\'}) > -1
needsQuote = valText.find({' ', '='}) > -1
needsEscape = valText.find(escapedChars) > -1
needsQuote = valText.find(quoteChars) > -1

if needsEscape or needsQuote:
escaped = newStringOfCap(valText.len + valText.len div 8)
add(escaped, '"')
if needsEscape:
# addQuoted adds quotes and escapes a bunch of characters
# XXX addQuoted escapes more characters than what we look for in above
# needsEscape check - it's a bit weird that way
addQuoted(escaped, valText)
escaped.writeEscapedString(valText)
elif needsQuote:
add(escaped, '"')
add(escaped, valText)
add(escaped, '"')
add(escaped, '"')
valueToWrite = addr escaped
else:
valueToWrite = unsafeAddr valText
Expand Down

0 comments on commit b879544

Please sign in to comment.