file_console_logger.odin 3.9 KB

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