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, |
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 { |
177 #if defined(OS_WIN) | 194 // The defaults values are: |
178 typedef wchar_t PathChar; | 195 // |
179 #else | 196 // logging_dest: LOG_DEFAULT |
180 typedef char PathChar; | 197 // log_file: NULL |
181 #endif | 198 // lock_log: LOCK_LOG_FILE |
| 199 // delete_old: APPEND_TO_OLD_LOG_FILE |
| 200 // dcheck_state: DISABLE_DCHECK_FOR_NON_OFFICIAL_RELEASE_BUILDS |
| 201 LoggingSettings(); |
| 202 |
| 203 LoggingDestination logging_dest; |
| 204 |
| 205 // The three settings below have an effect only when LOG_TO_FILE is |
| 206 // set in |logging_dest|. |
| 207 const PathChar* log_file; |
| 208 LogLockingState lock_log; |
| 209 OldFileDeletionState delete_old; |
| 210 |
| 211 DcheckState dcheck_state; |
| 212 }; |
182 | 213 |
183 // Define different names for the BaseInitLoggingImpl() function depending on | 214 // 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 | 215 // 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, | 216 // to compile logging.cc with NDEBUG but includes logging.h without defining it, |
186 // or vice versa. | 217 // or vice versa. |
187 #if NDEBUG | 218 #if NDEBUG |
188 #define BaseInitLoggingImpl BaseInitLoggingImpl_built_with_NDEBUG | 219 #define BaseInitLoggingImpl BaseInitLoggingImpl_built_with_NDEBUG |
189 #else | 220 #else |
190 #define BaseInitLoggingImpl BaseInitLoggingImpl_built_without_NDEBUG | 221 #define BaseInitLoggingImpl BaseInitLoggingImpl_built_without_NDEBUG |
191 #endif | 222 #endif |
192 | 223 |
193 // Implementation of the InitLogging() method declared below. We use a | 224 // Implementation of the InitLogging() method declared below. We use a |
194 // more-specific name so we can #define it above without affecting other code | 225 // more-specific name so we can #define it above without affecting other code |
195 // that has named stuff "InitLogging". | 226 // that has named stuff "InitLogging". |
196 BASE_EXPORT bool BaseInitLoggingImpl(const PathChar* log_file, | 227 BASE_EXPORT bool BaseInitLoggingImpl(const LoggingSettings& settings); |
197 LoggingDestination logging_dest, | |
198 LogLockingState lock_log, | |
199 OldFileDeletionState delete_old, | |
200 DcheckState dcheck_state); | |
201 | 228 |
202 // Sets the log file name and other global logging state. Calling this function | 229 // 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. | 230 // 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 | 231 // 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 | 232 // 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. | 233 // 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. | 234 // See the definition of the enums above for descriptions and default values. |
208 // | 235 // |
209 // The default log file is initialized to "debug.log" in the application | 236 // The default log file is initialized to "debug.log" in the application |
210 // directory. You probably don't want this, especially since the program | 237 // directory. You probably don't want this, especially since the program |
211 // directory may not be writable on an enduser's system. | 238 // directory may not be writable on an enduser's system. |
212 // | 239 // |
213 // This function may be called a second time to re-direct logging (e.g after | 240 // 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 | 241 // loging in to a user partition), however it should never be called more than |
215 // twice. | 242 // twice. |
216 inline bool InitLogging(const PathChar* log_file, | 243 inline bool InitLogging(const LoggingSettings& settings) { |
217 LoggingDestination logging_dest, | 244 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 } | 245 } |
224 | 246 |
225 // Sets the log level. Anything at or above this level will be written to the | 247 // 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 | 248 // 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 | 249 // will be silently ignored. The log level defaults to 0 (everything is logged |
228 // up to level INFO) if this function is not called. | 250 // 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 | 251 // 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. | 252 // the min log level to negative values enables verbose logging. |
231 BASE_EXPORT void SetMinLogLevel(int level); | 253 BASE_EXPORT void SetMinLogLevel(int level); |
232 | 254 |
(...skipping 758 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
991 #elif NOTIMPLEMENTED_POLICY == 5 | 1013 #elif NOTIMPLEMENTED_POLICY == 5 |
992 #define NOTIMPLEMENTED() do {\ | 1014 #define NOTIMPLEMENTED() do {\ |
993 static bool logged_once = false;\ | 1015 static bool logged_once = false;\ |
994 LOG_IF(ERROR, !logged_once) << NOTIMPLEMENTED_MSG;\ | 1016 LOG_IF(ERROR, !logged_once) << NOTIMPLEMENTED_MSG;\ |
995 logged_once = true;\ | 1017 logged_once = true;\ |
996 } while(0);\ | 1018 } while(0);\ |
997 EAT_STREAM_PARAMETERS | 1019 EAT_STREAM_PARAMETERS |
998 #endif | 1020 #endif |
999 | 1021 |
1000 #endif // BASE_LOGGING_H_ | 1022 #endif // BASE_LOGGING_H_ |
OLD | NEW |