25 #define USE_WIN32COLOREDCONSOLE
36 #ifdef USE_WIN32COLOREDCONSOLE
38 static HANDLE g_hConsole;
43 std::vector<std::string> path;
45 std::string current, category;
51 if (current.size() > 0)
53 if (current.compare(
"..") == 0)
60 path.push_back(current);
66 std::string simplify(std::string x)
70 std::string lft(
""), rgt(
"");
79 int xs =
static_cast<int>(x.size());
93 rp[0] = toupper(rp[0]);
94 x = x.substr(0, xs - s);
102 rp[0] = toupper(rp[0]);
103 x = x.substr(s, xs - s);
111 x[0] = toupper(x[0]);
115 x[0] = tolower(x[0]);
120 std::string concat(std::string a, std::string b,
bool opt)
122 int as =
static_cast<int>(a.size());
123 int bs =
static_cast<int>(b.size());
126 if (as == bs && a.compare(b) == 0)
129 if (strncmp(a.c_str(), b.c_str(), as) == 0)
131 b = b.substr(as, bs - as);
132 b[0] = tolower(b[0]);
139 bool search2p2(
char *a,
char *b,
bool optimize =
true)
141 int size =
static_cast<int>(path.size()) - 3;
142 for (
int r = 0; r < size; r++)
143 if (path[r].
compare(a) == 0 && path[r + 1].compare(b) == 0)
146 category = concat(simplify(path[pos]), simplify(path[path.size() - 1]), optimize);
152 bool search2p1(
char *a,
char *b)
154 int size =
static_cast<int>(path.size()) - 2;
155 for (
int r = 0; r < size; r++)
156 if (path[r].
compare(a) == 0 && path[r + 1].compare(b) == 0)
159 category = simplify(path[path.size() - 1]);
165 bool search1p2(
char *a,
bool optimize =
true)
167 int size =
static_cast<int>(path.size()) - 2;
168 for (
int r = 0; r < size; r++)
172 category = concat(simplify(path[pos]), simplify(path[path.size() - 1]), optimize);
181 int size =
static_cast<int>(strlen(l.
filePath));
185 for (
int r = 0; r < size; r++)
188 if (c ==
'\\' || c ==
'/')
191 current += tolower(c);
197 std::string GetPrefix()
200 if (search2p2(
"mbi-sb",
"core",
false))
202 if (search2p1(
"mbi-sb",
"q4mitk"))
204 if (search2p2(
"mbi",
"applications"))
206 if (search2p2(
"mbi-sb",
"q4applications"))
208 if (search2p2(
"mbi-sb",
"utilities"))
210 if (search2p2(
"mbi-sb",
"bundles"))
212 if (search2p2(
"mbi-sb",
"bundlesqt"))
214 if (search2p2(
"mbi",
"modules"))
217 if (search2p2(
"mbi-qm",
"core",
false))
219 if (search2p2(
"mbi-qm",
"utilities"))
222 if (search2p2(
"modules",
"mitkext",
false))
224 if (search2p1(
"modules",
"qmitkext"))
226 if (search2p2(
"modules",
"bundles"))
229 if (search2p2(
"blueberry",
"bundles"))
232 if (search2p2(
"core",
"code",
false))
234 if (search2p1(
"coreui",
"qmitk"))
236 if (search2p2(
"coreui",
"bundles"))
240 if (search1p2(
"modules"))
242 if (search1p2(
"utilities"))
244 if (search1p2(
"applications"))
250 std::string GetCategory() {
return category; }
291 AppendTimeStamp(out);
296 std::locale originalLocale = out.getloc();
299 out << std::fixed << std::setprecision(3) << ((double)std::clock()) / CLOCKS_PER_SEC;
301 out.imbue(originalLocale);
303 out << c_close <<
" ";
362 AppendTimeStamp(out);
372 out <<
"|" << std::hex << threadID;
390 #ifdef USE_WIN32COLOREDCONSOLE
391 FormatSmartWindows(l, threadID);
393 FormatSmart(std::cout, l, threadID);
399 FormatFull(std::cout, l, threadID);
404 time_t rawtime = time(
nullptr);
405 std::string timestring(ctime(&rawtime));
406 timestring.replace(timestring.length() - 1,
411 std::locale originalLocale = out.getloc();
416 out.imbue(originalLocale);
419 #ifdef USE_WIN32COLOREDCONSOLE
423 int colorNormal = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE;
425 int lastColor = colorNormal;
426 #define ChangeColor(_col) \
429 if (lastColor != (col)) \
431 SetConsoleTextAttribute(g_hConsole, (col)); \
436 int colorTime = FOREGROUND_GREEN;
437 int colorText = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE;
438 int colorCat = FOREGROUND_BLUE | FOREGROUND_RED;
439 bool showColon =
true;
440 bool forceCat =
false;
444 g_hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
447 std::string title =
"mbilog";
449 SetConsoleTitle(title.c_str());
463 ChangeColor(colorTime);
464 AppendTimeStamp(std::cout);
465 std::cout << std::endl;
474 colorTime = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_INTENSITY;
476 colorCat = FOREGROUND_BLUE | FOREGROUND_RED | FOREGROUND_INTENSITY;
482 colorTime = FOREGROUND_RED | FOREGROUND_INTENSITY;
484 colorCat = FOREGROUND_BLUE | FOREGROUND_RED | FOREGROUND_INTENSITY;
490 colorTime = FOREGROUND_RED | FOREGROUND_BLUE | FOREGROUND_INTENSITY;
492 colorCat = FOREGROUND_BLUE | FOREGROUND_RED | FOREGROUND_INTENSITY;
498 colorTime = FOREGROUND_BLUE | FOREGROUND_INTENSITY;
504 ChangeColor(colorTime);
507 std::locale originalLocale = std::cout.getloc();
510 std::cout << std::fixed << std::setprecision(2) << ((double)std::clock()) / CLOCKS_PER_SEC <<
" ";
512 std::cout.imbue(originalLocale);
516 AutoCategorize ac(l);
517 std::string pre = ac.GetPrefix();
518 std::string cat = ac.GetCategory();
527 ChangeColor(colorCat);
531 std::cout << cat << std::flush;
539 ChangeColor(FOREGROUND_BLUE | FOREGROUND_INTENSITY);
540 std::cout <<
": " << std::flush;
553 ChangeColor(colorTime);
554 std::cout <<
"WARNING" << std::flush;
555 ChangeColor(FOREGROUND_BLUE | FOREGROUND_INTENSITY);
556 std::cout <<
": " << std::flush;
560 ChangeColor(colorTime);
561 std::cout <<
"ERROR" << std::flush;
562 ChangeColor(FOREGROUND_BLUE | FOREGROUND_INTENSITY);
563 std::cout <<
": " << std::flush;
567 ChangeColor(colorTime);
568 std::cout <<
"FATAL" << std::flush;
569 ChangeColor(FOREGROUND_BLUE | FOREGROUND_INTENSITY);
570 std::cout <<
": " << std::flush;
574 ChangeColor(colorTime);
575 std::cout <<
"DBG" << std::flush;
576 ChangeColor(FOREGROUND_BLUE | FOREGROUND_INTENSITY);
577 std::cout <<
": " << std::flush;
581 ChangeColor(colorText);
582 std::cout << l.
message << std::endl;
584 ChangeColor(colorNormal);
const char * moduleName
Name of the module where the logging message was emitted which is generated by the macros in file mbi...
const char * filePath
File name of the source file where the logging message was emitted which is generated by the macros i...
std::string message
The actual logging message.
An object of this class represents a single logging message (logging event) of the mbi logging mechan...
const int lineNumber
Line of the source source file where the logging message was emitted which is generated by the macros...
void FormatSmartWindows(const mbilog::LogMessage &l, int)
Special variant of method FormatSmart which uses colored messages (only for windows).
const char * functionName
Name of the method where the logging message was emitted which is generated by the macros in file mbi...
bool compare(std::pair< double, int > i, std::pair< double, int > j)
std::string category
Category of the logging event, which was defined by the user.
const int level
Logging level which is defined in the enum mbilogLoggingTypes.h TODO: convert to enum.
virtual ~TextBackendBase()
static const char * replace[]
This is a dictionary to replace long names of classes, modules, etc. to shorter versions in the conso...
void AppendTimeStamp(std::ostream &out)
Writes the system time to the given stream.
void FormatFull(const LogMessage &l, int threadID=0)
Method formats the given LogMessage in the full/long format and writes it to std::cout.
void FormatSmart(const LogMessage &l, int threadID=0)
Method formats the given LogMessage in the smart/short format and writes it to std::cout.