GGEMS  1.1
GPU GEant4-based Monte Carlo Simulations
GGEMSPrint.hh
Go to the documentation of this file.
1 #ifndef GUARD_GGEMS_TOOLS_GGEMSPRINT_HH
2 #define GUARD_GGEMS_TOOLS_GGEMSPRINT_HH
3 
4 // ************************************************************************
5 // * This file is part of GGEMS. *
6 // * *
7 // * GGEMS is free software: you can redistribute it and/or modify *
8 // * it under the terms of the GNU General Public License as published by *
9 // * the Free Software Foundation, either version 3 of the License, or *
10 // * (at your option) any later version. *
11 // * *
12 // * GGEMS is distributed in the hope that it will be useful, *
13 // * but WITHOUT ANY WARRANTY; without even the implied warranty of *
14 // * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
15 // * GNU General Public License for more details. *
16 // * *
17 // * You should have received a copy of the GNU General Public License *
18 // * along with GGEMS. If not, see <https://www.gnu.org/licenses/>. *
19 // * *
20 // ************************************************************************
21 
34 #include <iostream>
35 #include <ostream>
36 #include <iomanip>
37 #include <mutex>
38 
42 namespace {
44 }
45 
46 #include "GGEMS/global/GGEMSExport.hh"
48 
49 #ifdef _WIN32
50 #ifdef _MSC_VER
51 #define NOMINMAX
52 #endif
53 #include <windows.h>
54 #endif
55 
60 #define GGendl (static_cast<std::ostream& (*)(std::ostream&)>(std::endl))
61 
66 #define GGcin (std::cin)
67 
68 #ifdef _MSC_VER
69 #pragma warning(disable: 4251) // Deleting warning exporting STL members!!!
70 #endif
71 
77 {
78  black = 0,
79  blue,
80  green,
81  aqua,
82  red,
83  purple,
84  yellow,
85  white,
86  gray
87 };
88 
92 namespace
93 {
94  #ifdef _WIN32
95  WORD constexpr kColor [] = {
96  0x00,
97  0x01,
98  0x02,
99  0x03,
100  0x04,
101  0x05,
102  0x06,
103  0x07,
104  0x08
105  };
106  #else
107  std::string const kColor[] = {
108  "\033[30m",
109  "\033[34m",
110  "\033[32m",
111  "\033[36m",
112  "\033[31m",
113  "\033[35m",
114  "\033[33m",
115  "\033[97m",
116  "\033[37m"
117  };
118  std::string const kDefaultColor("\033[0m");
119  #endif
120 }
121 
126 class GGEMS_EXPORT GGEMSStream
127 {
128  public:
134  GGEMSStream(std::ostream& stream, GGEMSConsoleColor const& color);
135 
140 
141  public:
150  GGEMSStream& operator()(std::string const& class_name, std::string const& method_name, GGint const& verbosity_level);
151 
159  template<typename T>
160  GGEMSStream& operator<<(T const& message);
161 
167  void SetVerbosity(GGint const& verbosity_limit);
168 
169  private:
170  std::string class_name_;
171  std::string method_name_;
176  protected: // Avoid warnings using clang on Windows system
177  std::ostream& stream_;
179 };
180 
184 
185 template <typename T>
187 {
188  #ifdef _WIN32
189  // Get current color of terminal
190  CONSOLE_SCREEN_BUFFER_INFO info;
191  GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &info);
192  HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
193  FlushConsoleInputBuffer(hConsole);
194  #endif
195 
196  if (stream_counter_ == 0) {
198  stream_ << std::scientific << "[";
199  if (!class_name_.empty() && !method_name_.empty()) {
200  #ifdef _WIN32
201  SetConsoleTextAttribute(hConsole, ::kColor[color_index_]);
202  stream_ << "GGEMS " << class_name_ << "::" << method_name_;
203  SetConsoleTextAttribute(hConsole, info.wAttributes);
204  #else
205  stream_ << kColor[color_index_] << "GGEMS " << class_name_ << "::" << method_name_ << kDefaultColor;
206  #endif
207  }
208  else {
209  #ifdef _WIN32
210  SetConsoleTextAttribute(hConsole, ::kColor[color_index_]);
211  stream_ << "GGEMS";
212  SetConsoleTextAttribute(hConsole, info.wAttributes);
213  #else
214  stream_ << kColor[color_index_] << "GGEMS" << kDefaultColor;
215  #endif
216  }
217  stream_ << "](" << verbosity_level_ << ") " << message;
218  stream_counter_++;
219  }
220  }
221  else {
222  if (verbosity_level_ <= verbosity_limit_) stream_ << message;
223  }
224  return *this;
225 }
226 
230 
231 extern GGEMS_EXPORT GGEMSStream GGcout;
232 extern GGEMS_EXPORT GGEMSStream GGcerr;
233 extern GGEMS_EXPORT GGEMSStream GGwarn;
240 extern "C" GGEMS_EXPORT void set_ggems_verbose(GGint verbosity);
241 
242 #endif // GUARD_GGEMS_TOOLS_GGPRINT_HH
GGwarn
GGEMSStream GGwarn
Definition: GGEMSPrint.cc:36
GGEMSStream::verbosity_limit_
GGint verbosity_limit_
Definition: GGEMSPrint.hh:172
GGEMSStream::verbosity_level_
GGint verbosity_level_
Definition: GGEMSPrint.hh:173
set_ggems_verbose
void set_ggems_verbose(GGint verbosity)
Set the verbosity of output stream.
Definition: GGEMSPrint.cc:81
GGint
#define GGint
Definition: GGEMSTypes.hh:224
GGEMSConsoleColor
GGEMSConsoleColor
define a color for the console terminal
Definition: GGEMSPrint.hh:77
GGEMSStream::class_name_
std::string class_name_
Definition: GGEMSPrint.hh:170
GGEMSTypes.hh
Redefining types for OpenCL device and host.
GGcout
GGEMSStream GGcout
Definition: GGEMSPrint.cc:34
GGEMSStream::~GGEMSStream
~GGEMSStream()
GGStream destructor.
Definition: GGEMSPrint.hh:139
GGcerr
GGEMSStream GGcerr
Definition: GGEMSPrint.cc:35
GGEMSStream
Generic class redefining standard output.
Definition: GGEMSPrint.hh:127
GGEMSStream::color_index_
GGEMSConsoleColor color_index_
Definition: GGEMSPrint.hh:178
GGuchar
#define GGuchar
Definition: GGEMSTypes.hh:203
GGEMSStream::method_name_
std::string method_name_
Definition: GGEMSPrint.hh:171
anonymous_namespace{GGEMSPrint.hh}::mutex
std::mutex mutex
Definition: GGEMSPrint.hh:43
GGEMSStream::operator<<
GGEMSStream & operator<<(T const &message)
overloading << to print a message in standard output
Definition: GGEMSPrint.hh:186
GGEMSStream::stream_counter_
GGint stream_counter_
Definition: GGEMSPrint.hh:174
T
__constant GGfloat T
Definition: GGEMSSystemOfUnits.hh:150
GGEMSStream::stream_
std::ostream & stream_
Definition: GGEMSPrint.hh:177
anonymous_namespace{GGEMSPrint.hh}::kColor
constexpr WORD kColor[]
Definition: GGEMSPrint.hh:95