|
| 1 | +// Taken from here: http://www.drdobbs.com/cpp/logging-in-c/201804215 |
| 2 | +// Original author: Petru Marginean <[email protected]> |
| 3 | +// Modified by: Czarek Tomczak <[email protected]> |
| 4 | + |
| 5 | +// Usage: |
| 6 | +// LOG_DEBUG << "Browser: someVar = " << someVar; |
| 7 | +// Warning: |
| 8 | +// Log is written when ~Log() destructor is called, which |
| 9 | +// occurs when outside of scope. This could result in log |
| 10 | +// not being written if program crashes before code goes |
| 11 | +// outside of current scope. You could embrace LOG_DEBUG |
| 12 | +// statements with braces {} to guarantee the log to be |
| 13 | +// written immediately. |
| 14 | + |
| 15 | +#ifndef __LOG_H__ |
| 16 | +#define __LOG_H__ |
| 17 | + |
| 18 | +#include <sstream> |
| 19 | +#include <string> |
| 20 | +#include <stdio.h> |
| 21 | +#include "DebugLog.h" |
| 22 | + |
| 23 | +inline std::string NowTime(); |
| 24 | + |
| 25 | +enum TLogLevel {logERROR, logWARNING, logINFO, logDEBUG, |
| 26 | + logDEBUG1, logDEBUG2, logDEBUG3, logDEBUG4}; |
| 27 | + |
| 28 | +template <typename T> |
| 29 | +class Log |
| 30 | +{ |
| 31 | +public: |
| 32 | + Log(); |
| 33 | + virtual ~Log(); |
| 34 | + std::ostringstream& Get(TLogLevel level = logINFO); |
| 35 | +public: |
| 36 | + static TLogLevel& ReportingLevel(); |
| 37 | + static std::string ToString(TLogLevel level); |
| 38 | + static TLogLevel FromString(const std::string& level); |
| 39 | +protected: |
| 40 | + std::ostringstream os; |
| 41 | +private: |
| 42 | + Log(const Log&); |
| 43 | + Log& operator =(const Log&); |
| 44 | +}; |
| 45 | + |
| 46 | +template <typename T> |
| 47 | +Log<T>::Log() |
| 48 | +{ |
| 49 | +} |
| 50 | + |
| 51 | +template <typename T> |
| 52 | +std::ostringstream& Log<T>::Get(TLogLevel level) |
| 53 | +{ |
| 54 | + // os << "- " << NowTime(); |
| 55 | + // os << " " << ToString(level) << ": "; |
| 56 | + // os << std::string(level > logDEBUG ? level - logDEBUG : 0, '\t'); |
| 57 | + return os; |
| 58 | +} |
| 59 | + |
| 60 | +template <typename T> |
| 61 | +Log<T>::~Log() |
| 62 | +{ |
| 63 | + os << std::endl; |
| 64 | + /* |
| 65 | + if (T::Stream() != stderr) { |
| 66 | + fprintf(stderr, "%s", os.str().c_str()); |
| 67 | + fflush(stderr); |
| 68 | + } |
| 69 | + T::Output(os.str()); |
| 70 | + */ |
| 71 | + DebugLog(os.str().c_str()); |
| 72 | +} |
| 73 | + |
| 74 | +template <typename T> |
| 75 | +TLogLevel& Log<T>::ReportingLevel() |
| 76 | +{ |
| 77 | + static TLogLevel reportingLevel = logDEBUG4; |
| 78 | + return reportingLevel; |
| 79 | +} |
| 80 | + |
| 81 | +template <typename T> |
| 82 | +std::string Log<T>::ToString(TLogLevel level) |
| 83 | +{ |
| 84 | + static const char* const buffer[] = {"ERROR", "WARNING", "INFO", "DEBUG", |
| 85 | + "DEBUG1", "DEBUG2", "DEBUG3", "DEBUG4"}; |
| 86 | + return buffer[level]; |
| 87 | +} |
| 88 | + |
| 89 | +template <typename T> |
| 90 | +TLogLevel Log<T>::FromString(const std::string& level) |
| 91 | +{ |
| 92 | + if (level == "DEBUG4") |
| 93 | + return logDEBUG4; |
| 94 | + if (level == "DEBUG3") |
| 95 | + return logDEBUG3; |
| 96 | + if (level == "DEBUG2") |
| 97 | + return logDEBUG2; |
| 98 | + if (level == "DEBUG1") |
| 99 | + return logDEBUG1; |
| 100 | + if (level == "DEBUG") |
| 101 | + return logDEBUG; |
| 102 | + if (level == "INFO") |
| 103 | + return logINFO; |
| 104 | + if (level == "WARNING") |
| 105 | + return logWARNING; |
| 106 | + if (level == "ERROR") |
| 107 | + return logERROR; |
| 108 | + Log<T>().Get(logWARNING) << "Unknown logging level '" << level |
| 109 | + << "'. Using INFO level as default."; |
| 110 | + return logINFO; |
| 111 | +} |
| 112 | + |
| 113 | +class Output2FILE |
| 114 | +{ |
| 115 | +public: |
| 116 | + static FILE*& Stream(); |
| 117 | + static void Output(const std::string& msg); |
| 118 | +}; |
| 119 | + |
| 120 | +inline FILE*& Output2FILE::Stream() |
| 121 | +{ |
| 122 | + static FILE* pStream = stderr; |
| 123 | + return pStream; |
| 124 | +} |
| 125 | + |
| 126 | +inline void Output2FILE::Output(const std::string& msg) |
| 127 | +{ |
| 128 | + /* |
| 129 | + FILE* pStream = Stream(); |
| 130 | + if (!pStream) |
| 131 | + return; |
| 132 | + fprintf(pStream, "%s", msg.c_str()); |
| 133 | + fflush(pStream); |
| 134 | + */ |
| 135 | +} |
| 136 | + |
| 137 | +#if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) |
| 138 | +# if defined (BUILDING_FILELOG_DLL) |
| 139 | +# define FILELOG_DECLSPEC __declspec (dllexport) |
| 140 | +# elif defined (USING_FILELOG_DLL) |
| 141 | +# define FILELOG_DECLSPEC __declspec (dllimport) |
| 142 | +# else |
| 143 | +# define FILELOG_DECLSPEC |
| 144 | +# endif // BUILDING_DBSIMPLE_DLL |
| 145 | +#else |
| 146 | +# define FILELOG_DECLSPEC |
| 147 | +#endif // _WIN32 |
| 148 | + |
| 149 | +class FILELOG_DECLSPEC FILELog : public Log<Output2FILE> {}; |
| 150 | +//typedef Log<Output2FILE> FILELog; |
| 151 | + |
| 152 | +#ifndef FILELOG_MAX_LEVEL |
| 153 | +#define FILELOG_MAX_LEVEL logDEBUG4 |
| 154 | +#endif |
| 155 | + |
| 156 | +#define LOG(level) \ |
| 157 | + if (level > FILELOG_MAX_LEVEL) ;\ |
| 158 | + else if (level > FILELog::ReportingLevel() || !Output2FILE::Stream()) ; \ |
| 159 | + else FILELog().Get(level) \ |
| 160 | + |
| 161 | +#define LOG_ERROR LOG(logERROR) |
| 162 | +#define LOG_WARNING LOG(logWARNING) |
| 163 | +#define LOG_INFO LOG(logINFO) |
| 164 | +#define LOG_DEBUG LOG(logDEBUG) |
| 165 | + |
| 166 | +#if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) |
| 167 | + |
| 168 | +#include <windows.h> |
| 169 | + |
| 170 | +inline std::string NowTime() |
| 171 | +{ |
| 172 | + const int MAX_LEN = 200; |
| 173 | + char buffer[MAX_LEN]; |
| 174 | + if (GetTimeFormatA(LOCALE_USER_DEFAULT, 0, 0, |
| 175 | + "HH':'mm':'ss", buffer, MAX_LEN) == 0) |
| 176 | + return "Error in NowTime()"; |
| 177 | + |
| 178 | + char result[100] = {0}; |
| 179 | + static DWORD first = GetTickCount(); |
| 180 | + #pragma warning(suppress: 4996) |
| 181 | + sprintf(result, "%s.%03ld", buffer, (long)(GetTickCount() - first) % 1000); |
| 182 | + return result; |
| 183 | +} |
| 184 | + |
| 185 | +#else |
| 186 | + |
| 187 | +#include <sys/time.h> |
| 188 | + |
| 189 | +inline std::string NowTime() |
| 190 | +{ |
| 191 | + char buffer[11]; |
| 192 | + time_t t; |
| 193 | + time(&t); |
| 194 | + tm r = {0}; |
| 195 | + strftime(buffer, sizeof(buffer), "%X", localtime_r(&t, &r)); |
| 196 | + struct timeval tv; |
| 197 | + gettimeofday(&tv, 0); |
| 198 | + char result[100] = {0}; |
| 199 | + std::sprintf(result, "%s.%03ld", buffer, (long)tv.tv_usec / 1000); |
| 200 | + return result; |
| 201 | +} |
| 202 | + |
| 203 | +#endif //WIN32 |
| 204 | + |
| 205 | +#endif //__LOG_H__ |
0 commit comments