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, write to the *
18 * Free Software Foundation, Inc., *
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
20 ***************************************************************************/
21
44#ifndef LOGGER_H
45#define LOGGER_H
46
47#include <chrono>
48#include <fstream>
49#include <ostream>
50#include <sstream>
51#include <string>
52
53#include <wx/log.h>
54
55#define DO_LOG_MESSAGE(level, fmt, ...) \
56 { \
57 if (level <= wxLog::GetLogLevel()) { \
58 Logger::logMessage(level, __FILE__, __LINE__, fmt, ##__VA_ARGS__); \
59 } \
60 }
61
62#define _LOG(level) \
63 if (level > wxLog::GetLogLevel()) \
64 ; \
65 else \
66 Logger().get(level, __FILE__, __LINE__)
67
68#define TRACE_LOG _LOG(wxLOG_Trace)
69#define DEBUG_LOG _LOG(wxLOG_Debug)
70#define INFO_LOG _LOG(wxLOG_Info)
71#define MESSAGE_LOG _LOG(wxLOG_Message)
72#define WARNING_LOG _LOG(wxLOG_Warning)
73#define ERROR_LOG _LOG(wxLOG_Error)
74
75#define LOG_TRACE(fmt, ...) DO_LOG_MESSAGE(wxLOG_Trace, fmt, ##__VA_ARGS__);
76#define LOG_DEBUG(fmt, ...) DO_LOG_MESSAGE(wxLOG_Debug, fmt, ##__VA_ARGS__);
77#define LOG_INFO(fmt, ...) DO_LOG_MESSAGE(wxLOG_Info, fmt, ##__VA_ARGS__);
78#define LOG_MESSAGE(fmt, ...) DO_LOG_MESSAGE(wxLOG_Message, fmt, ##__VA_ARGS__);
79#define LOG_WARNING(fmt, ...) DO_LOG_MESSAGE(wxLOG_Warning, fmt, ##__VA_ARGS__);
80#define LOG_ERROR(fmt, ...) DO_LOG_MESSAGE(wxLOG_Error, fmt, ##__VA_ARGS__);
81
88class OcpnLog : public wxLog {
89public:
90 static const wxLogLevel LOG_BADLEVEL;
91
93 OcpnLog(const char* path);
94
95 virtual ~OcpnLog();
96
97 void Flush() override;
98
99 void DoLogRecord(wxLogLevel level, const wxString& msg,
100 const wxLogRecordInfo& info) override;
101
102 static wxLogLevel str2level(const char* string);
103 static std::string level2str(wxLogLevel level);
104
105protected:
106 std::ofstream log;
107};
108
110class Logger {
111public:
112 Logger();
113 ~Logger();
114
116 void logRecord(wxLogLevel level, const char* msg, const wxLogRecordInfo info);
117
118 std::ostream& get(wxLogLevel level, const char* path, int line);
119
120 static void logMessage(wxLogLevel level, const char* path, int line,
121 const char* fmt, ...);
122
123protected:
124 std::stringstream os;
125 wxLogRecordInfo info;
126 wxLogLevel level;
127};
128
131public:
132 CountedLogFilter(unsigned n, wxLogLevel level = wxLOG_Message)
133 : m_count(n), m_level(level), m_not_logged(0) {}
134
136 void Log(const std::string& message);
137
138private:
139 const unsigned m_count;
140 const wxLogLevel m_level;
141 unsigned m_not_logged;
142};
143
146public:
147 TimedLogFilter(const std::chrono::seconds& interval,
148 wxLogLevel level = wxLOG_Message)
149 : m_interval(interval), m_level(level), m_not_logged(0) {}
150
152 void Log(const std::string& message);
153
154private:
155 const std::chrono::seconds m_interval;
156 const wxLogLevel m_level;
157 std::chrono::time_point<std::chrono::steady_clock> m_last_logged;
158 unsigned m_not_logged;
159};
160
161#endif // LOGGER_H
Filter logging every nth message.
Definition logger.h:130
void Log(const std::string &message)
Log a repeated message after suppressing n ones.
Definition logger.cpp:143
Transient logger class, instantiated/used by the LOG macros.
Definition logger.h:110
void logRecord(wxLogLevel level, const char *msg, const wxLogRecordInfo info)
DoLogRecord public wrapper.
Definition logger.cpp:120
Customized logger class appending to a file providing:
Definition logger.h:88
Filter logging repeated message with specified interval.
Definition logger.h:145
void Log(const std::string &message)
Log a repeated message after interval seconds.
Definition logger.cpp:152