21 #define USE_WIN32COLOREDCONSOLE 32 #ifdef USE_WIN32COLOREDCONSOLE 34 static HANDLE g_hConsole;
39 std::vector<std::string> path;
41 std::string current, category;
47 if (current.size() > 0)
49 if (current.compare(
"..") == 0)
56 path.push_back(current);
62 std::string simplify(std::string x)
66 std::string lft(
""), rgt(
"");
75 int xs =
static_cast<int>(x.size());
89 rp[0] = toupper(rp[0]);
90 x = x.substr(0, xs - s);
98 rp[0] = toupper(rp[0]);
99 x = x.substr(s, xs - s);
107 x[0] = toupper(x[0]);
111 x[0] = tolower(x[0]);
116 std::string concat(std::string a, std::string b,
bool opt)
118 int as =
static_cast<int>(a.size());
119 int bs =
static_cast<int>(b.size());
122 if (as == bs && a.compare(b) == 0)
125 if (strncmp(a.c_str(), b.c_str(), as) == 0)
127 b = b.substr(as, bs - as);
128 b[0] = tolower(b[0]);
135 bool search2p2(
char *a,
char *b,
bool optimize =
true)
137 int size =
static_cast<int>(path.size()) - 3;
138 for (
int r = 0; r < size; r++)
139 if (path[r].
compare(a) == 0 && path[r + 1].compare(b) == 0)
142 category = concat(simplify(path[pos]), simplify(path[path.size() - 1]), optimize);
148 bool search2p1(
char *a,
char *b)
150 int size =
static_cast<int>(path.size()) - 2;
151 for (
int r = 0; r < size; r++)
152 if (path[r].
compare(a) == 0 && path[r + 1].compare(b) == 0)
155 category = simplify(path[path.size() - 1]);
161 bool search1p2(
char *a,
bool optimize =
true)
163 int size =
static_cast<int>(path.size()) - 2;
164 for (
int r = 0; r < size; r++)
168 category = concat(simplify(path[pos]), simplify(path[path.size() - 1]), optimize);
177 int size =
static_cast<int>(strlen(l.
filePath));
181 for (
int r = 0; r < size; r++)
184 if (c ==
'\\' || c ==
'/')
187 current += tolower(c);
193 std::string GetPrefix()
196 if (search2p2(
"mbi-sb",
"core",
false))
198 if (search2p1(
"mbi-sb",
"q4mitk"))
200 if (search2p2(
"mbi",
"applications"))
202 if (search2p2(
"mbi-sb",
"q4applications"))
204 if (search2p2(
"mbi-sb",
"utilities"))
206 if (search2p2(
"mbi-sb",
"bundles"))
208 if (search2p2(
"mbi-sb",
"bundlesqt"))
210 if (search2p2(
"mbi",
"modules"))
213 if (search2p2(
"mbi-qm",
"core",
false))
215 if (search2p2(
"mbi-qm",
"utilities"))
218 if (search2p2(
"modules",
"mitkext",
false))
220 if (search2p1(
"modules",
"qmitkext"))
222 if (search2p2(
"modules",
"bundles"))
225 if (search2p2(
"blueberry",
"bundles"))
228 if (search2p2(
"core",
"code",
false))
230 if (search2p1(
"coreui",
"qmitk"))
232 if (search2p2(
"coreui",
"bundles"))
236 if (search1p2(
"modules"))
238 if (search1p2(
"utilities"))
240 if (search1p2(
"applications"))
246 std::string GetCategory() {
return category; }
292 std::locale originalLocale = out.getloc();
295 out << std::fixed << std::setprecision(3) << ((double)std::clock()) / CLOCKS_PER_SEC;
297 out.imbue(originalLocale);
299 out << c_close <<
" ";
368 out <<
"|" << std::hex << threadID;
386 #ifdef USE_WIN32COLOREDCONSOLE 400 time_t rawtime = time(
nullptr);
401 std::string timestring(ctime(&rawtime));
402 timestring.replace(timestring.length() - 1,
407 std::locale originalLocale = out.getloc();
412 out.imbue(originalLocale);
415 #ifdef USE_WIN32COLOREDCONSOLE 418 void GetDesktopResolution(
int& horizontal,
int& vertical)
422 const HWND hDesktop = GetDesktopWindow();
424 GetWindowRect(hDesktop, &desktop);
428 horizontal = desktop.right;
429 vertical = desktop.bottom;
432 BOOL CALLBACK MonitorEnumProc(HMONITOR hMonitor, HDC hdcMonitor, LPRECT lprcMonitor, LPARAM dwData)
434 int *Count = (
int*)dwData;
439 int GetMonitorCount()
442 if (EnumDisplayMonitors(NULL, NULL, MonitorEnumProc, (LPARAM)&Count))
449 int colorNormal = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE;
451 int lastColor = colorNormal;
452 #define ChangeColor(_col) \ 455 if (lastColor != (col)) \ 457 SetConsoleTextAttribute(g_hConsole, (col)); \ 462 int colorTime = FOREGROUND_GREEN;
463 int colorText = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE;
464 int colorCat = FOREGROUND_BLUE | FOREGROUND_RED;
465 bool showColon =
true;
466 bool forceCat =
false;
470 g_hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
473 std::string title =
"mbilog";
475 SetConsoleTitle(title.c_str());
489 ChangeColor(colorTime);
491 std::cout << std::endl;
500 colorTime = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_INTENSITY;
502 colorCat = FOREGROUND_BLUE | FOREGROUND_RED | FOREGROUND_INTENSITY;
508 colorTime = FOREGROUND_RED | FOREGROUND_INTENSITY;
510 colorCat = FOREGROUND_BLUE | FOREGROUND_RED | FOREGROUND_INTENSITY;
516 colorTime = FOREGROUND_RED | FOREGROUND_BLUE | FOREGROUND_INTENSITY;
518 colorCat = FOREGROUND_BLUE | FOREGROUND_RED | FOREGROUND_INTENSITY;
524 colorTime = FOREGROUND_BLUE | FOREGROUND_INTENSITY;
530 ChangeColor(colorTime);
533 std::locale originalLocale = std::cout.getloc();
536 std::cout << std::fixed << std::setprecision(2) << ((double)std::clock()) / CLOCKS_PER_SEC <<
" ";
538 std::cout.imbue(originalLocale);
542 AutoCategorize ac(l);
543 std::string pre = ac.GetPrefix();
544 std::string cat = ac.GetCategory();
553 ChangeColor(colorCat);
557 std::cout << cat << std::flush;
565 ChangeColor(FOREGROUND_BLUE | FOREGROUND_INTENSITY);
566 std::cout <<
": " << std::flush;
579 ChangeColor(colorTime);
580 std::cout <<
"WARNING" << std::flush;
581 ChangeColor(FOREGROUND_BLUE | FOREGROUND_INTENSITY);
582 std::cout <<
": " << std::flush;
586 ChangeColor(colorTime);
587 std::cout <<
"ERROR" << std::flush;
588 ChangeColor(FOREGROUND_BLUE | FOREGROUND_INTENSITY);
589 std::cout <<
": " << std::flush;
593 ChangeColor(colorTime);
594 std::cout <<
"FATAL" << std::flush;
595 ChangeColor(FOREGROUND_BLUE | FOREGROUND_INTENSITY);
596 std::cout <<
": " << std::flush;
600 ChangeColor(colorTime);
601 std::cout <<
"DBG" << std::flush;
602 ChangeColor(FOREGROUND_BLUE | FOREGROUND_INTENSITY);
603 std::cout <<
": " << std::flush;
607 ChangeColor(colorText);
608 std::cout << l.
message << std::endl;
610 ChangeColor(colorNormal);
612 int monitorCount = GetMonitorCount();
613 if (monitorCount > 1) {
614 HWND consoleWindow = GetConsoleWindow();
615 int horizontal = 0, vertical = 0;
616 const int verticalSizeOfConsoleWindow = 300;
617 GetDesktopResolution(horizontal, vertical);
618 SetWindowPos(consoleWindow, 0, horizontal, vertical/2 - verticalSizeOfConsoleWindow, 0, 0, SWP_NOSIZE | SWP_NOZORDER);
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.
~TextBackendBase() override
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.
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.