2005-04-07 20:57:36 +00:00
|
|
|
/** @file
|
|
|
|
|
|
|
|
$Id$
|
|
|
|
|
|
|
|
$Date$
|
|
|
|
$Author$
|
|
|
|
|
|
|
|
@copy © Marc Wäckerlin
|
|
|
|
@license LGPL, see file <a href="license.html">COPYING</a>
|
|
|
|
|
|
|
|
$Log$
|
2005-11-29 12:42:01 +00:00
|
|
|
Revision 1.3 2005/11/29 12:39:42 marc
|
|
|
|
make it compilable with gcc 4.0.2 and newer doxygen
|
|
|
|
|
2005-04-14 19:06:37 +00:00
|
|
|
Revision 1.2 2005/04/14 19:06:37 marc
|
|
|
|
no more duplicated traces, better filtering for Qt
|
|
|
|
|
2005-04-07 20:57:36 +00:00
|
|
|
Revision 1.1 2005/04/07 20:57:36 marc
|
|
|
|
initial version
|
|
|
|
|
|
|
|
|
|
|
|
1 2 3 4 5 6 7 8
|
|
|
|
5678901234567890123456789012345678901234567890123456789012345678901234567890
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <log4cxx/propertyconfigurator.h>
|
|
|
|
#include <log4cxx/helpers/properties.h>
|
|
|
|
#include <fstream>
|
2012-07-06 09:35:05 +00:00
|
|
|
#include <cstdlib>
|
2005-04-07 20:57:36 +00:00
|
|
|
|
2007-08-20 16:09:31 +00:00
|
|
|
#if (__GNUC__==3 && __GNUC_MINOR__<4 || __GNUC__<3) \
|
|
|
|
&& defined(_REENTRANT) && !defined(_MT)
|
2005-11-29 12:42:01 +00:00
|
|
|
#define _MT
|
|
|
|
#endif
|
|
|
|
|
2005-04-07 20:57:36 +00:00
|
|
|
namespace mrw {
|
|
|
|
|
|
|
|
/** @addtogroup debug
|
|
|
|
*/
|
|
|
|
//@{
|
|
|
|
|
|
|
|
/** @defgroup AutoInitLog4cxx Automatically initialize log4cxx
|
|
|
|
|
|
|
|
If you want to enable log4cxx
|
|
|
|
(http://logging.apache.org/log4cxx), you have to configure it in
|
|
|
|
your application, normally in the main. If you want to configure
|
|
|
|
it automatically, without changing a single line in your code
|
|
|
|
(e.g. for temporary debugging), simply link to @c
|
|
|
|
libmrwlog4cxxconfiguration, either with option @c
|
|
|
|
-lmrwlog4cxxconfiguration for single threaded, or with @c
|
|
|
|
-lmrwlog4cxxconfiguration-mt for multi threaded applications.
|
|
|
|
|
|
|
|
With @ref trclog4cxx and @ref
|
|
|
|
AutoFunctionTrace, you can add tracing based on @c log4cxx,
|
|
|
|
without even change a single line in your code. But your code
|
|
|
|
still has to initialize @c log4cxx. If you even want to
|
|
|
|
automatically initialize @c log4cxx, use this library!
|
|
|
|
|
|
|
|
Configures @c log4cxx in the following way:
|
|
|
|
-# if available, read @c log4cxx property file
|
|
|
|
(read first file found, in this order)
|
|
|
|
-# file specified in environment variable
|
|
|
|
@c $MRW_LOG4CXX_CONFIGFILE
|
|
|
|
-# <code>.mrwlog4cxx</code> (local file)
|
|
|
|
-# <code>~/.mrwlog4cxx</code> (file in user's home)
|
|
|
|
-# <code>/etc/mrwlog4cxx</code> (global configuration)
|
|
|
|
-# if no configuration file is found, use default configuration:
|
|
|
|
@verbatim
|
2007-08-05 08:20:01 +00:00
|
|
|
log4j.rootLogger = WARN, A1
|
|
|
|
log4j.logger.mrw.fntrace = OFF
|
|
|
|
log4j.logger.mrw.fn = OFF
|
2005-04-14 19:06:37 +00:00
|
|
|
log4j.logger.mrw.fn.log4cxx = OFF
|
|
|
|
log4j.logger.mrw.fn.boost = OFF
|
|
|
|
log4j.logger.mrw.fn.Thread = OFF
|
|
|
|
log4j.logger.mrw.fn.QString = OFF
|
|
|
|
log4j.logger.mrw.fn.QShared = OFF
|
|
|
|
log4j.logger.mrw.fn.QWidget = OFF
|
|
|
|
log4j.logger.mrw.fn.QRect = OFF
|
|
|
|
log4j.logger.mrw.fn.qstrcmp = OFF
|
|
|
|
log4j.logger.mrw.fn.*.qt_cast = OFF
|
|
|
|
log4j.logger.mrw.fn.mrw = OFF
|
|
|
|
log4j.logger.mrw.fn.std = OFF
|
|
|
|
log4j.logger.mrw.fn.CppUnit = OFF
|
|
|
|
log4j.logger.mrw.fn.__gnu_cxx = OFF
|
2005-04-07 20:57:36 +00:00
|
|
|
log4j.appender.A1 = org.apache.log4j.ConsoleAppender
|
|
|
|
log4j.appender.A1.layout = org.apache.log4j.PatternLayout
|
|
|
|
@endverbatim
|
|
|
|
and for multi threaded programs in addition:
|
|
|
|
@verbatim
|
2007-08-05 08:20:01 +00:00
|
|
|
log4j.appender.A1.layout.ConversionPattern = \%t-\%-40l - \%m\%n
|
2005-04-07 20:57:36 +00:00
|
|
|
@endverbatim
|
|
|
|
on the other hand, single threaded programs are formatted as:
|
|
|
|
@verbatim
|
2007-08-05 08:20:01 +00:00
|
|
|
log4j.appender.A1.layout.ConversionPattern = \%-40l - \%m\%n
|
2005-04-07 20:57:36 +00:00
|
|
|
@endverbatim
|
|
|
|
This results in the following behaviour:
|
|
|
|
- trace to console
|
|
|
|
- format as...
|
2007-08-05 08:20:01 +00:00
|
|
|
- @c "\%t-\%-40l - \%m\%n" if threaded
|
|
|
|
- @c "\%-40l - \%m\%n" if not threaded
|
2005-04-07 20:57:36 +00:00
|
|
|
- enable all tracing to @c DEBUG
|
|
|
|
- enable tracing of @ref AutoFunctionTrace
|
|
|
|
- disable function trace for @c log4cxx
|
|
|
|
- disable function trace for @c boost
|
|
|
|
- disable function trace for @c Qt base classes
|
|
|
|
- disable function trace for @c libmrw
|
2005-04-14 19:06:37 +00:00
|
|
|
- disable function trace for @c std
|
2005-04-07 20:57:36 +00:00
|
|
|
- disable function trace for @c CppUnit
|
|
|
|
- disable function trace for @c gcc internals
|
|
|
|
*/
|
|
|
|
//@{
|
|
|
|
|
|
|
|
class InitLog4cxx {
|
|
|
|
public:
|
|
|
|
InitLog4cxx() {
|
|
|
|
std::string logconfigfile;
|
|
|
|
char* c(getenv("MRW_LOG4CXX_CONFIGFILE"));
|
|
|
|
if (c && std::ifstream(c))
|
|
|
|
logconfigfile = c;
|
|
|
|
else if (std::ifstream(".mrwlog4cxx"))
|
|
|
|
logconfigfile = ".mrwlog4cxx";
|
|
|
|
else if ((c=getenv("HOME")) &&
|
|
|
|
std::ifstream((std::string(c)+"/.mrwlog4cxx").c_str()))
|
|
|
|
logconfigfile = std::string(c)+"/.mrwlog4cxx";
|
|
|
|
else if (std::ifstream("/etc/mrwlog4cxx"))
|
|
|
|
logconfigfile = "/etc/mrwlog4cxx";
|
|
|
|
if (logconfigfile.size()) {
|
2007-08-05 08:20:01 +00:00
|
|
|
log4cxx::PropertyConfigurator::configure
|
2012-07-06 09:35:05 +00:00
|
|
|
(std::string(logconfigfile.begin(), logconfigfile.end()));
|
2005-04-07 20:57:36 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
log4cxx::helpers::Properties properties;
|
2007-08-05 08:20:01 +00:00
|
|
|
std::string name, cont;
|
|
|
|
properties.setProperty((name="log4j.rootLogger",
|
2012-07-06 09:35:05 +00:00
|
|
|
std::string(name.begin(), name.end())),
|
2007-08-05 08:20:01 +00:00
|
|
|
(cont="WARN, A1",
|
2012-07-06 09:35:05 +00:00
|
|
|
std::string(cont.begin(), cont.end())));
|
2007-08-05 08:20:01 +00:00
|
|
|
properties.setProperty((name="log4j.logger.mrw.fntracea",
|
2012-07-06 09:35:05 +00:00
|
|
|
std::string(name.begin(), name.end())),
|
2007-08-05 08:20:01 +00:00
|
|
|
(cont="OFF",
|
2012-07-06 09:35:05 +00:00
|
|
|
std::string(cont.begin(), cont.end())));
|
2007-08-05 08:20:01 +00:00
|
|
|
properties.setProperty((name="log4j.logger.mrw.fn",
|
2012-07-06 09:35:05 +00:00
|
|
|
std::string(name.begin(), name.end())),
|
2007-08-05 08:20:01 +00:00
|
|
|
(cont="OFF",
|
2012-07-06 09:35:05 +00:00
|
|
|
std::string(cont.begin(), cont.end())));
|
2007-08-05 08:20:01 +00:00
|
|
|
properties.setProperty((name="log4j.logger.mrw.fn.log4cxx",
|
2012-07-06 09:35:05 +00:00
|
|
|
std::string(name.begin(), name.end())),
|
2007-08-05 08:20:01 +00:00
|
|
|
(cont="OFF",
|
2012-07-06 09:35:05 +00:00
|
|
|
std::string(cont.begin(), cont.end())));
|
2007-08-05 08:20:01 +00:00
|
|
|
properties.setProperty((name="log4j.logger.mrw.fn.boost",
|
2012-07-06 09:35:05 +00:00
|
|
|
std::string(name.begin(), name.end())),
|
2007-08-05 08:20:01 +00:00
|
|
|
(cont="OFF",
|
2012-07-06 09:35:05 +00:00
|
|
|
std::string(cont.begin(), cont.end())));
|
2007-08-05 08:20:01 +00:00
|
|
|
properties.setProperty((name="log4j.logger.mrw.fn.Thread",
|
2012-07-06 09:35:05 +00:00
|
|
|
std::string(name.begin(), name.end())),
|
2007-08-05 08:20:01 +00:00
|
|
|
(cont="OFF",
|
2012-07-06 09:35:05 +00:00
|
|
|
std::string(cont.begin(), cont.end())));
|
2007-08-05 08:20:01 +00:00
|
|
|
properties.setProperty((name="log4j.logger.mrw.fn.QString",
|
2012-07-06 09:35:05 +00:00
|
|
|
std::string(name.begin(), name.end())),
|
2007-08-05 08:20:01 +00:00
|
|
|
(cont="OFF",
|
2012-07-06 09:35:05 +00:00
|
|
|
std::string(cont.begin(), cont.end())));
|
2007-08-05 08:20:01 +00:00
|
|
|
properties.setProperty((name="log4j.logger.mrw.fn.QShared",
|
2012-07-06 09:35:05 +00:00
|
|
|
std::string(name.begin(), name.end())),
|
2007-08-05 08:20:01 +00:00
|
|
|
(cont="OFF",
|
2012-07-06 09:35:05 +00:00
|
|
|
std::string(cont.begin(), cont.end())));
|
2007-08-05 08:20:01 +00:00
|
|
|
properties.setProperty((name="log4j.logger.mrw.fn.QWidget",
|
2012-07-06 09:35:05 +00:00
|
|
|
std::string(name.begin(), name.end())),
|
2007-08-05 08:20:01 +00:00
|
|
|
(cont="OFF",
|
2012-07-06 09:35:05 +00:00
|
|
|
std::string(cont.begin(), cont.end())));
|
2007-08-05 08:20:01 +00:00
|
|
|
properties.setProperty((name="log4j.logger.mrw.fn.QRect",
|
2012-07-06 09:35:05 +00:00
|
|
|
std::string(name.begin(), name.end())),
|
2007-08-05 08:20:01 +00:00
|
|
|
(cont="OFF",
|
2012-07-06 09:35:05 +00:00
|
|
|
std::string(cont.begin(), cont.end())));
|
2007-08-05 08:20:01 +00:00
|
|
|
properties.setProperty((name="log4j.logger.mrw.fn.qstrcmp",
|
2012-07-06 09:35:05 +00:00
|
|
|
std::string(name.begin(), name.end())),
|
2007-08-05 08:20:01 +00:00
|
|
|
(cont="OFF",
|
2012-07-06 09:35:05 +00:00
|
|
|
std::string(cont.begin(), cont.end())));
|
2007-08-05 08:20:01 +00:00
|
|
|
properties.setProperty((name="log4j.logger.mrw.fn.*.qt_cast",
|
2012-07-06 09:35:05 +00:00
|
|
|
std::string(name.begin(), name.end())),
|
2007-08-05 08:20:01 +00:00
|
|
|
(cont="OFF",
|
2012-07-06 09:35:05 +00:00
|
|
|
std::string(cont.begin(), cont.end())));
|
2007-08-05 08:20:01 +00:00
|
|
|
properties.setProperty((name="log4j.logger.mrw.fn.mrw",
|
2012-07-06 09:35:05 +00:00
|
|
|
std::string(name.begin(), name.end())),
|
2007-08-05 08:20:01 +00:00
|
|
|
(cont="OFF",
|
2012-07-06 09:35:05 +00:00
|
|
|
std::string(cont.begin(), cont.end())));
|
2007-08-05 08:20:01 +00:00
|
|
|
properties.setProperty((name="log4j.logger.mrw.fn.std",
|
2012-07-06 09:35:05 +00:00
|
|
|
std::string(name.begin(), name.end())),
|
2007-08-05 08:20:01 +00:00
|
|
|
(cont="OFF",
|
2012-07-06 09:35:05 +00:00
|
|
|
std::string(cont.begin(), cont.end())));
|
2007-08-05 08:20:01 +00:00
|
|
|
properties.setProperty((name="log4j.logger.mrw.fn.CppUnit",
|
2012-07-06 09:35:05 +00:00
|
|
|
std::string(name.begin(), name.end())),
|
2007-08-05 08:20:01 +00:00
|
|
|
(cont="OFF",
|
2012-07-06 09:35:05 +00:00
|
|
|
std::string(cont.begin(), cont.end())));
|
2007-08-05 08:20:01 +00:00
|
|
|
properties.setProperty((name="log4j.logger.mrw.fn.__gnu_cxx",
|
2012-07-06 09:35:05 +00:00
|
|
|
std::string(name.begin(), name.end())),
|
2007-08-05 08:20:01 +00:00
|
|
|
(cont="OFF",
|
2012-07-06 09:35:05 +00:00
|
|
|
std::string(cont.begin(), cont.end())));
|
2007-08-05 08:20:01 +00:00
|
|
|
properties.setProperty((name="log4j.appender.A1",
|
2012-07-06 09:35:05 +00:00
|
|
|
std::string(name.begin(), name.end())),
|
2007-08-05 08:20:01 +00:00
|
|
|
(cont="org.apache.log4j.ConsoleAppender",
|
2012-07-06 09:35:05 +00:00
|
|
|
std::string(cont.begin(), cont.end())));
|
2007-08-05 08:20:01 +00:00
|
|
|
properties.setProperty((name="log4j.appender.A1.layout",
|
2012-07-06 09:35:05 +00:00
|
|
|
std::string(name.begin(), name.end())),
|
2007-08-05 08:20:01 +00:00
|
|
|
(cont="org.apache.log4j.PatternLayout",
|
2012-07-06 09:35:05 +00:00
|
|
|
std::string(cont.begin(), cont.end())));
|
2005-11-29 12:42:01 +00:00
|
|
|
#ifdef _MT
|
2007-08-05 08:20:01 +00:00
|
|
|
properties.setProperty((name="log4j.appender.A1.layout.ConversionPattern",
|
2012-07-06 09:35:05 +00:00
|
|
|
std::string(name.begin(), name.end())),
|
2007-08-05 08:20:01 +00:00
|
|
|
(cont="%t-%-40l - %m%n",
|
2012-07-06 09:35:05 +00:00
|
|
|
std::string(cont.begin(), cont.end())));
|
2005-04-07 20:57:36 +00:00
|
|
|
#else
|
2007-08-05 08:20:01 +00:00
|
|
|
properties.setProperty((name="log4j.appender.A1.layout.ConversionPattern",
|
2012-07-06 09:35:05 +00:00
|
|
|
std::string(name.begin(), name.end())),
|
2007-08-05 08:20:01 +00:00
|
|
|
(cont="%-40l - %m%n",
|
2012-07-06 09:35:05 +00:00
|
|
|
std::string(cont.begin(), cont.end())));
|
2005-04-07 20:57:36 +00:00
|
|
|
#endif
|
|
|
|
log4cxx::PropertyConfigurator::configure(properties);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
static InitLog4cxx INIT_LOG4CXX;
|
|
|
|
|
|
|
|
//@}
|
|
|
|
//@}
|
2005-11-29 12:42:01 +00:00
|
|
|
}
|