RIC:1060: Change in PTL
[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 /*
18  * This source code is part of the near-RT RIC (RAN Intelligent Controller)
19  * platform project (RICP).
20 */
21
22 #include "private/systemlogger.hpp"
23 #include <ostream>
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     std::ostringstream os;
51     os << "%s: %." << n << 's';
52     syslog(level, os.str().c_str(), prefix.c_str(), s);
53     return n;
54 }
55
56 SystemLogger::SystemLogger(const std::string& prefix):
57     prefix(prefix)
58 {
59 }
60
61 SystemLogger::~SystemLogger()
62 {
63 }
64
65 std::ostream& SystemLogger::emerg()
66 {
67     if (osEmerg == nullptr)
68         osEmerg.reset(new boost::iostreams::stream<Sink>(Sink(prefix, LOG_EMERG)));
69     return *osEmerg;
70 }
71
72 std::ostream& SystemLogger::alert()
73 {
74     if (osAlert == nullptr)
75         osAlert.reset(new boost::iostreams::stream<Sink>(Sink(prefix, LOG_ALERT)));
76     return *osAlert;
77 }
78
79 std::ostream& SystemLogger::crit()
80 {
81     if (osCrit == nullptr)
82         osCrit.reset(new boost::iostreams::stream<Sink>(Sink(prefix, LOG_CRIT)));
83     return *osCrit;
84 }
85
86 std::ostream& SystemLogger::error()
87 {
88     if (osError == nullptr)
89         osError.reset(new boost::iostreams::stream<Sink>(Sink(prefix, LOG_ERR)));
90     return *osError;
91 }
92
93 std::ostream& SystemLogger::warning()
94 {
95     if (osWarning == nullptr)
96         osWarning.reset(new boost::iostreams::stream<Sink>(Sink(prefix, LOG_WARNING)));
97     return *osWarning;
98 }
99
100 std::ostream& SystemLogger::notice()
101 {
102     if (osNotice == nullptr)
103         osNotice.reset(new boost::iostreams::stream<Sink>(Sink(prefix, LOG_NOTICE)));
104     return *osNotice;
105 }
106
107 std::ostream& SystemLogger::info()
108 {
109     if (osInfo == nullptr)
110         osInfo.reset(new boost::iostreams::stream<Sink>(Sink(prefix, LOG_INFO)));
111     return *osInfo;
112 }
113
114 std::ostream& SystemLogger::debug()
115 {
116     if (osDebug == nullptr)
117         osDebug.reset(new boost::iostreams::stream<Sink>(Sink(prefix, LOG_DEBUG)));
118     return *osDebug;
119 }