blob: 80f2d92c6035a6293cf2aa63adbc6e34dc4b5c12 [file] [log] [blame]
Neale Ranns812ed392017-10-16 04:20:13 -07001/*
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>
Damjan Marion6e3b5a22017-11-01 11:20:50 +010018#include <vector>
Neale Ranns812ed392017-10-16 04:20:13 -070019
20#include <boost/algorithm/string.hpp>
21
22#include "vom/logger.hpp"
23
24namespace VOM {
Neale Rannsc3a2a2f2017-11-08 09:00:44 -080025const log_level_t log_level_t::CRITICAL(4, "critical");
26const log_level_t log_level_t::ERROR(3, "error");
27const log_level_t log_level_t::WARNING(2, "warning");
28const log_level_t log_level_t::INFO(1, "info");
Neale Ranns812ed392017-10-16 04:20:13 -070029const log_level_t log_level_t::DEBUG(0, "debug");
30
31log_level_t::log_level_t(int v, const std::string& s)
32 : enum_base<log_level_t>(v, s)
33{
34}
35
36static log_t slog;
37
38log_t&
39logger()
40{
41 return slog;
42}
43
44log_t::log_t()
45 : m_level(log_level_t::ERROR)
Neale Rannsa2ee0292017-11-28 22:29:13 -080046 , m_handler(new cout_handler())
Neale Ranns812ed392017-10-16 04:20:13 -070047{
48}
49
50void
51log_t::set(const log_level_t& level)
52{
53 m_level = level;
54}
55
56void
Neale Rannsa2ee0292017-11-28 22:29:13 -080057log_t::set(handler* h)
Neale Ranns812ed392017-10-16 04:20:13 -070058{
Neale Rannsa2ee0292017-11-28 22:29:13 -080059 m_handler = h;
Neale Ranns812ed392017-10-16 04:20:13 -070060}
61
Neale Rannsa2ee0292017-11-28 22:29:13 -080062void
63log_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)
Neale Ranns812ed392017-10-16 04:20:13 -070068{
Neale Rannsa2ee0292017-11-28 22:29:13 -080069 m_handler->handle_message(file, line, function, level, message);
Neale Ranns812ed392017-10-16 04:20:13 -070070}
71
72/**
73 * The configured level
74 */
75const log_level_t&
76log_t::level() const
77{
78 return (m_level);
79}
Neale Rannsa2ee0292017-11-28 22:29:13 -080080
81static std::string
82get_filename(const std::string& file)
83{
84 std::vector<std::string> dirs;
85 boost::split(dirs, file, boost::is_any_of("/"));
86
87 return dirs.back();
Neale Ranns812ed392017-10-16 04:20:13 -070088}
89
Neale Rannsa2ee0292017-11-28 22:29:13 -080090log_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
101log_t::entry::~entry()
102{
103 logger().write(m_file, m_line, m_function, m_level, m_stream.str());
104}
105
106std::stringstream&
107log_t::entry::stream()
108{
109 return (m_stream);
110}
111
112static std::string
113get_timestamp()
114{
115 auto end = std::chrono::system_clock::now();
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
128file_handler::file_handler(const std::string& ofile)
129{
130 m_file_stream.open(ofile);
131}
132
133file_handler::~file_handler()
134{
135 m_file_stream.close();
136}
137
138void
139file_handler::handle_message(const std::string& file,
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
150void
151cout_handler::handle_message(const std::string& file,
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
Neale Ranns812ed392017-10-16 04:20:13 -0700164/*
165 * fd.io coding-style-patch-verification: ON
166 *
167 * Local Variables:
168 * eval: (c-set-style "mozilla")
169 * End:
170 */