file_console_logger.odin 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. //+build !freestanding
  2. package log
  3. import "core:fmt"
  4. import "core:strings"
  5. import "core:os"
  6. import "core:time"
  7. Level_Headers := [?]string{
  8. 0..<10 = "[DEBUG] --- ",
  9. 10..<20 = "[INFO ] --- ",
  10. 20..<30 = "[WARN ] --- ",
  11. 30..<40 = "[ERROR] --- ",
  12. 40..<50 = "[FATAL] --- ",
  13. }
  14. Default_Console_Logger_Opts :: Options{
  15. .Level,
  16. .Terminal_Color,
  17. .Short_File_Path,
  18. .Line,
  19. .Procedure,
  20. } | Full_Timestamp_Opts
  21. Default_File_Logger_Opts :: Options{
  22. .Level,
  23. .Short_File_Path,
  24. .Line,
  25. .Procedure,
  26. } | Full_Timestamp_Opts
  27. File_Console_Logger_Data :: struct {
  28. file_handle: os.Handle,
  29. ident: string,
  30. }
  31. create_file_logger :: proc(h: os.Handle, lowest := Level.Debug, opt := Default_File_Logger_Opts, ident := "") -> Logger {
  32. data := new(File_Console_Logger_Data)
  33. data.file_handle = h
  34. data.ident = ident
  35. return Logger{file_console_logger_proc, data, lowest, opt}
  36. }
  37. destroy_file_logger :: proc(log: ^Logger) {
  38. data := cast(^File_Console_Logger_Data)log.data
  39. if data.file_handle != os.INVALID_HANDLE {
  40. os.close(data.file_handle)
  41. }
  42. free(data)
  43. }
  44. create_console_logger :: proc(lowest := Level.Debug, opt := Default_Console_Logger_Opts, ident := "") -> Logger {
  45. data := new(File_Console_Logger_Data)
  46. data.file_handle = os.INVALID_HANDLE
  47. data.ident = ident
  48. return Logger{file_console_logger_proc, data, lowest, opt}
  49. }
  50. destroy_console_logger :: proc(log: Logger) {
  51. free(log.data)
  52. }
  53. file_console_logger_proc :: proc(logger_data: rawptr, level: Level, text: string, options: Options, location := #caller_location) {
  54. data := cast(^File_Console_Logger_Data)logger_data
  55. h: os.Handle = os.stdout if level <= Level.Error else os.stderr
  56. if data.file_handle != os.INVALID_HANDLE {
  57. h = data.file_handle
  58. }
  59. backing: [1024]byte //NOTE(Hoej): 1024 might be too much for a header backing, unless somebody has really long paths.
  60. buf := strings.builder_from_bytes(backing[:])
  61. do_level_header(options, level, &buf)
  62. when time.IS_SUPPORTED {
  63. if Full_Timestamp_Opts & options != nil {
  64. fmt.sbprint(&buf, "[")
  65. t := time.now()
  66. y, m, d := time.date(t)
  67. h, min, s := time.clock(t)
  68. if .Date in options { fmt.sbprintf(&buf, "%d-%02d-%02d ", y, m, d) }
  69. if .Time in options { fmt.sbprintf(&buf, "%02d:%02d:%02d", h, min, s) }
  70. fmt.sbprint(&buf, "] ")
  71. }
  72. }
  73. do_location_header(options, &buf, location)
  74. if .Thread_Id in options {
  75. // NOTE(Oskar): not using context.thread_id here since that could be
  76. // incorrect when replacing context for a thread.
  77. fmt.sbprintf(&buf, "[{}] ", os.current_thread_id())
  78. }
  79. if data.ident != "" {
  80. fmt.sbprintf(&buf, "[%s] ", data.ident)
  81. }
  82. //TODO(Hoej): When we have better atomics and such, make this thread-safe
  83. fmt.fprintf(h, "%s%s\n", strings.to_string(buf), text)
  84. }
  85. do_level_header :: proc(opts: Options, level: Level, str: ^strings.Builder) {
  86. RESET :: "\x1b[0m"
  87. RED :: "\x1b[31m"
  88. YELLOW :: "\x1b[33m"
  89. DARK_GREY :: "\x1b[90m"
  90. col := RESET
  91. switch level {
  92. case .Debug: col = DARK_GREY
  93. case .Info: col = RESET
  94. case .Warning: col = YELLOW
  95. case .Error, .Fatal: col = RED
  96. }
  97. if .Level in opts {
  98. if .Terminal_Color in opts {
  99. fmt.sbprint(str, col)
  100. }
  101. fmt.sbprint(str, Level_Headers[level])
  102. if .Terminal_Color in opts {
  103. fmt.sbprint(str, RESET)
  104. }
  105. }
  106. }
  107. do_location_header :: proc(opts: Options, buf: ^strings.Builder, location := #caller_location) {
  108. if Location_Header_Opts & opts == nil {
  109. return
  110. }
  111. fmt.sbprint(buf, "[")
  112. file := location.file_path
  113. if .Short_File_Path in opts {
  114. last := 0
  115. for r, i in location.file_path {
  116. if r == '/' {
  117. last = i+1
  118. }
  119. }
  120. file = location.file_path[last:]
  121. }
  122. if Location_File_Opts & opts != nil {
  123. fmt.sbprint(buf, file)
  124. }
  125. if .Line in opts {
  126. if Location_File_Opts & opts != nil {
  127. fmt.sbprint(buf, ":")
  128. }
  129. fmt.sbprint(buf, location.line)
  130. }
  131. if .Procedure in opts {
  132. if (Location_File_Opts | {.Line}) & opts != nil {
  133. fmt.sbprint(buf, ":")
  134. }
  135. fmt.sbprintf(buf, "%s()", location.procedure)
  136. }
  137. fmt.sbprint(buf, "] ")
  138. }