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 
95  static void set_logger_file(const std::string &filename,
96  bool force_flush = false,
97  size_t max_size = 1024 * 1024 * 5,
98  size_t max_files = 3);
99 
101  // NOLINTNEXTLINE(runtime/references)
102  static void set_logger_ostream(std::ostream &os);
103 
104  static void unset_logger();
105 
106  private:
107  static spdlog::logger *init_logger();
108 
109  static spdlog::level::level_enum to_spd_level(LogLevel level);
110 
111  static void set_pattern();
112 
113  private:
114  static spdlog::logger *logger;
115 };
116 
117 } // namespace bm
118 
119 #ifdef BM_LOG_DEBUG_ON
120 #define BMLOG_DEBUG(...) bm::Logger::get()->debug(__VA_ARGS__);
123 #else
124 #define BMLOG_DEBUG(...)
125 #endif
126 
127 #ifdef BM_LOG_TRACE_ON
128 #define BMLOG_TRACE(...) bm::Logger::get()->trace(__VA_ARGS__);
131 #else
132 #define BMLOG_TRACE(...)
133 #endif
134 
138 #define BMLOG_DEBUG_PKT(pkt, s, ...) \
139  BMLOG_DEBUG("[{}] [cxt {}] " s, (pkt).get_unique_id(), \
140  (pkt).get_context(), ##__VA_ARGS__)
141 #define BMLOG_TRACE_PKT(pkt, s, ...) \
145  BMLOG_TRACE("[{}] [cxt {}] " s, (pkt).get_unique_id(), \
146  (pkt).get_context(), ##__VA_ARGS__)
147 #define BMLOG_TRACE_SI_PKT(pkt, source_info, s, ...) \
150  BMLOG_TRACE("[{}] [cxt {}] {}" s, (pkt).get_unique_id(), \
151  (pkt).get_context(), \
152  (source_info == nullptr) ? "" \
153  : (source_info->to_string() + " "), \
154  ##__VA_ARGS__)
155 
156 #define BMLOG_ERROR(...) bm::Logger::get()->error(__VA_ARGS__)
157 
158 #define BMLOG_ERROR_PKT(pkt, s, ...) \
159  bm::Logger::get()->error("[{}] [cxt {}] " s, (pkt).get_unique_id(), \
160  (pkt).get_context(), ##__VA_ARGS__)
161 
162 #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, size_t max_size=1024 *1024 *5, size_t max_files=3)
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