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/stdstreamlogger.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)
46 std::cout << prefix << ": " << std::string(s, n);
48 std::cerr << prefix << ": " << std::string(s, n);
52 StdStreamLogger::StdStreamLogger(const std::string& prefix):
57 StdStreamLogger::~StdStreamLogger()
61 std::ostream& StdStreamLogger::emerg()
63 if (osEmerg == nullptr)
64 osEmerg.reset(new boost::iostreams::stream<Sink>(Sink(prefix, LOG_EMERG)));
68 std::ostream& StdStreamLogger::alert()
70 if (osAlert == nullptr)
71 osAlert.reset(new boost::iostreams::stream<Sink>(Sink(prefix, LOG_ALERT)));
75 std::ostream& StdStreamLogger::crit()
77 if (osCrit == nullptr)
78 osCrit.reset(new boost::iostreams::stream<Sink>(Sink(prefix, LOG_CRIT)));
82 std::ostream& StdStreamLogger::error()
84 if (osError == nullptr)
85 osError.reset(new boost::iostreams::stream<Sink>(Sink(prefix, LOG_ERR)));
89 std::ostream& StdStreamLogger::warning()
91 if (osWarning == nullptr)
92 osWarning.reset(new boost::iostreams::stream<Sink>(Sink(prefix, LOG_WARNING)));
96 std::ostream& StdStreamLogger::notice()
98 if (osNotice == nullptr)
99 osNotice.reset(new boost::iostreams::stream<Sink>(Sink(prefix, LOG_NOTICE)));
103 std::ostream& StdStreamLogger::info()
105 if (osInfo == nullptr)
106 osInfo.reset(new boost::iostreams::stream<Sink>(Sink(prefix, LOG_INFO)));
110 std::ostream& StdStreamLogger::debug()
112 if (osDebug == nullptr)
113 osDebug.reset(new boost::iostreams::stream<Sink>(Sink(prefix, LOG_DEBUG)));