8b8336b6c6446646d824ffe3d46f7f170247d781
[ric-plt/sdl.git] / src / systemlogger.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/systemlogger.hpp"
18 #include <ostream>
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     std::ostringstream os;
46     os << "%s: %." << n << 's';
47     syslog(level, os.str().c_str(), prefix.c_str(), s);
48     return n;
49 }
50
51 SystemLogger::SystemLogger(const std::string& prefix):
52     prefix(prefix)
53 {
54 }
55
56 SystemLogger::~SystemLogger()
57 {
58 }
59
60 std::ostream& SystemLogger::emerg()
61 {
62     if (osEmerg == nullptr)
63         osEmerg.reset(new boost::iostreams::stream<Sink>(Sink(prefix, LOG_EMERG)));
64     return *osEmerg;
65 }
66
67 std::ostream& SystemLogger::alert()
68 {
69     if (osAlert == nullptr)
70         osAlert.reset(new boost::iostreams::stream<Sink>(Sink(prefix, LOG_ALERT)));
71     return *osAlert;
72 }
73
74 std::ostream& SystemLogger::crit()
75 {
76     if (osCrit == nullptr)
77         osCrit.reset(new boost::iostreams::stream<Sink>(Sink(prefix, LOG_CRIT)));
78     return *osCrit;
79 }
80
81 std::ostream& SystemLogger::error()
82 {
83     if (osError == nullptr)
84         osError.reset(new boost::iostreams::stream<Sink>(Sink(prefix, LOG_ERR)));
85     return *osError;
86 }
87
88 std::ostream& SystemLogger::warning()
89 {
90     if (osWarning == nullptr)
91         osWarning.reset(new boost::iostreams::stream<Sink>(Sink(prefix, LOG_WARNING)));
92     return *osWarning;
93 }
94
95 std::ostream& SystemLogger::notice()
96 {
97     if (osNotice == nullptr)
98         osNotice.reset(new boost::iostreams::stream<Sink>(Sink(prefix, LOG_NOTICE)));
99     return *osNotice;
100 }
101
102 std::ostream& SystemLogger::info()
103 {
104     if (osInfo == nullptr)
105         osInfo.reset(new boost::iostreams::stream<Sink>(Sink(prefix, LOG_INFO)));
106     return *osInfo;
107 }
108
109 std::ostream& SystemLogger::debug()
110 {
111     if (osDebug == nullptr)
112         osDebug.reset(new boost::iostreams::stream<Sink>(Sink(prefix, LOG_DEBUG)));
113     return *osDebug;
114 }