Version: SMASH-1.7
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 
160 #define DECLARE_LOGAREA(id__, name__) \
161  struct name__ { \
162  static constexpr int id = id__; \
163  static constexpr const char *textual() { return #name__; } \
164  static constexpr int textual_length() { return sizeof(#name__) - 1; } \
165  }
166 
174 namespace LogArea {
191 DECLARE_LOGAREA(16, List); // ListModus
205 
210 using AreaTuple =
217 } // namespace LogArea
218 
227 void create_all_loggers(Configuration config);
228 
234 
240 template <typename LogAreaTag>
242  static_assert(LogAreaTag::id < std::tuple_size<LogArea::AreaTuple>::value &&
243  LogAreaTag::id >= 0,
244  "The LogArea::AreaTuple is out of sync with the declared log "
245  "areas. Please fix! (see top of 'include/logging.h')");
246  return retrieve_logger_impl(LogAreaTag::id);
247 }
248 
253 #define source_location \
254  __FILE__ ":" + std::to_string(__LINE__) + " (" + __func__ + ')'
255 
260 
268 
274 template <typename T>
277  const T &value;
279  const int width;
281  const int precision;
283  const char *const unit;
289  friend std::ostream &operator<<(std::ostream &out,
290  const FormattingHelper &h) {
291  if (h.width > 0) {
292  out << std::setfill(' ') << std::setw(h.width);
293  }
294  if (h.precision >= 0) {
295  out << std::setprecision(h.precision);
296  }
297  out << h.value;
298  if (h.unit) {
299  out << ' ' << h.unit;
300  }
301  return out;
302  }
303 };
304 
316 template <typename T>
317 FormattingHelper<T> format(const T &value, const char *unit, int width = -1,
318  int precision = -1) {
319  return {value, width, precision, unit};
320 }
321 } // namespace smash
322 
323 namespace YAML {
328 template <>
336  static Node encode(const einhard::LogLevel &x) {
337  return Node{einhard::getLogLevelString(x)};
338  }
339 
348  static bool decode(const Node &node, einhard::LogLevel &x) {
349  if (!node.IsScalar()) {
350  return false;
351  } else {
352  x = einhard::getLogLevel(node.Scalar());
353  return true;
354  }
355  }
356 };
357 } // namespace YAML
358 
359 // @}
360 
361 #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:317
const T & value
Value that is being formatted.
Definition: logging.h:277
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 > AreaTuple
This type collects all existing log areas so they will be created with the correct log level automati...
Definition: logging.h:216
Log area tag type.
Definition: logging.h:181
Log area tag type.
Definition: logging.h:178
Log area tag type.
Definition: logging.h:176
Log area tag type.
Definition: logging.h:190
Log area tag type.
Definition: logging.h:201
Log area tag type.
Definition: logging.h:199
Log area tag type.
Definition: logging.h:183
Log area tag type.
Definition: logging.h:195
friend std::ostream & operator<<(std::ostream &out, const FormattingHelper &h)
Nicely formatted output.
Definition: logging.h:289
static bool decode(const Node &node, einhard::LogLevel &x)
Convert from YAML::Node to einhard::LogLevel.
Definition: logging.h:348
static Node encode(const einhard::LogLevel &x)
Convert from einhard::LogLevel to YAML::Node.
Definition: logging.h:336
Log area tag type.
Definition: logging.h:192
const int precision
Precision that value is being formatted with.
Definition: logging.h:281
Interface to the SMASH configuration files.
Log area tag type.
Definition: logging.h:202
Convert from YAML::Node to SMASH-readable (C++) format and vice versa.
Definition: configuration.h:34
Log area tag type.
Definition: logging.h:182
Log area tag type.
Definition: logging.h:180
Log area tag type.
Definition: logging.h:177
Log area tag type.
Definition: logging.h:191
Log area tag type.
Definition: logging.h:194
einhard::LogLevel default_loglevel()
Definition: logging.cc:22
Log area tag type.
Definition: logging.h:197
Log area tag type.
Definition: logging.h:198
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:175
#define DECLARE_LOGAREA(id__, name__)
Declares the necessary interface to identify a new log area.
Definition: logging.h:160
Log area tag type.
Definition: logging.h:184
const char * getLogLevelString() noexcept
Retrieve a human readable representation of the given log level value.
Log area tag type.
Definition: logging.h:179
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:189
Log area tag type.
Definition: logging.h:196
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:283
Log area tag type.
Definition: logging.h:186
Log area tag type.
Definition: logging.h:185
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:279
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:188
Log area tag type.
Definition: logging.h:187
einhard::Logger & logger()
Definition: logging.h:241
Log area tag type.
Definition: logging.h:203
Log area tag type.
Definition: logging.h:193
Definition: action.h:24