Version: SMASH-3.1
logging.h
Go to the documentation of this file.
1 /*
2  *
3  * Copyright (c) 2014-2023
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 "einhard.hpp"
17 #include "yaml-cpp/yaml.h"
18 
19 #include "macros.h"
20 
21 namespace smash {
22 
24 class Configuration;
25 
75 #define DECLARE_LOGAREA(id__, name__) \
76  struct name__ { \
77  static constexpr int id = id__; \
78  static constexpr const char *textual() { return #name__; } \
79  static constexpr int textual_length() { return sizeof(#name__) - 1; } \
80  }
81 
90 namespace LogArea {
107 DECLARE_LOGAREA(16, List); // ListModus
124 
129 using AreaTuple =
136  RootSolver>;
137 } // namespace LogArea
138 
147 void create_all_loggers(Configuration config);
148 
153 #define SMASH_SOURCE_LOCATION \
154  __FILE__ ":" + std::to_string(__LINE__) + " (" + __func__ + ')'
155 
160 
168 
174 template <typename T>
177  const T &value;
179  const int width;
181  const int precision;
183  const char *const unit;
189  friend std::ostream &operator<<(std::ostream &out,
190  const FormattingHelper &h) {
191  if (h.width > 0) {
192  out << std::setfill(' ') << std::setw(h.width);
193  }
194  if (h.precision >= 0) {
195  out << std::setprecision(h.precision);
196  }
197  out << h.value;
198  if (h.unit) {
199  out << ' ' << h.unit;
200  }
201  return out;
202  }
203 };
204 
216 template <typename T>
217 FormattingHelper<T> format(const T &value, const char *unit, int width = -1,
218  int precision = -1) {
219  return {value, width, precision, unit};
220 }
221 
226 extern std::array<einhard::Logger<>, std::tuple_size<LogArea::AreaTuple>::value>
227  logg;
228 } // namespace smash
229 
230 namespace YAML {
235 template <>
243  static Node encode(const einhard::LogLevel &x) {
244  return Node{einhard::getLogLevelString(x)};
245  }
246 
255  static bool decode(const Node &node, einhard::LogLevel &x) {
256  if (!node.IsScalar()) {
257  return false;
258  } else {
259  x = einhard::getLogLevel(node.Scalar());
260  return true;
261  }
262  }
263 };
264 } // namespace YAML
265 
266 // @}
267 
268 #endif // SRC_INCLUDE_SMASH_LOGGING_H_
Interface to the SMASH configuration files.
This is the main include file for Einhard.
const T & value
Value that is being formatted.
Definition: logging.h:177
const int width
Output width.
Definition: logging.h:179
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, Pythia, GrandcanThermalizer, CrossSections, Output, HyperSurfaceCrossing, InitialConditions, ScatterActionMulti, Configuration, Potentials, RootSolver > AreaTuple
This type collects all existing log areas so they will be created with the correct log level automati...
Definition: logging.h:136
const char *const unit
Unit that is attached at the end of value.
Definition: logging.h:183
#define DECLARE_LOGAREA(id__, name__)
Declares the necessary interface to identify a new log area.
Definition: logging.h:75
std::array< einhard::Logger<>, std::tuple_size< LogArea::AreaTuple >::value > logg
An array that stores all pre-configured Logger objects.
Definition: logging.cc:39
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
static bool decode(const Node &node, einhard::LogLevel &x)
Convert from YAML::Node to einhard::LogLevel.
Definition: logging.h:255
const int precision
Precision that value is being formatted with.
Definition: logging.h:181
friend std::ostream & operator<<(std::ostream &out, const FormattingHelper &h)
Nicely formatted output.
Definition: logging.h:189
static Node encode(const einhard::LogLevel &x)
Convert from einhard::LogLevel to YAML::Node.
Definition: logging.h:243
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:217
einhard::LogLevel default_loglevel()
Definition: logging.cc:22
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:119
This namespace contains all objects required for logging using Einhard.
Definition: einhard.hpp:93
LogLevel getLogLevel(const std::string &level)
Compares the string level against the strings for LogLevel and returns the one it matches.
LogLevel
Specification of the message severity.
Definition: einhard.hpp:105
const char * getLogLevelString() noexcept
Retrieve a human readable representation of the given log level value.
Definition: action.h:24
Convert from YAML::Node to SMASH-readable (C++) format and vice versa.
Definition: configuration.h:37
Log area tag type.
Definition: logging.h:96
Log area tag type.
Definition: logging.h:93
Log area tag type.
Definition: logging.h:100
Log area tag type.
Definition: logging.h:94
Log area tag type.
Definition: logging.h:121
Log area tag type.
Definition: logging.h:116
Log area tag type.
Definition: logging.h:101
Log area tag type.
Definition: logging.h:109
Log area tag type.
Definition: logging.h:104
Log area tag type.
Definition: logging.h:92
Log area tag type.
Definition: logging.h:99
Log area tag type.
Definition: logging.h:112
Log area tag type.
Definition: logging.h:106
Log area tag type.
Definition: logging.h:97
Log area tag type.
Definition: logging.h:113
Log area tag type.
Definition: logging.h:107
Log area tag type.
Definition: logging.h:91
Log area tag type.
Definition: logging.h:108
Log area tag type.
Definition: logging.h:117
Log area tag type.
Definition: logging.h:98
Log area tag type.
Definition: logging.h:110
Log area tag type.
Definition: logging.h:122
Log area tag type.
Definition: logging.h:105
Log area tag type.
Definition: logging.h:114
Log area tag type.
Definition: logging.h:102
Log area tag type.
Definition: logging.h:123
Log area tag type.
Definition: logging.h:103
Log area tag type.
Definition: logging.h:95
Log area tag type.
Definition: logging.h:111