lua-users home
lua-l archive

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


On Monday 14, Wolfgang Pupp wrote:
> The assert function in Lua is very nice, but IMHO it has one major
> drawback: string concatenation for its optional "message"-argument
> is often necessary, but slow and happens too early. This is mildly
> annoying for the Lua 5.2 implementation, and even worse for LuaJIT
> (because the string concatenation prevents JIT compilation of the
> surrounding code).
> 
> My suggestion is to not only allow strings as "message"-argument,
> but also functions; if a function is supplied it only gets called
> when the assertion actually fails and is expected to return a
> string describing the problem. Example:

The latest version of lua-logging [1] has support for custom log 
formatter callback functions and string.format() style formatting.  
See some examples at the bottom of the README.  The log message is 
only formatted if logging is enabled for that message's level.  See 
the LOG_MSG function [2] for how the log formatter works.

I refactored the LOG_MSG function into an assertion [3] function with 
the advanced formatting support.

LuaJIT should be able to eliminate almost all of the code for disabled 
log statements.  Even without JIT support it should still be cheap 
enough to keep a lot of logging statement in the code when they are 
normally disabled at runtime.


1. https://github.com/Neopallium/lualogging
2. 
https://github.com/Neopallium/lualogging/blob/master/src/logging.lua#L50
3. https://gist.github.com/4536604

-- 
Robert G. Jakabosky