Add standard stream logger
[ric-plt/sdl.git] / src / stdstreamlogger.cpp
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
17 #include "private/stdstreamlogger.hpp"
18 #include <iostream>
19 #include <sstream>
20 #include <syslog.h>
21 #include <boost/iostreams/stream.hpp>
22 #include <boost/iostreams/concepts.hpp>
23
24 using namespace shareddatalayer;
25
26 namespace
27 {
28     class Sink: public boost::iostreams::sink
29     {
30     public:
31         Sink(const std::string& prefix, int level): prefix(prefix), level(level) { }
32
33         ~Sink() { }
34
35         std::streamsize write(const char* s, std::streamsize n);
36
37     private:
38         const std::string prefix;
39         const int level;
40     };
41 }
42
43 std::streamsize Sink::write(const char* s, std::streamsize n)
44 {
45     if (level < LOG_ERR)
46         std::cout << prefix << ": " << std::string(s, n);
47     else
48         std::cerr << prefix << ": " << std::string(s, n);
49     return n;
50 }
51
52 StdStreamLogger::StdStreamLogger(const std::string& prefix):
53     prefix(prefix)
54 {
55 }
56
57 StdStreamLogger::~StdStreamLogger()
58 {
59 }
60
61 std::ostream& StdStreamLogger::emerg()
62 {
63     if (osEmerg == nullptr)
64         osEmerg.reset(new boost::iostreams::stream<Sink>(Sink(prefix, LOG_EMERG)));
65     return *osEmerg;
66 }
67
68 std::ostream& StdStreamLogger::alert()
69 {
70     if (osAlert == nullptr)
71         osAlert.reset(new boost::iostreams::stream<Sink>(Sink(prefix, LOG_ALERT)));
72     return *osAlert;
73 }
74
75 std::ostream& StdStreamLogger::crit()
76 {
77     if (osCrit == nullptr)
78         osCrit.reset(new boost::iostreams::stream<Sink>(Sink(prefix, LOG_CRIT)));
79     return *osCrit;
80 }
81
82 std::ostream& StdStreamLogger::error()
83 {
84     if (osError == nullptr)
85         osError.reset(new boost::iostreams::stream<Sink>(Sink(prefix, LOG_ERR)));
86     return *osError;
87 }
88
89 std::ostream& StdStreamLogger::warning()
90 {
91     if (osWarning == nullptr)
92         osWarning.reset(new boost::iostreams::stream<Sink>(Sink(prefix, LOG_WARNING)));
93     return *osWarning;
94 }
95
96 std::ostream& StdStreamLogger::notice()
97 {
98     if (osNotice == nullptr)
99         osNotice.reset(new boost::iostreams::stream<Sink>(Sink(prefix, LOG_NOTICE)));
100     return *osNotice;
101 }
102
103 std::ostream& StdStreamLogger::info()
104 {
105     if (osInfo == nullptr)
106         osInfo.reset(new boost::iostreams::stream<Sink>(Sink(prefix, LOG_INFO)));
107     return *osInfo;
108 }
109
110 std::ostream& StdStreamLogger::debug()
111 {
112     if (osDebug == nullptr)
113         osDebug.reset(new boost::iostreams::stream<Sink>(Sink(prefix, LOG_DEBUG)));
114     return *osDebug;
115 }