mirror of
https://github.com/Ed94/Odin.git
synced 2026-08-06 07:38:48 +00:00
Enhance logger interface with 'f' and not 'f' variants, also move level detection out
This commit is contained in:
@@ -30,17 +30,15 @@ Default_File_Logger_Opts :: Options{
|
|||||||
|
|
||||||
|
|
||||||
File_Console_Logger_Data :: struct {
|
File_Console_Logger_Data :: struct {
|
||||||
lowest_level: Level,
|
|
||||||
file_handle: os.Handle,
|
file_handle: os.Handle,
|
||||||
ident : string,
|
ident : string,
|
||||||
}
|
}
|
||||||
|
|
||||||
create_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 := new(File_Console_Logger_Data);
|
||||||
data.lowest_level = lowest;
|
|
||||||
data.file_handle = h;
|
data.file_handle = h;
|
||||||
data.ident = ident;
|
data.ident = ident;
|
||||||
return Logger{file_console_logger_proc, data, opt};
|
return Logger{file_console_logger_proc, data, lowest, opt};
|
||||||
}
|
}
|
||||||
|
|
||||||
destroy_file_logger ::proc(log : ^Logger) {
|
destroy_file_logger ::proc(log : ^Logger) {
|
||||||
@@ -52,10 +50,9 @@ destroy_file_logger ::proc(log : ^Logger) {
|
|||||||
|
|
||||||
create_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 := new(File_Console_Logger_Data);
|
||||||
data.lowest_level = lowest;
|
|
||||||
data.file_handle = os.INVALID_HANDLE;
|
data.file_handle = os.INVALID_HANDLE;
|
||||||
data.ident = ident;
|
data.ident = ident;
|
||||||
return Logger{file_console_logger_proc, data, opt};
|
return Logger{file_console_logger_proc, data, lowest, opt};
|
||||||
}
|
}
|
||||||
|
|
||||||
destroy_console_logger ::proc(log : ^Logger) {
|
destroy_console_logger ::proc(log : ^Logger) {
|
||||||
@@ -65,8 +62,6 @@ destroy_console_logger ::proc(log : ^Logger) {
|
|||||||
|
|
||||||
file_console_logger_proc :: proc(logger_data: rawptr, level: Level, 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;
|
data := cast(^File_Console_Logger_Data)logger_data;
|
||||||
if level < data.lowest_level do return;
|
|
||||||
|
|
||||||
h : os.Handle;
|
h : os.Handle;
|
||||||
if(data.file_handle != os.INVALID_HANDLE) do h = data.file_handle;
|
if(data.file_handle != os.INVALID_HANDLE) do h = data.file_handle;
|
||||||
else do h = level <= Level.Error ? context.stdout : context.stderr;
|
else do h = level <= Level.Error ? context.stdout : context.stderr;
|
||||||
|
|||||||
+25
-10
@@ -60,9 +60,10 @@ Logger_Proc :: #type proc(data: rawptr, level: Level, text: string, options: Opt
|
|||||||
Logger :: runtime.Logger;
|
Logger :: runtime.Logger;
|
||||||
/*
|
/*
|
||||||
Logger :: struct {
|
Logger :: struct {
|
||||||
procedure: Logger_Proc,
|
procedure: Logger_Proc,
|
||||||
data: rawptr,
|
data: rawptr,
|
||||||
options: Options,
|
lowest_level: Level,
|
||||||
|
options: Logger_Options,
|
||||||
}
|
}
|
||||||
*/
|
*/
|
||||||
|
|
||||||
@@ -74,7 +75,7 @@ create_multi_logger :: proc(logs: ..Logger) -> Logger {
|
|||||||
data := new(Multi_Logger_Data);
|
data := new(Multi_Logger_Data);
|
||||||
data.loggers = make([]Logger, len(logs));
|
data.loggers = make([]Logger, len(logs));
|
||||||
copy(data.loggers, logs);
|
copy(data.loggers, logs);
|
||||||
return Logger{multi_logger_proc, data, nil};
|
return Logger{multi_logger_proc, data, Level.Debug, nil};
|
||||||
}
|
}
|
||||||
|
|
||||||
destroy_multi_logger :: proc(log : ^Logger) {
|
destroy_multi_logger :: proc(log : ^Logger) {
|
||||||
@@ -98,18 +99,32 @@ nil_logger_proc :: proc(data: rawptr, level: Level, text: string, options: Optio
|
|||||||
}
|
}
|
||||||
|
|
||||||
nil_logger :: proc() -> Logger {
|
nil_logger :: proc() -> Logger {
|
||||||
return Logger{nil_logger_proc, nil, nil};
|
return Logger{nil_logger_proc, nil, Level.Debug, nil};
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO(bill): Should these be redesigned so that they are do not rely upon `package fmt`?
|
// TODO(bill): Should these be redesigned so that they are do not rely upon `package fmt`?
|
||||||
debug :: proc(fmt_str : string, args : ..any, location := #caller_location) do logf(level=Level.Debug, fmt_str=fmt_str, args=args, location=location);
|
debugf :: 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);
|
infof :: 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);
|
warnf :: 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);
|
errorf :: 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);
|
fatalf :: proc(fmt_str : string, args : ..any, location := #caller_location) do logf(level=Level.Fatal, fmt_str=fmt_str, args=args, location=location);
|
||||||
|
|
||||||
|
debug :: proc(args : ..any, location := #caller_location) do log(level=Level.Debug, args=args, location=location);
|
||||||
|
info :: proc(args : ..any, location := #caller_location) do log(level=Level.Info, args=args, location=location);
|
||||||
|
warn :: proc(args : ..any, location := #caller_location) do log(level=Level.Warning, args=args, location=location);
|
||||||
|
error :: proc(args : ..any, location := #caller_location) do log(level=Level.Error, args=args, location=location);
|
||||||
|
fatal :: proc(args : ..any, location := #caller_location) do log(level=Level.Fatal, args=args, location=location);
|
||||||
|
|
||||||
|
log :: proc(level : Level, args : ..any, location := #caller_location) {
|
||||||
|
logger := context.logger;
|
||||||
|
if level < logger.lowest_level do return;
|
||||||
|
str := fmt.tprint(..args); //NOTE(Hoej): While tprint isn't thread-safe, no logging is.
|
||||||
|
logger.procedure(logger.data, level, str, logger.options, location);
|
||||||
|
}
|
||||||
|
|
||||||
logf :: 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;
|
logger := context.logger;
|
||||||
|
if level < logger.lowest_level do return;
|
||||||
str := len(args) > 0 ? fmt.tprintf(fmt_str, ..args) : fmt.tprint(fmt_str); //NOTE(Hoej): While tprint isn't thread-safe, no logging is.
|
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);
|
logger.procedure(logger.data, level, str, logger.options, location);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -276,9 +276,10 @@ Logger_Options :: bit_set[Logger_Option];
|
|||||||
Logger_Proc :: #type proc(data: rawptr, level: Logger_Level, text: string, options: Logger_Options, location := #caller_location);
|
Logger_Proc :: #type proc(data: rawptr, level: Logger_Level, text: string, options: Logger_Options, location := #caller_location);
|
||||||
|
|
||||||
Logger :: struct {
|
Logger :: struct {
|
||||||
procedure: Logger_Proc,
|
procedure: Logger_Proc,
|
||||||
data: rawptr,
|
data: rawptr,
|
||||||
options: Logger_Options,
|
lowest_level: Logger_Level,
|
||||||
|
options: Logger_Options,
|
||||||
}
|
}
|
||||||
|
|
||||||
Context :: struct {
|
Context :: struct {
|
||||||
@@ -434,7 +435,7 @@ default_logger_proc :: proc(data: rawptr, level: Logger_Level, text: string, opt
|
|||||||
}
|
}
|
||||||
|
|
||||||
default_logger :: proc() -> Logger {
|
default_logger :: proc() -> Logger {
|
||||||
return Logger{default_logger_proc, nil, nil};
|
return Logger{default_logger_proc, nil, Logger_Level.Debug, nil};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user