diff options
| author | Dragos Popescu <dragos.andreip@yahoo.com> | 2024-01-23 20:56:13 +0200 |
|---|---|---|
| committer | Dragos Popescu <dragos.andreip@yahoo.com> | 2024-01-23 20:56:13 +0200 |
| commit | 90d1f9ab276c6dc5ed3d208d3f3c7e6323f681d8 (patch) | |
| tree | 0562e4ba9001dc1234156c3fc0789bd3cff11d96 /core/log | |
| parent | 98b539ac5c64004c1bd1d3f8fed5e59028b12739 (diff) | |
Removed return value of assertf. assertf now correctly responds to -disable-assert. Added log.assert and log.assertf. All asserts now do the @cold trick, first added to builtin.assert
Diffstat (limited to 'core/log')
| -rw-r--r-- | core/log/log.odin | 36 |
1 files changed, 36 insertions, 0 deletions
diff --git a/core/log/log.odin b/core/log/log.odin index 021a46000..b4039caa0 100644 --- a/core/log/log.odin +++ b/core/log/log.odin @@ -116,6 +116,42 @@ panicf :: proc(fmt_str: string, args: ..any, location := #caller_location) -> ! runtime.panic("log.panicf", location) } +@(disabled=ODIN_DISABLE_ASSERT) +assert :: proc(condition: bool, message := "", loc := #caller_location) { + if !condition { + @(cold) + internal :: proc(message: string, loc: runtime.Source_Code_Location) { + p := context.assertion_failure_proc + if p == nil { + p = runtime.default_assertion_failure_proc + } + log(.Fatal, message, location=loc) + p("runtime assertion", message, loc) + } + internal(message, loc) + } +} + +@(disabled=ODIN_DISABLE_ASSERT) +assertf :: proc(condition: bool, fmt_str: string, args: ..any, loc := #caller_location) { + if !condition { + // NOTE(dragos): We are using the same trick as in builtin.assert + // to improve performance to make the CPU not + // execute speculatively, making it about an order of + // magnitude faster + @(cold) + internal :: proc(loc: runtime.Source_Code_Location, fmt_str: string, args: ..any) { + p := context.assertion_failure_proc + if p == nil { + p = runtime.default_assertion_failure_proc + } + message := fmt.tprintf(fmt_str, ..args) + log(.Fatal, message, location=loc) + p("Runtime assertion", message, loc) + } + internal(loc, fmt_str, ..args) + } +} |