Version: SMASH-1.5
logging.h
Go to the documentation of this file.
1 /*
2  *
3  * Copyright (c) 2014-2018
4  * SMASH Team
5  *
6  * GNU General Public License (GPLv3 or later)
7  *
8  */
9 
10 #ifndef SRC_INCLUDE_LOGGING_H_
11 #define SRC_INCLUDE_LOGGING_H_
12 
13 #include <stdexcept>
14 #include <tuple>
15 
16 #include <yaml-cpp/yaml.h> // NOLINT(build/include_order)
17 #include <einhard.hpp>
18 
19 #include "macros.h"
20 
21 namespace smash {
22 
24 class Configuration;
25 
156 #define DECLARE_LOGAREA(id__, name__) \
157  struct name__ { \
158  static constexpr int id = id__; \
159  static constexpr const char *textual() { return #name__; } \
160  static constexpr int textual_length() { return sizeof(#name__) - 1; } \
161  }
162 
170 namespace LogArea {
187 DECLARE_LOGAREA(16, List); // ListModus
199 
204 using AreaTuple =
210 } // namespace LogArea
211 
220 void create_all_loggers(Configuration config);
221 
227 
233 template <typename LogAreaTag>
235  static_assert(LogAreaTag::id < std::tuple_size<LogArea::AreaTuple>::value &&
236  LogAreaTag::id >= 0,
237  "The LogArea::AreaTuple is out of sync with the declared log "
238  "areas. Please fix! (see top of 'include/logging.h')");
239  return retrieve_logger_impl(LogAreaTag::id);
240 }
241 
246 #define source_location \
247  __FILE__ ":" + std::to_string(__LINE__) + " (" + __func__ + ')'
248 
253 
261 
267 template <typename T>
270  const T &value;
272  const int width;
274  const int precision;
276  const char *const unit;
282  friend std::ostream &operator<<(std::ostream &out,
283  const FormattingHelper &h) {
284  if (h.width > 0) {
285  out << std::setfill(' ') << std::setw(h.width);
286  }
287  if (h.precision >= 0) {
288  out << std::setprecision(h.precision);
289  }
290  out << h.value;
291  if (h.unit) {
292  out << ' ' << h.unit;
293  }
294  return out;
295  }
296 };
297 
309 template <typename T>
310 FormattingHelper<T> format(const T &value, const char *unit, int width = -1,
311  int precision = -1) {
312  return {value, width, precision, unit};
313 }
314 } // namespace smash
315 
316 namespace YAML {
321 template <>
329  static Node encode(const einhard::LogLevel &x) {
330  return Node{einhard::getLogLevelString(x)};
331  }
332 
341  static bool decode(const Node &node, einhard::LogLevel &x) {
342  if (!node.IsScalar()) {
343  return false;
344  } else {
345  x = einhard::getLogLevel(node.Scalar());
346  return true;
347  }
348  }
349 };
350 } // namespace YAML
351 
352 // @}
353 
354 #endif // SRC_INCLUDE_LOGGING_H_
FormattingHelper< T > format(const T &value, const char *unit, int width=-1, int precision=-1)
Acts as a stream modifier for std::ostream to output an object with an optional suffix string and wit...
Definition: logging.h:310
const T & value
Value that is being formatted.
Definition: logging.h:270
Log area tag type.
Definition: logging.h:177
Log area tag type.
Definition: logging.h:174
Log area tag type.
Definition: logging.h:172
Log area tag type.
Definition: logging.h:186
Log area tag type.
Definition: logging.h:197
std::tuple< Main, Experiment, Box, Collider, Sphere, Action, InputParser, ParticleType, FindScatter, Clock, DecayModes, Resonances, ScatterAction, Distributions, Propagation, Grid, List, Nucleus, Density, PauliBlocking, Tmn, Fpe, Lattice, Sampling, Pythia, GrandcanThermalizer, CrossSections, Output > AreaTuple
This type collects all existing log areas so they will be created with the correct log level automati...
Definition: logging.h:209
Log area tag type.
Definition: logging.h:195
Log area tag type.
Definition: logging.h:179
Log area tag type.
Definition: logging.h:191
friend std::ostream & operator<<(std::ostream &out, const FormattingHelper &h)
Nicely formatted output.
Definition: logging.h:282
static bool decode(const Node &node, einhard::LogLevel &x)
Convert from YAML::Node to einhard::LogLevel.
Definition: logging.h:341
static Node encode(const einhard::LogLevel &x)
Convert from einhard::LogLevel to YAML::Node.
Definition: logging.h:329
Log area tag type.
Definition: logging.h:188
const int precision
Precision that value is being formatted with.
Definition: logging.h:274
Interface to the SMASH configuration files.
Log area tag type.
Definition: logging.h:198
Convert from YAML::Node to SMASH-readable (C++) format and vice versa.
Definition: configuration.h:34
Log area tag type.
Definition: logging.h:178
Log area tag type.
Definition: logging.h:176
Log area tag type.
Definition: logging.h:173
Log area tag type.
Definition: logging.h:187
Log area tag type.
Definition: logging.h:190
einhard::LogLevel default_loglevel()
Definition: logging.cc:22
Log area tag type.
Definition: logging.h:193
Log area tag type.
Definition: logging.h:194
void create_all_loggers(Configuration config)
Called from main() right after the Configuration object is fully set up to create all logger objects ...
Definition: logging.cc:116
Log area tag type.
Definition: logging.h:171
#define DECLARE_LOGAREA(id__, name__)
Declares the necessary interface to identify a new log area.
Definition: logging.h:156
Log area tag type.
Definition: logging.h:180
const char * getLogLevelString() noexcept
Retrieve a human readable representation of the given log level value.
Log area tag type.
Definition: logging.h:175
This is the main include file for Einhard.
This namespace contains all objects required for logging using Einhard.
Definition: einhard.hpp:92
Log area tag type.
Definition: logging.h:185
Log area tag type.
Definition: logging.h:192
A Logger object can be used to output messages to stdout.
Definition: einhard.hpp:347
void set_default_loglevel(einhard::LogLevel level)
Set the default log level (what will be returned from subsequent default_loglevel calls)...
Definition: logging.cc:24
const char *const unit
Unit that is attached at the end of value.
Definition: logging.h:276
Log area tag type.
Definition: logging.h:182
Log area tag type.
Definition: logging.h:181
Sampling
Possible methods of impact parameter sampling.
einhard::Logger & retrieve_logger_impl(int id)
Definition: logging.cc:35
const int width
Output width.
Definition: logging.h:272
LogLevel
Specification of the message severity.
Definition: einhard.hpp:104
LogLevel getLogLevel(const std::string &level)
Compares the string level against the strings for LogLevel and returns the one it matches...
Log area tag type.
Definition: logging.h:184
Log area tag type.
Definition: logging.h:183
einhard::Logger & logger()
Definition: logging.h:234
Log area tag type.
Definition: logging.h:189
Definition: action.h:24