This package sits in the Nemerle.Logging
namespace. It is a mental shortcut for the following:
#define VERB
class A {
SomeMethod () : void
{
#if VERB
DoLog ("some stuff");
#endif
...
#if VERB
def previous_value = GetValue ();
#endif
...
UpdateValue ();
...
#if VERB
DoLog ($ "value $(GetValue ()) --> $previous_value");
#endif
}
}
The above is replaced by:
using Nemerle.Logging;
[assembly: LogFunction (DoLog)]
[assembly: LogFlag (VERB, true)]
class A {
SomeMethod () : void
{
log (VERB, "some stuff");
...
whenlogging (VERB)
def previous_value = GetValue ();
...
UpdateValue ();
...
log (VERB, $ "value change $previous_value --> $(GetValue ())");
}
}
The flags set with LogFlag
are global to the current compilation.
Same goes to the LogFunction
. It is an error to use log
(or whenlogging
) with an unknown flag (use
LogFlag (FOO, false)
to disable given flag). This should save
you from typos.
Setting a flag or printing function several times is also an error.
Enabling given logging is a compile time operation -- without the flag
the logging code is not compiled in.
It is also allowed to pass several arguments to the logging function, provided it supports such overload. So the code like below is OK:
[LogFunction (DoLog)]
...
DoLog (color : int, s : string) : void { ... }
...
log (SOME_FLAG, 12, "ble bla");
...
If you want to customize which logging function is used for every
flag, you can specify mapping from each flag name to logging function.
It is done by listing expressions of form FLAG => func_expr
as arguments of
LogFunction
.
For example you may want to use log4net's functions for Debug and Info, so you specify:
using Nemerle.Logging;
[assembly: LogFunction (DEBUG => log4net_category.Debug, TRACE => log4net_category.Info)]
and then
log (DEBUG, "I'm here");
log (TRACE, "enter business functionality");
will use log4net_category.debug
in first expression and log4net_category.info
in second.
The following won't work (that is x and y will be local to the scope):
whenlogging (FOO) {
def x = ...;
def y = ...;
}
Source is available at http://nemerle.org/svn/nemerle/trunk/macros/Logging.n