OpenCPN Partial API docs
logger.h
1 /******************************************************************************
2  *
3  * Project: OpenCPN
4  *
5  ***************************************************************************
6  * Copyright (C) 2013 by David S. Register *
7  * *
8  * This program is free software; you can redistribute it and/or modify *
9  * it under the terms of the GNU General Public License as published by *
10  * the Free Software Foundation; either version 2 of the License, or *
11  * (at your option) any later version. *
12  * *
13  * This program is distributed in the hope that it will be useful, *
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
16  * GNU General Public License for more details. *
17  * *
18  * You should have received a copy of the GNU General Public License *
19  * along with this program; if not, write to the *
20  * Free Software Foundation, Inc., *
21  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
22  ***************************************************************************
23  */
24 
25 #ifndef LOGGER_H
26 #define LOGGER_H
27 
28 #include <fstream>
29 #include <ostream>
30 #include <sstream>
31 #include <string>
32 
33 #include <wx/log.h>
34 
62 class OcpnLog : public wxLog {
63 public:
64  static const wxLogLevel LOG_BADLEVEL;
65 
67  OcpnLog(const char* path);
68 
69  virtual ~OcpnLog();
70 
71  void Flush() override;
72 
73  void DoLogRecord(wxLogLevel level, const wxString& msg,
74  const wxLogRecordInfo& info) override;
75 
76  static wxLogLevel str2level(const char* string);
77  static std::string level2str(wxLogLevel level);
78 
79 protected:
80  std::ofstream log;
81 };
82 
84 class Logger {
85 public:
86  Logger();
87  ~Logger();
88 
90  void logRecord(wxLogLevel level, const char* msg, const wxLogRecordInfo info);
91 
92  std::ostream& get(wxLogLevel level, const char* path, int line);
93 
94  static void logMessage(wxLogLevel level, const char* path, int line,
95  const char* fmt, ...);
96 
97 protected:
98  std::stringstream os;
99  wxLogRecordInfo info;
100  wxLogLevel level;
101 };
102 
103 #define DO_LOG_MESSAGE(level, fmt, ...) \
104  { \
105  if (level <= wxLog::GetLogLevel()) { \
106  Logger::logMessage(level, __FILE__, __LINE__, fmt, ##__VA_ARGS__); \
107  } \
108  }
109 
110 #define _LOG(level) \
111  if (level > wxLog::GetLogLevel()) \
112  ; \
113  else \
114  Logger().get(level, __FILE__, __LINE__)
115 
116 #define TRACE_LOG _LOG(wxLOG_Trace)
117 #define DEBUG_LOG _LOG(wxLOG_Debug)
118 #define INFO_LOG _LOG(wxLOG_Info)
119 #define MESSAGE_LOG _LOG(wxLOG_Message)
120 #define WARNING_LOG _LOG(wxLOG_Warning)
121 #define ERROR_LOG _LOG(wxLOG_Error)
122 
123 #define LOG_TRACE(fmt, ...) DO_LOG_MESSAGE(wxLOG_Trace, fmt, ##__VA_ARGS__);
124 #define LOG_DEBUG(fmt, ...) DO_LOG_MESSAGE(wxLOG_Debug, fmt, ##__VA_ARGS__);
125 #define LOG_INFO(fmt, ...) DO_LOG_MESSAGE(wxLOG_Info, fmt, ##__VA_ARGS__);
126 #define LOG_MESSAGE(fmt, ...) DO_LOG_MESSAGE(wxLOG_Message, fmt, ##__VA_ARGS__);
127 #define LOG_WARNING(fmt, ...) DO_LOG_MESSAGE(wxLOG_Warning, fmt, ##__VA_ARGS__);
128 #define LOG_ERROR(fmt, ...) DO_LOG_MESSAGE(wxLOG_Error, fmt, ##__VA_ARGS__);
129 
130 #endif // LOGGER_H
Transient logger class, instantiated/used by the LOG macros.
Definition: logger.h:84
void logRecord(wxLogLevel level, const char *msg, const wxLogRecordInfo info)
DoLogRecord public wrapper.
Definition: logger.cpp:119
Logging interface.
Definition: logger.h:62
OcpnLog(const char *path)
Create logger appending to given filename.
Definition: logger.cpp:82