lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]

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 [1] logger code to do that.  See my fork on github 
[2].  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:
logger:debug(dump_packet, packet)

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