21 #ifndef INCLUDE_UTIL_LOG_HPP 22 #define INCLUDE_UTIL_LOG_HPP 31 #include <RCF/Config.hpp> 32 #include <RCF/Export.hpp> 33 #include <RCF/MemStream.hpp> 34 #include <RCF/ThreadLibrary.hpp> 35 #include <RCF/Tools.hpp> 37 #include <RCF/VariableArgMacro.hpp> 47 void printToOstream(MemOstream & os,
const std::vector<T> &v)
50 for (std::size_t i=0; i<v.size(); ++i)
59 void printToOstream(MemOstream & os,
const std::deque<T> &d)
62 for (std::size_t i=0; i<d.size(); ++i)
74 MemOstream mTlsUserBuffer;
75 MemOstream mTlsLoggerBuffer;
76 MemOstream mTlsVarArgBuffer1;
77 MemOstream mTlsVarArgBuffer2;
86 typedef std::shared_ptr<Logger> LoggerPtr;
88 class RCF_EXPORT LogManager
97 static LogManager & instance();
99 void deactivateAllLoggers();
100 void deactivateAllLoggers(
int name);
102 bool isEnabled(
int name,
int level);
104 typedef std::map< int, std::vector< LoggerPtr > > Loggers;
105 ReadWriteMutex mLoggersMutex;
108 void writeToLoggers(
const LogEntry & logEntry);
109 void activateLogger(LoggerPtr loggerPtr);
110 void deactivateLogger(LoggerPtr loggerPtr);
111 bool isLoggerActive(LoggerPtr loggerPtr);
113 const std::string DefaultLogFormat;
115 Mutex DefaultLoggerPtrMutex;
116 LoggerPtr DefaultLoggerPtr;
123 typedef std::shared_ptr<LogTarget> LogTargetPtr;
133 virtual void write(
const ByteBuffer & output) = 0;
144 static Mutex sIoMutex;
153 class RCF_EXPORT LogToDebugWindow :
public LogTarget 161 class RCF_EXPORT LogToEventLog :
public LogTarget 164 LogToEventLog(
const std::string & appName,
int eventLogLevel);
171 std::string mAppName;
182 LogToFile(
const std::string & filePath,
bool flushAfterEachWrite =
false);
193 std::string mFilePath;
199 typedef std::function<void(const ByteBuffer &)> LogFunctor;
211 LogFunctor mLogFunctor;
217 class RCF_EXPORT LogEntry
221 LogEntry(
int name,
int level);
222 LogEntry(
int name,
int level,
const char * szFile,
int line,
const char * szFunc);
227 const LogEntry& operator<<(
const T& t)
const 229 const_cast<MemOstream&
>(*mpOstream) << t;
233 const LogEntry& operator<<(
const std::wstring& t)
const 235 const_cast<MemOstream&
>(*mpOstream) << wstringToString(t);
239 MemOstream & getOstream()
246 friend class LogManager;
257 std::uint32_t mTimeMs;
259 MemOstream * mpOstream;
265 typedef std::function<void(const LogEntry &, ByteBuffer&)> LogFormatFunctor;
267 class RCF_EXPORT Logger :
public std::enable_shared_from_this<Logger>
270 Logger(
int name,
int level,
const LogTarget& logTarget,
const std::string & logFormat =
"");
271 Logger(
int name,
int level,
const LogTarget& logTarget, LogFormatFunctor logFormatFunctor);
273 Logger(
int name,
int level, LogTargetPtr logTargetPtr,
const std::string & logFormat =
"");
274 Logger(
int name,
int level, LogTargetPtr logTargetPtr, LogFormatFunctor logFormatFunctor);
276 void setName(
int name);
277 void setLevel(
int level);
278 void setTarget(
const LogTarget & logTarget);
279 void setFormat(
const std::string & logFormat);
282 int getLevel()
const;
284 std::string getFormat()
const;
286 void write(
const LogEntry & logEntry);
296 LogTargetPtr mTargetPtr;
298 LogFormatFunctor mFormatFunctor;
301 typedef std::shared_ptr<Logger> LoggerPtr;
307 LogNameValue(
const char * name,
const T & value) :
316 friend MemOstream& operator<<(MemOstream & os,
const LogNameValue& lnv)
318 os <<
"(" << lnv.mName <<
" = ";
319 printToOstream(os, lnv.mValue);
326 LogNameValue<T> makeNameValue(
const char * name,
const T & value)
328 return LogNameValue<T>(name, value);
331 #define NAMEVALUE(x) RCF::makeNameValue(#x, x) 333 class LogVarsFunctor :
public VariableArgMacroFunctor
337 LogVarsFunctor() : mLogEntry(0, 0, NULL, 0, NULL)
341 LogVarsFunctor(
int name,
int level,
const char * file,
int line,
const char * szFunc) :
342 mLogEntry(name, level, file, line, szFunc)
348 if (mArgs->tellp() > 0)
350 mLogEntry <<
" [Args: ";
351 mLogEntry.getOstream().write(mArgs->str(), mArgs->tellp());
357 const LogVarsFunctor & operator<<(
const T & t)
const 359 const_cast<LogEntry &
>(mLogEntry) << t;
367 #if defined(_MSC_VER) 368 #pragma warning(push) 369 #pragma warning(disable: 4355) // warning C4355: 'this' : used in base member initializer list 372 DECLARE_VARIABLE_ARG_MACRO( UTIL_LOG, LogVarsFunctor );
374 #if defined(_MSC_VER) 381 #define UTIL_LOG_GCC_33_HACK 384 #define UTIL_LOG(name, level) \ 385 if (RCF::LogManager::instance().isEnabled(name, level)) \ 386 UTIL_LOG_GCC_33_HACK RCF::VariableArgMacro<RCF::LogVarsFunctor>( \ 387 name, level, __FILE__, __LINE__, RCF_CURRENT_FUNCTION) \ 388 .cast( (RCF::VariableArgMacro<RCF::LogVarsFunctor> *) NULL ) \ 391 #define UTIL_LOG_A(x) UTIL_LOG_OP(x, B) 392 #define UTIL_LOG_B(x) UTIL_LOG_OP(x, A) 393 #define UTIL_LOG_OP(x, next) UTIL_LOG_A.notify_((x), #x).UTIL_LOG_ ## next 426 const LogTarget & logTarget = DefaultLogTarget(),
428 const std::string & logFormat =
"");
437 #endif // ! INCLUDE_UTIL_LOG_HPP Configures log output to be directed to a user-supplied function.
Definition: Log.hpp:202
Configures log output to be directed to standard output.
Definition: Log.hpp:137
RCF_EXPORT void disableLogging()
Disables logging for the RCF runtime.
Configures log output to be directed to a log file.
Definition: Log.hpp:179
RCF_EXPORT bool deinit()
Reference-counted deinitialization of RCF library. For actual deinitialization to take place...
Definition: ByteBuffer.hpp:40
Definition: AmiIoHandler.hpp:24
RCF_EXPORT void enableLogging(const LogTarget &logTarget=DefaultLogTarget(), int logLevel=2, const std::string &logFormat="")
Base class for log targets.
Definition: Log.hpp:128
RCF_EXPORT bool init(RcfConfigT *=nullptr)
Reference-counted initialization of RCF library. May be called multiple times (see deinit())...