2 * Copyright (c) 2017 Cisco and/or its affiliates.
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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 #ifndef __VOM_LOGGER_H__
17 #define __VOM_LOGGER_H__
23 #include "vom/enum_base.hpp"
26 struct log_level_t : enum_base<log_level_t>
28 const static log_level_t CRITICAL;
29 const static log_level_t ERROR;
30 const static log_level_t WARNING;
31 const static log_level_t INFO;
32 const static log_level_t DEBUG;
36 * Private constructor taking the value and the string name
38 log_level_t(int v, const std::string& s);
41 * not allowed to construct
43 log_level_t() = delete;
47 * Ideally we'd use the boost logger but that is not prevelent
48 * in many distros. So something simple here instead.
67 virtual ~handler() = default;
70 * Handle a log message
72 virtual void handle_message(const std::string& file,
74 const std::string& function,
75 const log_level_t& level,
76 const std::string& message) = 0;
86 * The configured level
88 const log_level_t& level() const;
91 * set the logging level
93 void set(const log_level_t& level);
96 * set a file to receive the logging data
101 * An entry in the log
106 entry(const char* file,
107 const char* function,
109 const log_level_t& level);
112 std::stringstream& stream();
115 const std::string m_file;
116 const std::string m_function;
117 const log_level_t m_level;
120 std::stringstream m_stream;
123 * Register a log handler to receive the log output
125 void register_handler(handler& h);
128 void write(const std::string& file,
130 const std::string& function,
131 const log_level_t& level,
132 const std::string& message);
135 * the configured logging level
140 * Pointer to a registered handler. Null if no handler registerd
145 class file_handler : public log_t::handler
148 file_handler(const std::string& ofile);
151 virtual void handle_message(const std::string& file,
153 const std::string& function,
154 const log_level_t& level,
155 const std::string& message);
159 * Opened file for debugging
161 std::ofstream m_file_stream;
164 class cout_handler : public log_t::handler
167 cout_handler() = default;
168 ~cout_handler() = default;
169 virtual void handle_message(const std::string& file,
171 const std::string& function,
172 const log_level_t& level,
173 const std::string& message);
177 * Return a log object into which VPP objects can write
181 #define VOM_LOG(lvl) \
182 if (lvl >= logger().level()) \
183 log_t::entry(__FILE__, __FUNCTION__, __LINE__, lvl).stream()
187 * fd.io coding-style-patch-verification: ON
190 * eval: (c-set-style "mozilla")