FD.io VPP  v21.06-3-gbb25fbf28
Vector Packet Processing
logger.cpp
Go to the documentation of this file.
1 /*
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:
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 #include <chrono>
17 #include <ctime>
18 #include <vector>
19 
20 #include <boost/algorithm/string.hpp>
21 
22 #include "vom/logger.hpp"
23 
24 namespace VOM {
25 const log_level_t log_level_t::CRITICAL(4, "critical");
26 const log_level_t log_level_t::ERROR(3, "error");
27 const log_level_t log_level_t::WARNING(2, "warning");
28 const log_level_t log_level_t::INFO(1, "info");
29 const log_level_t log_level_t::DEBUG(0, "debug");
30 
31 log_level_t::log_level_t(int v, const std::string& s)
32  : enum_base<log_level_t>(v, s)
33 {
34 }
35 
36 static log_t slog;
37 
38 log_t&
40 {
41  return slog;
42 }
43 
45  : m_level(log_level_t::ERROR)
46  , m_handler(new cout_handler())
47 {
48 }
49 
50 void
51 log_t::set(const log_level_t& level)
52 {
53  m_level = level;
54 }
55 
56 void
58 {
59  m_handler = h;
60 }
61 
62 void
63 log_t::write(const std::string& file,
64  const int line,
65  const std::string& function,
66  const log_level_t& level,
67  const std::string& message)
68 {
69  m_handler->handle_message(file, line, function, level, message);
70 }
71 
72 /**
73  * The configured level
74  */
75 const log_level_t&
76 log_t::level() const
77 {
78  return (m_level);
79 }
80 
81 static std::string
83 {
84  std::vector<std::string> dirs;
85  boost::split(dirs, file, boost::is_any_of("/"));
86 
87  return dirs.back();
88 }
89 
90 log_t::entry::entry(const char* file,
91  const char* function,
92  int line,
93  const log_level_t& level)
94  : m_file(get_filename(file))
95  , m_function(function)
96  , m_level(level)
97  , m_line(line)
98 {
99 }
100 
102 {
103  logger().write(m_file, m_line, m_function, m_level, m_stream.str());
104 }
105 
106 std::stringstream&
108 {
109  return (m_stream);
110 }
111 
112 static std::string
114 {
116  auto end_time = std::chrono::system_clock::to_time_t(end);
117 
118  /*
119  * put-time is not support in gcc in 4.8
120  * so we play this dance with ctime
121  */
122  std::string display = std::ctime(&end_time);
123  display.pop_back();
124 
125  return (display);
126 }
127 
129 {
130  m_file_stream.open(ofile);
131 }
132 
134 {
135  m_file_stream.close();
136 }
137 
138 void
140  const int line,
141  const std::string& function,
142  const log_level_t& level,
143  const std::string& message)
144 {
145  m_file_stream << get_timestamp();
146  m_file_stream << " [" << level.to_string() << "]" << file << ":" << line
147  << " " << function << "() " << message << std::endl;
148 }
149 
150 void
152  const int line,
153  const std::string& function,
154  const log_level_t& level,
155  const std::string& message)
156 {
157  std::cout << get_timestamp();
158  std::cout << " [" << level.to_string() << "]" << file << ":" << line << " "
159  << function << "() " << message << std::endl;
160 }
161 
162 }; // namespace VOM
163 
164 /*
165  * fd.io coding-style-patch-verification: OFF
166  *
167  * Local Variables:
168  * eval: (c-set-style "mozilla")
169  * End:
170  */
VOM
The VPP Object Model (VOM) library.
Definition: acl_binding.cpp:19
VOM::logger
log_t & logger()
Return a log object into which VPP objects can write.
Definition: logger.cpp:39
VOM::cout_handler::handle_message
virtual void handle_message(const std::string &file, const int line, const std::string &function, const log_level_t &level, const std::string &message)
Handle a log message.
Definition: logger.cpp:151
VOM::log_level_t::INFO
const static log_level_t INFO
Definition: logger.hpp:31
VOM::log_t::entry::entry
entry(const char *file, const char *function, int line, const log_level_t &level)
Definition: logger.cpp:90
VOM::log_t::handler::handle_message
virtual void handle_message(const std::string &file, const int line, const std::string &function, const log_level_t &level, const std::string &message)=0
Handle a log message.
VOM::get_filename
static std::string get_filename(const std::string &file)
Definition: logger.cpp:82
h
h
Definition: flowhash_template.h:372
log_level_t
log_level_t
Definition: ip6_ra.c:218
VOM::log_t::entry::~entry
~entry()
Definition: logger.cpp:101
end
f64 end
end of the time range
Definition: mactime.api:44
VOM::get_timestamp
static std::string get_timestamp()
Definition: logger.cpp:113
VOM::log_t::handler
Definition: logger.hpp:56
VOM::log_t
Ideally we'd use the boost logger but that is not prevelent in many distros.
Definition: logger.hpp:50
new
vl_api_cnat_endpoint_t new
Definition: cnat.api:119
VOM::log_level_t::DEBUG
const static log_level_t DEBUG
Definition: logger.hpp:32
function
u8 function
Definition: pci_types.api:23
VOM::log_level_t
Definition: logger.hpp:26
VOM::log_level_t::CRITICAL
const static log_level_t CRITICAL
Definition: logger.hpp:28
VOM::enum_base::to_string
const std::string & to_string() const
convert to string format for debug purposes
Definition: enum_base.hpp:36
VOM::slog
static log_t slog
Definition: logger.cpp:36
VOM::log_t::log_t
log_t()
Definition: logger.cpp:44
VOM::log_level_t::WARNING
const static log_level_t WARNING
Definition: logger.hpp:30
logger.hpp
now
f64 now
Definition: nat44_ei_out2in.c:710
VOM::cout_handler
Definition: logger.hpp:164
VOM::log_t::level
const log_level_t & level() const
The configured level.
Definition: logger.cpp:76
VOM::file_handler::file_handler
file_handler(const std::string &ofile)
Definition: logger.cpp:128
VOM::file_handler::~file_handler
~file_handler()
Definition: logger.cpp:133
VOM::log_level_t::ERROR
const static log_level_t ERROR
Definition: logger.hpp:29
VOM::log_t::set
void set(const log_level_t &level)
set the logging level
Definition: logger.cpp:51
VOM::file_handler::handle_message
virtual void handle_message(const std::string &file, const int line, const std::string &function, const log_level_t &level, const std::string &message)
Handle a log message.
Definition: logger.cpp:139
VOM::log_t::entry::stream
std::stringstream & stream()
Definition: logger.cpp:107
string
const char *const string
Definition: cJSON.h:172