blob: 7410fe6d5364535922758994fbae5e2787a99aa5 [file] [log] [blame]
Rolf Badorek65fb3b12019-10-02 17:22:45 +03001/*
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
Timo Tietavainena0745d22019-11-28 09:55:22 +020017/*
18 * This source code is part of the near-RT RIC (RAN Intelligent Controller)
19 * platform project (RICP).
20*/
21
Rolf Badorek65fb3b12019-10-02 17:22:45 +030022#include "private/stdstreamlogger.hpp"
23#include <iostream>
24#include <sstream>
25#include <syslog.h>
26#include <boost/iostreams/stream.hpp>
27#include <boost/iostreams/concepts.hpp>
28
29using namespace shareddatalayer;
30
31namespace
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
48std::streamsize Sink::write(const char* s, std::streamsize n)
49{
50 if (level < LOG_ERR)
51 std::cout << prefix << ": " << std::string(s, n);
52 else
53 std::cerr << prefix << ": " << std::string(s, n);
54 return n;
55}
56
57StdStreamLogger::StdStreamLogger(const std::string& prefix):
58 prefix(prefix)
59{
60}
61
62StdStreamLogger::~StdStreamLogger()
63{
64}
65
66std::ostream& StdStreamLogger::emerg()
67{
68 if (osEmerg == nullptr)
69 osEmerg.reset(new boost::iostreams::stream<Sink>(Sink(prefix, LOG_EMERG)));
70 return *osEmerg;
71}
72
73std::ostream& StdStreamLogger::alert()
74{
75 if (osAlert == nullptr)
76 osAlert.reset(new boost::iostreams::stream<Sink>(Sink(prefix, LOG_ALERT)));
77 return *osAlert;
78}
79
80std::ostream& StdStreamLogger::crit()
81{
82 if (osCrit == nullptr)
83 osCrit.reset(new boost::iostreams::stream<Sink>(Sink(prefix, LOG_CRIT)));
84 return *osCrit;
85}
86
87std::ostream& StdStreamLogger::error()
88{
89 if (osError == nullptr)
90 osError.reset(new boost::iostreams::stream<Sink>(Sink(prefix, LOG_ERR)));
91 return *osError;
92}
93
94std::ostream& StdStreamLogger::warning()
95{
96 if (osWarning == nullptr)
97 osWarning.reset(new boost::iostreams::stream<Sink>(Sink(prefix, LOG_WARNING)));
98 return *osWarning;
99}
100
101std::ostream& StdStreamLogger::notice()
102{
103 if (osNotice == nullptr)
104 osNotice.reset(new boost::iostreams::stream<Sink>(Sink(prefix, LOG_NOTICE)));
105 return *osNotice;
106}
107
108std::ostream& StdStreamLogger::info()
109{
110 if (osInfo == nullptr)
111 osInfo.reset(new boost::iostreams::stream<Sink>(Sink(prefix, LOG_INFO)));
112 return *osInfo;
113}
114
115std::ostream& StdStreamLogger::debug()
116{
117 if (osDebug == nullptr)
118 osDebug.reset(new boost::iostreams::stream<Sink>(Sink(prefix, LOG_DEBUG)));
119 return *osDebug;
120}