Rolf Badorek | 65fb3b1 | 2019-10-02 17:22:45 +0300 | [diff] [blame] | 1 | /* |
| 2 | Copyright (c) 2018-2019 Nokia. |
| 3 | |
| 4 | Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | you may not use this file except in compliance with the License. |
| 6 | You may obtain a copy of the License at |
| 7 | |
| 8 | http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | |
| 10 | Unless required by applicable law or agreed to in writing, software |
| 11 | distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | See the License for the specific language governing permissions and |
| 14 | limitations under the License. |
| 15 | */ |
| 16 | |
Timo Tietavainen | a0745d2 | 2019-11-28 09:55:22 +0200 | [diff] [blame] | 17 | /* |
| 18 | * This source code is part of the near-RT RIC (RAN Intelligent Controller) |
| 19 | * platform project (RICP). |
| 20 | */ |
| 21 | |
Rolf Badorek | 65fb3b1 | 2019-10-02 17:22:45 +0300 | [diff] [blame] | 22 | #include "private/stdstreamlogger.hpp" |
| 23 | #include <iostream> |
| 24 | #include <sstream> |
| 25 | #include <syslog.h> |
| 26 | #include <boost/iostreams/stream.hpp> |
| 27 | #include <boost/iostreams/concepts.hpp> |
| 28 | |
| 29 | using namespace shareddatalayer; |
| 30 | |
| 31 | namespace |
| 32 | { |
| 33 | class Sink: public boost::iostreams::sink |
| 34 | { |
| 35 | public: |
| 36 | Sink(const std::string& prefix, int level): prefix(prefix), level(level) { } |
| 37 | |
| 38 | ~Sink() { } |
| 39 | |
| 40 | std::streamsize write(const char* s, std::streamsize n); |
| 41 | |
| 42 | private: |
| 43 | const std::string prefix; |
| 44 | const int level; |
| 45 | }; |
| 46 | } |
| 47 | |
| 48 | std::streamsize Sink::write(const char* s, std::streamsize n) |
| 49 | { |
| 50 | if (level < LOG_ERR) |
| 51 | std::cout << prefix << ": " << std::string(s, n); |
| 52 | else |
| 53 | std::cerr << prefix << ": " << std::string(s, n); |
| 54 | return n; |
| 55 | } |
| 56 | |
| 57 | StdStreamLogger::StdStreamLogger(const std::string& prefix): |
| 58 | prefix(prefix) |
| 59 | { |
| 60 | } |
| 61 | |
| 62 | StdStreamLogger::~StdStreamLogger() |
| 63 | { |
| 64 | } |
| 65 | |
| 66 | std::ostream& StdStreamLogger::emerg() |
| 67 | { |
| 68 | if (osEmerg == nullptr) |
| 69 | osEmerg.reset(new boost::iostreams::stream<Sink>(Sink(prefix, LOG_EMERG))); |
| 70 | return *osEmerg; |
| 71 | } |
| 72 | |
| 73 | std::ostream& StdStreamLogger::alert() |
| 74 | { |
| 75 | if (osAlert == nullptr) |
| 76 | osAlert.reset(new boost::iostreams::stream<Sink>(Sink(prefix, LOG_ALERT))); |
| 77 | return *osAlert; |
| 78 | } |
| 79 | |
| 80 | std::ostream& StdStreamLogger::crit() |
| 81 | { |
| 82 | if (osCrit == nullptr) |
| 83 | osCrit.reset(new boost::iostreams::stream<Sink>(Sink(prefix, LOG_CRIT))); |
| 84 | return *osCrit; |
| 85 | } |
| 86 | |
| 87 | std::ostream& StdStreamLogger::error() |
| 88 | { |
| 89 | if (osError == nullptr) |
| 90 | osError.reset(new boost::iostreams::stream<Sink>(Sink(prefix, LOG_ERR))); |
| 91 | return *osError; |
| 92 | } |
| 93 | |
| 94 | std::ostream& StdStreamLogger::warning() |
| 95 | { |
| 96 | if (osWarning == nullptr) |
| 97 | osWarning.reset(new boost::iostreams::stream<Sink>(Sink(prefix, LOG_WARNING))); |
| 98 | return *osWarning; |
| 99 | } |
| 100 | |
| 101 | std::ostream& StdStreamLogger::notice() |
| 102 | { |
| 103 | if (osNotice == nullptr) |
| 104 | osNotice.reset(new boost::iostreams::stream<Sink>(Sink(prefix, LOG_NOTICE))); |
| 105 | return *osNotice; |
| 106 | } |
| 107 | |
| 108 | std::ostream& StdStreamLogger::info() |
| 109 | { |
| 110 | if (osInfo == nullptr) |
| 111 | osInfo.reset(new boost::iostreams::stream<Sink>(Sink(prefix, LOG_INFO))); |
| 112 | return *osInfo; |
| 113 | } |
| 114 | |
| 115 | std::ostream& StdStreamLogger::debug() |
| 116 | { |
| 117 | if (osDebug == nullptr) |
| 118 | osDebug.reset(new boost::iostreams::stream<Sink>(Sink(prefix, LOG_DEBUG))); |
| 119 | return *osDebug; |
| 120 | } |