Interface Documentation
Version: invalid
message.hh
Go to the documentation of this file.
1 /*
2  @@@@@@@@ @@ @@@@@@ @@@@@@@@ @@
3  /@@///// /@@ @@////@@ @@////// /@@
4  /@@ /@@ @@@@@ @@ // /@@ /@@
5  /@@@@@@@ /@@ @@///@@/@@ /@@@@@@@@@/@@
6  /@@//// /@@/@@@@@@@/@@ ////////@@/@@
7  /@@ /@@/@@//// //@@ @@ /@@/@@
8  /@@ @@@//@@@@@@ //@@@@@@ @@@@@@@@ /@@
9  // /// ////// ////// //////// //
10 
11  Copyright (c) 2016, Triad National Security, LLC
12  All rights reserved.
13  */
14 #pragma once
15 
18 #include <flecsi-config.h>
19 
20 #include "flecsi/log/state.hh"
21 #include "flecsi/log/types.hh"
22 #include "flecsi/log/utils.hh"
23 
24 #include <iostream>
25 
26 namespace flecsi {
27 namespace log {
28 
33 inline bool
35  return true;
36 } // true_state
37 
43 struct log_message_t {
44 
56  log_message_t(const char * file, int line)
57  : file_(file), line_(line), clean_color_(false) {
58 #if defined(FLOG_ENABLE_DEBUG)
59  std::cerr << FLOG_COLOR_LTGRAY << "FLOG: log_message_t constructor " << file
60  << " " << line << FLOG_COLOR_PLAIN << std::endl;
61 #endif
62  } // log_message_t
63 
64  virtual ~log_message_t() {
65 #if defined(FLOG_ENABLE_DEBUG)
66  std::cerr << FLOG_COLOR_LTGRAY << "FLOG: log_message_t destructor "
67  << FLOG_COLOR_PLAIN << std::endl;
68 #endif
69 
70 #if defined(FLOG_ENABLE_MPI)
71  if(flog_t::instance().initialized()) {
72  flog_t::instance().buffer_output();
73  }
74  else {
75  if(!flog_t::instance().initialized()) {
76 #if defined(FLOG_ENABLE_EXTERNAL)
77  std::cout << flog_t::instance().buffer_stream().str();
78 #endif
79  }
80  else {
82  } // if
83  } // if
84 #else
86 #endif // FLOG_ENABLE_MPI
87 
88  flog_t::instance().buffer_stream().str(std::string{});
89  } // ~log_message_t
90 
96  virtual std::ostream & stream() {
98  } // stream
99 
100 protected:
101  const char * file_;
102  int line_;
103  bool clean_color_;
104 
105 }; // struct log_message_t
106 
107 /*----------------------------------------------------------------------------*
108  Convenience macro to define severity levels.
109  *----------------------------------------------------------------------------*/
110 
111 #define severity_message_t(severity, format) \
112  struct severity##_log_message_t : public log_message_t { \
113  severity##_log_message_t(const char * file, int line, bool devel = false) \
114  : log_message_t(file, line), devel_(devel) {} \
115  \
116  ~severity##_log_message_t() { \
117  /* Clean colors from the stream */ \
118  if(clean_color_ && !flog_t::instance().buffer_stream().str().empty()) { \
119  auto str = flog_t::instance().buffer_stream().str(); \
120  if(str.back() == '\n') { \
121  str = str.substr(0, str.size() - 1); \
122  str += FLOG_COLOR_PLAIN; \
123  str += '\n'; \
124  flog_t::instance().buffer_stream().str(std::string{}); \
125  flog_t::instance().buffer_stream() << str; \
126  } \
127  else { \
128  flog_t::instance().buffer_stream() << FLOG_COLOR_PLAIN; \
129  } \
130  } \
131  } \
132  \
133  std::ostream & \
134  stream() override /* This is replaced by the scoped logic */ \
135  format \
136  \
137  private : bool devel_; \
138  }
139 
140 #define verbose_stamp \
141  timestamp() << " " << rstrip<'/'>(file_) << ":" << line_ << " "
142 
143 #if defined(FLOG_ENABLE_MPI)
144 #define process_stamp " p" << flog_t::instance().process()
145 #else
146 #define process_stamp ""
147 #endif
148 
149 //----------------------------------------------------------------------------//
150 // utility
151 //
152 // This is currently only used by ftest.
153 //----------------------------------------------------------------------------//
154 
155 severity_message_t(utility, {
156  (void)devel_;
157  std::ostream & stream =
159  return stream;
160 });
161 
162 //----------------------------------------------------------------------------//
163 // trace
164 //----------------------------------------------------------------------------//
165 
166 severity_message_t(trace, {
167  std::ostream & stream = flog_t::instance().severity_stream(
168  FLOG_STRIP_LEVEL < 1 && flog_t::instance().tag_enabled());
169 
170  std::string devel = devel_ ? "(devel)" : "";
171  std::string active_tag = flog_t::instance().initialized()
173  : "external";
174 
175  switch(flog_t::instance().verbose()) {
176  case 1: {
177  stream << FLOG_OUTPUT_CYAN("[trace ") << FLOG_OUTPUT_PURPLE(devel);
178  stream << FLOG_OUTPUT_LTGRAY(verbose_stamp);
179  stream << FLOG_OUTPUT_CYAN(active_tag);
180  stream << FLOG_OUTPUT_GREEN(process_stamp);
181  stream << FLOG_OUTPUT_CYAN("] ");
182  } // scope
183  break;
184 
185  case 0: {
186  stream << FLOG_OUTPUT_CYAN("[trace ") << FLOG_OUTPUT_PURPLE(devel);
187  stream << FLOG_OUTPUT_CYAN(active_tag);
188  stream << FLOG_OUTPUT_GREEN(process_stamp);
189  stream << FLOG_OUTPUT_CYAN("] ");
190  } // scope
191  break;
192 
193  default:
194  break;
195  } // switch
196 
197  return stream;
198 });
199 
200 //----------------------------------------------------------------------------//
201 // info
202 //----------------------------------------------------------------------------//
203 
204 severity_message_t(info, {
205  std::ostream & stream = flog_t::instance().severity_stream(
206  FLOG_STRIP_LEVEL < 2 && flog_t::instance().tag_enabled());
207 
208  std::string devel = devel_ ? "devel " : "";
209  std::string active_tag = flog_t::instance().initialized()
211  : "external";
212 
213  switch(flog_t::instance().verbose()) {
214  case 1: {
215  stream << FLOG_OUTPUT_GREEN("[info ") << FLOG_OUTPUT_PURPLE(devel);
216  stream << FLOG_OUTPUT_LTGRAY(verbose_stamp);
217  stream << FLOG_OUTPUT_CYAN(active_tag);
218  stream << FLOG_OUTPUT_GREEN(process_stamp);
219  stream << FLOG_OUTPUT_GREEN("] ");
220  } // scope
221  break;
222 
223  case 0: {
224  stream << FLOG_OUTPUT_GREEN("[info ") << FLOG_OUTPUT_PURPLE(devel);
225  stream << FLOG_OUTPUT_CYAN(active_tag);
226  stream << FLOG_OUTPUT_GREEN(process_stamp);
227  stream << FLOG_OUTPUT_GREEN("] ");
228  } // scope
229  break;
230 
231  default:
232  break;
233  } // switch
234 
235  return stream;
236 });
237 
238 //----------------------------------------------------------------------------//
239 // warn
240 //----------------------------------------------------------------------------//
241 
242 severity_message_t(warn, {
243  std::ostream & stream = flog_t::instance().severity_stream(
244  FLOG_STRIP_LEVEL < 3 && flog_t::instance().tag_enabled());
245 
246  std::string devel = devel_ ? "(devel)" : "";
247  std::string active_tag = flog_t::instance().initialized()
249  : "external";
250 
251  switch(flog_t::instance().verbose()) {
252  case 1: {
253  stream << FLOG_OUTPUT_BROWN("[Warn ") << FLOG_OUTPUT_PURPLE(devel);
254  stream << FLOG_OUTPUT_LTGRAY(verbose_stamp);
255  stream << FLOG_OUTPUT_CYAN(active_tag);
256  stream << FLOG_OUTPUT_GREEN(process_stamp);
257  stream << FLOG_OUTPUT_BROWN("] ") << FLOG_COLOR_YELLOW;
258  } // scope
259  break;
260 
261  case 0: {
262  stream << FLOG_OUTPUT_BROWN("[Warn ") << FLOG_OUTPUT_PURPLE(devel);
263  stream << FLOG_OUTPUT_CYAN(active_tag);
264  stream << FLOG_OUTPUT_GREEN(process_stamp);
265  stream << FLOG_OUTPUT_BROWN("] ") << FLOG_COLOR_YELLOW;
266  } // scope
267  break;
268 
269  default:
270  break;
271  } // switch
272 
273  clean_color_ = true;
274  return stream;
275 });
276 
277 //----------------------------------------------------------------------------//
278 // error
279 //----------------------------------------------------------------------------//
280 
281 severity_message_t(error, {
282  std::ostream & stream = flog_t::instance().severity_stream(
283  FLOG_STRIP_LEVEL < 4 && flog_t::instance().tag_enabled());
284 
285  std::string devel = devel_ ? "(devel)" : "";
286  std::string active_tag = flog_t::instance().initialized()
288  : "external";
289 
290  switch(flog_t::instance().verbose()) {
291  case 1: {
292  stream << FLOG_OUTPUT_RED("[ERROR ") << FLOG_OUTPUT_PURPLE(devel);
293  stream << FLOG_OUTPUT_LTGRAY(verbose_stamp);
294  stream << FLOG_OUTPUT_CYAN(active_tag);
295  stream << FLOG_OUTPUT_GREEN(process_stamp);
296  stream << FLOG_OUTPUT_RED("] ") << FLOG_COLOR_LTRED;
297  } // scope
298  break;
299 
300  case 0: {
301  stream << FLOG_OUTPUT_RED("[ERROR ") << FLOG_OUTPUT_PURPLE(devel);
302  stream << FLOG_OUTPUT_CYAN(active_tag);
303  stream << FLOG_OUTPUT_GREEN(process_stamp);
304  stream << FLOG_OUTPUT_RED("] ") << FLOG_COLOR_LTRED;
305  } // scope
306  break;
307 
308  default:
309  break;
310  } // switch
311 
312  clean_color_ = true;
313  return stream;
314 });
315 
316 #undef severity_message_t
317 
318 } // namespace log
319 } // namespace flecsi
std::ostream & severity_stream(bool active=true)
Definition: state.hh:219
Definition: message.hh:43
std::stringstream & buffer_stream()
Definition: state.hh:201
virtual std::ostream & stream()
Definition: message.hh:96
bool true_state()
Definition: message.hh:34
static flog_t & instance()
Definition: state.hh:73
std::string active_tag_name()
Definition: state.hh:291
std::ostream & stream()
Definition: state.hh:209
log_message_t(const char *file, int line)
Definition: message.hh:56
Definition: control.hh:31