19 #ifndef INCLUDE_UTIL_LOG_HPP
20 #define INCLUDE_UTIL_LOG_HPP
26 #include <boost/config.hpp>
27 #include <boost/current_function.hpp>
28 #include <boost/enable_shared_from_this.hpp>
29 #include <boost/function.hpp>
30 #include <boost/scoped_ptr.hpp>
31 #include <boost/shared_ptr.hpp>
33 #include <RCF/Export.hpp>
34 #include <RCF/MemStream.hpp>
35 #include <RCF/ThreadLibrary.hpp>
36 #include <RCF/Tools.hpp>
38 #include <RCF/util/Tchar.hpp>
39 #include <RCF/util/VariableArgMacro.hpp>
49 void printToOstream(MemOstream & os,
const std::vector<T> &v)
52 for (std::size_t i=0; i<v.size(); ++i)
61 void printToOstream(MemOstream & os,
const std::deque<T> &d)
64 for (std::size_t i=0; i<d.size(); ++i)
72 class RemoteException;
73 class SerializationException;
75 RCF_EXPORT
void printToOstream(MemOstream & os,
const std::type_info & ti);
76 RCF_EXPORT
void printToOstream(MemOstream & os,
const std::exception & e);
77 RCF_EXPORT
void printToOstream(MemOstream & os,
const Exception &e);
78 RCF_EXPORT
void printToOstream(MemOstream & os,
const RemoteException &e);
79 RCF_EXPORT
void printToOstream(MemOstream & os,
const SerializationException &e);
86 MemOstream mTlsUserBuffer;
87 MemOstream mTlsLoggerBuffer;
88 MemOstream mTlsVarArgBuffer1;
89 MemOstream mTlsVarArgBuffer2;
98 typedef boost::shared_ptr<Logger> LoggerPtr;
100 class RCF_EXPORT LogManager
108 static void deinit();
109 static LogManager & instance();
111 void deactivateAllLoggers();
112 void deactivateAllLoggers(
int name);
114 bool isEnabled(
int name,
int level);
116 typedef std::map< int, std::vector< LoggerPtr > > Loggers;
117 ReadWriteMutex mLoggersMutex;
120 void writeToLoggers(
const LogEntry & logEntry);
121 void activateLogger(LoggerPtr loggerPtr);
122 void deactivateLogger(LoggerPtr loggerPtr);
123 bool isLoggerActive(LoggerPtr loggerPtr);
125 const std::string DefaultLogFormat;
127 Mutex DefaultLoggerPtrMutex;
128 LoggerPtr DefaultLoggerPtr;
135 typedef boost::shared_ptr<LogTarget> LogTargetPtr;
144 virtual void write(
const ByteBuffer & output) = 0;
153 void write(
const ByteBuffer & output);
155 static Mutex sIoMutex;
164 class RCF_EXPORT LogToDebugWindow :
public LogTarget
168 void write(
const ByteBuffer & output);
172 class RCF_EXPORT LogToEventLog :
public LogTarget
175 LogToEventLog(
const std::string & appName,
int eventLogLevel);
178 LogTarget * clone()
const;
179 void write(
const ByteBuffer & output);
182 std::string mAppName;
193 LogToFile(
const std::string & filePath,
bool flushAfterEachWrite =
false);
198 void write(
const ByteBuffer & output);
204 std::string mFilePath;
210 typedef boost::function1<void, const ByteBuffer &> LogFunctor;
212 class RCF_EXPORT LogToFunc :
public LogTarget
215 LogToFunc(LogFunctor logFunctor);
218 void write(
const ByteBuffer & output);
221 LogFunctor mLogFunctor;
227 class RCF_EXPORT LogEntry
231 LogEntry(
int name,
int level);
232 LogEntry(
int name,
int level,
const char * szFile,
int line,
const char * szFunc);
237 const LogEntry& operator<<(
const T& t)
const
239 const_cast<MemOstream&
>(*mpOstream) << t;
243 #ifndef BOOST_NO_STD_WSTRING
244 const LogEntry& operator<<(
const std::wstring& t)
const
246 const_cast<MemOstream&
>(*mpOstream) << wstringToString(t);
251 MemOstream & getOstream()
258 friend class LogManager;
269 boost::uint32_t mTimeMs;
271 MemOstream * mpOstream;
277 typedef boost::function2<void, const LogEntry &, ByteBuffer&> LogFormatFunctor;
279 class RCF_EXPORT Logger :
public boost::enable_shared_from_this<Logger>
282 Logger(
int name,
int level,
const LogTarget& logTarget,
const std::string & logFormat =
"");
283 Logger(
int name,
int level,
const LogTarget& logTarget, LogFormatFunctor logFormatFunctor);
285 Logger(
int name,
int level, LogTargetPtr logTargetPtr,
const std::string & logFormat =
"");
286 Logger(
int name,
int level, LogTargetPtr logTargetPtr, LogFormatFunctor logFormatFunctor);
288 void setName(
int name);
289 void setLevel(
int level);
290 void setTarget(
const LogTarget & logTarget);
291 void setFormat(
const std::string & logFormat);
294 int getLevel()
const;
295 const LogTarget& getTarget()
const;
296 std::string getFormat()
const;
298 void write(
const LogEntry & logEntry);
308 LogTargetPtr mTargetPtr;
310 LogFormatFunctor mFormatFunctor;
313 typedef boost::shared_ptr<Logger> LoggerPtr;
319 LogNameValue(
const char * name,
const T & value) :
328 friend MemOstream& operator<<(MemOstream & os,
const LogNameValue& lnv)
330 os <<
"(" << lnv.mName <<
" = ";
331 printToOstream(os, lnv.mValue);
338 LogNameValue<T> makeNameValue(
const char * name,
const T & value)
340 return LogNameValue<T>(name, value);
343 #define NAMEVALUE(x) RCF::makeNameValue(#x, x)
345 class LogVarsFunctor :
public VariableArgMacroFunctor
349 LogVarsFunctor() : mLogEntry(0, 0, NULL, 0, NULL)
353 LogVarsFunctor(
int name,
int level,
const char * file,
int line,
const char * szFunc) :
354 mLogEntry(name, level, file, line, szFunc)
360 if (mArgs->tellp() > 0)
362 mLogEntry <<
" [Args: ";
363 mLogEntry.getOstream().write(mArgs->str(), mArgs->tellp());
369 const LogVarsFunctor & operator<<(
const T & t)
const
371 const_cast<LogEntry &
>(mLogEntry) << t;
379 #if defined(_MSC_VER)
380 #pragma warning(push)
381 #pragma warning(disable: 4355) // warning C4355: 'this' : used in base member initializer list
384 DECLARE_VARIABLE_ARG_MACRO( UTIL_LOG, LogVarsFunctor );
386 #if defined(_MSC_VER)
390 #if defined(__GNUC__) && (__GNUC__ < 3 || (__GNUC__ == 3 && __GNUC_MINOR__ < 4))
391 #define UTIL_LOG_GCC_33_HACK (const RCF::VariableArgMacro<RCF::LogVarsFunctor> &)
393 #define UTIL_LOG_GCC_33_HACK
396 #define UTIL_LOG(name, level) \
397 if (RCF::LogManager::instance().isEnabled(name, level)) \
398 UTIL_LOG_GCC_33_HACK RCF::VariableArgMacro<RCF::LogVarsFunctor>( \
399 name, level, __FILE__, __LINE__, BOOST_CURRENT_FUNCTION) \
400 .cast( (RCF::VariableArgMacro<RCF::LogVarsFunctor> *) NULL ) \
403 #define UTIL_LOG_A(x) UTIL_LOG_OP(x, B)
404 #define UTIL_LOG_B(x) UTIL_LOG_OP(x, A)
405 #define UTIL_LOG_OP(x, next) UTIL_LOG_A.notify_((x), #x).UTIL_LOG_ ## next
409 typedef LogToDebugWindow DefaultLogTarget;
411 typedef LogToStdout DefaultLogTarget;
433 RCF_EXPORT
void enableLogging(
434 const LogTarget & logTarget = DefaultLogTarget(),
436 const std::string & logFormat =
"");
439 RCF_EXPORT
void disableLogging();
443 #endif // ! INCLUDE_UTIL_LOG_HPP