2 Copyright (c) 2018-2019 Nokia.
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
8 http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include "private/systemlogger.hpp"
21 #include <boost/iostreams/stream.hpp>
22 #include <boost/iostreams/concepts.hpp>
24 using namespace shareddatalayer;
28 class Sink: public boost::iostreams::sink
31 Sink(const std::string& prefix, int level): prefix(prefix), level(level) { }
35 std::streamsize write(const char* s, std::streamsize n);
38 const std::string prefix;
43 std::streamsize Sink::write(const char* s, std::streamsize n)
45 std::ostringstream os;
46 os << "%s: %." << n << 's';
47 syslog(level, os.str().c_str(), prefix.c_str(), s);
51 SystemLogger::SystemLogger(const std::string& prefix):
56 SystemLogger::~SystemLogger()
60 std::ostream& SystemLogger::emerg()
62 if (osEmerg == nullptr)
63 osEmerg.reset(new boost::iostreams::stream<Sink>(Sink(prefix, LOG_EMERG)));
67 std::ostream& SystemLogger::alert()
69 if (osAlert == nullptr)
70 osAlert.reset(new boost::iostreams::stream<Sink>(Sink(prefix, LOG_ALERT)));
74 std::ostream& SystemLogger::crit()
76 if (osCrit == nullptr)
77 osCrit.reset(new boost::iostreams::stream<Sink>(Sink(prefix, LOG_CRIT)));
81 std::ostream& SystemLogger::error()
83 if (osError == nullptr)
84 osError.reset(new boost::iostreams::stream<Sink>(Sink(prefix, LOG_ERR)));
88 std::ostream& SystemLogger::warning()
90 if (osWarning == nullptr)
91 osWarning.reset(new boost::iostreams::stream<Sink>(Sink(prefix, LOG_WARNING)));
95 std::ostream& SystemLogger::notice()
97 if (osNotice == nullptr)
98 osNotice.reset(new boost::iostreams::stream<Sink>(Sink(prefix, LOG_NOTICE)));
102 std::ostream& SystemLogger::info()
104 if (osInfo == nullptr)
105 osInfo.reset(new boost::iostreams::stream<Sink>(Sink(prefix, LOG_INFO)));
109 std::ostream& SystemLogger::debug()
111 if (osDebug == nullptr)
112 osDebug.reset(new boost::iostreams::stream<Sink>(Sink(prefix, LOG_DEBUG)));