Version: SMASH-2.0
logging.h
Go to the documentation of this file.
1 /*
2  *
3  * Copyright (c) 2014-2020
4  * SMASH Team
5  *
6  * GNU General Public License (GPLv3 or later)
7  *
8  */
9 
10 #ifndef SRC_INCLUDE_SMASH_LOGGING_H_
11 #define SRC_INCLUDE_SMASH_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 
168 #define DECLARE_LOGAREA(id__, name__) \
169  struct name__ { \
170  static constexpr int id = id__; \
171  static constexpr const char *textual() { return #name__; } \
172  static constexpr int textual_length() { return sizeof(#name__) - 1; } \
173  }
174 
182 namespace LogArea {
199 DECLARE_LOGAREA(16, List); // ListModus
215 
220 using AreaTuple =
227 } // namespace LogArea
228 
237 void create_all_loggers(Configuration config);
238 
243 #define source_location \
244  __FILE__ ":" + std::to_string(__LINE__) + " (" + __func__ + ')'
245 
250 
258 
264 template <typename T>
267  const T &value;
269  const int width;
271  const int precision;
273  const char *const unit;
279  friend std::ostream &operator<<(std::ostream &out,
280  const FormattingHelper &h) {
281  if (h.width > 0) {
282  out << std::setfill(' ') << std::setw(h.width);
283  }
284  if (h.precision >= 0) {
285  out << std::setprecision(h.precision);
286  }
287  out << h.value;
288  if (h.unit) {
289  out << ' ' << h.unit;
290  }
291  return out;
292  }
293 };
294 
306 template <typename T>
307 FormattingHelper<T> format(const T &value, const char *unit, int width = -1,
308  int precision = -1) {
309  return {value, width, precision, unit};
310 }
311 
316 extern std::array<einhard::Logger<>, std::tuple_size<LogArea::AreaTuple>::value>
317  logg;
318 } // namespace smash
319 
320 namespace YAML {
325 template <>
333  static Node encode(const einhard::LogLevel &x) {
334  return Node{einhard::getLogLevelString(x)};
335  }
336 
345  static bool decode(const Node &node, einhard::LogLevel &x) {
346  if (!node.IsScalar()) {
347  return false;
348  } else {
349  x = einhard::getLogLevel(node.Scalar());
350  return true;
351  }
352  }
353 };
354 } // namespace YAML
355 
356 // @}
357 
358 #endif // SRC_INCLUDE_SMASH_LOGGING_H_
smash
Definition: action.h:24
smash::LogArea::FindScatter
Log area tag type.
Definition: logging.h:191
smash::LogArea::InputParser
Log area tag type.
Definition: logging.h:189
smash::FormattingHelper::precision
const int precision
Precision that value is being formatted with.
Definition: logging.h:271
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, ScatterActionMulti > AreaTuple
This type collects all existing log areas so they will be created with the correct log level automati...
Definition: logging.h:226
smash::LogArea::Output
Log area tag type.
Definition: logging.h:210
smash::LogArea::Action
Log area tag type.
Definition: logging.h:188
smash::LogArea::InitialConditions
Log area tag type.
Definition: logging.h:213
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:195
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:345
smash::LogArea::Resonances
Log area tag type.
Definition: logging.h:194
smash::LogArea::Sampling
Log area tag type.
Definition: logging.h:206
YAML::convert< einhard::LogLevel >::encode
static Node encode(const einhard::LogLevel &x)
Convert from einhard::LogLevel to YAML::Node.
Definition: logging.h:333
smash::LogArea::ParticleType
Log area tag type.
Definition: logging.h:190
smash::FormattingHelper::value
const T & value
Value that is being formatted.
Definition: logging.h:267
DECLARE_LOGAREA
#define DECLARE_LOGAREA(id__, name__)
Declares the necessary interface to identify a new log area.
Definition: logging.h:168
smash::LogArea::Tmn
Log area tag type.
Definition: logging.h:203
smash::LogArea::Density
Log area tag type.
Definition: logging.h:201
YAML
Definition: configuration.h:25
smash::LogArea::Sphere
Log area tag type.
Definition: logging.h:187
smash::LogArea::Experiment
Log area tag type.
Definition: logging.h:184
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:193
smash::Configuration
Interface to the SMASH configuration files.
Definition: configuration.h:464
smash::LogArea::Fpe
Log area tag type.
Definition: logging.h:204
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:307
smash::LogArea::Main
Log area tag type.
Definition: logging.h:183
smash::FormattingHelper::width
const int width
Output width.
Definition: logging.h:269
smash::FormattingHelper::unit
const char *const unit
Unit that is attached at the end of value.
Definition: logging.h:273
Sampling
Sampling
Possible methods of impact parameter sampling.
Definition: forwarddeclarations.h:104
smash::LogArea::Collider
Log area tag type.
Definition: logging.h:186
smash::LogArea::GrandcanThermalizer
Log area tag type.
Definition: logging.h:208
smash::FormattingHelper::operator<<
friend std::ostream & operator<<(std::ostream &out, const FormattingHelper &h)
Nicely formatted output.
Definition: logging.h:279
smash::LogArea::Grid
Log area tag type.
Definition: logging.h:198
einhard.hpp
smash::LogArea::CrossSections
Log area tag type.
Definition: logging.h:209
smash::LogArea::Lattice
Log area tag type.
Definition: logging.h:205
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:197
smash::LogArea::Clock
Log area tag type.
Definition: logging.h:192
smash::LogArea::Nucleus
Log area tag type.
Definition: logging.h:200
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:202
smash::FormattingHelper
Definition: logging.h:265
smash::LogArea::HyperSurfaceCrossing
Log area tag type.
Definition: logging.h:212
smash::LogArea::ScatterActionMulti
Log area tag type.
Definition: logging.h:214
smash::LogArea::Distributions
Log area tag type.
Definition: logging.h:196
smash::LogArea::HadronGasEos
Log area tag type.
Definition: logging.h:211
smash::LogArea::List
Log area tag type.
Definition: logging.h:199
einhard::LogLevel
LogLevel
Specification of the message severity.
Definition: einhard.hpp:104
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:207
smash::LogArea::Box
Log area tag type.
Definition: logging.h:185