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.
20 #include <boost/algorithm/string.hpp>
22 #include "vom/logger.hpp"
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");
31 log_level_t::log_level_t(int v, const std::string& s)
32 : enum_base<log_level_t>(v, s)
45 : m_level(log_level_t::ERROR)
46 , m_handler(new cout_handler())
51 log_t::set(const log_level_t& level)
57 log_t::set(handler* h)
63 log_t::write(const std::string& file,
65 const std::string& function,
66 const log_level_t& level,
67 const std::string& message)
69 m_handler->handle_message(file, line, function, level, message);
73 * The configured level
82 get_filename(const std::string& file)
84 std::vector<std::string> dirs;
85 boost::split(dirs, file, boost::is_any_of("/"));
90 log_t::entry::entry(const char* file,
93 const log_level_t& level)
94 : m_file(get_filename(file))
95 , m_function(function)
101 log_t::entry::~entry()
103 logger().write(m_file, m_line, m_function, m_level, m_stream.str());
107 log_t::entry::stream()
115 auto end = std::chrono::system_clock::now();
116 auto end_time = std::chrono::system_clock::to_time_t(end);
119 * put-time is not support in gcc in 4.8
120 * so we play this dance with ctime
122 std::string display = std::ctime(&end_time);
128 file_handler::file_handler(const std::string& ofile)
130 m_file_stream.open(ofile);
133 file_handler::~file_handler()
135 m_file_stream.close();
139 file_handler::handle_message(const std::string& file,
141 const std::string& function,
142 const log_level_t& level,
143 const std::string& message)
145 m_file_stream << get_timestamp();
146 m_file_stream << " [" << level.to_string() << "]" << file << ":" << line
147 << " " << function << "() " << message << std::endl;
151 cout_handler::handle_message(const std::string& file,
153 const std::string& function,
154 const log_level_t& level,
155 const std::string& message)
157 std::cout << get_timestamp();
158 std::cout << " [" << level.to_string() << "]" << file << ":" << line << " "
159 << function << "() " << message << std::endl;
165 * fd.io coding-style-patch-verification: ON
168 * eval: (c-set-style "mozilla")