20 #ifndef INCLUDE_UTIL_LOG_HPP 21 #define INCLUDE_UTIL_LOG_HPP 30 #include <RCF/Config.hpp> 31 #include <RCF/Export.hpp> 32 #include <RCF/MemStream.hpp> 33 #include <RCF/ThreadLibrary.hpp> 34 #include <RCF/Tools.hpp> 36 #include <RCF/VariableArgMacro.hpp> 46 void printToOstream(MemOstream & os,
const std::vector<T> &v)
49 for (std::size_t i=0; i<v.size(); ++i)
58 void printToOstream(MemOstream & os,
const std::deque<T> &d)
61 for (std::size_t i=0; i<d.size(); ++i)
73 MemOstream mTlsUserBuffer;
74 MemOstream mTlsLoggerBuffer;
75 MemOstream mTlsVarArgBuffer1;
76 MemOstream mTlsVarArgBuffer2;
85 typedef std::shared_ptr<Logger> LoggerPtr;
87 class RCF_EXPORT LogManager
96 static LogManager & instance();
98 void deactivateAllLoggers();
99 void deactivateAllLoggers(
int name);
101 bool isEnabled(
int name,
int level);
103 typedef std::map< int, std::vector< LoggerPtr > > Loggers;
104 ReadWriteMutex mLoggersMutex;
107 void writeToLoggers(
const LogEntry & logEntry);
108 void activateLogger(LoggerPtr loggerPtr);
109 void deactivateLogger(LoggerPtr loggerPtr);
110 bool isLoggerActive(LoggerPtr loggerPtr);
112 const std::string DefaultLogFormat;
114 Mutex DefaultLoggerPtrMutex;
115 LoggerPtr DefaultLoggerPtr;
122 typedef std::shared_ptr<LogTarget> LogTargetPtr;
132 virtual void write(
const ByteBuffer & output) = 0;
143 static Mutex sIoMutex;
152 class RCF_EXPORT LogToDebugWindow :
public LogTarget 160 class RCF_EXPORT LogToEventLog :
public LogTarget 163 LogToEventLog(
const std::string & appName,
int eventLogLevel);
170 std::string mAppName;
181 LogToFile(
const std::string & filePath,
bool flushAfterEachWrite =
false);
192 std::string mFilePath;
198 typedef std::function<void(const ByteBuffer &)> LogFunctor;
210 LogFunctor mLogFunctor;
216 class RCF_EXPORT LogEntry
220 LogEntry(
int name,
int level);
221 LogEntry(
int name,
int level,
const char * szFile,
int line,
const char * szFunc);
226 const LogEntry& operator<<(
const T& t)
const 228 const_cast<MemOstream&
>(*mpOstream) << t;
232 const LogEntry& operator<<(
const std::wstring& t)
const 234 const_cast<MemOstream&
>(*mpOstream) << wstringToString(t);
238 MemOstream & getOstream()
245 friend class LogManager;
256 std::uint32_t mTimeMs;
258 MemOstream * mpOstream;
264 typedef std::function<void(const LogEntry &, ByteBuffer&)> LogFormatFunctor;
266 class RCF_EXPORT Logger :
public std::enable_shared_from_this<Logger>
269 Logger(
int name,
int level,
const LogTarget& logTarget,
const std::string & logFormat =
"");
270 Logger(
int name,
int level,
const LogTarget& logTarget, LogFormatFunctor logFormatFunctor);
272 Logger(
int name,
int level, LogTargetPtr logTargetPtr,
const std::string & logFormat =
"");
273 Logger(
int name,
int level, LogTargetPtr logTargetPtr, LogFormatFunctor logFormatFunctor);
275 void setName(
int name);
276 void setLevel(
int level);
277 void setTarget(
const LogTarget & logTarget);
278 void setFormat(
const std::string & logFormat);
281 int getLevel()
const;
283 std::string getFormat()
const;
285 void write(
const LogEntry & logEntry);
295 LogTargetPtr mTargetPtr;
297 LogFormatFunctor mFormatFunctor;
300 typedef std::shared_ptr<Logger> LoggerPtr;
306 LogNameValue(
const char * name,
const T & value) :
315 friend MemOstream& operator<<(MemOstream & os,
const LogNameValue& lnv)
317 os <<
"(" << lnv.mName <<
" = ";
318 printToOstream(os, lnv.mValue);
325 LogNameValue<T> makeNameValue(
const char * name,
const T & value)
327 return LogNameValue<T>(name, value);
330 #define NAMEVALUE(x) RCF::makeNameValue(#x, x) 332 class LogVarsFunctor :
public VariableArgMacroFunctor
336 LogVarsFunctor() : mLogEntry(0, 0, NULL, 0, NULL)
340 LogVarsFunctor(
int name,
int level,
const char * file,
int line,
const char * szFunc) :
341 mLogEntry(name, level, file, line, szFunc)
347 if (mArgs->tellp() > 0)
349 mLogEntry <<
" [Args: ";
350 mLogEntry.getOstream().write(mArgs->str(), mArgs->tellp());
356 const LogVarsFunctor & operator<<(
const T & t)
const 358 const_cast<LogEntry &
>(mLogEntry) << t;
366 #if defined(_MSC_VER) 367 #pragma warning(push) 368 #pragma warning(disable: 4355) // warning C4355: 'this' : used in base member initializer list 371 DECLARE_VARIABLE_ARG_MACRO( UTIL_LOG, LogVarsFunctor );
373 #if defined(_MSC_VER) 380 #define UTIL_LOG_GCC_33_HACK 383 #define UTIL_LOG(name, level) \ 384 if (RCF::LogManager::instance().isEnabled(name, level)) \ 385 UTIL_LOG_GCC_33_HACK RCF::VariableArgMacro<RCF::LogVarsFunctor>( \ 386 name, level, __FILE__, __LINE__, RCF_CURRENT_FUNCTION) \ 387 .cast( (RCF::VariableArgMacro<RCF::LogVarsFunctor> *) NULL ) \ 390 #define UTIL_LOG_A(x) UTIL_LOG_OP(x, B) 391 #define UTIL_LOG_B(x) UTIL_LOG_OP(x, A) 392 #define UTIL_LOG_OP(x, next) UTIL_LOG_A.notify_((x), #x).UTIL_LOG_ ## next 425 const LogTarget & logTarget = DefaultLogTarget(),
427 const std::string & logFormat =
"");
436 #endif // ! INCLUDE_UTIL_LOG_HPP Configures log output to be directed to a user-supplied function.
Definition: Log.hpp:201
Configures log output to be directed to standard output.
Definition: Log.hpp:136
RCF_EXPORT void disableLogging()
Disables logging for the RCF runtime.
Configures log output to be directed to a log file.
Definition: Log.hpp:178
RCF_EXPORT bool deinit()
Reference-counted deinitialization of RCF library. For actual deinitialization to take place...
Definition: ByteBuffer.hpp:39
Definition: AmiIoHandler.hpp:23
RCF_EXPORT void enableLogging(const LogTarget &logTarget=DefaultLogTarget(), int logLevel=2, const std::string &logFormat="")
Base class for log targets.
Definition: Log.hpp:127
RCF_EXPORT bool init(RcfConfigT *=nullptr)
Reference-counted initialization of RCF library. May be called multiple times (see deinit())...