Line data Source code
1 : /*
2 : * If not stated otherwise in this file or this component's LICENSE file the
3 : * following copyright and licenses apply:
4 : *
5 : * Copyright 2023 Sky UK
6 : *
7 : * Licensed under the Apache License, Version 2.0 (the "License");
8 : * you may not use this file except in compliance with the License.
9 : * You may obtain a copy of the License at
10 : *
11 : * http://www.apache.org/licenses/LICENSE-2.0
12 : *
13 : * Unless required by applicable law or agreed to in writing, software
14 : * distributed under the License is distributed on an "AS IS" BASIS,
15 : * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 : * See the License for the specific language governing permissions and
17 : * limitations under the License.
18 : */
19 :
20 : #ifndef LOGGER_H_
21 : #define LOGGER_H_
22 :
23 : #include <fstream>
24 : #include <mutex>
25 : #include <sstream>
26 : #include <string>
27 :
28 : enum Severity
29 : {
30 : fatal = 0,
31 : error = 1,
32 : warn = 2,
33 : mil = 3,
34 : info = 4,
35 : debug = 5
36 : };
37 :
38 : class LogFile
39 : {
40 : public:
41 : static LogFile &instance();
42 : bool write(const std::string &line);
43 : bool isEnabled() const;
44 : void reset();
45 :
46 : private:
47 : LogFile();
48 : ~LogFile();
49 :
50 : void tryOpenFile();
51 : void tryCloseFile();
52 :
53 : private:
54 : std::fstream m_file;
55 : std::mutex m_mutex;
56 : };
57 :
58 : class Logger;
59 : class Flusher
60 : {
61 : public:
62 : Flusher(std::stringstream &stream, const std::string &componentName, const Severity &severity);
63 : ~Flusher();
64 :
65 823 : template <typename T> Flusher &operator<<(const T &text)
66 : {
67 823 : m_stream << text;
68 823 : return *this;
69 : }
70 :
71 : private:
72 : std::stringstream &m_stream;
73 : Severity m_severity;
74 : };
75 :
76 : class Logger
77 : {
78 : public:
79 : explicit Logger(const std::string &componentName);
80 : Flusher operator<<(const Severity &) const;
81 :
82 : private:
83 : const std::string m_componentName;
84 : mutable std::stringstream m_stream;
85 : };
86 :
87 : #endif // LOGGER_H_
|