|
1 /* |
|
2 * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved. |
|
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
4 * |
|
5 * This code is free software; you can redistribute it and/or modify it |
|
6 * under the terms of the GNU General Public License version 2 only, as |
|
7 * published by the Free Software Foundation. Oracle designates this |
|
8 * particular file as subject to the "Classpath" exception as provided |
|
9 * by Oracle in the LICENSE file that accompanied this code. |
|
10 * |
|
11 * This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 * version 2 for more details (a copy is included in the LICENSE file that |
|
15 * accompanied this code). |
|
16 * |
|
17 * You should have received a copy of the GNU General Public License version |
|
18 * 2 along with this work; if not, write to the Free Software Foundation, |
|
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 * |
|
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
22 * or visit www.oracle.com if you need additional information or have any |
|
23 * questions. |
|
24 */ |
|
25 |
|
26 #ifndef __LOG_H_INCLUDED_ |
|
27 #define __LOG_H_INCLUDED_ |
|
28 |
|
29 #include <windows.h> |
|
30 #include "tstrings.h" |
|
31 |
|
32 |
|
33 /* Default logger (Logger::defaultLogger()) writes log messages to the default log file. |
|
34 * Common scenario: |
|
35 * - main() function configures default logger: |
|
36 * FileLogAppender appender(_T("my_log_filename.log")); |
|
37 * Logger::defaultLogger().setAppender(appender); |
|
38 * Logger::defaultLogger().setLogLevel(LOG_INFO); |
|
39 * If the default file name and log level are not set, _T("jusched.log")/LOG_TRACE are used. |
|
40 * |
|
41 * Logger fileName specifies only file name, |
|
42 * full path for the log file depends on the platform (usually value of the TMP env. var) |
|
43 */ |
|
44 |
|
45 |
|
46 struct LogEvent { |
|
47 SYSTEMTIME ts; |
|
48 long tid; |
|
49 long pid; |
|
50 tstring moduleName; |
|
51 tstring logLevel; |
|
52 tstring fileName; |
|
53 int lineNum; |
|
54 tstring funcName; |
|
55 tstring message; |
|
56 |
|
57 LogEvent(); |
|
58 }; |
|
59 |
|
60 |
|
61 class LogAppender { |
|
62 public: |
|
63 virtual ~LogAppender() { |
|
64 } |
|
65 virtual void append(const LogEvent& v) = 0; |
|
66 }; |
|
67 |
|
68 |
|
69 class NopLogAppender: public LogAppender { |
|
70 public: |
|
71 virtual void append(const LogEvent& v) {}; |
|
72 }; |
|
73 |
|
74 |
|
75 class TeeLogAppender: public LogAppender { |
|
76 public: |
|
77 TeeLogAppender(LogAppender* first, LogAppender* second): |
|
78 first(first), second(second) { |
|
79 } |
|
80 virtual ~TeeLogAppender() { |
|
81 } |
|
82 virtual void append(const LogEvent& v) { |
|
83 if (first) { |
|
84 first->append(v); |
|
85 } |
|
86 if (second) { |
|
87 second->append(v); |
|
88 } |
|
89 } |
|
90 private: |
|
91 LogAppender* first; |
|
92 LogAppender* second; |
|
93 }; |
|
94 |
|
95 |
|
96 /** |
|
97 * Writes log events to stderr. |
|
98 */ |
|
99 class StderrLogAppender: public LogAppender { |
|
100 public: |
|
101 explicit StderrLogAppender(); |
|
102 |
|
103 virtual void append(const LogEvent& v); |
|
104 }; |
|
105 |
|
106 |
|
107 class Logger { |
|
108 public: |
|
109 enum LogLevel { |
|
110 LOG_TRACE, |
|
111 LOG_INFO, |
|
112 LOG_WARNING, |
|
113 LOG_ERROR |
|
114 }; |
|
115 |
|
116 static Logger& defaultLogger(); |
|
117 |
|
118 explicit Logger(LogAppender& appender, LogLevel logLevel = LOG_TRACE); |
|
119 ~Logger(); |
|
120 |
|
121 LogAppender& setAppender(LogAppender& v) { |
|
122 LogAppender& oldAppender = *appender; |
|
123 appender = &v; |
|
124 return oldAppender; |
|
125 } |
|
126 |
|
127 LogAppender& getAppender() const { |
|
128 return *appender; |
|
129 } |
|
130 |
|
131 void setLogLevel(LogLevel logLevel); |
|
132 |
|
133 bool isLoggable(LogLevel logLevel) const ; |
|
134 void log(LogLevel logLevel, LPCTSTR fileName, int lineNum, LPCTSTR funcName, const tstring& message) const; |
|
135 void log(LogLevel logLevel, LPCTSTR fileName, int lineNum, |
|
136 LPCTSTR funcName, const tstrings::any& message) const { |
|
137 return log(logLevel, fileName, lineNum, funcName, message.tstr()); |
|
138 } |
|
139 void log(LogLevel logLevel, LPCTSTR fileName, int lineNum, |
|
140 LPCTSTR funcName, tstring::const_pointer message) const { |
|
141 return log(logLevel, fileName, lineNum, funcName, tstring(message)); |
|
142 } |
|
143 |
|
144 // internal class for scope tracing |
|
145 class ScopeTracer { |
|
146 public: |
|
147 ScopeTracer(Logger &logger, LogLevel logLevel, LPCTSTR fileName, int lineNum, LPCTSTR funcName, const tstring& scopeName); |
|
148 ~ScopeTracer(); |
|
149 |
|
150 private: |
|
151 const Logger &log; |
|
152 const LogLevel level; |
|
153 const bool needLog; |
|
154 const tstring file; |
|
155 const int line; |
|
156 const tstring func; |
|
157 const tstring scope; |
|
158 }; |
|
159 |
|
160 private: |
|
161 LogLevel level; |
|
162 LogAppender* appender; |
|
163 }; |
|
164 |
|
165 |
|
166 // base logging macro |
|
167 #define LOGGER_LOG(logger, logLevel, message) \ |
|
168 do { \ |
|
169 if (logger.isLoggable(logLevel)) { \ |
|
170 logger.log(logLevel, _T(__FILE__), __LINE__, _T(__FUNCTION__), message); \ |
|
171 } \ |
|
172 } while(false) |
|
173 |
|
174 |
|
175 // custom logger macros |
|
176 #define LOGGER_TRACE(logger, message) LOGGER_LOG(logger, Logger::LOG_TRACE, message) |
|
177 #define LOGGER_INFO(logger, message) LOGGER_LOG(logger, Logger::LOG_INFO, message) |
|
178 #define LOGGER_WARNING(logger, message) LOGGER_LOG(logger, Logger::LOG_WARNING, message) |
|
179 #define LOGGER_ERROR(logger, message) LOGGER_LOG(logger, Logger::LOG_ERROR, message) |
|
180 // scope tracing macros |
|
181 #define LOGGER_TRACE_SCOPE(logger, scopeName) \ |
|
182 Logger::ScopeTracer tracer__COUNTER__(logger, Logger::LOG_TRACE, _T(__FILE__), __LINE__, _T(__FUNCTION__), scopeName) |
|
183 #define LOGGER_TRACE_FUNCTION(logger) LOGGER_TRACE_SCOPE(logger, _T(__FUNCTION__)) |
|
184 |
|
185 |
|
186 // default logger macros |
|
187 #define LOG_TRACE(message) LOGGER_LOG(Logger::defaultLogger(), Logger::LOG_TRACE, message) |
|
188 #define LOG_INFO(message) LOGGER_LOG(Logger::defaultLogger(), Logger::LOG_INFO, message) |
|
189 #define LOG_WARNING(message) LOGGER_LOG(Logger::defaultLogger(), Logger::LOG_WARNING, message) |
|
190 #define LOG_ERROR(message) LOGGER_LOG(Logger::defaultLogger(), Logger::LOG_ERROR, message) |
|
191 // scope tracing macros |
|
192 // logs (_T("Entering ") + scopeName) at the beging, (_T("Exiting ") + scopeName) at the end of scope |
|
193 #define LOG_TRACE_SCOPE(scopeName) LOGGER_TRACE_SCOPE(Logger::defaultLogger(), scopeName) |
|
194 // logs (_T("Entering ") + functionName) at the beging, (_T("Exiting ") + __FUNCTION__) at the end of scope |
|
195 #define LOG_TRACE_FUNCTION() LOGGER_TRACE_FUNCTION(Logger::defaultLogger()) |
|
196 |
|
197 |
|
198 #endif // __LOG_H_INCLUDED_ |