OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef BASE_LOGGING_H_ | 5 #ifndef BASE_LOGGING_H_ |
6 #define BASE_LOGGING_H_ | 6 #define BASE_LOGGING_H_ |
7 | 7 |
8 #include <cassert> | 8 #include <cassert> |
9 #include <string> | 9 #include <string> |
10 #include <cstring> | 10 #include <cstring> |
(...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
139 // | 139 // |
140 // Note the special severity of ERROR_REPORT only available/relevant in normal | 140 // Note the special severity of ERROR_REPORT only available/relevant in normal |
141 // mode, which displays error dialog without terminating the program. There is | 141 // mode, which displays error dialog without terminating the program. There is |
142 // no error dialog for severity ERROR or below in normal mode. | 142 // no error dialog for severity ERROR or below in normal mode. |
143 // | 143 // |
144 // There is also the special severity of DFATAL, which logs FATAL in | 144 // There is also the special severity of DFATAL, which logs FATAL in |
145 // debug mode, ERROR in normal mode. | 145 // debug mode, ERROR in normal mode. |
146 | 146 |
147 namespace logging { | 147 namespace logging { |
148 | 148 |
149 // Where to record logging output? A flat file and/or system debug log via | 149 // TODO(avi): do we want to do a unification of character types here? |
150 // OutputDebugString. Defaults on Windows to LOG_ONLY_TO_FILE, and on | 150 #if defined(OS_WIN) |
151 // POSIX to LOG_ONLY_TO_SYSTEM_DEBUG_LOG (aka stderr). | 151 typedef wchar_t PathChar; |
152 enum LoggingDestination { LOG_NONE, | 152 #else |
153 LOG_ONLY_TO_FILE, | 153 typedef char PathChar; |
154 LOG_ONLY_TO_SYSTEM_DEBUG_LOG, | 154 #endif |
155 LOG_TO_BOTH_FILE_AND_SYSTEM_DEBUG_LOG }; | 155 |
156 // Where to record logging output? A flat file and/or system debug log | |
157 // via OutputDebugString. | |
158 enum LoggingDestination { | |
159 LOG_NONE = 0, | |
160 LOG_TO_FILE = 1 << 0, | |
161 LOG_TO_SYSTEM_DEBUG_LOG = 1 << 1, | |
162 | |
163 LOG_TO_ALL = LOG_TO_FILE | LOG_TO_SYSTEM_DEBUG_LOG, | |
164 | |
165 // On Windows, use a file next to the exe; on POSIX platforms, where | |
166 // it may not even be possible to locate the executable on disk, use | |
167 // stderr. | |
168 #if defined(OS_WIN) | |
169 LOG_DEFAULT = LOG_TO_FILE, | |
170 #elif defined(OS_POSIX) | |
171 LOG_DEFAULT = LOG_TO_SYSTEM_DEBUG_LOG, | |
172 #endif | |
173 }; | |
156 | 174 |
157 // Indicates that the log file should be locked when being written to. | 175 // Indicates that the log file should be locked when being written to. |
158 // Often, there is no locking, which is fine for a single threaded program. | 176 // Unless there is only one single-threaded process that is logging to |
159 // If logging is being done from multiple threads or there can be more than | 177 // the log file, the file should be locked during writes to make each |
160 // one process doing the logging, the file should be locked during writes to | 178 // log outut atomic. Other writers will block. |
161 // make each log outut atomic. Other writers will block. | |
162 // | 179 // |
163 // All processes writing to the log file must have their locking set for it to | 180 // All processes writing to the log file must have their locking set for it to |
164 // work properly. Defaults to DONT_LOCK_LOG_FILE. | 181 // work properly. Defaults to LOCK_LOG_FILE. |
165 enum LogLockingState { LOCK_LOG_FILE, DONT_LOCK_LOG_FILE }; | 182 enum LogLockingState { LOCK_LOG_FILE, DONT_LOCK_LOG_FILE }; |
166 | 183 |
167 // On startup, should we delete or append to an existing log file (if any)? | 184 // On startup, should we delete or append to an existing log file (if any)? |
168 // Defaults to APPEND_TO_OLD_LOG_FILE. | 185 // Defaults to APPEND_TO_OLD_LOG_FILE. |
169 enum OldFileDeletionState { DELETE_OLD_LOG_FILE, APPEND_TO_OLD_LOG_FILE }; | 186 enum OldFileDeletionState { DELETE_OLD_LOG_FILE, APPEND_TO_OLD_LOG_FILE }; |
170 | 187 |
171 enum DcheckState { | 188 enum DcheckState { |
172 DISABLE_DCHECK_FOR_NON_OFFICIAL_RELEASE_BUILDS, | 189 DISABLE_DCHECK_FOR_NON_OFFICIAL_RELEASE_BUILDS, |
brettw
2013/06/19 18:10:13
I actually think this would be a good opportunity
akalin
2013/06/19 19:59:18
I tried this, but I ultimately decided it wasn't a
| |
173 ENABLE_DCHECK_FOR_NON_OFFICIAL_RELEASE_BUILDS | 190 ENABLE_DCHECK_FOR_NON_OFFICIAL_RELEASE_BUILDS |
174 }; | 191 }; |
175 | 192 |
176 // TODO(avi): do we want to do a unification of character types here? | 193 struct BASE_EXPORT LoggingSettings { |
brettw
2013/06/19 18:10:13
I think the settings here should all be explicit a
akalin
2013/06/19 19:59:18
Done. Some of the info is in the comments for the
| |
177 #if defined(OS_WIN) | 194 LoggingSettings(); |
178 typedef wchar_t PathChar; | 195 |
179 #else | 196 LoggingDestination logging_dest; |
180 typedef char PathChar; | 197 |
181 #endif | 198 // The three settings below have an effect only when LOG_TO_FILE is |
199 // set in |logging_dest|. | |
200 const PathChar* log_file; | |
201 LogLockingState lock_log; | |
202 OldFileDeletionState delete_old; | |
203 | |
204 DcheckState dcheck_state; | |
205 }; | |
182 | 206 |
183 // Define different names for the BaseInitLoggingImpl() function depending on | 207 // Define different names for the BaseInitLoggingImpl() function depending on |
184 // whether NDEBUG is defined or not so that we'll fail to link if someone tries | 208 // whether NDEBUG is defined or not so that we'll fail to link if someone tries |
185 // to compile logging.cc with NDEBUG but includes logging.h without defining it, | 209 // to compile logging.cc with NDEBUG but includes logging.h without defining it, |
186 // or vice versa. | 210 // or vice versa. |
187 #if NDEBUG | 211 #if NDEBUG |
188 #define BaseInitLoggingImpl BaseInitLoggingImpl_built_with_NDEBUG | 212 #define BaseInitLoggingImpl BaseInitLoggingImpl_built_with_NDEBUG |
189 #else | 213 #else |
190 #define BaseInitLoggingImpl BaseInitLoggingImpl_built_without_NDEBUG | 214 #define BaseInitLoggingImpl BaseInitLoggingImpl_built_without_NDEBUG |
191 #endif | 215 #endif |
192 | 216 |
193 // Implementation of the InitLogging() method declared below. We use a | 217 // Implementation of the InitLogging() method declared below. We use a |
194 // more-specific name so we can #define it above without affecting other code | 218 // more-specific name so we can #define it above without affecting other code |
195 // that has named stuff "InitLogging". | 219 // that has named stuff "InitLogging". |
196 BASE_EXPORT bool BaseInitLoggingImpl(const PathChar* log_file, | 220 BASE_EXPORT bool BaseInitLoggingImpl(const LoggingSettings& settings); |
197 LoggingDestination logging_dest, | |
198 LogLockingState lock_log, | |
199 OldFileDeletionState delete_old, | |
200 DcheckState dcheck_state); | |
201 | 221 |
202 // Sets the log file name and other global logging state. Calling this function | 222 // Sets the log file name and other global logging state. Calling this function |
203 // is recommended, and is normally done at the beginning of application init. | 223 // is recommended, and is normally done at the beginning of application init. |
204 // If you don't call it, all the flags will be initialized to their default | 224 // If you don't call it, all the flags will be initialized to their default |
205 // values, and there is a race condition that may leak a critical section | 225 // values, and there is a race condition that may leak a critical section |
206 // object if two threads try to do the first log at the same time. | 226 // object if two threads try to do the first log at the same time. |
207 // See the definition of the enums above for descriptions and default values. | 227 // See the definition of the enums above for descriptions and default values. |
208 // | 228 // |
209 // The default log file is initialized to "debug.log" in the application | 229 // The default log file is initialized to "debug.log" in the application |
210 // directory. You probably don't want this, especially since the program | 230 // directory. You probably don't want this, especially since the program |
211 // directory may not be writable on an enduser's system. | 231 // directory may not be writable on an enduser's system. |
212 // | 232 // |
213 // This function may be called a second time to re-direct logging (e.g after | 233 // This function may be called a second time to re-direct logging (e.g after |
214 // loging in to a user partition), however it should never be called more than | 234 // loging in to a user partition), however it should never be called more than |
215 // twice. | 235 // twice. |
216 inline bool InitLogging(const PathChar* log_file, | 236 inline bool InitLogging(const LoggingSettings& settings) { |
217 LoggingDestination logging_dest, | 237 return BaseInitLoggingImpl(settings); |
218 LogLockingState lock_log, | |
219 OldFileDeletionState delete_old, | |
220 DcheckState dcheck_state) { | |
221 return BaseInitLoggingImpl(log_file, logging_dest, lock_log, | |
222 delete_old, dcheck_state); | |
223 } | 238 } |
224 | 239 |
225 // Sets the log level. Anything at or above this level will be written to the | 240 // Sets the log level. Anything at or above this level will be written to the |
226 // log file/displayed to the user (if applicable). Anything below this level | 241 // log file/displayed to the user (if applicable). Anything below this level |
227 // will be silently ignored. The log level defaults to 0 (everything is logged | 242 // will be silently ignored. The log level defaults to 0 (everything is logged |
228 // up to level INFO) if this function is not called. | 243 // up to level INFO) if this function is not called. |
229 // Note that log messages for VLOG(x) are logged at level -x, so setting | 244 // Note that log messages for VLOG(x) are logged at level -x, so setting |
230 // the min log level to negative values enables verbose logging. | 245 // the min log level to negative values enables verbose logging. |
231 BASE_EXPORT void SetMinLogLevel(int level); | 246 BASE_EXPORT void SetMinLogLevel(int level); |
232 | 247 |
(...skipping 750 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
983 #elif NOTIMPLEMENTED_POLICY == 5 | 998 #elif NOTIMPLEMENTED_POLICY == 5 |
984 #define NOTIMPLEMENTED() do {\ | 999 #define NOTIMPLEMENTED() do {\ |
985 static bool logged_once = false;\ | 1000 static bool logged_once = false;\ |
986 LOG_IF(ERROR, !logged_once) << NOTIMPLEMENTED_MSG;\ | 1001 LOG_IF(ERROR, !logged_once) << NOTIMPLEMENTED_MSG;\ |
987 logged_once = true;\ | 1002 logged_once = true;\ |
988 } while(0);\ | 1003 } while(0);\ |
989 EAT_STREAM_PARAMETERS | 1004 EAT_STREAM_PARAMETERS |
990 #endif | 1005 #endif |
991 | 1006 |
992 #endif // BASE_LOGGING_H_ | 1007 #endif // BASE_LOGGING_H_ |
OLD | NEW |