Use this function from the Logger
class instead:
void log (spi::InternalLoggingEvent const &) const;
Prepare the InternalLoggingEvent
instance in advance. Use InternalLoggingEvent::setFunction()
to set the function information on the event instance.
UPDATE
I have re-read your question and the code I think your are not using log4cplus the way it is intended.
First off, your LogMessage
should not be creating appenders or attaching appenders to loggers each time it is invoked. Appenders and loggers should be set up at the start of your application.
Take a look loggingmacros.h
and the macros there. You can avoid having to define your own macros or functions if you use the LOG4CPLUS_ERROR
, LOG4CPLUS_INFO
, etc. macros. See the tests/
directory for some examples of how to use these macros.
If you want to still avoid using the log4cplus provided macros, take a look at the source in loggingmacros.cxx
:
void
macro_forced_log (log4cplus::Logger const & logger,
log4cplus::LogLevel log_level, log4cplus::tstring const & msg,
char const * filename, int line, char const * func)
{
log4cplus::spi::InternalLoggingEvent & ev = internal::get_ptd ()->forced_log_ev;
ev.setLoggingEvent (logger.getName (), log_level, msg, filename, line);
ev.setFunction (func ? func : "");
logger.forcedLog (ev);
}
This function is internally used by the various LOG4CPLUS_*()
macros in loggingmacros.h
. It shows you how to fill the InternalLoggingEvent
instance and how to force its log. Use it as an example for your own LogMessage()
function.
You would have to replace forcedLog()
with log()
to have it check the Logger threshold.
One implementation could look like this:
HRESULT
LogMessage(
__in DWORD dwLogLevel,
__in LPCSTR lpszFileName,
__in DWORD dwLineNumber,
__in LPCSTR lpszLogMessage,
__in LPCSTR lpszFunction)
{
log4cplus::spi::InternalLoggingEvent ev;
log4cplus::Logger logger (Logger::getRoot());
ev.setLoggingEvent (logger.getName (), dwLogLevel, lpszLogMessage, lpszFileName,
dwLineNumber);
ev.setFunction (func ? lpszFunction : "");
logger.forcedLog (ev); // or logger.log(ev)
return S_OK;
}
#define PrintLogMessage(ll, msg) \
LogMessage(ll, __FILE__, __LINE__, msg, LOG4CPLUS_MACRO_FUNCTION())