You are viewing a plain text version of this content. The canonical link for it is here.
Posted to log4cxx-dev@logging.apache.org by ca...@apache.org on 2007/07/03 00:45:39 UTC
svn commit: r552619 - in /logging/log4cxx/trunk:
include/log4cxx/helpers/loglog.h include/log4cxx/helpers/mutex.h
src/loglog.cpp src/mutex.cpp
Author: carnold
Date: Mon Jul 2 15:45:38 2007
New Revision: 552619
URL: http://svn.apache.org/viewvc?view=rev&rev=552619
Log:
LOGCXX-187: LogLog::emit could potentially interleave messages
Modified:
logging/log4cxx/trunk/include/log4cxx/helpers/loglog.h
logging/log4cxx/trunk/include/log4cxx/helpers/mutex.h
logging/log4cxx/trunk/src/loglog.cpp
logging/log4cxx/trunk/src/mutex.cpp
Modified: logging/log4cxx/trunk/include/log4cxx/helpers/loglog.h
URL: http://svn.apache.org/viewvc/logging/log4cxx/trunk/include/log4cxx/helpers/loglog.h?view=diff&rev=552619&r1=552618&r2=552619
==============================================================================
--- logging/log4cxx/trunk/include/log4cxx/helpers/loglog.h (original)
+++ logging/log4cxx/trunk/include/log4cxx/helpers/loglog.h Mon Jul 2 15:45:38 2007
@@ -19,6 +19,7 @@
#define _LOG4CXX_HELPERS_LOG_LOG_H
#include <log4cxx/logstring.h>
+#include <log4cxx/helpers/mutex.h>
#include <exception>
namespace log4cxx
@@ -40,13 +41,19 @@
*/
class LOG4CXX_EXPORT LogLog
{
- protected:
- static bool debugEnabled;
+ private:
+ bool debugEnabled;
/**
In quietMode not even errors generate any output.
*/
- static bool quietMode;
+ bool quietMode;
+ Mutex mutex;
+ LogLog();
+ LogLog(const LogLog&);
+ LogLog& operator=(const LogLog&);
+ static LogLog& getInstance();
+
public:
/**
@@ -72,12 +79,12 @@
/**
- In quite mode LogLog generates strictly no output, not even
+ In quiet mode LogLog generates strictly no output, not even
for errors.
@param quietMode <code>true</code> for no output.
*/
- static void setQuietMode(bool quietMode);
+ static void setQuietMode(bool quietMode);
/**
This method is used to output log4cxx internal warning
Modified: logging/log4cxx/trunk/include/log4cxx/helpers/mutex.h
URL: http://svn.apache.org/viewvc/logging/log4cxx/trunk/include/log4cxx/helpers/mutex.h?view=diff&rev=552619&r1=552618&r2=552619
==============================================================================
--- logging/log4cxx/trunk/include/log4cxx/helpers/mutex.h (original)
+++ logging/log4cxx/trunk/include/log4cxx/helpers/mutex.h Mon Jul 2 15:45:38 2007
@@ -20,6 +20,7 @@
#include <log4cxx/log4cxx.h>
+typedef void log4cxx_pool_t;
typedef void log4cxx_thread_mutex_t;
namespace log4cxx
@@ -32,6 +33,7 @@
{
public:
Mutex(log4cxx::helpers::Pool& p);
+ Mutex(log4cxx_pool_t* p);
~Mutex();
log4cxx_thread_mutex_t* getAPRMutex() const;
Modified: logging/log4cxx/trunk/src/loglog.cpp
URL: http://svn.apache.org/viewvc/logging/log4cxx/trunk/src/loglog.cpp?view=diff&rev=552619&r1=552618&r2=552619
==============================================================================
--- logging/log4cxx/trunk/src/loglog.cpp (original)
+++ logging/log4cxx/trunk/src/loglog.cpp Mon Jul 2 15:45:38 2007
@@ -20,21 +20,34 @@
#include <log4cxx/helpers/transcoder.h>
#include <iostream>
#include <log4cxx/private/log4cxx_private.h>
+#include <log4cxx/helpers/synchronized.h>
+#include <log4cxx/helpers/aprinitializer.h>
using namespace log4cxx;
using namespace log4cxx::helpers;
-bool LogLog::debugEnabled = false;
-bool LogLog::quietMode = false;
+LogLog::LogLog() : mutex((log4cxx_pool_t*) APRInitializer::getRootPool()) {
+ synchronized sync(mutex);
+ debugEnabled = false;
+ quietMode = false;
+}
+
+LogLog& LogLog::getInstance() {
+ static LogLog internalLogger;
+ return internalLogger;
+}
+
void LogLog::setInternalDebugging(bool debugEnabled1)
{
- LogLog::debugEnabled = debugEnabled1;
+ synchronized sync(getInstance().mutex);
+ getInstance().debugEnabled = debugEnabled1;
}
void LogLog::debug(const LogString& msg)
{
- if(debugEnabled && !quietMode)
+ synchronized sync(getInstance().mutex);
+ if(getInstance().debugEnabled && !getInstance().quietMode)
{
emit(msg);
}
@@ -42,6 +55,7 @@
void LogLog::debug(const LogString& msg, const std::exception& e)
{
+ synchronized sync(getInstance().mutex);
debug(msg);
emit(e.what());
}
@@ -49,32 +63,36 @@
void LogLog::error(const LogString& msg)
{
- if(quietMode)
- return;
- emit(msg);
+ synchronized sync(getInstance().mutex);
+ if(!getInstance().quietMode) {
+ emit(msg);
+ }
}
void LogLog::error(const LogString& msg, const std::exception& e)
{
+ synchronized sync(getInstance().mutex);
error(msg);
emit(e.what());
}
void LogLog::setQuietMode(bool quietMode1)
{
- LogLog::quietMode = quietMode1;
+ synchronized sync(getInstance().mutex);
+ getInstance().quietMode = quietMode1;
}
void LogLog::warn(const LogString& msg)
{
- if(quietMode)
- return;
-
- emit(msg);
+ synchronized sync(getInstance().mutex);
+ if(!getInstance().quietMode) {
+ emit(msg);
+ }
}
void LogLog::warn(const LogString& msg, const std::exception& e)
{
+ synchronized sync(getInstance().mutex);
warn(msg);
emit(e.what());
}
Modified: logging/log4cxx/trunk/src/mutex.cpp
URL: http://svn.apache.org/viewvc/logging/log4cxx/trunk/src/mutex.cpp?view=diff&rev=552619&r1=552618&r2=552619
==============================================================================
--- logging/log4cxx/trunk/src/mutex.cpp (original)
+++ logging/log4cxx/trunk/src/mutex.cpp Mon Jul 2 15:45:38 2007
@@ -38,6 +38,18 @@
#endif
}
+Mutex::Mutex(log4cxx_pool_t* p) {
+#if APR_HAS_THREADS
+ apr_thread_mutex_t* aprMutex = NULL;
+ apr_status_t stat = apr_thread_mutex_create(&aprMutex,
+ APR_THREAD_MUTEX_NESTED, (apr_pool_t*) p);
+ if (stat != APR_SUCCESS) {
+ throw MutexException(stat);
+ }
+ mutex = aprMutex;
+#endif
+}
+
Mutex::~Mutex() {
#if APR_HAS_THREADS