Version: SMASH-1.8
logging.h
Go to the documentation of this file.
1 /*
2  *
3  * Copyright (c) 2014-2019
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 
166 #define DECLARE_LOGAREA(id__, name__) \
167  struct name__ { \
168  static constexpr int id = id__; \
169  static constexpr const char *textual() { return #name__; } \
170  static constexpr int textual_length() { return sizeof(#name__) - 1; } \
171  }
172 
180 namespace LogArea {
197 DECLARE_LOGAREA(16, List); // ListModus
212 
217 using AreaTuple =
224 } // namespace LogArea
225 
234 void create_all_loggers(Configuration config);
235 
240 #define source_location \
241  __FILE__ ":" + std::to_string(__LINE__) + " (" + __func__ + ')'
242 
247 
255 
261 template <typename T>
264  const T &value;
266  const int width;
268  const int precision;
270  const char *const unit;
276  friend std::ostream &operator<<(std::ostream &out,
277  const FormattingHelper &h) {
278  if (h.width > 0) {
279  out << std::setfill(' ') << std::setw(h.width);
280  }
281  if (h.precision >= 0) {
282  out << std::setprecision(h.precision);
283  }
284  out << h.value;
285  if (h.unit) {
286  out << ' ' << h.unit;
287  }
288  return out;
289  }
290 };
291 
303 template <typename T>
304 FormattingHelper<T> format(const T &value, const char *unit, int width = -1,
305  int precision = -1) {
306  return {value, width, precision, unit};
307 }
308 
313 extern std::array<einhard::Logger<>, std::tuple_size<LogArea::AreaTuple>::value>
314  logg;
315 } // namespace smash
316 
317 namespace YAML {
322 template <>
330  static Node encode(const einhard::LogLevel &x) {
331  return Node{einhard::getLogLevelString(x)};
332  }
333 
342  static bool decode(const Node &node, einhard::LogLevel &x) {
343  if (!node.IsScalar()) {
344  return false;
345  } else {
346  x = einhard::getLogLevel(node.Scalar());
347  return true;
348  }
349  }
350 };
351 } // namespace YAML
352 
353 // @}
354 
355 #endif // SRC_INCLUDE_LOGGING_H_
smash
Definition: action.h:24
smash::LogArea::FindScatter
Log area tag type.
Definition: logging.h:189
smash::LogArea::InputParser
Log area tag type.
Definition: logging.h:187
smash::FormattingHelper::precision
const int precision
Precision that value is being formatted with.
Definition: logging.h:268
smash::LogArea::Output
Log area tag type.
Definition: logging.h:208
smash::LogArea::Action
Log area tag type.
Definition: logging.h:186
smash::LogArea::InitialConditions
Log area tag type.
Definition: logging.h:211
YAML::convert
Convert from YAML::Node to SMASH-readable (C++) format and vice versa.
Definition: configuration.h:34
smash::LogArea::ScatterAction
Log area tag type.
Definition: logging.h:193
smash::default_loglevel
einhard::LogLevel default_loglevel()
Definition: logging.cc:22
macros.h
YAML::convert< einhard::LogLevel >::decode
static bool decode(const Node &node, einhard::LogLevel &x)
Convert from YAML::Node to einhard::LogLevel.
Definition: logging.h:342
smash::LogArea::Resonances
Log area tag type.
Definition: logging.h:192
smash::LogArea::Sampling
Log area tag type.
Definition: logging.h:204
YAML::convert< einhard::LogLevel >::encode
static Node encode(const einhard::LogLevel &x)
Convert from einhard::LogLevel to YAML::Node.
Definition: logging.h:330
smash::LogArea::ParticleType
Log area tag type.
Definition: logging.h:188
smash::FormattingHelper::value
const T & value
Value that is being formatted.
Definition: logging.h:264
DECLARE_LOGAREA
#define DECLARE_LOGAREA(id__, name__)
Declares the necessary interface to identify a new log area.
Definition: logging.h:166
smash::LogArea::Tmn
Log area tag type.
Definition: logging.h:201
smash::LogArea::Density
Log area tag type.
Definition: logging.h:199
YAML
Definition: configuration.h:25
smash::LogArea::Sphere
Log area tag type.
Definition: logging.h:185
smash::LogArea::Experiment
Log area tag type.
Definition: logging.h:182
smash::set_default_loglevel
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
smash::logg
std::array< einhard::Logger<>, std::tuple_size< LogArea::AreaTuple >::value > logg
An array that stores all pre-configured Logger objects.
Definition: logging.cc:39
smash::create_all_loggers
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:118
smash::LogArea::DecayModes
Log area tag type.
Definition: logging.h:191
smash::Configuration
Interface to the SMASH configuration files.
Definition: configuration.h:464
smash::LogArea::Fpe
Log area tag type.
Definition: logging.h:202
smash::format
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:304
smash::LogArea::Main
Log area tag type.
Definition: logging.h:181
smash::FormattingHelper::width
const int width
Output width.
Definition: logging.h:266
smash::FormattingHelper::unit
const char *const unit
Unit that is attached at the end of value.
Definition: logging.h:270
Sampling
Sampling
Possible methods of impact parameter sampling.
Definition: forwarddeclarations.h:103
smash::LogArea::Collider
Log area tag type.
Definition: logging.h:184
smash::LogArea::GrandcanThermalizer
Log area tag type.
Definition: logging.h:206
smash::FormattingHelper::operator<<
friend std::ostream & operator<<(std::ostream &out, const FormattingHelper &h)
Nicely formatted output.
Definition: logging.h:276
smash::LogArea::Grid
Log area tag type.
Definition: logging.h:196
einhard.hpp
smash::LogArea::CrossSections
Log area tag type.
Definition: logging.h:207
smash::LogArea::Lattice
Log area tag type.
Definition: logging.h:203
einhard
This namespace contains all objects required for logging using Einhard.
Definition: einhard.hpp:92
smash::LogArea::Propagation
Log area tag type.
Definition: logging.h:195
smash::LogArea::Clock
Log area tag type.
Definition: logging.h:190
smash::LogArea::Nucleus
Log area tag type.
Definition: logging.h:198
einhard::getLogLevelString
const char * getLogLevelString() noexcept
Retrieve a human readable representation of the given log level value.
smash::LogArea::PauliBlocking
Log area tag type.
Definition: logging.h:200
smash::FormattingHelper
Definition: logging.h:262
smash::LogArea::HyperSurfaceCrossing
Log area tag type.
Definition: logging.h:210
smash::LogArea::Distributions
Log area tag type.
Definition: logging.h:194
smash::LogArea::HadronGasEos
Log area tag type.
Definition: logging.h:209
smash::LogArea::List
Log area tag type.
Definition: logging.h:197
einhard::LogLevel
LogLevel
Specification of the message severity.
Definition: einhard.hpp:104
smash::LogArea::AreaTuple
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, HadronGasEos, HyperSurfaceCrossing, InitialConditions > AreaTuple
This type collects all existing log areas so they will be created with the correct log level automati...
Definition: logging.h:223
einhard::getLogLevel
LogLevel getLogLevel(const std::string &level)
Compares the string level against the strings for LogLevel and returns the one it matches.
smash::LogArea::Pythia
Log area tag type.
Definition: logging.h:205
smash::LogArea::Box
Log area tag type.
Definition: logging.h:183