Interface Documentation
Version: invalid
flog.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/utils.hh"
21 
22 #if defined(FLECSI_ENABLE_FLOG)
23 #include "flecsi/log/message.hh"
24 #include "flecsi/log/tag_scope.hh"
25 #endif
26 
27 #include <iostream>
28 #include <sstream>
29 
30 #include <unistd.h>
31 
32 #if defined(FLECSI_ENABLE_FLOG)
33 
34 namespace flecsi {
35 namespace log {
36 
37 struct guard;
38 
47 struct tag {
48  friend guard;
49 
50  tag(const char * label) : label_(label) {
52  }
53 
54 private:
55  std::string label_;
56 }; // struct tag
57 
67 struct guard {
68  guard(tag const & t)
69  : scope_(flog_t::instance().lookup_tag(t.label_.c_str())) {}
70 
71 private:
72  tag_scope_t scope_;
73 }; // struct guard
74 
75 #if defined(FLOG_ENABLE_DEVELOPER_MODE)
76 using devel_tag = tag;
77 using devel_guard = guard;
78 #else
79 struct devel_tag {
80  devel_tag(const char *) {}
81 };
82 struct devel_guard {
83  devel_guard(devel_tag const &) {}
84 };
85 #endif
86 
100 inline void
101 add_output_stream(std::string const & label,
102  std::ostream & stream,
103  bool colorize = false) {
104  flog_t::instance().config_stream().add_buffer(label, stream, colorize);
105 } // add_output_stream
106 
107 } // namespace log
108 } // namespace flecsi
109 
136 #define flog(severity) \
137  /* MACRO IMPLEMENTATION */ \
138  \
139  true && ::flecsi::log::severity##_log_message_t(__FILE__, __LINE__, false) \
140  .stream()
141 
142 #if defined(FLOG_ENABLE_DEVELOPER_MODE)
143 
144 #define flog_devel(severity) \
145  /* MACRO IMPLEMENTATION */ \
146  \
147  true && \
148  ::flecsi::log::severity##_log_message_t(__FILE__, __LINE__, true).stream()
149 
150 #else
151 
152 #define flog_devel(severity) \
153  if(true) { \
154  } \
155  else \
156  std::cerr
157 
158 #endif // FLOG_ENABLE_DEVELOPER_MODE
159 
178 #define flog_trace(message) \
179  /* MACRO IMPLEMENTATION */ \
180  \
181  ::flecsi::log::trace_log_message_t(__FILE__, __LINE__).stream() << message
182 
201 #define flog_info(message) \
202  /* MACRO IMPLEMENTATION */ \
203  \
204  ::flecsi::log::info_log_message_t(__FILE__, __LINE__).stream() << message
205 
224 #define flog_warn(message) \
225  /* MACRO IMPLEMENTATION */ \
226  \
227  ::flecsi::log::warn_log_message_t(__FILE__, __LINE__).stream() << message
228 
247 #define flog_error(message) \
248  /* MACRO IMPLEMENTATION */ \
249  \
250  ::flecsi::log::error_log_message_t(__FILE__, __LINE__).stream() << message
251 
252 #define __flog_internal_wait_on_flusher() usleep(FLOG_PACKET_FLUSH_INTERVAL)
253 
254 #else // FLECSI_ENABLE_FLOG
255 
256 namespace flecsi {
257 namespace log {
258 
259 struct tag {
260  tag(const char *) {}
261 };
262 struct guard {
263  guard(tag const &) {}
264 };
265 struct devel_tag {
266  devel_tag(const char *) {}
267 };
268 struct devel_guard {
269  devel_guard(devel_tag const &) {}
270 };
271 
272 inline void
273 add_output_stream(std::string const &, std::ostream &, bool = false) {}
274 
275 } // namespace log
276 } // namespace flecsi
277 
278 #define flog_initialize(active)
279 #define flog_finalize()
280 
281 #define flog(severity) \
282  if(true) { \
283  } \
284  else \
285  std::cerr
286 
287 #define flog_devel(severity) \
288  if(true) { \
289  } \
290  else \
291  std::cerr
292 
293 #define flog_trace(message)
294 #define flog_info(message)
295 #define flog_warn(message)
296 #define flog_error(message)
297 
298 #define __flog_internal_wait_on_flusher()
299 
300 #endif // FLECSI_ENABLE_FLOG
301 
310 #define fixme() flog(warn)
311 
312 #include <boost/stacktrace.hpp>
313 
314 namespace flecsi {
315 namespace log {
316 
317 inline void
318 dumpstack() {
319 #if !defined(NDEBUG)
320  std::cerr << FLOG_OUTPUT_RED("FleCSI Runtime: std::abort called.")
321  << std::endl
322  << FLOG_OUTPUT_GREEN("Dumping stacktrace...") << std::endl;
323  std::cerr << boost::stacktrace::stacktrace() << std::endl;
324 #else
325  std::cerr << FLOG_OUTPUT_RED("FleCSI Runtime: std::abort called.")
326  << std::endl
327  << FLOG_OUTPUT_BROWN("Build with '-DCMAKE_BUILD_TYPE=Debug'"
328  << " to enable FleCSI runtime stacktrace.")
329  << std::endl;
330 #endif
331 } // dumpstack
332 
333 } // namespace log
334 } // namespace flecsi
335 
358 #define flog_fatal(message) \
359  /* MACRO IMPLEMENTATION */ \
360  \
361  { \
362  std::stringstream _sstream; \
363  _sstream << FLOG_OUTPUT_LTRED("FATAL ERROR ") \
364  << FLOG_OUTPUT_YELLOW(::flecsi::log::rstrip<'/'>(__FILE__) \
365  << ":" << __LINE__ << " ") \
366  << FLOG_OUTPUT_LTRED(message) << std::endl; \
367  __flog_internal_wait_on_flusher(); \
368  std::cerr << _sstream.str() << std::endl; \
369  ::flecsi::log::dumpstack(); \
370  std::abort(); \
371  } /* scope */
372 
398 /*
399  This implementation avoids unused variables error
400  Attribution: https://stackoverflow.com/questions/777261/
401  avoiding-unused-variables-warnings-when-using-assert-in-a-release-build
402  */
403 #ifdef NDEBUG
404 #define flog_assert(test, message) \
405  /* MACRO IMPLEMENTATION */ \
406  \
407  do { \
408  (void)sizeof(test); \
409  } while(0)
410 #else
411 #define flog_assert(test, message) \
412  /* MACRO IMPLEMENTATION */ \
413  \
414  do \
415  if(!(test)) { \
416  flog_fatal(message); \
417  } \
418  while(0)
419 #endif // NDEBUG
Definition: flog.hh:79
void add_buffer(std::string const &key, std::ostream &s, bool colorized=false)
Definition: types.hh:276
tee_stream_t & config_stream()
Definition: state.hh:236
Definition: tag_scope.hh:33
size_t register_tag(const char *tag)
Definition: state.hh:244
void add_output_stream(std::string const &label, std::ostream &stream, bool colorize=false)
Definition: flog.hh:101
Definition: flog.hh:82
Definition: flog.hh:47
static flog_t & instance()
Definition: state.hh:73
Definition: flog.hh:67
Definition: control.hh:31