bmv2
Designing your own switch target with bmv2
logger.h
Go to the documentation of this file.
1 /* Copyright 2013-present Barefoot Networks, Inc.
2  *
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 /*
17  * Antonin Bas (antonin@barefootnetworks.com)
18  *
19  */
20 
32 
33 #ifndef BM_BM_SIM_LOGGER_H_
34 #define BM_BM_SIM_LOGGER_H_
35 
36 #include <bm/config.h>
37 #include <bm/spdlog/spdlog.h>
38 
39 #include <iosfwd>
40 #include <string>
41 
42 namespace bm {
43 
60 class Logger {
61  public:
63  enum class LogLevel {
64  TRACE, DEBUG, INFO, NOTICE, WARN, ERROR, CRITICAL, ALERT, EMERG, OFF
65  };
66 
67  public:
79  static spdlog::logger *get() {
80  static spdlog::logger *logger_ = init_logger();
81  (void) logger_;
82  return logger;
83  }
84 
86  static void set_log_level(LogLevel level);
87 
89  static void set_logger_console();
90 
93  static void set_logger_file(const std::string &filename,
94  bool force_flush = false);
95 
97  // NOLINTNEXTLINE(runtime/references)
98  static void set_logger_ostream(std::ostream &os);
99 
100  static void unset_logger();
101 
102  private:
103  static spdlog::logger *init_logger();
104 
105  static spdlog::level::level_enum to_spd_level(LogLevel level);
106 
107  static void set_pattern();
108 
109  private:
110  static spdlog::logger *logger;
111 };
112 
113 } // namespace bm
114 
115 #ifdef BM_LOG_DEBUG_ON
116 #define BMLOG_DEBUG(...) bm::Logger::get()->debug(__VA_ARGS__);
119 #else
120 #define BMLOG_DEBUG(...)
121 #endif
122 
123 #ifdef BM_LOG_TRACE_ON
124 #define BMLOG_TRACE(...) bm::Logger::get()->trace(__VA_ARGS__);
127 #else
128 #define BMLOG_TRACE(...)
129 #endif
130 
134 #define BMLOG_DEBUG_PKT(pkt, s, ...) \
135  BMLOG_DEBUG("[{}] [cxt {}] " s, (pkt).get_unique_id(), \
136  (pkt).get_context(), ##__VA_ARGS__)
137 #define BMLOG_TRACE_PKT(pkt, s, ...) \
141  BMLOG_TRACE("[{}] [cxt {}] " s, (pkt).get_unique_id(), \
142  (pkt).get_context(), ##__VA_ARGS__)
143 #define BMLOG_TRACE_SI_PKT(pkt, source_info, s, ...) \
146  BMLOG_TRACE("[{}] [cxt {}] {}" s, (pkt).get_unique_id(), \
147  (pkt).get_context(), \
148  (source_info == nullptr) ? "" \
149  : (source_info->to_string() + " "), \
150  ##__VA_ARGS__)
151 
152 #define BMLOG_ERROR(...) bm::Logger::get()->error(__VA_ARGS__)
153 
154 #define BMLOG_ERROR_PKT(pkt, s, ...) \
155  bm::Logger::get()->error("[{}] [cxt {}] " s, (pkt).get_unique_id(), \
156  (pkt).get_context(), ##__VA_ARGS__)
157 
158 #endif // BM_BM_SIM_LOGGER_H_
bm::Logger
Definition: logger.h:60
bm::Logger::set_log_level
static void set_log_level(LogLevel level)
Set the log level. Messages with a lesser level will not be logged.
bm::Logger::set_logger_file
static void set_logger_file(const std::string &filename, bool force_flush=false)
bm::Logger::LogLevel
LogLevel
Different log levels.
Definition: logger.h:63
bm::Logger::set_logger_ostream
static void set_logger_ostream(std::ostream &os)
Log all messages to the given output stream. Mostly used for testing.
bm::Logger::set_logger_console
static void set_logger_console()
Log all messages to stdout.
bm::Logger::get
static spdlog::logger * get()
Definition: logger.h:79