MachineIntelligenceCore:Toolchain
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
ConsoleOutput.hpp
Go to the documentation of this file.
1 
23 #ifndef SRC_CONFIGURATION_CONSOLEOUTPUT_HPP_
24 #define SRC_CONFIGURATION_CONSOLEOUTPUT_HPP_
25 
26 #include <logger/LoggerOutput.hpp>
27 
28 #include <string>
29 #include <cstdio>
30 #include <sstream>
31 
32 #include <iostream>
33 
34 #if defined(WIN32)
35 # define WIN32_LEAN_AND_MEAN
36 # include <windows.h>
37 #endif
38 
39 namespace mic {
40 namespace logger {
41 
45 inline std::ostream& blue(std::ostream &s)
46 {
47 #if defined(WIN32)
48  HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
49  SetConsoleTextAttribute(hStdout, FOREGROUND_BLUE);
50 #else
51  s << "\033[34m";
52 #endif
53 
54  return s;
55 }
56 
60 inline std::ostream& green(std::ostream &s)
61 {
62  #if defined(WIN32)
63  HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
64  SetConsoleTextAttribute(hStdout, FOREGROUND_GREEN);
65 #else
66  s << "\033[32m";
67 #endif
68 
69  return s;
70 }
71 
75 inline std::ostream& red(std::ostream &s)
76 {
77 #if defined(WIN32)
78  HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
79  SetConsoleTextAttribute(hStdout, FOREGROUND_RED);
80 #else
81  s << "\033[31m";
82 #endif
83 
84  return s;
85 }
86 
90 inline std::ostream& white(std::ostream &s)
91 {
92 #if defined(WIN32)
93  HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
94  SetConsoleTextAttribute(hStdout, FOREGROUND_RED|FOREGROUND_GREEN|FOREGROUND_BLUE);
95 #else
96  s << "\033[37m";
97 #endif
98 
99  return s;
100 }
101 
105 inline std::ostream& yellow(std::ostream &s)
106 {
107 #if defined(WIN32)
108  HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
109  SetConsoleTextAttribute(hStdout, FOREGROUND_RED|FOREGROUND_GREEN);
110 #else
111  s << "\033[33m";
112 #endif
113 
114  return s;
115 }
116 
120 inline std::ostream& magenta(std::ostream &s)
121 {
122 #if defined(WIN32)
123  HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
124  SetConsoleTextAttribute(hStdout, FOREGROUND_RED|FOREGROUND_BLUE);
125 #else
126  s << "\033[35m";
127 #endif
128 
129  return s;
130 }
131 
135 inline std::ostream& cyan(std::ostream &s)
136 {
137 #if defined(WIN32)
138  HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
139  SetConsoleTextAttribute(hStdout, FOREGROUND_GREEN|FOREGROUND_BLUE);
140 #else
141  s << "\033[36m";
142 #endif
143 
144  return s;
145 }
146 
150 inline std::ostream& reset(std::ostream &s)
151 {
152  #if defined(WIN32)
153  HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
154  SetConsoleTextAttribute(hStdout, FOREGROUND_RED|FOREGROUND_GREEN|FOREGROUND_BLUE);
155 #else
156  s << "\033[00m";
157 #endif
158 
159  return s;
160 }
161 
165 inline std::ostream& intense(std::ostream &s)
166 {
167 #if defined(WIN32)
168  HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
169  CONSOLE_SCREEN_BUFFER_INFO csbiInfo;
170 
171  if (! GetConsoleScreenBufferInfo(hStdout, &csbiInfo))
172  return s;
173 
174  SetConsoleTextAttribute(hStdout, csbiInfo.wAttributes | FOREGROUND_INTENSITY);
175 #else
176  s << "\033[1m";
177 #endif
178 
179  return s;
180 }
181 
185 class ConsoleOutput : public LoggerOutput {
186 public:
192  LoggerOutput(sev)
193  {
194 
195  }
196 
200  virtual ~ConsoleOutput(){}
201 
202 
210  void print(const std::string & msg, Severity_t sev, const std::string & file, int line) const {
211  switch (sev) {
212  case Trace:
213  std::cout << cyan << sev2str(sev) << reset << " in " << file << " [" << cyan << line << reset << "]";
214  break;
215  case Debug:
216  std::cout << cyan << intense << sev2str(sev) << reset << " in " << file << " [" << cyan << intense << line << reset << "]";
217  break;
218  case Notice:
219  std::cout << sev2str(sev) << reset;
220  break;
221  case Info:
222  std::cout << green << sev2str(sev) << reset;
223  break;
224  case Status:
225  std::cout << green << intense << sev2str(sev) << reset;
226  break;
227  case Warning:
228  std::cout << yellow << sev2str(sev) << reset;
229  break;
230  case Error:
231  std::cout << red << sev2str(sev) << reset;
232  break;
233  case Fatal:
234  std::cout << red << intense << sev2str(sev) << reset;
235  break;
236  }
237 
238  std::cout << ": " << msg << std::endl;
239  }
240 };
241 
242 
243 } /* namespace logger */
244 } /* namespace mic */
245 
246 
247 #endif /* SRC_CONFIGURATION_CONSOLEOUTPUT_HPP_ */
Severity_t
Message severity level.
Definition: LoggerAux.hpp:35
Information, contain user defined information.
Definition: LoggerAux.hpp:40
void print(const std::string &msg, Severity_t sev, const std::string &file, int line) const
#define LINFO
Definition: LoggerAux.hpp:55
Something very bad happened, no chance to continue execution.
Definition: LoggerAux.hpp:44
std::ostream & magenta(std::ostream &s)
ConsoleOutput(Severity_t sev=LINFO)
std::ostream & cyan(std::ostream &s)
Something bad happened, try to terminate.
Definition: LoggerAux.hpp:43
std::ostream & red(std::ostream &s)
Contains definition of an abstract logger output class, from which all output classes should be derri...
Information, contain user defined information, with small importance.
Definition: LoggerAux.hpp:39
std::ostream & yellow(std::ostream &s)
Debug message with file and line number, contain user defined debug information.
Definition: LoggerAux.hpp:38
Abstract interface for different logger outputs.
std::ostream & green(std::ostream &s)
Warning, continue execution.
Definition: LoggerAux.hpp:42
Status, contains statuses such as status of application, phase of operation etc.
Definition: LoggerAux.hpp:41
std::string sev2str(Severity_t sev_)
Definition: LoggerAux.cpp:30
std::ostream & reset(std::ostream &s)
std::ostream & intense(std::ostream &s)
std::ostream & white(std::ostream &s)
Class responsible for printing logs on console.
Trace program execution, prints file name and line, typically logged when entering function...
Definition: LoggerAux.hpp:37
std::ostream & blue(std::ostream &s)