[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: Re: Proposal: Add "level" argument to assert()
- From: "Robert G. Jakabosky" <bobby@<a href="/cgi-bin/echo.cgi?sharedrealm.com">...</a>>
- Date: Sat, 9 Apr 2011 16:53:49 -0700
On Saturday 09, Enrico Colombini wrote:
> Maybe a varargs (print-like) version of assert() could avoid this
> dilemma. Or a generic:
> assert(cond, func, ...)
> that calls func(...) in case of assertion failure, before exiting.
I changed the lualogging  logger code to do that. See my fork on github
. The logger could be made to raise an error when logging a message at
level "FATAL", I am not sure why it doesn't do that already.
I added support for two styles of logging:
* Use string.format() style formatting:
logger:info("Some message: val1='%s', val2=%d", "string value", 123)
* For more complex log message formatting:
local function log_callback(val1, val2)
-- Do some complex pre-processing, maybe dump a table to a string.
-- Note: all returned values are passed to string.format()
return "Some message: val1='%s', val2=%d", val1, val2
-- log_callback() will only be called if the current log level is "DEBUG"
logger:debug(log_callback, "some value", 1234)
This allows logging complex info and only paying the cost "if" that log level
is turned on.
I use debug log level to provide a full dump of packets from my parsing code:
This way I can just set the log level to debug and get full packet dumps
without needing to go into the packet parser and add code to dump the packets.
The lualogging interface should still be backwards compatible with the
original interface. It even detects if you just pass it one string/table
value and doesn't apply 'string.format()' to that value.
Also I just saw that the lualogging project is looking for a maintainer. If
the project is still unmaintained I am willing to maintain it now.
Robert G. Jakabosky