9 #if ((defined WIN32) && (defined __CINT__))
10 typedef unsigned int uint32_t;
11 typedef unsigned int DWORD;
12 #include <Windows4Root.h>
15 typedef unsigned int uint32_t;
21 #endif //WIN32 && CINT
24 #define __func__ __FUNCTION__
54 std::ostringstream& Get(TLogLevel level = logINFO, std::string file =
"", std::string
function =
"", uint32_t line = 0);
56 static TLogLevel& ReportingLevel();
57 static std::string ToString(TLogLevel level);
58 static TLogLevel FromString(
const std::string& level);
60 std::ostringstream os;
64 std::string NowTime();
74 std::string pxarLog<T>::NowTime(){
75 const int MAX_LEN = 200;
77 if (GetTimeFormatA(LOCALE_USER_DEFAULT, 0, 0,
78 "HH':'mm':'ss", buffer, MAX_LEN) == 0)
79 return "Error in NowTime()";
81 char result[100] = {0};
82 static DWORD first = GetTickCount();
83 std::sprintf(result,
"%s.%03ld", buffer, static_cast<long>(GetTickCount() - first) % 1000);
90 std::string pxarLog<T>::NowTime() {
94 tm r = * localtime(&t);
95 strftime(buffer,
sizeof(buffer),
"%X", localtime_r(&t, &r));
98 char result[100] = {0};
99 std::sprintf(result,
"%s.%03ld", buffer, static_cast<long>(tv.tv_usec) / 1000);
105 template <
typename T>
106 std::ostringstream& pxarLog<T>::Get(TLogLevel level, std::string file, std::string
function, uint32_t line) {
107 os <<
"[" << NowTime() <<
"] ";
108 os << std::setw(8) << ToString(level) <<
": ";
111 if (level != logINFO && level != logQUIET && level != logDEBUGRPC && level != logWARNING && level != logDEBUGPIPES)
112 os <<
"<" << file <<
"/" <<
function <<
":L" << line <<
"> ";
113 else if(level == logDEBUGRPC)
114 os <<
"\"" <<
function <<
"\" ";
119 template <
typename T>
120 pxarLog<T>::~pxarLog() {
125 template <
typename T>
126 TLogLevel& pxarLog<T>::ReportingLevel() {
127 static TLogLevel reportingLevel = logINFO;
128 return reportingLevel;
131 template <
typename T>
132 std::string pxarLog<T>::ToString(TLogLevel level) {
133 static const char*
const buffer[] = {
"QUIET",
"CRITICAL",
"ERROR",
"WARNING",
"INFO",
"DEBUG",
"DEBUGAPI",
"DEBUGHAL",
"DEBUGRPC",
"DEBUGPIPES",
"INTERFACE"};
134 return buffer[level];
137 template <
typename T>
138 TLogLevel pxarLog<T>::FromString(
const std::string& level) {
139 if (level ==
"INTERFACE")
141 if( level ==
"DEBUGPIPES")
142 return logDEBUGPIPES;
143 if (level ==
"DEBUGRPC")
145 if (level ==
"DEBUGHAL")
147 if (level ==
"DEBUGAPI")
149 if (level ==
"DEBUG")
153 if (level ==
"WARNING")
155 if (level ==
"ERROR")
157 if (level ==
"CRITICAL")
159 if (level ==
"QUIET")
161 pxarLog<T>().Get(logWARNING) <<
"Unknown logging level '" << level <<
"'. Using WARNING level as default.";
169 static FILE*& Stream();
170 static bool& Duplicate();
171 static void Output(
const std::string& msg);
174 inline bool& SetLogOutput::Duplicate()
176 static bool duplic =
false;
180 inline FILE*& SetLogOutput::Stream()
182 static FILE* pStream = stderr;
186 inline void SetLogOutput::Output(
const std::string& msg)
188 FILE* pStream = Stream();
192 if (Duplicate() && pStream != stderr)
193 fprintf(stderr,
"%s", msg.c_str());
194 fprintf(pStream,
"%s", msg.c_str());
198 typedef pxarLog<SetLogOutput> Log;
200 #define __FILE_NAME__ (strrchr(__FILE__, '/') ? strrchr(__FILE__, '/') + 1 : __FILE__)
202 #define IFLOG(level) \
203 if (level > pxar::Log::ReportingLevel())
206 if (level > pxar::Log::ReportingLevel() || !pxar::SetLogOutput::Stream()) ; \
207 else pxar::Log().Get(level,__FILE_NAME__,__func__,__LINE__)