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" 90 virtual void _write(
const char* msg, va_list arg)
134 fileName_ = fileName;
137 std::ofstream fileToClear(fileName_.c_str(), std::ios::out);
143 fopen_s(&fout_, fileName_.c_str(),
"a");
145 fout_ = fopen(fileName_.c_str(),
"a");
149 printf(
"FileLogger : Cannot open file : %s\n", fileName_.c_str());
155 virtual void _write(
const char* msg, va_list arg)
159 vfprintf(fout_, msg, arg);
166 fprintf(fout_,
"%s", msg);
208 for(
unsigned int i=0;i<ids.size();++i)
233 if(iter->second ==
id)
309 std::string endline =
"";
314 std::string time =
"";
363 const char *
function,
392 const char* color =
NULL;
413 std::string endline =
"";
418 std::string time =
"";
426 std::string levelStr =
"";
429 const int bufSize = 30;
430 char buf[bufSize] = {0};
433 sprintf_s(buf, bufSize,
"[%s]",
levelName_[level]);
435 snprintf(buf, bufSize,
"[%s]",
levelName_[level]);
438 levelStr.append(
" ");
447 std::string whereStr =
"";
454 whereStr.append(file);
462 fileName.append(
"~");
464 whereStr.append(fileName);
468 whereStr.append(
":");
470 whereStr.append(lineStr);
473 whereStr.append(
"::");
474 std::string funcStr =
function;
480 funcStr.append(
"()");
481 whereStr.append(funcStr);
483 whereStr.append(
" ");
492 HANDLE H = GetStdHandle(STD_OUTPUT_HANDLE);
497 SetConsoleTextAttribute(H,color);
554 std::string fullMsg =
uFormat(
"%s%s%s%s", levelStr.c_str(), pidStr.c_str(), time.c_str(), whereStr.c_str());
556 fullMsg.append(
uFormatv(msg, args));
572 std::string fullMsg =
uFormat(
"%s%s%s%s", levelStr.c_str(), pidStr.c_str(), time.c_str(), whereStr.c_str());
574 fullMsg.append(
uFormatv(msg, args));
596 const int bufSize = 30;
597 char buf[bufSize] = {0};
602 localtime_s (&timeinfo, &rawtime );
603 int result = sprintf_s(buf, bufSize,
"%d-%s%d-%s%d %s%d:%s%d:%s%d",
604 timeinfo.tm_year+1900,
605 (timeinfo.tm_mon+1) < 10 ?
"0":
"", timeinfo.tm_mon+1,
606 (timeinfo.tm_mday) < 10 ?
"0":
"", timeinfo.tm_mday,
607 (timeinfo.tm_hour) < 10 ?
"0":
"", timeinfo.tm_hour,
608 (timeinfo.tm_min) < 10 ?
"0":
"", timeinfo.tm_min,
609 (timeinfo.tm_sec) < 10 ?
"0":
"", timeinfo.tm_sec);
613 timeinfo = *localtime (&rawtime);
614 int result = snprintf(buf, bufSize,
"%d-%s%d-%s%d %s%d:%s%d:%s%d",
615 timeinfo.tm_year+1900,
616 (timeinfo.tm_mon+1) < 10 ?
"0":
"", timeinfo.tm_mon+1,
617 (timeinfo.tm_mday) < 10 ?
"0":
"", timeinfo.tm_mday,
618 (timeinfo.tm_hour) < 10 ?
"0":
"", timeinfo.tm_hour,
619 (timeinfo.tm_min) < 10 ?
"0":
"", timeinfo.tm_min,
620 (timeinfo.tm_sec) < 10 ?
"0":
"", timeinfo.tm_sec);
622 struct timeval rawtime;
623 gettimeofday(&rawtime,
NULL);
624 localtime_r (&rawtime.tv_sec, &timeinfo);
625 int result = snprintf(buf, bufSize,
"%d-%s%d-%s%d %s%d:%s%d:%s%d.%s%d",
626 timeinfo.tm_year+1900,
627 (timeinfo.tm_mon+1) < 10 ?
"0":
"", timeinfo.tm_mon+1,
628 (timeinfo.tm_mday) < 10 ?
"0":
"", timeinfo.tm_mday,
629 (timeinfo.tm_hour) < 10 ?
"0":
"", timeinfo.tm_hour,
630 (timeinfo.tm_min) < 10 ?
"0":
"", timeinfo.tm_min,
631 (timeinfo.tm_sec) < 10 ?
"0":
"", timeinfo.tm_sec,
632 (rawtime.tv_usec/1000) < 10 ?
"00":(rawtime.tv_usec/1000) < 100?
"0":
"",
int(rawtime.tv_usec/1000));
static const std::string kDefaultLogFileName
static void post(UEvent *event, bool async=true, const UEventsSender *sender=0)
static std::map< std::string, unsigned long > registeredThreads_
bool setDoomed(T *doomed)
virtual void _writeStr(const char *msg)
static std::map< std::string, unsigned long > getRegisteredThreads()
static std::string bufferedMsgs_
static void unregisterCurrentThread()
static bool printThreadID_
static ULogger * createInstance()
static bool printEndline_
static bool limitWhereLength_
static ULogger * instance_
Some conversion functions.
static void setTreadIdFilter(const std::set< unsigned long > &ids)
static void setBuffered(bool buffered)
#define UASSERT(condition)
Wrappers of STL for convenient functions.
virtual void _writeStr(const char *)
static bool printWhereFullPath_
static ULogger * getInstance()
static const char * levelName_[5]
static void registerCurrentThread(const std::string &name)
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,...)
std::string UTILITE_EXP uFormatv(const char *fmt, va_list ap)
static std::string logFileName_
virtual void _writeStr(const char *msg)
static bool printColored_
static ULogger::Level level()
virtual ~UConsoleLogger()
std::string bufferedMsgs_
virtual void _write(const char *, va_list)
ULogger class and convenient macros.
std::string fileName_
the file name
static UDestroyer< ULogger > destroyer_
static std::set< unsigned long > threadIdFilter_
std::string UTILITE_EXP uFormat(const char *fmt,...)
std::string UTILITE_EXP uNumber2Str(unsigned int number)
UFileLogger(const std::string &fileName, bool append)
static UMutex loggerMutex_
void uInsert(std::map< K, V > &map, const std::pair< K, V > &pair)
static unsigned long currentThreadId()
virtual void _write(const char *msg, va_list arg)
virtual void _write(const char *msg, va_list arg)