35 #define COLOR_NORMAL FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED 36 #define COLOR_RED FOREGROUND_RED | FOREGROUND_INTENSITY 37 #define COLOR_GREEN FOREGROUND_GREEN 38 #define COLOR_YELLOW FOREGROUND_GREEN | FOREGROUND_RED 40 #define COLOR_NORMAL "\033[0m" 41 #define COLOR_RED "\033[31m" 42 #define COLOR_GREEN "\033[32m" 43 #define COLOR_YELLOW "\033[33m" 89 virtual void _write(
const char* msg, va_list arg)
133 fileName_ = fileName;
136 std::ofstream fileToClear(fileName_.c_str(), std::ios::out);
142 fopen_s(&fout_, fileName_.c_str(),
"a");
144 fout_ = fopen(fileName_.c_str(),
"a");
148 printf(
"FileLogger : Cannot open file : %s\n", fileName_.c_str());
154 virtual void _write(
const char* msg, va_list arg)
158 vfprintf(fout_, msg, arg);
165 fprintf(fout_,
"%s", msg);
256 std::string endline =
"";
261 std::string time =
"";
310 const char *
function,
337 const char* color = NULL;
358 std::string endline =
"";
363 std::string time =
"";
371 std::string levelStr =
"";
374 const int bufSize = 30;
375 char buf[bufSize] = {0};
378 sprintf_s(buf, bufSize,
"[%s]",
levelName_[level]);
380 snprintf(buf, bufSize,
"[%s]",
levelName_[level]);
383 levelStr.append(
" ");
386 std::string whereStr =
"";
393 whereStr.append(file);
401 fileName.append(
"~");
403 whereStr.append(fileName);
407 whereStr.append(
":");
409 whereStr.append(lineStr);
412 whereStr.append(
"::");
413 std::string funcStr =
function;
419 funcStr.append(
"()");
420 whereStr.append(funcStr);
422 whereStr.append(
" ");
431 HANDLE H = GetStdHandle(STD_OUTPUT_HANDLE);
436 SetConsoleTextAttribute(H,color);
491 printf(
"\n*******\n%s message occurred! Application will now exit.\n",
levelName_[level]);
494 printf(
" %s%s%s\n", levelStr.c_str(), time.c_str(), whereStr.c_str());
518 const int bufSize = 30;
519 char buf[bufSize] = {0};
524 localtime_s (&timeinfo, &rawtime );
525 int result = sprintf_s(buf, bufSize,
"%d-%s%d-%s%d %s%d:%s%d:%s%d",
526 timeinfo.tm_year+1900,
527 (timeinfo.tm_mon+1) < 10 ?
"0":
"", timeinfo.tm_mon+1,
528 (timeinfo.tm_mday) < 10 ?
"0":
"", timeinfo.tm_mday,
529 (timeinfo.tm_hour) < 10 ?
"0":
"", timeinfo.tm_hour,
530 (timeinfo.tm_min) < 10 ?
"0":
"", timeinfo.tm_min,
531 (timeinfo.tm_sec) < 10 ?
"0":
"", timeinfo.tm_sec);
535 timeinfo = *localtime (&rawtime);
536 int result = snprintf(buf, bufSize,
"%d-%s%d-%s%d %s%d:%s%d:%s%d",
537 timeinfo.tm_year+1900,
538 (timeinfo.tm_mon+1) < 10 ?
"0":
"", timeinfo.tm_mon+1,
539 (timeinfo.tm_mday) < 10 ?
"0":
"", timeinfo.tm_mday,
540 (timeinfo.tm_hour) < 10 ?
"0":
"", timeinfo.tm_hour,
541 (timeinfo.tm_min) < 10 ?
"0":
"", timeinfo.tm_min,
542 (timeinfo.tm_sec) < 10 ?
"0":
"", timeinfo.tm_sec);
544 struct timeval rawtime;
545 gettimeofday(&rawtime, NULL);
546 localtime_r (&rawtime.tv_sec, &timeinfo);
547 int result = snprintf(buf, bufSize,
"%d-%s%d-%s%d %s%d:%s%d:%s%d.%s%d",
548 timeinfo.tm_year+1900,
549 (timeinfo.tm_mon+1) < 10 ?
"0":
"", timeinfo.tm_mon+1,
550 (timeinfo.tm_mday) < 10 ?
"0":
"", timeinfo.tm_mday,
551 (timeinfo.tm_hour) < 10 ?
"0":
"", timeinfo.tm_hour,
552 (timeinfo.tm_min) < 10 ?
"0":
"", timeinfo.tm_min,
553 (timeinfo.tm_sec) < 10 ?
"0":
"", timeinfo.tm_sec,
554 (rawtime.tv_usec/1000) < 10 ?
"00":(rawtime.tv_usec/1000) < 100?
"0":
"",
int(rawtime.tv_usec/1000));
static const std::string kDefaultLogFileName
bool setDoomed(T *doomed)
virtual void _writeStr(const char *msg)
virtual void _writeStr(const char *msg)
static std::string bufferedMsgs_
static ULogger * createInstance()
static bool printEndline_
static bool limitWhereLength_
static ULogger * instance_
Some conversion functions.
static void setBuffered(bool buffered)
Wrappers of STL for convenient functions.
std::string uNumber2Str(unsigned int number)
static bool printWhereFullPath_
static ULogger * getInstance()
static const char * levelName_[5]
static void setType(Type type, const std::string &fileName=kDefaultLogFileName, bool append=true)
static int getTime(std::string &timeStr)
static void write(const char *msg,...)
static bool exitingState_
static std::string logFileName_
virtual void _writeStr(const char *msg)
static bool printColored_
static ULogger::Level level()
virtual ~UConsoleLogger()
std::string bufferedMsgs_
ULogger class and convenient macros.
std::string fileName_
the file name
static UDestroyer< ULogger > destroyer_
std::string uFormatv(const char *fmt, va_list args)
UFileLogger(const std::string &fileName, bool append)
static UMutex loggerMutex_
virtual void _write(const char *msg, va_list arg)
virtual void _write(const char *msg, va_list arg)
virtual void _write(const char *msg, va_list arg)