Version: SMASH-3.4
logging.cc
Go to the documentation of this file.
1 /*
2  *
3  * Copyright (c) 2014-2015,2017-2019,2022,2024,2026
4  * SMASH Team
5  *
6  * GNU General Public License (GPLv3 or later)
7  *
8  */
9 
10 #include "smash/logging.h"
11 
12 #include <array>
13 
14 #include "smash/configuration.h"
15 #include "smash/input_keys.h"
16 #include "smash/stringfunctions.h"
17 
18 namespace smash {
19 
20 /// The default logging level is ALL.
22 
24 
27 }
28 
29 std::array<einhard::Logger<>, std::tuple_size<LogArea::AreaTuple>::value>
31  static std::array<einhard::Logger<>,
32  std::tuple_size<LogArea::AreaTuple>::value>
33  logg_instance;
34  return logg_instance;
35 }
36 
37 /**
38  * \internal
39  * Recursively find the longest logger name at compile time.
40  *
41  * Beginning of the recursion.
42  *
43  * \tparam index Recursion index.
44  * \tparam stop Stopping index.
45  * \return Current maximal logger name length.
46  */
47 template <int index, int stop = 0>
48 constexpr typename std::enable_if<(index == stop), int>::type
50  using LogAreaTag = typename std::remove_reference<decltype(std::get<index>(
51  std::declval<LogArea::AreaTuple &>()))>::type;
52  return LogAreaTag::textual_length();
53 }
54 
55 /**
56  * \internal
57  * Recursively find the longest logger name at compile time.
58  *
59  * All cases except for the beginning of the recursion.
60  *
61  * \tparam index Recursion index.
62  * \tparam stop Stopping index.
63  * \tparam mid Middle index.
64  * \return Current maximal logger name length.
65  */
66 template <int index, int stop = 0, int mid = (index + stop) / 2>
67 constexpr typename std::enable_if<(index > stop), int>::type
69  return find_longest_logger_name<index, mid + 1>() >
70  find_longest_logger_name<mid, stop>()
71  ? find_longest_logger_name<index, mid + 1>()
72  : find_longest_logger_name<mid, stop>();
73 }
74 
75 /**
76  * \internal
77  * Recurse over the log areas in the LogArea::AreaTuple type. Do nothing
78  * here to end the recursion (see also below).
79  */
80 template <std::size_t index, int>
81 inline typename std::enable_if<(index == 0)>::type create_all_loggers_impl(
82  Configuration &) {}
83 
84 /**
85  * \internal
86  * Recurse over the log areas in the LogArea::AreaTuple type. (The recursion is
87  * ended via the overload above.)
88  *
89  * For every entry in the list the corresponding Logger object in
90  * logg is set up with area name and verbosity.
91  *
92  * \tparam index Recursion index.
93  * \tparam longest_name Length of longest log area name.
94  * \param[inout] config Configuration object.
95  */
96 template <std::size_t index,
97  int longest_name = find_longest_logger_name<index - 1>()>
98 inline typename std::enable_if<(index != 0)>::type create_all_loggers_impl(
99  Configuration &config) {
100  using LogAreaTag =
101  typename std::remove_reference<decltype(std::get<index - 1>(
102  std::declval<LogArea::AreaTuple &>()))>::type;
103  static_assert(LogAreaTag::id == index - 1,
104  "The order of types in LogArea::AreaTuple does not match the "
105  "id values in the LogArea types. Please fix! (see top of "
106  "'include/logging.h')");
107  auto &logger = logg[LogAreaTag::id];
108  const auto tmp = utf8::fill_both(LogAreaTag::textual(), longest_name);
109  logger.setAreaName(tmp);
110  auto logging_key = InputKeys::get_logging_key(LogAreaTag::textual());
111  logger.setVerbosity(config.take(logging_key, global_default_loglevel));
112  create_all_loggers_impl<index - 1, longest_name>(config);
113 }
114 
116  create_all_loggers_impl<std::tuple_size<LogArea::AreaTuple>::value>(config);
117 }
118 
119 } // namespace smash
Interface to the SMASH configuration files.
T take(const Key< T > &key)
The default interface for SMASH to read configuration values.
std::array< einhard::Logger<>, std::tuple_size< LogArea::AreaTuple >::value > & logg
An array that stores all pre-configured Logger objects.
Definition: logging.h:245
std::array< einhard::Logger<>, std::tuple_size< LogArea::AreaTuple >::value > & get_loggers()
Return the globally shared logger array.
Definition: logging.cc:30
void set_default_loglevel(einhard::LogLevel level)
Set the default log level (what will be returned from subsequent default_loglevel calls).
Definition: logging.cc:25
einhard::LogLevel default_loglevel()
Definition: logging.cc:23
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:115
LogLevel
Specification of the message severity.
Definition: einhard.hpp:109
@ ALL
Log all message.
Definition: einhard.hpp:110
std::string fill_both(const std::string &s, size_t width, char fill=' ')
Fill string with characters at both sides until the given width is reached.
Definition: action.h:24
static einhard::LogLevel global_default_loglevel
The default logging level is ALL.
Definition: logging.cc:21
constexpr std::enable_if<(index==stop), int >::type find_longest_logger_name()
Definition: logging.cc:49
std::enable_if<(index==0)>::type create_all_loggers_impl(Configuration &)
Definition: logging.cc:81
static const Key< einhard::LogLevel > & get_logging_key(std::string_view area)
Get the logging Key given a logging area.
Definition: input_keys.h:7773