Add timestamp support using the new core:time

This commit is contained in:
Mikkel Hjortshoej
2018-12-08 16:02:33 +01:00
parent 984fa1c672
commit 411d1450b0
5 changed files with 41 additions and 10 deletions

View File

@@ -2,6 +2,7 @@ package log
import "core:fmt";
import "core:os";
import "core:time";
Level_Headers := []string{
"[DEBUG] --- ",
@@ -33,15 +34,22 @@ File_Console_Logger_Data :: struct {
ident : string,
}
file_logger :: proc(h: os.Handle, lowest := Level.Debug, opt := Default_File_Logger_Opts, ident := "") -> Logger {
create_file_logger :: proc(h: os.Handle, lowest := Level.Debug, opt := Default_File_Logger_Opts, ident := "") -> Logger {
data := new(File_Console_Logger_Data);
data.lowest_level = lowest;
data.file_handle = h;
data.ident = ident;
return Logger{file_console_logger_proc, data, opt};
}
destroy_file_logger ::proc(log : ^Logger) {
data := cast(^File_Console_Logger_Data)log.data;
if data.file_handle != os.INVALID_HANDLE do os.close(data.file_handle);
free(data);
log^ = nil_logger();
}
console_logger :: proc(lowest := Level.Debug, opt := Default_Console_Logger_Opts, ident := "") -> Logger {
create_console_logger :: proc(lowest := Level.Debug, opt := Default_Console_Logger_Opts, ident := "") -> Logger {
data := new(File_Console_Logger_Data);
data.lowest_level = lowest;
data.file_handle = os.INVALID_HANDLE;
@@ -49,6 +57,11 @@ console_logger :: proc(lowest := Level.Debug, opt := Default_Console_Logger_Opts
return Logger{file_console_logger_proc, data, opt};
}
destroy_console_logger ::proc(log : ^Logger) {
free(log.data);
log^ = nil_logger();
}
file_console_logger_proc :: proc(logger_data: rawptr, level: Level, text: string, options: Options, location := #caller_location) {
data := cast(^File_Console_Logger_Data)logger_data;
if level < data.lowest_level do return;
@@ -61,13 +74,18 @@ file_console_logger_proc :: proc(logger_data: rawptr, level: Level, text: string
do_level_header(options, level, &buf);
/*if Full_Timestamp_Opts & options != nil {
time := os.get_current_system_time();
if Option.Date in options do fmt.sbprintf(&buf, "%d-%d-%d ", time.year, time.month, time.day);
if Option.Time in options do fmt.sbprintf(&buf, "%d:%d:%d ", time.hour, time.minute, time.second);
when time.IS_SUPPORTED {
if Full_Timestamp_Opts & options != nil {
fmt.sbprint(&buf, "[");
t := time.now();
y, m, d := time.date(t);
h, min, s := time.clock(t);
if Option.Date in options do fmt.sbprintf(&buf, "%d-%02d-%02d ", y, m, d);
if Option.Time in options do fmt.sbprintf(&buf, "%02d:%02d:%02d", h, min, s);
fmt.sbprint(&buf, "] ");
}
}
*/
do_location_header(options, &buf, location);
if data.ident != "" do fmt.sbprintf(&buf, "[%s] ", data.ident);

View File

@@ -50,13 +50,18 @@ Multi_Logger_Data :: struct {
loggers : []Logger,
}
multi_logger :: proc(logs: ..Logger) -> Logger {
create_multi_logger :: proc(logs: ..Logger) -> Logger {
data := new(Multi_Logger_Data);
data.loggers = make([]Logger, len(logs));
for log, i in logs do data.loggers[i] = log;
return Logger{multi_logger_proc, data, nil};
}
destroy_multi_logger ::proc(log : ^Logger) {
free(log.data);
log^ = nil_logger();
}
multi_logger_proc :: proc(logger_data: rawptr, level: Level, text: string,
options: Options, location := #caller_location) {
data := cast(^Multi_Logger_Data)logger_data;

View File

@@ -0,0 +1,3 @@
package time
IS_SUPPORTED :: false;

3
core/time/time_osx.odin Normal file
View File

@@ -0,0 +1,3 @@
package time
IS_SUPPORTED :: false;

View File

@@ -2,6 +2,8 @@ package time
import "core:sys/win32"
IS_SUPPORTED :: true;
now :: proc() -> Time {
file_time: win32.Filetime;
@@ -18,5 +20,5 @@ now :: proc() -> Time {
sleep :: proc(d: Duration) {
win32.Sleep(u32(d/Millisecond));
win32.sleep(i32(d/Millisecond));
}