remove ident from logger struct

This commit is contained in:
Mikkel Hjortshoej
2018-10-31 19:20:55 +01:00
parent 12c810f85d
commit 984fa1c672
2 changed files with 21 additions and 19 deletions

View File

@@ -30,23 +30,26 @@ Default_File_Logger_Opts :: Options{
File_Console_Logger_Data :: struct {
lowest_level: Level,
file_handle: os.Handle,
ident : string,
}
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;
return Logger{file_console_logger_proc, data, opt, ident};
data.ident = ident;
return Logger{file_console_logger_proc, data, opt};
}
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;
return Logger{file_console_logger_proc, data, opt, ident};
data.ident = ident;
return Logger{file_console_logger_proc, data, opt};
}
file_console_logger_proc :: proc(logger_data: rawptr, level: Level, ident: string, text: string, options: Options, location := #caller_location) {
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;
@@ -67,7 +70,7 @@ file_console_logger_proc :: proc(logger_data: rawptr, level: Level, ident: strin
*/
do_location_header(options, &buf, location);
if ident != "" do fmt.sbprintf(&buf, "[%s] ", ident);
if data.ident != "" do fmt.sbprintf(&buf, "[%s] ", data.ident);
//TODO(Hoej): When we have better atomics and such, make this thread-safe
fmt.fprintf(h, "%s %s\n", fmt.to_string(buf), text);
}

View File

@@ -38,13 +38,12 @@ Location_File_Opts :: Options{
Option.Long_File_Path
};
Logger_Proc :: #type proc(data: rawptr, level: Level, ident, text: string, options: Options, location := #caller_location);
Logger_Proc :: #type proc(data: rawptr, level: Level, text: string, options: Options, location := #caller_location);
Logger :: struct {
procedure: Logger_Proc,
data: rawptr,
options: Options,
ident: string
}
Multi_Logger_Data :: struct {
@@ -55,32 +54,32 @@ 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, ""};
return Logger{multi_logger_proc, data, nil};
}
multi_logger_proc :: proc(logger_data: rawptr, level: Level, ident: string, text: string,
multi_logger_proc :: proc(logger_data: rawptr, level: Level, text: string,
options: Options, location := #caller_location) {
data := cast(^Multi_Logger_Data)logger_data;
if data.loggers == nil || len(data.loggers) == 0 do return;
for log in data.loggers do log.procedure(log.data, level, log.ident, text, log.options, location);
for log in data.loggers do log.procedure(log.data, level, text, log.options, location);
}
nil_logger_proc :: proc(data: rawptr, level: Level, ident, text: string, options: Options, location := #caller_location) {
nil_logger_proc :: proc(data: rawptr, level: Level, text: string, options: Options, location := #caller_location) {
// Do nothing
}
nil_logger :: proc() -> Logger {
return Logger{nil_logger_proc, nil, nil, ""};
return Logger{nil_logger_proc, nil, nil};
}
debug :: proc(fmt_str : string, args : ..any, location := #caller_location) do log(level=Level.Debug, fmt_str=fmt_str, args=args, location=location);
info :: proc(fmt_str : string, args : ..any, location := #caller_location) do log(level=Level.Info, fmt_str=fmt_str, args=args, location=location);
warn :: proc(fmt_str : string, args : ..any, location := #caller_location) do log(level=Level.Warning, fmt_str=fmt_str, args=args, location=location);
error :: proc(fmt_str : string, args : ..any, location := #caller_location) do log(level=Level.Error, fmt_str=fmt_str, args=args, location=location);
fatal :: proc(fmt_str : string, args : ..any, location := #caller_location) do log(level=Level.Fatal, fmt_str=fmt_str, args=args, location=location);
debug :: proc(fmt_str : string, args : ..any, location := #caller_location) do logf(level=Level.Debug, fmt_str=fmt_str, args=args, location=location);
info :: proc(fmt_str : string, args : ..any, location := #caller_location) do logf(level=Level.Info, fmt_str=fmt_str, args=args, location=location);
warn :: proc(fmt_str : string, args : ..any, location := #caller_location) do logf(level=Level.Warning, fmt_str=fmt_str, args=args, location=location);
error :: proc(fmt_str : string, args : ..any, location := #caller_location) do logf(level=Level.Error, fmt_str=fmt_str, args=args, location=location);
fatal :: proc(fmt_str : string, args : ..any, location := #caller_location) do logf(level=Level.Fatal, fmt_str=fmt_str, args=args, location=location);
log :: proc(level : Level, fmt_str : string, args : ..any, location := #caller_location) {
logf :: proc(level : Level, fmt_str : string, args : ..any, location := #caller_location) {
logger := context.logger;
str := fmt.tprintf(fmt_str, ..args); //NOTE(Hoej): While tprint isn't thread-safe, no logging is.
logger.procedure(logger.data, level, logger.ident, str, logger.options, location);
str := len(args) > 0 ? fmt.tprintf(fmt_str, ..args) : fmt.tprint(fmt_str); //NOTE(Hoej): While tprint isn't thread-safe, no logging is.
logger.procedure(logger.data, level, str, logger.options, location);
}