OpenCPN Partial API docs
Loading...
Searching...
No Matches
logger.h
Go to the documentation of this file.
1
2/**************************************************************************
3 * Copyright (C) 2013 David S. Register *
4 * Copyright (C) 2022 - 2024 Alec Leamas *
5 * *
6 * This program is free software; you can redistribute it and/or modify *
7 * it under the terms of the GNU General Public License as published by *
8 * the Free Software Foundation; either version 2 of the License, or *
9 * (at your option) any later version. *
10 * *
11 * This program is distributed in the hope that it will be useful, *
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14 * GNU General Public License for more details. *
15 * *
16 * You should have received a copy of the GNU General Public License *
17 * along with this program; if not, see <https://www.gnu.org/licenses/>. *
18 ***************************************************************************/
19
43#ifndef LOGGER_H
44#define LOGGER_H
45
46#include <chrono>
47#include <fstream>
48#include <ostream>
49#include <sstream>
50#include <string>
51
52#include <wx/log.h>
53
54#define DO_LOG_MESSAGE(level, fmt, ...) \
55 { \
56 if (level <= wxLog::GetLogLevel()) { \
57 Logger::logMessage(level, __FILE__, __LINE__, fmt, ##__VA_ARGS__); \
58 } \
59 }
60
61#define _LOG(level) \
62 if (level > wxLog::GetLogLevel()) \
63 ; \
64 else \
65 Logger().get(level, __FILE__, __LINE__)
66
67#define TRACE_LOG _LOG(wxLOG_Trace)
68#define DEBUG_LOG _LOG(wxLOG_Debug)
69#define INFO_LOG _LOG(wxLOG_Info)
70#define MESSAGE_LOG _LOG(wxLOG_Message)
71#define WARNING_LOG _LOG(wxLOG_Warning)
72#define ERROR_LOG _LOG(wxLOG_Error)
73
74#define LOG_TRACE(fmt, ...) DO_LOG_MESSAGE(wxLOG_Trace, fmt, ##__VA_ARGS__);
75#define LOG_DEBUG(fmt, ...) DO_LOG_MESSAGE(wxLOG_Debug, fmt, ##__VA_ARGS__);
76#define LOG_INFO(fmt, ...) DO_LOG_MESSAGE(wxLOG_Info, fmt, ##__VA_ARGS__);
77#define LOG_MESSAGE(fmt, ...) DO_LOG_MESSAGE(wxLOG_Message, fmt, ##__VA_ARGS__);
78#define LOG_WARNING(fmt, ...) DO_LOG_MESSAGE(wxLOG_Warning, fmt, ##__VA_ARGS__);
79#define LOG_ERROR(fmt, ...) DO_LOG_MESSAGE(wxLOG_Error, fmt, ##__VA_ARGS__);
80
87class OcpnLog : public wxLog {
88public:
89 static const wxLogLevel LOG_BADLEVEL;
90
92 OcpnLog(const char* path);
93
94 virtual ~OcpnLog();
95
96 void Flush() override;
97
98 void DoLogRecord(wxLogLevel level, const wxString& msg,
99 const wxLogRecordInfo& info) override;
100
101 static wxLogLevel str2level(const char* string);
102 static std::string level2str(wxLogLevel level);
103
104protected:
105 std::ofstream log;
106};
107
109class Logger {
110public:
111 Logger();
112 ~Logger();
113
115 void logRecord(wxLogLevel level, const char* msg, const wxLogRecordInfo info);
116
117 std::ostream& get(wxLogLevel level, const char* path, int line);
118
119 static void logMessage(wxLogLevel level, const char* path, int line,
120 const char* fmt, ...);
121
122protected:
123 std::stringstream os;
124 wxLogRecordInfo info;
125 wxLogLevel level;
126};
127
130public:
131 CountedLogFilter(unsigned n, wxLogLevel level = wxLOG_Message)
132 : m_count(n), m_level(level), m_not_logged(0) {}
133
135 void Log(const std::string& message);
136
137private:
138 const unsigned m_count;
139 const wxLogLevel m_level;
140 unsigned m_not_logged;
141};
142
145public:
146 TimedLogFilter(const std::chrono::seconds& interval,
147 wxLogLevel level = wxLOG_Message)
148 : m_interval(interval), m_level(level), m_not_logged(0) {}
149
151 void Log(const std::string& message);
152
153private:
154 const std::chrono::seconds m_interval;
155 const wxLogLevel m_level;
156 std::chrono::time_point<std::chrono::steady_clock> m_last_logged;
157 unsigned m_not_logged;
158};
159
160#endif // LOGGER_H
Filter logging every nth message.
Definition logger.h:129
void Log(const std::string &message)
Log a repeated message after suppressing n ones.
Definition logger.cpp:142
Transient logger class, instantiated/used by the LOG macros.
Definition logger.h:109
void logRecord(wxLogLevel level, const char *msg, const wxLogRecordInfo info)
DoLogRecord public wrapper.
Definition logger.cpp:119
Customized logger class appending to a file providing:
Definition logger.h:87
Filter logging repeated message with specified interval.
Definition logger.h:144
void Log(const std::string &message)
Log a repeated message after interval seconds.
Definition logger.cpp:151