sptk2 logo
SPTK Home Page
LogEngine.h
1/*
2╔══════════════════════════════════════════════════════════════════════════════╗
3║ SIMPLY POWERFUL TOOLKIT (SPTK) ║
4╟──────────────────────────────────────────────────────────────────────────────╢
5║ copyright © 1999-2022 Alexey Parshin. All rights reserved. ║
6║ email alexeyp@gmail.com ║
7╚══════════════════════════════════════════════════════════════════════════════╝
8┌──────────────────────────────────────────────────────────────────────────────┐
9│ This library is free software; you can redistribute it and/or modify it │
10│ under the terms of the GNU Library General Public License as published by │
11│ the Free Software Foundation; either version 2 of the License, or (at your │
12│ option) any later version. │
13│ │
14│ This library is distributed in the hope that it will be useful, but │
15│ WITHOUT ANY WARRANTY; without even the implied warranty of │
16│ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library │
17│ General Public License for more details. │
18│ │
19│ You should have received a copy of the GNU Library General Public License │
20│ along with this library; if not, write to the Free Software Foundation, │
21│ Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. │
22│ │
23│ Please report all bugs and problems to alexeyp@gmail.com. │
24└──────────────────────────────────────────────────────────────────────────────┘
25*/
26
27#pragma once
28
29#include <sptk5/DateTime.h>
30#include <sptk5/LogPriority.h>
31#include <sptk5/Logger.h>
32#include <sptk5/threads/SynchronizedQueue.h>
33
34#include <atomic>
35#include <iostream>
36#include <sptk5/threads/Thread.h>
37
38namespace sptk {
39
45// Log options
46constexpr int LO_STDOUT = 1;
47constexpr int LO_DATE = 2;
48constexpr int LO_TIME = 4;
49constexpr int LO_PRIORITY = 8;
50constexpr int LO_ENABLE = 16;
51
58class SP_EXPORT LogEngine
59 : public Thread
60{
61 friend class Logger;
62
63public:
68 virtual void saveMessage(const Logger::UMessage& message) = 0;
69
75 explicit LogEngine(const String& logEngineName);
76
80 ~LogEngine() noexcept override;
81
88 virtual void reset()
89 {
90 // Implement in derived class
91 }
92
97 void options(int ops)
98 {
99 m_options = ops;
100 }
101
106 size_t options() const
107 {
108 return m_options;
109 }
110
116 void option(int options, bool flag);
117
123 {
124 m_minPriority = prt;
125 }
126
133 virtual void minPriority(LogPriority prt)
134 {
135 m_minPriority = prt;
136 }
137
143 virtual LogPriority minPriority() const
144 {
145 return m_minPriority;
146 }
147
152
157
158protected:
159 void threadFunction() override;
160
165 void log(Logger::UMessage& message);
166
170 void shutdown() noexcept;
171
172private:
176 mutable SharedMutex m_mutex;
177
181 std::atomic<LogPriority> m_minPriority {LogPriority::INFO};
182
186 std::atomic<uint32_t> m_options {LO_ENABLE | LO_DATE | LO_TIME | LO_PRIORITY};
187
188 using MessageQueue = SynchronizedQueue<Logger::UMessage>;
192 MessageQueue m_messages;
193};
194
198} // namespace sptk
Definition: LogEngine.h:60
void priority(LogPriority prt)
Definition: LogEngine.h:122
void threadFunction() override
static String priorityName(LogPriority prt)
virtual LogPriority minPriority() const
Definition: LogEngine.h:143
void options(int ops)
Definition: LogEngine.h:97
virtual void saveMessage(const Logger::UMessage &message)=0
LogEngine(const String &logEngineName)
void option(int options, bool flag)
size_t options() const
Definition: LogEngine.h:106
void log(Logger::UMessage &message)
void shutdown() noexcept
static LogPriority priorityFromName(const String &prt)
virtual void minPriority(LogPriority prt)
Definition: LogEngine.h:133
~LogEngine() noexcept override
A log that sends all the log messages into another log.
Definition: Logger.h:53
Definition: String.h:49
Definition: Thread.h:51
constexpr int LO_DATE
Print date for every log message.
Definition: LogEngine.h:47
constexpr int LO_ENABLE
Enable logging (doesn't affect stdout if CLO_STDOUT is on)
Definition: LogEngine.h:50
LogPriority
Log message priority.
Definition: LogPriority.h:68
constexpr int LO_TIME
Print time for every log message.
Definition: LogEngine.h:48
constexpr int LO_PRIORITY
Print message priority.
Definition: LogEngine.h:49
constexpr int LO_STDOUT
Duplicate messages to stdout.
Definition: LogEngine.h:46

Fri Oct 14 2022 09:58:31: SPTK 5.4.1