From 9d4fa39daa55d0fe775068c123cba8ce5f2950e9 Mon Sep 17 00:00:00 2001 From: Laytan Laats Date: Mon, 13 Jan 2025 20:33:49 +0100 Subject: [PATCH] add ensure and ensuref to fmt and log, fix some inconsistencies --- core/fmt/fmt.odin | 26 ++++++++++++++++++++++++-- core/log/log.odin | 35 +++++++++++++++++++++++++++++++++-- 2 files changed, 57 insertions(+), 4 deletions(-) diff --git a/core/fmt/fmt.odin b/core/fmt/fmt.odin index 49e9f2e6d..da3b419d5 100644 --- a/core/fmt/fmt.odin +++ b/core/fmt/fmt.odin @@ -314,7 +314,29 @@ assertf :: proc(condition: bool, fmt: string, args: ..any, loc := #caller_locati p = runtime.default_assertion_failure_proc } message := tprintf(fmt, ..args) - p("Runtime assertion", message, loc) + p("runtime assertion", message, loc) + } + internal(loc, fmt, ..args) + } +} +// Runtime ensure with a formatted message +// +// Inputs: +// - condition: The boolean condition to be asserted +// - fmt: A format string with placeholders for the provided arguments +// - args: A variadic list of arguments to be formatted +// - loc: The location of the caller +// +ensuref :: proc(condition: bool, fmt: string, args: ..any, loc := #caller_location) { + if !condition { + @(cold) + internal :: proc(loc: runtime.Source_Code_Location, fmt: string, args: ..any) { + p := context.assertion_failure_proc + if p == nil { + p = runtime.default_assertion_failure_proc + } + message := tprintf(fmt, ..args) + p("unsatisfied ensure", message, loc) } internal(loc, fmt, ..args) } @@ -332,7 +354,7 @@ panicf :: proc(fmt: string, args: ..any, loc := #caller_location) -> ! { p = runtime.default_assertion_failure_proc } message := tprintf(fmt, ..args) - p("Panic", message, loc) + p("panic", message, loc) } // Creates a formatted C string diff --git a/core/log/log.odin b/core/log/log.odin index cbb2e922b..2b6317060 100644 --- a/core/log/log.odin +++ b/core/log/log.odin @@ -115,7 +115,7 @@ panicf :: proc(fmt_str: string, args: ..any, location := #caller_location) -> ! } @(disabled=ODIN_DISABLE_ASSERT) -assert :: proc(condition: bool, message := "", loc := #caller_location) { +assert :: proc(condition: bool, message := #caller_expression(condition), loc := #caller_location) { if !condition { @(cold) internal :: proc(message: string, loc: runtime.Source_Code_Location) { @@ -145,7 +145,38 @@ assertf :: proc(condition: bool, fmt_str: string, args: ..any, loc := #caller_lo } message := fmt.tprintf(fmt_str, ..args) log(.Fatal, message, location=loc) - p("Runtime assertion", message, loc) + p("runtime assertion", message, loc) + } + internal(loc, fmt_str, ..args) + } +} + +ensure :: proc(condition: bool, message := #caller_expression(condition), 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("unsatisfied ensure", message, loc) + } + internal(message, loc) + } +} + +ensuref :: proc(condition: bool, fmt_str: string, args: ..any, loc := #caller_location) { + if !condition { + @(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("unsatisfied ensure", message, loc) } internal(loc, fmt_str, ..args) }