ThreadSafeLogger.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030 #include "config.h"
00031
00032 #include <ctime>
00033
00034 #include "Logger.hpp"
00035
00036
00037
00038 using namespace mermaid::support::logging;
00039
00040
00041
00042 shared_ptr<Logger> ThreadSafeLogger::build()
00043 {
00044 return shared_ptr<Logger> (new ThreadSafeLogger());
00045 }
00046
00047
00048
00049 shared_ptr<Logger> ThreadSafeLogger::build (const std::string& prefix)
00050 {
00051 return shared_ptr<Logger> (new ThreadSafeLogger (prefix));
00052 }
00053
00054
00055
00056 ThreadSafeLogger::ThreadSafeLogger() :
00057 mutex_(),
00058 indentation_ (0),
00059 prefix_(),
00060 levelOutputs_ (std::map<LogLevel, std::list<shared_ptr<LogOutput> > >())
00061 {
00062 }
00063
00064
00065
00066 ThreadSafeLogger::ThreadSafeLogger (const std::string& prefix) :
00067 mutex_(),
00068 indentation_ (0),
00069 prefix_ (prefix),
00070 levelOutputs_ (std::map<LogLevel, std::list<shared_ptr<LogOutput> > >())
00071 {
00072 }
00073
00074 void ThreadSafeLogger::enableLogLevel (const LogLevel& logLevel,
00075 shared_ptr<LogOutput> logOutput)
00076 {
00077 ACE_Write_Guard<ACE_RW_Mutex> guard (mutex_);
00078 levelOutputs_[logLevel].push_back (logOutput);
00079 }
00080
00081
00082
00083 void ThreadSafeLogger::disableLogLevel (const LogLevel& logLevel,
00084 shared_ptr<LogOutput> logOutput)
00085 {
00086 ACE_Write_Guard<ACE_RW_Mutex> guard (mutex_);
00087 levelOutputs_[logLevel].remove (logOutput);
00088 }
00089
00090
00091
00092 void ThreadSafeLogger::disableLogLevel (const LogLevel& logLevel)
00093 {
00094 ACE_Write_Guard<ACE_RW_Mutex> guard (mutex_);
00095 levelOutputs_[logLevel].clear();
00096 }
00097
00098
00099
00100 void ThreadSafeLogger::setPrefix (const std::string& prefix)
00101 {
00102 ACE_Write_Guard<ACE_RW_Mutex> guard (mutex_);
00103 prefix_ = prefix;
00104 }
00105
00106
00107
00108 void ThreadSafeLogger::log (const LogLevel& logLevel,
00109 const std::string& message)
00110 {
00111 time_t t = time (NULL);
00112
00113
00114 if ( (logLevel == LOG_BEGIN) || (logLevel == LOG_END)) {
00115 mutex_.acquire_write();
00116 }
00117 else {
00118 mutex_.acquire_read();
00119 }
00120
00121
00122 if ( (logLevel == LOG_END) && (indentation_ > 0)) {
00123 indentation_--;
00124 }
00125 int indentation = indentation_;
00126 if (logLevel == LOG_BEGIN) {
00127 indentation_++;
00128 }
00129
00130 std::list<shared_ptr<LogOutput> >& logList = levelOutputs_[logLevel];
00131 for (std::list<shared_ptr<LogOutput> >::iterator i = logList.begin();
00132 i != logList.end();
00133 i++) {
00134 (*i)->outputMessage (LogMessage (logLevel,
00135 t,
00136 indentation,
00137 prefix_,
00138 message));
00139 }
00140
00141 mutex_.release();
00142 }