core/log
log
Types
4File_Console_Logger_Data
File_Console_Logger_Data :: struct {
file_handle: ^os.File,
ident: string,
}SourceData backing a file or console logger.
Log_Allocator
Log_Allocator :: struct {
allocator: runtime.Allocator,
level: Level,
prefix: string,
lock: sync.Mutex,
size_fmt: Log_Allocator_Format,
}SourceLog_Allocator is an allocator which calls context.logger on each of its allocations operations. The format can be changed by setting the size_fmt: Log_Allocator_Format field to either Bytes or Human.
Log_Allocator_Format
Log_Allocator_Format :: enum int {
Bytes = 0, // Actual number of bytes.
Human = 1, // Bytes in human units like bytes, kibibytes, etc. as appropriate.
}SourceFormat to use when logging allocations.
Multi_Logger_Data
Multi_Logger_Data :: struct {
loggers: []Logger,
}SourceA container backing for multiple loggers.
Constants
11Default_Console_Logger_Opts
Default_Console_Logger_Opts :: Options = Options{
.Level,
.Terminal_Color,
.Short_File_Path,
.Line,
.Procedure,
} + Full_Timestamp_OptsSourceThe default option set for a console logger.
It is similar to the file logger default option set, but the output includes colors.
When you use this set of options you can expect the following output:
[LEVEL] --- [YYYY-MM-DD HH:MM:SS] [file.odin:L:proc()] Message
For example:
[INFO ] --- [2025-01-02 12:34:56] [main.odin:8:main()] Hello World!Default_File_Logger_Opts
Default_File_Logger_Opts :: Options = Options{
.Level,
.Short_File_Path,
.Line,
.Procedure,
} + Full_Timestamp_OptsSourceThe default option set for a file logger.
It is similar to the console logger default option set, but the output is not colored.
When you use this set of options you can expect the following output:
[LEVEL] --- [YYYY-MM-DD HH:MM:SS] [file.odin:L:proc()] Message
For example:
[INFO ] --- [2025-01-02 12:34:56] [main.odin:8:main()] Hello World!Full_Timestamp_Opts
Full_Timestamp_Opts :: Options = Options{
.Date,
.Time,
}SourceA preset option set for a logger.
When you use this set of options you can expect the following output:
[YYYY-MM-DD HH:MM:SS] Message
For example:
[2025-01-02 12:34:56] Hello World!Level
Level :: runtime.Logger_LevelSourceThese are defined in package base:runtime as they are used in the context. This is to prevent an import definition cycle. Logger_Level :: enum {
Debug = 0,
Info = 10,
Warning = 20,
Error = 30,
Fatal = 40,
}Location_File_Opts
Location_File_Opts :: Options = Options{
.Short_File_Path,
.Long_File_Path,
}SourceA preset option set for a logger.
When you use this set of options you can expect the following output:
[file.odin] Message
For example:
[main.odin] Hello World!Location_Header_Opts
Location_Header_Opts :: Options = Options{
.Short_File_Path,
.Long_File_Path,
.Line,
.Procedure,
}SourceA preset option set for a logger.
When you use this set of options you can expect the following output:
[file.odin:L:proc()] Message
For example:
[main.odin:8:main()] Hello World!Logger
Logger :: runtime.LoggerSourceData backing the logger.
Defined in package runtime as it is used in the context. This is to prevent an import definition cycle.
Logger :: struct {
// Implementation
procedure: Logger_Proc,
// Configuration data passed to the implementation
data: rawptr,
// Minimum level for messages passed to the implementation
lowest_level: Level,
// Additional data present in the log output
options: Logger_Options,
}Logger_Proc
Logger_Proc :: runtime.Logger_ProcSourceImplementation of the logger.
Defined in package runtime as it is used in the context. This is to prevent an import definition cycle.
Logger_Proc :: #type proc(data: rawptr, level: Level, text: string, options: Options, location := #caller_location);Option
Option :: runtime.Logger_OptionSourceSpecifies additional data present in the log output.
Defined in package runtime as it is used in the context. This is to prevent an import definition cycle.
Option :: enum {
// The log level, e.g. "[DEBUG] ---"
Level,
// The date, e.g. [2025-01-02]
Date,
// The time, e.g. [12:34:56]
Time,
// Just the filename, e.g. [main.odin]
Short_File_Path,
// Full file path, e.g. [/tmp/project/main.odin]
Long_File_Path,
// File line of the log statement, e.g. [8]
Line,
// Calling procedure, e.g. [main()]
Procedure,
// Enables colored output
Terminal_Color
}Options
Options :: runtime.Logger_OptionsSourceSpecifies additional data present in the log output.
Defined in package runtime as it is used in the context. This is to prevent an import definition cycle.
Options :: bit_set[Option];nil_logger_proc
nil_logger_proc :: runtime.default_logger_procSourceDo nothing.
Defined in package runtime as it is used in the context. This is to prevent an import definition cycle.
Variables
1Level_Headers
Level_Headers :: [?]string = [?]string{
0..<10 = "[DEBUG] --- ",
10..<20 = "[INFO ] --- ",
20..<30 = "[WARN ] --- ",
30..<40 = "[ERROR] --- ",
40..<50 = "[FATAL] --- ",
}SourceStrings to output when .Level is included in the logger options.
Procedures
34assert
assert :: proc(condition: bool, message = #caller_expression(condition), loc = #caller_location)SourceWhen condition is false log a message at the Fatal level and abort the program.
Can be disabled using ODIN_DISABLE_ASSERT.
Inputs:
condition: A boolean to checkmessage: Message to log when condition is false (a default is provided)loc: Location of the caller (default is #caller_location)
assertf
assertf :: proc(condition: bool, fmt_str: string, args, loc = #caller_location)SourceWhen condition is false log a formatted message at the Fatal level and abort the program.
Can be disabled using ODIN_DISABLE_ASSERT.
Inputs:
condition: A boolean to checkfmt_str: A format string to use when condition is false, e.g. `"a: %v, b: %v"args: Arguments for the format stringloc: Location of the caller (default is #caller_location)
console_logger_proc
console_logger_proc :: proc(logger_data: rawptr, level: Level, text: string, options: Options, location = #caller_location)Sourcecreate_console_logger
create_console_logger :: proc(lowest = Level.Debug, opt = Default_Console_Logger_Opts, ident: untyped string = "", allocator: mem.Allocator = context.allocator) -> (Logger)SourceCreate a logger that outputs to the terminal.
Allocates Using Provided Allocator
When no longer needed can be destroyed with destroy_console_logger.
Inputs:
lowest: Log level to use (default is.Debug)opt: Specifies additional data present in the log output (default islog.Default_Console_Logger_Opts)ident: Identifier to include in the output (default is"")allocator: Allocator to use for data backing the logger (default iscontext.allocator)
create_file_logger
create_file_logger :: proc(f: ^os.File, lowest = Level.Debug, opt = Default_File_Logger_Opts, ident: untyped string = "", allocator: mem.Allocator = context.allocator) -> (Logger)SourceCreate a logger that outputs to a file.
Allocates Using Provided Allocator
When no longer needed can be destroyed with destroy_file_logger.
Inputs:
h: A handle to the output filelowest: Log level to use (default is.Debug)opt: Specifies additional data present in the log output (default islog.Default_File_Logger_Opts)ident: Identifier to include in the output (default is"")allocator: Allocator to use for data backing the logger (default iscontext.allocator)
create_multi_logger
create_multi_logger :: proc(logs, allocator: mem.Allocator = context.allocator) -> (Logger)SourceCreate a logger that logs to all backing loggers.
Allocates Using Provided Allocator
When no longer needed can be destroyed with destroy_multi_logger.
Note: Logs using a multi logger take both the multi logger and the backing loggers' log levels into account.
Inputs:
logs- Backing loggers passed as multiple argumentsallocator- An allocator used to allocate data to store backing loggers (default iscontext.allocator)
Returns:
- A multi logger
debug
debug :: proc(args, sep: untyped string = " ", location = #caller_location)SourceLog a message at the Debug level.
Inputs:
args: values to be concatenated into the outputsep: separator to use when concatenating (default is" ")location: Location of the caller (default is #caller_location)
debugf
debugf :: proc(fmt_str: string, args, location = #caller_location)SourceLog a formatted message at the Debug level.
Inputs:
fmt_str: A format string, e.g. `"a: %v, b: %v"args: Arguments for the format stringlocation: Location of the caller (default is #caller_location)
destroy_console_logger
destroy_console_logger :: proc(log: Logger, allocator: mem.Allocator = context.allocator)SourceFree the state allocated with create_console_logger.
Inputs:
log: Logger created withcreate_console_loggerallocator: Allocator passed tocreate_console_logger(default iscontext.allocator)
destroy_file_logger
destroy_file_logger :: proc(log: Logger, allocator: mem.Allocator = context.allocator)SourceFree the state allocated with create_file_logger and close the file handle.
Inputs:
log: Logger created withcreate_file_loggerallocator: Allocator passed tocreate_file_logger(default iscontext.allocator)
destroy_multi_logger
destroy_multi_logger :: proc(log: Logger, allocator: mem.Allocator = context.allocator)SourceFree the state allocated with create_multi_logger.
Inputs:
log: Logger created withcreate_multi_loggerallocator: Allocator passed tocreate_multi_logger(default iscontext.allocator)
do_level_header
do_level_header :: proc(opts: Options, str: ^strings.Builder, level: Level)SourceHelper used to build the part of the message including the log level.
do_location_header
do_location_header :: proc(opts: Options, buf: ^strings.Builder, location = #caller_location)SourceHelper used to build the part of the message including the file location.
do_time_header
do_time_header :: proc(opts: Options, buf: ^strings.Builder, t: time.Time)SourceHelper used to build the part of the message including the data and time.
ensure
ensure :: proc(condition: bool, message = #caller_expression(condition), loc = #caller_location)SourceWhen condition is false log a message at the Fatal level and abort the program.
Unlike assert this procedure cannot be disabled with ODIN_DISABLE_ASSERT and will always execute.
Inputs:
condition: A boolean to checkmessage: Message to log when condition is false (a default is provided)loc: Location of the caller (default is #caller_location)
ensuref
ensuref :: proc(condition: bool, fmt_str: string, args, loc = #caller_location)SourceWhen condition is false log a formatted message at the Fatal level and abort the program.
Unlike assertf this procedure cannot be disabled with ODIN_DISABLE_ASSERT and will always execute.
Inputs:
condition: A boolean to checkfmt_str: A format string to use when condition is false, e.g. `"a: %v, b: %v"args: Arguments for the format stringloc: Location of the caller (default is #caller_location)
error
error :: proc(args, sep: untyped string = " ", location = #caller_location)SourceLog a message at the Error level.
Inputs:
args: values to be concatenated into the outputsep: separator to use when concatenating (default is" ")location: Location of the caller (default is #caller_location)
errorf
errorf :: proc(fmt_str: string, args, location = #caller_location)SourceLog a formatted message at the Error level.
Inputs:
fmt_str: A format string, e.g. `"a: %v, b: %v"args: Arguments for the format stringlocation: Location of the caller (default is #caller_location)
fatal
fatal :: proc(args, sep: untyped string = " ", location = #caller_location)SourceLog a message at the Fatal level.
Inputs:
args: values to be concatenated into the outputsep: separator to use when concatenating (default is" ")location: Location of the caller (default is #caller_location)
fatalf
fatalf :: proc(fmt_str: string, args, location = #caller_location)SourceLog a formatted message at the Fatal level.
Inputs:
fmt_str: A format string, e.g. `"a: %v, b: %v"args: Arguments for the format stringlocation: Location of the caller (default is #caller_location)
file_logger_proc
file_logger_proc :: proc(logger_data: rawptr, level: Level, text: string, options: Options, location = #caller_location)Sourceinfo
info :: proc(args, sep: untyped string = " ", location = #caller_location)SourceLog a message at the Info level.
Inputs:
args: values to be concatenated into the outputsep: separator to use when concatenating (default is" ")location: Location of the caller (default is #caller_location)
infof
infof :: proc(fmt_str: string, args, location = #caller_location)SourceLog a formatted message at the Info level.
Inputs:
fmt_str: A format string, e.g. `"a: %v, b: %v"args: Arguments for the format stringlocation: Location of the caller (default is #caller_location)
log
log :: proc(level: Level, args, sep: untyped string = " ", location = #caller_location)SourceLog a message at the desired level.
Inputs:
level: The level of the messageargs: values to be concatenated into the outputsep: separator to use when concatenating (default is" ")location: Location of the caller (default is #caller_location)
log_allocator
log_allocator :: proc(la: ^Log_Allocator) -> (runtime.Allocator)SourceCreate an allocator that logs all allocations.
Inputs:
la: Pointer to the data structure backing the allocator
Returns:
- An allocator that logs all allocations
log_allocator_init
log_allocator_init :: proc(la: ^Log_Allocator, level: Level, size_fmt = Log_Allocator_Format.Bytes, allocator: mem.Allocator = context.allocator, prefix: untyped string = "")SourceInitialize the backing data for the allocator that logs all allocations.
Inputs:
la: Pointer to the data structure to initializelevel: Log level to use for allocationssize_fmt: Format to use when logging allocations (default is.Bytes)allocator: Wrapped allocator (default iscontext.allocator)prefix: Prefix to use in log messages (default is"")
log_allocator_proc
log_allocator_proc :: proc(
allocator_data: rawptr,
mode: runtime.Allocator_Mode,
size: int,
alignment: int,
old_memory: rawptr,
old_size: int,
location: _ = #caller_location,
) -> ([]u8, runtime.Allocator_Error)SourceBacking procedure for allocator that logs all allocations.
logf
logf :: proc(level: Level, fmt_str: string, args, location = #caller_location)SourceLog a formatted message at the desired level.
Inputs:
level: The level of the messagefmt_str: A format string, e.g. `"a: %v, b: %v"args: Arguments for the format stringlocation: Location of the caller (default is #caller_location)
multi_logger_proc
multi_logger_proc :: proc(logger_data: rawptr, level: Level, text: string, options: Options, location = #caller_location)SourceBacking procedure for the multi logger.
nil_logger
nil_logger :: proc() -> (Logger)SourceCreate a logger that does nothing.
Returns:
- A logger that does nothing
panic
panic :: proc(args, location = #caller_location) -> ()SourceLog a message at the Fatal level and abort the program.
Inputs:
args: values to be concatenated into the outputlocation: Location of the caller (default is #caller_location)
panicf
panicf :: proc(fmt_str: string, args, location = #caller_location) -> ()SourceLog a formatted message at the Fatal level and abort the program.
Inputs:
fmt_str: A format string, e.g. `"a: %v, b: %v"args: Arguments for the format stringlocation: Location of the caller (default is #caller_location)
warn
warn :: proc(args, sep: untyped string = " ", location = #caller_location)SourceLog a message at the Warn level.
Inputs:
args: values to be concatenated into the outputsep: separator to use when concatenating (default is" ")location: Location of the caller (default is #caller_location)
warnf
warnf :: proc(fmt_str: string, args, location = #caller_location)SourceLog a formatted message at the Warn level.
Inputs:
fmt_str: A format string, e.g. `"a: %v, b: %v"args: Arguments for the format stringlocation: Location of the caller (default is #caller_location)