Version: SMASH-2.2
logging.h
Go to the documentation of this file.
1 /*
2  *
3  * Copyright (c) 2014-2021
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 SMASH_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_
Interface to the SMASH configuration files.
This is the main include file for Einhard.
Sampling
Possible methods of impact parameter sampling.
const T & value
Value that is being formatted.
Definition: logging.h:267
const int width
Output width.
Definition: logging.h:269
const char *const unit
Unit that is attached at the end of value.
Definition: logging.h:273
#define DECLARE_LOGAREA(id__, name__)
Declares the necessary interface to identify a new log area.
Definition: logging.h:168
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
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:345
const int precision
Precision that value is being formatted with.
Definition: logging.h:271
friend std::ostream & operator<<(std::ostream &out, const FormattingHelper &h)
Nicely formatted output.
Definition: logging.h:279
static Node encode(const einhard::LogLevel &x)
Convert from einhard::LogLevel to YAML::Node.
Definition: logging.h:333
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
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:118
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:34
Log area tag type.
Definition: logging.h:188
Log area tag type.
Definition: logging.h:185
Log area tag type.
Definition: logging.h:192
Log area tag type.
Definition: logging.h:186
Log area tag type.
Definition: logging.h:209
Log area tag type.
Definition: logging.h:193
Log area tag type.
Definition: logging.h:201
Log area tag type.
Definition: logging.h:196
Log area tag type.
Definition: logging.h:184
Log area tag type.
Definition: logging.h:191
Log area tag type.
Definition: logging.h:204
Log area tag type.
Definition: logging.h:198
Log area tag type.
Definition: logging.h:211
Log area tag type.
Definition: logging.h:189
Log area tag type.
Definition: logging.h:205
Log area tag type.
Definition: logging.h:199
Log area tag type.
Definition: logging.h:183
Log area tag type.
Definition: logging.h:200
Log area tag type.
Definition: logging.h:210
Log area tag type.
Definition: logging.h:190
Log area tag type.
Definition: logging.h:202
Log area tag type.
Definition: logging.h:197
Log area tag type.
Definition: logging.h:207
Log area tag type.
Definition: logging.h:194
Log area tag type.
Definition: logging.h:206
Log area tag type.
Definition: logging.h:195
Log area tag type.
Definition: logging.h:187
Log area tag type.
Definition: logging.h:203