summary refs log tree commit diff stats
path: root/lib/std/stackframes.nim
blob: 28be7ce11f25bef2021cdf11f8e566f16a15b3a1 (plainpre { line-height: 125%; } td.linenos .normal { color: inherit; background-color: transparent; padding-left: 5px; padding-right: 5px; } span.linenos { color: inherit; background-color: transparent; padding-left: 5px; padding-right: 5px; } td.linenos .special { color: #000000; background-color: #ffffc0; padding-left: 5px; padding-right: 5px; } span.linenos.special { color: #000000; background-color: #ffffc0; padding-left: 5px; padding-right: 5px; } .highlight .hll { background-color: #ffffcc } .highlight .c { color: #888888 } /* Comment */ .highlight .err { color: #a61717; background-color: #e3d2d2 } /* Error */ .highlight .k { color: #008800; font-weight: bold } /* Keyword */ .highlight .ch { color: #888888 } /* Comment.Hashbang */ .highlight .cm { color: #888888 } /* Comment.Multiline */ .highlight .cp { color: #cc0000; font-weight: bold } /* Comment.Preproc */ .highlight .cpf { color: #888888 } /* Comment.PreprocFile */ .highlight .c1 { color: #888888 } /* Comment.Single */ .highlight .cs { color: #cc0000; font-weight: bold; background-color: #fff0f0 } /* Comment.Special */ .highlight .gd { color: #000000; background-c
const NimStackTrace = compileOption("stacktrace")
const NimStackTraceMsgs = compileOption("stacktraceMsgs")

template procName*(): string =
  ## returns current C/C++ function name
  when defined(c) or defined(cpp):
    var name {.inject, noinit.}: cstring
    {.emit: "`name` = __func__;".}
    $name

template getPFrame*(): PFrame =
  ## avoids a function call (unlike `getFrame()`)
  block:
    when NimStackTrace:
      var framePtr {.inject, noinit.}: PFrame
      {.emit: "`framePtr` = &FR_;".}
      framePtr

template setFrameMsg*(msg: string, prefix = " ") =
  ## attach a msg to current `PFrame`. This can be called multiple times
  ## in a given PFrame. Noop unless passing --stacktraceMsgs and --stacktrace
  when NimStackTrace and NimStackTraceMsgs:
    block:
      var fr {.inject, noinit.}: PFrame
      {.emit: "`fr` = &FR_;".}
      # consider setting a custom upper limit on size (analog to stack overflow)
      frameMsgBuf.setLen fr.frameMsgLen
      frameMsgBuf.add prefix
      frameMsgBuf.add msg
      fr.frameMsgLen += prefix.len + msg.len