Update of /cvsroot/objecthandler/log4cxx-0.9.7/tests/src/customlogger
In directory sc8-pr-cvs3.sourceforge.net:/tmp/cvs-serv25784/tests/src/customlogger
Added Files:
Makefile.am xlogger.cpp xlogger.h xloggertestcase.cpp
Log Message:
--- NEW FILE: Makefile.am ---
EXTRA_DIST = $(top_srcdir)/tests/src/customlogger/*.cpp
noinst_HEADERS= $(top_srcdir)/tests/src/customlogger/*.h
if TESTS
noinst_LIBRARIES = libcustomlogger.a
INCLUDES = -I$(top_srcdir)/include
libcustomlogger_a_SOURCES = \
xlogger.cpp\
xloggertestcase.cpp
check: libcustomlogger.a
endif
--- NEW FILE: xlogger.cpp ---
/*
* Copyright 2003,2004 The Apache Software Foundation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "xlogger.h"
#include <log4cxx/level.h>
#include <log4cxx/logmanager.h>
using namespace log4cxx;
IMPLEMENT_LOG4CXX_OBJECT(XLogger)
IMPLEMENT_LOG4CXX_OBJECT(XFactory)
String XLogger::FQCN = XLogger::getStaticClass().getName() + _T(".");
XFactoryPtr XLogger::factory = new XFactory();
void XLogger::debug(const String& message, const char* file, int line)
{
if (repository->isDisabled(Level::DEBUG_INT))
{
return;
}
if (XLevel::LETHAL->isGreaterOrEqual(this->getEffectiveLevel()))
{
forcedLog(FQCN, Level::DEBUG, message + _T(" ") + suffix, file,line);
}
}
void XLogger::lethal(const String& message, const char* file, int line)
{
if (repository->isDisabled(XLevel::LETHAL_INT))
{
return;
}
if (XLevel::LETHAL->isGreaterOrEqual(this->getEffectiveLevel()))
{
forcedLog(FQCN, XLevel::LETHAL, message, file,line);
}
}
void XLogger::lethal(const String& message)
{
if (repository->isDisabled(XLevel::LETHAL_INT))
{
return;
}
if (XLevel::LETHAL->isGreaterOrEqual(this->getEffectiveLevel()))
{
forcedLog(FQCN, XLevel::LETHAL, message);
}
}
LoggerPtr XLogger::getLogger(const String& name)
{
return LogManager::getLogger(name, factory);
}
LoggerPtr XLogger::getLogger(const helpers::Class& clazz)
{
return XLogger::getLogger(clazz.getName());
}
void XLogger::trace(const String& message, const char* file, int line)
{
if (repository->isDisabled(XLevel::TRACE_INT))
{
return;
}
if (XLevel::TRACE->isGreaterOrEqual(this->getEffectiveLevel()))
{
forcedLog(FQCN, XLevel::TRACE, message, file, line);
}
}
void XLogger::trace(const String& message)
{
if (repository->isDisabled(XLevel::TRACE_INT))
{
return;
}
if (XLevel::TRACE->isGreaterOrEqual(this->getEffectiveLevel()))
{
forcedLog(FQCN, XLevel::TRACE, message);
}
}
XFactory::XFactory()
{
}
LoggerPtr XFactory::makeNewLoggerInstance(const String& name)
{
return new XLogger(name);
}
--- NEW FILE: xloggertestcase.cpp ---
/*
* Copyright 2003,2004 The Apache Software Foundation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <log4cxx/config.h>
#ifdef HAVE_XML
#include <cppunit/TestFixture.h>
#include <cppunit/extensions/HelperMacros.h>
#include "xlogger.h"
#include <log4cxx/xml/domconfigurator.h>
#include "../util/transformer.h"
#include "../util/compare.h"
using namespace log4cxx;
using namespace log4cxx::helpers;
using namespace log4cxx::xml;
/**
Tests handling of custom loggers.
*/
class XLoggerTestCase : public CppUnit::TestFixture
{
CPPUNIT_TEST_SUITE(XLoggerTestCase);
CPPUNIT_TEST(test1);
CPPUNIT_TEST(test2);
CPPUNIT_TEST_SUITE_END();
XLoggerPtr logger;
public:
void setUp()
{
logger =
(XLoggerPtr) XLogger::getLogger(
_T("org.apache.log4j.customLogger.XLoggerTestCase"));
}
void tearDown()
{
logger->getLoggerRepository()->resetConfiguration();
}
void test1() { common(_T("1")); }
void test2() { common(_T("2")); }
void common(const String& number)
{
DOMConfigurator::configure(_T("input/xml/customLogger")
+number+_T(".xml"));
int i = -1;
LOG4CXX_TRACE(logger, _T("Message ") << ++i);
LOG4CXX_DEBUG(logger, _T("Message ") << ++i);
LOG4CXX_WARN(logger, _T("Message ") << ++i);
LOG4CXX_ERROR(logger, _T("Message ") << ++i);
LOG4CXX_FATAL(logger, _T("Message ") << ++i);
LOG4CXX_DEBUG(logger, _T("Message ") << ++i);
CPPUNIT_ASSERT(Compare::compare(_T("output/temp"),
_T("witness/customLogger.")+number));
}
};
CPPUNIT_TEST_SUITE_REGISTRATION(XLoggerTestCase);
#endif //HAVE_XML
--- NEW FILE: xlogger.h ---
/*
* Copyright 2003,2004 The Apache Software Foundation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <log4cxx/logger.h>
#include "../xml/xlevel.h"
namespace log4cxx
{
// Any sub-class of Logger must also have its own implementation of
// CategoryFactory.
class XFactory :
public virtual spi::LoggerFactory,
public virtual helpers::ObjectImpl
{
public:
DECLARE_ABSTRACT_LOG4CXX_OBJECT(XFactory)
BEGIN_LOG4CXX_CAST_MAP()
LOG4CXX_CAST_ENTRY(XFactory)
LOG4CXX_CAST_ENTRY(spi::LoggerFactory)
END_LOG4CXX_CAST_MAP()
XFactory();
virtual LoggerPtr makeNewLoggerInstance(const String& name);
};
typedef helpers::ObjectPtrT<XFactory> XFactoryPtr;
/**
A simple example showing Logger sub-classing. It shows the
minimum steps necessary to implement one's {@link LoggerFactory}.
Note that sub-classes follow the hierarchy even if its loggers
belong to different classes.
*/
class XLogger : public Logger
{
// It's usually a good idea to add a dot suffix to the fully
// qualified class name. This makes caller localization to work
// properly even from classes that have almost the same fully
// qualified class name as XLogger, such as XLogegoryTest.
static String FQCN;
// It's enough to instantiate a factory once and for all.
static XFactoryPtr factory;
String suffix;
public:
DECLARE_ABSTRACT_LOG4CXX_OBJECT(XLogger)
BEGIN_LOG4CXX_CAST_MAP()
LOG4CXX_CAST_ENTRY(XLogger)
LOG4CXX_CAST_ENTRY_CHAIN(Logger)
END_LOG4CXX_CAST_MAP()
/**
Just calls the parent constuctor.
*/
XLogger(const String& name) : Logger(name) {}
/**
Nothing to activate.
*/
void activateOptions() {}
/**
Overrides the standard debug method by appending the value of
suffix variable to each message.
*/
void debug(const String& message, const char* file=0, int line=-1);
/**
We introduce a new printing method in order to support {@link
XLevel#LETHAL}. */
void lethal(const String& message, const char* file=0, int line=-1);
/**
We introduce a new printing method in order to support {@link
XLevel#LETHAL}. */
void lethal(const String& message);
static LoggerPtr getLogger(const String& name);
static LoggerPtr getLogger(const helpers::Class& clazz);
String getSuffix() const
{ return suffix; }
void setSuffix(const String& suffix)
{ this->suffix = suffix; }
/**
We introduce a new printing method that takes the TRACE level.
*/
void trace(const String& message, const char* file=0, int line=-1);
/**
We introduce a new printing method that takes the TRACE level.
*/
void trace(const String& message);
};
typedef helpers::ObjectPtrT<XLogger> XLoggerPtr;
}
#define LOG4CXX_TRACE(logger, message) { \
if (logger->isEnabledFor(log4cxx::XLevel::TRACE)) {\
StringBuffer oss; \
oss << message; \
logger->trace(oss.str(), __FILE__, __LINE__); }}
|