file_console_logger.odin 3.9 KB

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