stackframes.nim 1.0 KB

12345678910111213141516171819202122232425262728293031
  1. const NimStackTrace = compileOption("stacktrace")
  2. const NimStackTraceMsgs = compileOption("stacktraceMsgs")
  3. template procName*(): string =
  4. ## returns current C/C++ function name
  5. when defined(c) or defined(cpp):
  6. var name {.inject, noinit.}: cstring
  7. {.emit: "`name` = __func__;".}
  8. $name
  9. template getPFrame*(): PFrame =
  10. ## avoids a function call (unlike `getFrame()`)
  11. block:
  12. when NimStackTrace:
  13. var framePtr {.inject, noinit.}: PFrame
  14. {.emit: "`framePtr` = &FR_;".}
  15. framePtr
  16. template setFrameMsg*(msg: string, prefix = " ") =
  17. ## attach a msg to current `PFrame`. This can be called multiple times
  18. ## in a given PFrame. Noop unless passing --stacktraceMsgs and --stacktrace
  19. when NimStackTrace and NimStackTraceMsgs:
  20. block:
  21. var fr {.inject, noinit.}: PFrame
  22. {.emit: "`fr` = &FR_;".}
  23. # consider setting a custom upper limit on size (analog to stack overflow)
  24. frameMsgBuf.setLen fr.frameMsgLen
  25. frameMsgBuf.add prefix
  26. frameMsgBuf.add msg
  27. fr.frameMsgLen += prefix.len + msg.len