Neale Ranns | 812ed39 | 2017-10-16 04:20:13 -0700 | [diff] [blame] | 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 | |
| 19 | #include <boost/algorithm/string.hpp> |
| 20 | |
| 21 | #include "vom/logger.hpp" |
| 22 | |
| 23 | namespace VOM { |
| 24 | const log_level_t log_level_t::CRITICAL(0, "critical"); |
| 25 | const log_level_t log_level_t::ERROR(0, "error"); |
| 26 | const log_level_t log_level_t::WARNING(0, "warning"); |
| 27 | const log_level_t log_level_t::INFO(0, "info"); |
| 28 | const log_level_t log_level_t::DEBUG(0, "debug"); |
| 29 | |
| 30 | log_level_t::log_level_t(int v, const std::string& s) |
| 31 | : enum_base<log_level_t>(v, s) |
| 32 | { |
| 33 | } |
| 34 | |
| 35 | static log_t slog; |
| 36 | |
| 37 | log_t& |
| 38 | logger() |
| 39 | { |
| 40 | return slog; |
| 41 | } |
| 42 | |
| 43 | log_t::log_t() |
| 44 | : m_level(log_level_t::ERROR) |
| 45 | , m_o_stream(&std::cout) |
| 46 | { |
| 47 | } |
| 48 | |
| 49 | void |
| 50 | log_t::set(const log_level_t& level) |
| 51 | { |
| 52 | m_level = level; |
| 53 | } |
| 54 | |
| 55 | void |
| 56 | log_t::set(const std::string& ofile) |
| 57 | { |
| 58 | m_file_stream.open(ofile); |
| 59 | m_o_stream = &m_file_stream; |
| 60 | } |
| 61 | |
| 62 | std::ostream& |
| 63 | log_t::stream(const char* file, int line) |
| 64 | { |
| 65 | auto end = std::chrono::system_clock::now(); |
| 66 | auto end_time = std::chrono::system_clock::to_time_t(end); |
| 67 | |
| 68 | /* |
| 69 | * put-time is not support in gcc in 4.8 |
| 70 | * so we play this dance with ctime |
| 71 | */ |
| 72 | std::string display = std::ctime(&end_time); |
| 73 | display.pop_back(); |
| 74 | |
| 75 | std::vector<std::string> dirs; |
| 76 | boost::split(dirs, file, boost::is_any_of("/")); |
| 77 | |
| 78 | *m_o_stream << std::endl |
| 79 | << display << "]" |
| 80 | << " " << dirs.back() << ":" << line << ": "; |
| 81 | |
| 82 | return (*m_o_stream); |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * The configured level |
| 87 | */ |
| 88 | const log_level_t& |
| 89 | log_t::level() const |
| 90 | { |
| 91 | return (m_level); |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | /* |
| 96 | * fd.io coding-style-patch-verification: ON |
| 97 | * |
| 98 | * Local Variables: |
| 99 | * eval: (c-set-style "mozilla") |
| 100 | * End: |
| 101 | */ |