I have a simple function that prints a string and exits:
let fatal s =
print_string "Log: ";
print_endline s;
exit 1
I can use printf to do something similar without the exit 1:
let log fmt =
printf ("Log: " ^^ fmt)
This log function takes a format string and returns a function that takes the parameters needed for that format string and prints “Log: ” in front. (Of course my prefix isn’t this simple for my real application.)
Taking these two and combining them is not easy. A first attempt:
let fatalf fmt =
Printf.printf ("Log: " ^^ fmt)
???
exit 1
The problem is that I have to return the result of my printf expression so that the remaining arguments can be applied to it. Once I’ve returned this value, I don’t have flow control anymore to run exit.
The printf formatter %t looks useful, as it takes a function and runs it:
printf ("Log: " ^^ fmt ^^ "%!%t") ... (fun _ -> exit 1)
This doesn’t seem to work as the %t must be last so it’s run after the log message is written, but this means that the exit function must be after user-specified parameters, and since there’s no way to know how many parameters there will be intervening, one can’t generate a closure that does the full application of printf when given the intervening arguments.
I recall there being some support for named printf arguments, but that this was pulled as it was buggy. Is there any way to emulate that, or to achieve the desired “exit after arbitrary printf” behavior?
You’re looking for
Printf.kprintf:kprintftakes a continuation with typestring -> 'aand applies it to the result ofsprintf-ing with the supplied format. The result of the continuation is the result of the entire call, as expected.