jsconsole.nim 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. #
  2. #
  3. # Nim's Runtime Library
  4. # (c) Copyright 2012 Andreas Rumpf
  5. #
  6. # See the file "copying.txt", included in this
  7. # distribution, for details about the copyright.
  8. #
  9. ## Wrapper for the `console` object for the `JavaScript backend
  10. ## <backends.html#backends-the-javascript-target>`_.
  11. ##
  12. ## Styled Messages
  13. ## ===============
  14. ##
  15. ## CSS-styled messages in the browser are useful for debugging purposes.
  16. ## To use them, prefix the message with one or more `%c`,
  17. ## and provide the CSS style as the last argument.
  18. ## The amount of `%c`'s must match the amount of CSS-styled strings.
  19. ##
  20. runnableExamples("-r:off"):
  21. console.log "%c My Debug Message", "color: red" # Notice the "%c"
  22. console.log "%c My Debug %c Message", "color: red", "font-size: 2em"
  23. import std/private/since, std/private/miscdollars # toLocation
  24. when not defined(js):
  25. {.error: "This module only works on the JavaScript platform".}
  26. type Console* = ref object of JsRoot
  27. proc log*(console: Console) {.importcpp, varargs.}
  28. ## https://developer.mozilla.org/docs/Web/API/Console/log
  29. proc debug*(console: Console) {.importcpp, varargs.}
  30. ## https://developer.mozilla.org/docs/Web/API/Console/debug
  31. proc info*(console: Console) {.importcpp, varargs.}
  32. ## https://developer.mozilla.org/docs/Web/API/Console/info
  33. proc error*(console: Console) {.importcpp, varargs.}
  34. ## https://developer.mozilla.org/docs/Web/API/Console/error
  35. template exception*(console: Console, args: varargs[untyped]) =
  36. ## Alias for `console.error()`.
  37. error(console, args)
  38. proc trace*(console: Console) {.importcpp, varargs.}
  39. ## https://developer.mozilla.org/docs/Web/API/Console/trace
  40. proc warn*(console: Console) {.importcpp, varargs.}
  41. ## https://developer.mozilla.org/docs/Web/API/Console/warn
  42. proc clear*(console: Console) {.importcpp, varargs.}
  43. ## https://developer.mozilla.org/docs/Web/API/Console/clear
  44. proc count*(console: Console, label = "".cstring) {.importcpp.}
  45. ## https://developer.mozilla.org/docs/Web/API/Console/count
  46. proc countReset*(console: Console, label = "".cstring) {.importcpp.}
  47. ## https://developer.mozilla.org/docs/Web/API/Console/countReset
  48. proc group*(console: Console, label = "".cstring) {.importcpp.}
  49. ## https://developer.mozilla.org/docs/Web/API/Console/group
  50. proc groupCollapsed*(console: Console, label = "".cstring) {.importcpp.}
  51. ## https://developer.mozilla.org/en-US/docs/Web/API/Console/groupCollapsed
  52. proc groupEnd*(console: Console) {.importcpp.}
  53. ## https://developer.mozilla.org/docs/Web/API/Console/groupEnd
  54. proc time*(console: Console, label = "".cstring) {.importcpp.}
  55. ## https://developer.mozilla.org/docs/Web/API/Console/time
  56. proc timeEnd*(console: Console, label = "".cstring) {.importcpp.}
  57. ## https://developer.mozilla.org/docs/Web/API/Console/timeEnd
  58. proc timeLog*(console: Console, label = "".cstring) {.importcpp.}
  59. ## https://developer.mozilla.org/docs/Web/API/Console/timeLog
  60. proc table*(console: Console) {.importcpp, varargs.}
  61. ## https://developer.mozilla.org/docs/Web/API/Console/table
  62. since (1, 5):
  63. type InstantiationInfo = tuple[filename: string, line: int, column: int]
  64. func getMsg(info: InstantiationInfo; msg: string): string =
  65. var temp = ""
  66. temp.toLocation(info.filename, info.line, info.column + 1)
  67. result.addQuoted("[jsAssert] " & temp)
  68. result.add ','
  69. result.addQuoted(msg)
  70. template jsAssert*(console: Console; assertion) =
  71. ## JavaScript `console.assert`, for NodeJS this prints to stderr,
  72. ## assert failure just prints to console and do not quit the program,
  73. ## this is not meant to be better or even equal than normal assertions,
  74. ## is just for when you need faster performance *and* assertions,
  75. ## otherwise use the normal assertions for better user experience.
  76. ## https://developer.mozilla.org/en-US/docs/Web/API/Console/assert
  77. runnableExamples:
  78. console.jsAssert(42 == 42) # OK
  79. console.jsAssert(42 != 42) # Fail, prints "Assertion failed" and continues
  80. console.jsAssert('`' == '\n' and '\t' == '\0') # Message correctly formatted
  81. assert 42 == 42 # Normal assertions keep working
  82. const
  83. loc = instantiationInfo(fullPaths = compileOption("excessiveStackTrace"))
  84. msg = getMsg(loc, astToStr(assertion)).cstring
  85. {.line: loc.}:
  86. {.emit: ["console.assert(", assertion, ", ", msg, ");"].}
  87. func dir*(console: Console; obj: auto) {.importcpp.}
  88. ## https://developer.mozilla.org/en-US/docs/Web/API/Console/dir
  89. func dirxml*(console: Console; obj: auto) {.importcpp.}
  90. ## https://developer.mozilla.org/en-US/docs/Web/API/Console/dirxml
  91. func timeStamp*(console: Console; label: cstring) {.importcpp.}
  92. ## https://developer.mozilla.org/en-US/docs/Web/API/Console/timeStamp
  93. ##
  94. ## ..warning:: non-standard
  95. var console* {.importc, nodecl.}: Console