FAST  3.2.0
Framework for Heterogeneous Medical Image Computing and Visualization
Reporter.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include <map>
4 #include <iostream>
5 #include <thread>
6 #include "FASTExport.hpp"
7 #ifdef WIN32
8 #include <windows.h>
9 #endif
10 
11 #undef ERROR // undefine some windows garbage
12 
13 namespace fast {
14 
15 // Use to signal end of report line
16 class FAST_EXPORT ReporterEnd {
17 
18 };
19 
20 class FAST_EXPORT Reporter {
21  public:
22  static ReporterEnd end();
23  static Reporter info();
24  static Reporter warning();
25  static Reporter error();
26  enum Type {INFO, WARNING, ERROR};
27  enum Method {NONE, COUT, LOG};
28  void setType(Type);
29  Reporter(Type type);
30  Reporter();
31  template <class T>
32  void process(const T& content);
33  void processEnd();
34  void setReportMethod(Method method);
35  void setReportMethod(Type type, Method method);
36  static void setGlobalReportMethod(Method method);
37  static void setGlobalReportMethod(Type type, Method method);
38  private:
39  Method getMethod(Type) const;
40  Type mType;
41  static std::map<Type, Method> mGlobalReporterMethods;
42  // The local report methods override the global, if they are defined
43  std::map<Type, Method> mLocalReporterMethods;
44 
45  // Variable to keep track of first <<
46  bool mFirst;
47 #ifdef WIN32
48  static WORD m_defaultAttributes;
49 #endif
50 };
51 
52 template <class T>
53 void Reporter::process(const T& content) {
54  Method reportMethod = getMethod(mType);
55 
56  if(mFirst) {
57  // Write prefix first
58  if(reportMethod == COUT) {
59  if(mType == INFO) {
60  std::cout << "INFO [" << std::this_thread::get_id() << "] ";
61  } else if(mType == WARNING) {
62 #ifdef WIN32
63  SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), (m_defaultAttributes & 0x00F0) | FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_INTENSITY);
64 #else
65  std::cout << "\033[1m";
66 #endif
67  std::cout << "WARNING [" << std::this_thread::get_id() << "] ";
68  } else if(mType == ERROR) {
69 #ifdef WIN32
70  SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), (m_defaultAttributes & 0x00F0) | FOREGROUND_RED | FOREGROUND_INTENSITY);
71 #else
72  std::cout << "\033[31;1m";
73 #endif
74  std::cerr << "ERROR [" << std::this_thread::get_id() << "] ";
75  }
76  }
77  mFirst = false;
78  }
79 
80  if(reportMethod == COUT) {
81  std::cout << content;
82  } else if(reportMethod == LOG) {
83  // Not implemented yet
84  }
85 }
86 
87 template <class T>
88 Reporter operator<<(Reporter report, const T& content) {
89  report.process(content);
90  return report;
91 }
92 
93 template <>
94 FAST_EXPORT Reporter operator<<(Reporter report, const ReporterEnd& end);
95 
96 } // end namespace fast
fast::Reporter::Type
Type
Definition: Reporter.hpp:26
fast
Definition: AffineTransformation.hpp:7
fast::Reporter::process
void process(const T &content)
Definition: Reporter.hpp:53
fast::Reporter::COUT
@ COUT
Definition: Reporter.hpp:27
fast::Reporter
Definition: Reporter.hpp:20
fast::operator<<
std::ostream & operator<<(std::ostream &os, DataBoundingBox &object)
fast::Reporter::WARNING
@ WARNING
Definition: Reporter.hpp:26
fast::Reporter::NONE
@ NONE
Definition: Reporter.hpp:27
fast::ReporterEnd
Definition: Reporter.hpp:16
fast::Reporter::LOG
@ LOG
Definition: Reporter.hpp:27
fast::Reporter::ERROR
@ ERROR
Definition: Reporter.hpp:26
fast::Reporter::Method
Method
Definition: Reporter.hpp:27
fast::Reporter::INFO
@ INFO
Definition: Reporter.hpp:26