1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361
|
/*
* This software is Copyright 2007 Petru Marginean <petru.marginean@gmail.com>
*
* This is free and unencumbered software released into the public domain.
*
* Anyone is free to copy, modify, publish, use, compile, sell, or
* distribute this software, either in source code form or as a compiled
* binary, for any purpose, commercial or non-commercial, and by any
* means.
*
* In jurisdictions that recognize copyright laws, the author or authors
* of this software dedicate any and all copyright interest in the
* software to the public domain. We make this dedication for the benefit
* of the public at large and to the detriment of our heirs and
* successors. We intend this dedication to be an overt act of
* relinquishment in perpetuity of all present and future rights to this
* software under copyright law.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*
* For more information, please refer to <http://unlicense.org/>
*
*
* simple logging class discussed in Dr Dobs journal
* http://www.drdobbs.com/cpp/logging-in-c/201804215
*
* changed the naming a little, converted to safe printf's
* and added IFLOG() macro to support calling procedure only
* if we are at/above a specific log level
*/
#ifndef __LOG_H__
#define __LOG_H__
#include <sstream>
#include <string>
#include <stdio.h>
#include <stdlib.h>
#include "DtaOptions.h"
inline std::string NowTime();
enum TLogLevel {
E, W, I, D, D1, D2, D3, D4
};
template <typename T>
class Log {
public:
Log();
virtual ~Log();
std::ostringstream& Get(TLogLevel level = I);
public:
static TLogLevel& Level();
static std::string ToString(TLogLevel level);
static TLogLevel FromString(const std::string& level);
static TLogLevel FromInt(const int level);
protected:
std::ostringstream os;
private:
Log(const Log&);
Log& operator =(const Log&);
};
template <typename T>
Log<T>::Log() {
}
template <typename T>
std::ostringstream& Log<T>::Get(TLogLevel level) {
os << "- " << NowTime();
os << " " << ToString(level) << ": ";
// os << std::string(level > D ? level - D : 0, '\t');
return os;
}
template <typename T>
Log<T>::~Log() {
os << std::endl;
T::Output(os.str());
}
template <typename T>
TLogLevel& Log<T>::Level() {
static TLogLevel Level = D4;
return Level;
}
template <typename T>
std::string Log<T>::ToString(TLogLevel level) {
static const char* const buffer[] = {"ERR ", "WARN", "INFO", "DBG ", "DBG1", "DBG2", "DBG3", "DBG4"};
return buffer[level];
}
template <typename T>
TLogLevel Log<T>::FromString(const std::string& level) {
if (level == "DEBUG4")
return D4;
if (level == "DEBUG3")
return D3;
if (level == "DEBUG2")
return D2;
if (level == "DEBUG1")
return D1;
if (level == "DEBUG")
return D;
if (level == "INFO")
return I;
if (level == "WARN")
return W;
if (level == "ERROR")
return E;
Log<T>().Get(W) << "Unknown logging level '" << level << "'. Using INFO level as default.";
return I;
}
template <typename T>
TLogLevel Log<T>::FromInt(const int level) {
if (level == 7)
return D4;
if (level == 6)
return D3;
if (level == 5)
return D2;
if (level == 4)
return D1;
if (level == 3)
return D;
if (level == 2)
return I;
if (level == 1)
return W;
if (level == 0)
return E;
Log<T>().Get(W) << "Unknown logging level '" << level << "'. Using INFO level as default.";
return I;
}
template <typename T>
class RLog {
public:
RLog();
virtual ~RLog();
std::ostringstream& Get(TLogLevel level = I, sedutiloutput format = sedutilReadable);
public:
static TLogLevel& Level();
static std::string ToString(TLogLevel level);
static TLogLevel FromString(const std::string& level);
static TLogLevel FromInt(const int level);
protected:
std::ostringstream os;
private:
RLog(const RLog&);
RLog& operator =(const RLog&);
TLogLevel curlevel;
sedutiloutput outputformat;
};
template <typename T>
RLog<T>::RLog() {
}
template <typename T>
std::ostringstream& RLog<T>::Get(TLogLevel level, sedutiloutput output_format) {
curlevel = level;
outputformat = output_format;
if (output_format == sedutilNormal) {
os << "- " << NowTime();
os << " " << ToString(level) << ": ";
}
// os << std::string(level > D ? level - D : 0, '\t');
return os;
}
template <typename T>
RLog<T>::~RLog() {
os << std::endl;
if ((curlevel == I) && (outputformat != sedutilNormal))
T::Output(os.str());
else
T::OutputErr(os.str());
}
template <typename T>
TLogLevel& RLog<T>::Level() {
static TLogLevel Level = D4;
return Level;
}
template <typename T>
std::string RLog<T>::ToString(TLogLevel level) {
static const char* const buffer[] = {"ERR ", "WARN", "INFO", "DBG ", "DBG1", "DBG2", "DBG3", "DBG4"};
return buffer[level];
}
template <typename T>
TLogLevel RLog<T>::FromString(const std::string& level) {
if (level == "DEBUG4")
return D4;
if (level == "DEBUG3")
return D3;
if (level == "DEBUG2")
return D2;
if (level == "DEBUG1")
return D1;
if (level == "DEBUG")
return D;
if (level == "INFO")
return I;
if (level == "WARN")
return W;
if (level == "ERROR")
return E;
RLog<T>().Get(W, sedutilNormal) << "Unknown logging level '" << level << "'. Using INFO level as default.";
return I;
}
template <typename T>
TLogLevel RLog<T>::FromInt(const int level) {
if (level == 7)
return D4;
if (level == 6)
return D3;
if (level == 5)
return D2;
if (level == 4)
return D1;
if (level == 3)
return D;
if (level == 2)
return I;
if (level == 1)
return W;
if (level == 0)
return E;
RLog<T>().Get(W, sedutilNormal) << "Unknown logging level '" << level << "'. Using INFO level as default.";
return I;
}
class Output2FILE {
public:
static FILE*& Stream();
static FILE*& StreamStdout();
static void Output(const std::string& msg);
static void OutputErr(const std::string& msg);
};
inline FILE*& Output2FILE::StreamStdout() {
static FILE* pStream = stdout;
return pStream;
}
inline FILE*& Output2FILE::Stream() {
static FILE* pStream = stderr;
return pStream;
}
inline void Output2FILE::OutputErr(const std::string& msg) {
FILE* pStream = Stream();
if (!pStream)
return;
fprintf(pStream, "%s", msg.c_str());
fflush(pStream);
}
inline void Output2FILE::Output(const std::string& msg) {
FILE* pStream = StreamStdout();
if (!pStream)
return;
fprintf(pStream, "%s", msg.c_str());
fflush(pStream);
}
#if defined(WIN32) || defined(_WIN32) || defined(__WIN32__)
#if defined (BUILDING_FILELOG_DLL)
#define FILELOG_DECLSPEC __declspec (dllexport)
#elif defined (USING_FILELOG_DLL)
#define FILELOG_DECLSPEC __declspec (dllimport)
#else
#define FILELOG_DECLSPEC
#endif // BUILDING_DBSIMPLE_DLL
#else
#define FILELOG_DECLSPEC
#endif // _WIN32
class FILELOG_DECLSPEC CLog : public Log<Output2FILE> {
};
//typedef Log<Output2FILE> FILELog;
class FILELOG_DECLSPEC RCLog : public RLog<Output2FILE> {
};
#ifndef CLOG_MAX_LEVEL
#define CLOG_MAX_LEVEL D4
#endif
#if 0
#define LOG(level) \
if (level > CLOG_MAX_LEVEL) ;\
else if (level > CLog::Level() || !Output2FILE::Stream()) ; \
else CLog().Get(level)
#endif
#define IFLOG(level) \
if (level > CLOG_MAX_LEVEL) ;\
else if (level > CLog::Level() || !Output2FILE::Stream()) ; \
else
extern sedutiloutput outputFormat;
#define LOGX(level) \
if (level > CLOG_MAX_LEVEL) ;\
else if (level > RCLog::Level() || !Output2FILE::Stream()) ; \
else RCLog().Get(level, outputFormat)
#define LOG LOGX
#if defined(WIN32) || defined(_WIN32) || defined(__WIN32__)
#include <windows.h>
#include <cstdlib>
inline std::string NowTime() {
const int MAX_LEN = 200;
char buffer[MAX_LEN];
if (GetTimeFormatA(LOCALE_USER_DEFAULT, 0, 0,
"HH':'mm':'ss", buffer, MAX_LEN) == 0)
return "Error in NowTime()";
char result[100] = {0};
static DWORD first = GetTickCount();
sprintf_s(result, 99, "%s.%03ld", buffer, (long) (GetTickCount() - first) % 1000);
return result;
}
#else
#include <sys/time.h>
inline std::string NowTime() {
char buffer[11];
time_t t;
time(&t);
tm r = {0};
strftime(buffer, sizeof (buffer), "%X", localtime_r(&t, &r));
struct timeval tv;
gettimeofday(&tv, 0);
char result[100] = {0};
snprintf(result, 95, "%s.%03ld", buffer, (long) tv.tv_usec / 1000);
return result;
}
#endif //WIN32
#endif //__LOG_H__
|