OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 #pragma once | 7 #pragma once |
8 | 8 |
9 #include <string> | 9 #include <string> |
10 #include <cstring> | 10 #include <cstring> |
11 #include <sstream> | 11 #include <sstream> |
12 | 12 |
| 13 #include "base/base_api.h" |
13 #include "base/basictypes.h" | 14 #include "base/basictypes.h" |
| 15 #include "build/build_config.h" |
14 | 16 |
15 // | 17 // |
16 // Optional message capabilities | 18 // Optional message capabilities |
17 // ----------------------------- | 19 // ----------------------------- |
18 // Assertion failed messages and fatal errors are displayed in a dialog box | 20 // Assertion failed messages and fatal errors are displayed in a dialog box |
19 // before the application exits. However, running this UI creates a message | 21 // before the application exits. However, running this UI creates a message |
20 // loop, which causes application messages to be processed and potentially | 22 // loop, which causes application messages to be processed and potentially |
21 // dispatched to existing application windows. Since the application is in a | 23 // dispatched to existing application windows. Since the application is in a |
22 // bad state when this assertion dialog is displayed, these messages may not | 24 // bad state when this assertion dialog is displayed, these messages may not |
23 // get processed and hang the dialog, or the application might go crazy. | 25 // get processed and hang the dialog, or the application might go crazy. |
(...skipping 159 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
183 // or vice versa. | 185 // or vice versa. |
184 #if NDEBUG | 186 #if NDEBUG |
185 #define BaseInitLoggingImpl BaseInitLoggingImpl_built_with_NDEBUG | 187 #define BaseInitLoggingImpl BaseInitLoggingImpl_built_with_NDEBUG |
186 #else | 188 #else |
187 #define BaseInitLoggingImpl BaseInitLoggingImpl_built_without_NDEBUG | 189 #define BaseInitLoggingImpl BaseInitLoggingImpl_built_without_NDEBUG |
188 #endif | 190 #endif |
189 | 191 |
190 // Implementation of the InitLogging() method declared below. We use a | 192 // Implementation of the InitLogging() method declared below. We use a |
191 // more-specific name so we can #define it above without affecting other code | 193 // more-specific name so we can #define it above without affecting other code |
192 // that has named stuff "InitLogging". | 194 // that has named stuff "InitLogging". |
193 bool BaseInitLoggingImpl(const PathChar* log_file, | 195 BASE_API bool BaseInitLoggingImpl(const PathChar* log_file, |
194 LoggingDestination logging_dest, | 196 LoggingDestination logging_dest, |
195 LogLockingState lock_log, | 197 LogLockingState lock_log, |
196 OldFileDeletionState delete_old, | 198 OldFileDeletionState delete_old, |
197 DcheckState dcheck_state); | 199 DcheckState dcheck_state); |
198 | 200 |
199 // Sets the log file name and other global logging state. Calling this function | 201 // Sets the log file name and other global logging state. Calling this function |
200 // is recommended, and is normally done at the beginning of application init. | 202 // is recommended, and is normally done at the beginning of application init. |
201 // If you don't call it, all the flags will be initialized to their default | 203 // If you don't call it, all the flags will be initialized to their default |
202 // values, and there is a race condition that may leak a critical section | 204 // values, and there is a race condition that may leak a critical section |
203 // object if two threads try to do the first log at the same time. | 205 // object if two threads try to do the first log at the same time. |
204 // See the definition of the enums above for descriptions and default values. | 206 // See the definition of the enums above for descriptions and default values. |
205 // | 207 // |
206 // The default log file is initialized to "debug.log" in the application | 208 // The default log file is initialized to "debug.log" in the application |
207 // directory. You probably don't want this, especially since the program | 209 // directory. You probably don't want this, especially since the program |
208 // directory may not be writable on an enduser's system. | 210 // directory may not be writable on an enduser's system. |
209 inline bool InitLogging(const PathChar* log_file, | 211 inline bool InitLogging(const PathChar* log_file, |
210 LoggingDestination logging_dest, | 212 LoggingDestination logging_dest, |
211 LogLockingState lock_log, | 213 LogLockingState lock_log, |
212 OldFileDeletionState delete_old, | 214 OldFileDeletionState delete_old, |
213 DcheckState dcheck_state) { | 215 DcheckState dcheck_state) { |
214 return BaseInitLoggingImpl(log_file, logging_dest, lock_log, | 216 return BaseInitLoggingImpl(log_file, logging_dest, lock_log, |
215 delete_old, dcheck_state); | 217 delete_old, dcheck_state); |
216 } | 218 } |
217 | 219 |
218 // Sets the log level. Anything at or above this level will be written to the | 220 // Sets the log level. Anything at or above this level will be written to the |
219 // log file/displayed to the user (if applicable). Anything below this level | 221 // log file/displayed to the user (if applicable). Anything below this level |
220 // will be silently ignored. The log level defaults to 0 (everything is logged | 222 // will be silently ignored. The log level defaults to 0 (everything is logged |
221 // up to level INFO) if this function is not called. | 223 // up to level INFO) if this function is not called. |
222 // Note that log messages for VLOG(x) are logged at level -x, so setting | 224 // Note that log messages for VLOG(x) are logged at level -x, so setting |
223 // the min log level to negative values enables verbose logging. | 225 // the min log level to negative values enables verbose logging. |
224 void SetMinLogLevel(int level); | 226 BASE_API void SetMinLogLevel(int level); |
225 | 227 |
226 // Gets the current log level. | 228 // Gets the current log level. |
227 int GetMinLogLevel(); | 229 BASE_API int GetMinLogLevel(); |
228 | 230 |
229 // Gets the VLOG default verbosity level. | 231 // Gets the VLOG default verbosity level. |
230 int GetVlogVerbosity(); | 232 BASE_API int GetVlogVerbosity(); |
231 | 233 |
232 // Gets the current vlog level for the given file (usually taken from | 234 // Gets the current vlog level for the given file (usually taken from |
233 // __FILE__). | 235 // __FILE__). |
234 | 236 |
235 // Note that |N| is the size *with* the null terminator. | 237 // Note that |N| is the size *with* the null terminator. |
236 int GetVlogLevelHelper(const char* file_start, size_t N); | 238 BASE_API int GetVlogLevelHelper(const char* file_start, size_t N); |
237 | 239 |
238 template <size_t N> | 240 template <size_t N> |
239 int GetVlogLevel(const char (&file)[N]) { | 241 int GetVlogLevel(const char (&file)[N]) { |
240 return GetVlogLevelHelper(file, N); | 242 return GetVlogLevelHelper(file, N); |
241 } | 243 } |
242 | 244 |
243 // Sets the common items you want to be prepended to each log message. | 245 // Sets the common items you want to be prepended to each log message. |
244 // process and thread IDs default to off, the timestamp defaults to on. | 246 // process and thread IDs default to off, the timestamp defaults to on. |
245 // If this function is not called, logging defaults to writing the timestamp | 247 // If this function is not called, logging defaults to writing the timestamp |
246 // only. | 248 // only. |
247 void SetLogItems(bool enable_process_id, bool enable_thread_id, | 249 BASE_API void SetLogItems(bool enable_process_id, bool enable_thread_id, |
248 bool enable_timestamp, bool enable_tickcount); | 250 bool enable_timestamp, bool enable_tickcount); |
249 | 251 |
250 // Sets whether or not you'd like to see fatal debug messages popped up in | 252 // Sets whether or not you'd like to see fatal debug messages popped up in |
251 // a dialog box or not. | 253 // a dialog box or not. |
252 // Dialogs are not shown by default. | 254 // Dialogs are not shown by default. |
253 void SetShowErrorDialogs(bool enable_dialogs); | 255 BASE_API void SetShowErrorDialogs(bool enable_dialogs); |
254 | 256 |
255 // Sets the Log Assert Handler that will be used to notify of check failures. | 257 // Sets the Log Assert Handler that will be used to notify of check failures. |
256 // The default handler shows a dialog box and then terminate the process, | 258 // The default handler shows a dialog box and then terminate the process, |
257 // however clients can use this function to override with their own handling | 259 // however clients can use this function to override with their own handling |
258 // (e.g. a silent one for Unit Tests) | 260 // (e.g. a silent one for Unit Tests) |
259 typedef void (*LogAssertHandlerFunction)(const std::string& str); | 261 typedef void (*LogAssertHandlerFunction)(const std::string& str); |
260 void SetLogAssertHandler(LogAssertHandlerFunction handler); | 262 BASE_API void SetLogAssertHandler(LogAssertHandlerFunction handler); |
261 | 263 |
262 // Sets the Log Report Handler that will be used to notify of check failures | 264 // Sets the Log Report Handler that will be used to notify of check failures |
263 // in non-debug mode. The default handler shows a dialog box and continues | 265 // in non-debug mode. The default handler shows a dialog box and continues |
264 // the execution, however clients can use this function to override with their | 266 // the execution, however clients can use this function to override with their |
265 // own handling. | 267 // own handling. |
266 typedef void (*LogReportHandlerFunction)(const std::string& str); | 268 typedef void (*LogReportHandlerFunction)(const std::string& str); |
267 void SetLogReportHandler(LogReportHandlerFunction handler); | 269 BASE_API void SetLogReportHandler(LogReportHandlerFunction handler); |
268 | 270 |
269 // Sets the Log Message Handler that gets passed every log message before | 271 // Sets the Log Message Handler that gets passed every log message before |
270 // it's sent to other log destinations (if any). | 272 // it's sent to other log destinations (if any). |
271 // Returns true to signal that it handled the message and the message | 273 // Returns true to signal that it handled the message and the message |
272 // should not be sent to other log destinations. | 274 // should not be sent to other log destinations. |
273 typedef bool (*LogMessageHandlerFunction)(int severity, | 275 typedef bool (*LogMessageHandlerFunction)(int severity, |
274 const char* file, int line, size_t message_start, const std::string& str); | 276 const char* file, int line, size_t message_start, const std::string& str); |
275 void SetLogMessageHandler(LogMessageHandlerFunction handler); | 277 BASE_API void SetLogMessageHandler(LogMessageHandlerFunction handler); |
276 LogMessageHandlerFunction GetLogMessageHandler(); | 278 BASE_API LogMessageHandlerFunction GetLogMessageHandler(); |
277 | 279 |
278 typedef int LogSeverity; | 280 typedef int LogSeverity; |
279 const LogSeverity LOG_VERBOSE = -1; // This is level 1 verbosity | 281 const LogSeverity LOG_VERBOSE = -1; // This is level 1 verbosity |
280 // Note: the log severities are used to index into the array of names, | 282 // Note: the log severities are used to index into the array of names, |
281 // see log_severity_names. | 283 // see log_severity_names. |
282 const LogSeverity LOG_INFO = 0; | 284 const LogSeverity LOG_INFO = 0; |
283 const LogSeverity LOG_WARNING = 1; | 285 const LogSeverity LOG_WARNING = 1; |
284 const LogSeverity LOG_ERROR = 2; | 286 const LogSeverity LOG_ERROR = 2; |
285 const LogSeverity LOG_ERROR_REPORT = 3; | 287 const LogSeverity LOG_ERROR_REPORT = 3; |
286 const LogSeverity LOG_FATAL = 4; | 288 const LogSeverity LOG_FATAL = 4; |
(...skipping 420 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
707 #define assert(x) DLOG_ASSERT(x) | 709 #define assert(x) DLOG_ASSERT(x) |
708 | 710 |
709 // This class more or less represents a particular log message. You | 711 // This class more or less represents a particular log message. You |
710 // create an instance of LogMessage and then stream stuff to it. | 712 // create an instance of LogMessage and then stream stuff to it. |
711 // When you finish streaming to it, ~LogMessage is called and the | 713 // When you finish streaming to it, ~LogMessage is called and the |
712 // full message gets streamed to the appropriate destination. | 714 // full message gets streamed to the appropriate destination. |
713 // | 715 // |
714 // You shouldn't actually use LogMessage's constructor to log things, | 716 // You shouldn't actually use LogMessage's constructor to log things, |
715 // though. You should use the LOG() macro (and variants thereof) | 717 // though. You should use the LOG() macro (and variants thereof) |
716 // above. | 718 // above. |
717 class LogMessage { | 719 class BASE_API LogMessage { |
718 public: | 720 public: |
719 LogMessage(const char* file, int line, LogSeverity severity, int ctr); | 721 LogMessage(const char* file, int line, LogSeverity severity, int ctr); |
720 | 722 |
721 // Two special constructors that generate reduced amounts of code at | 723 // Two special constructors that generate reduced amounts of code at |
722 // LOG call sites for common cases. | 724 // LOG call sites for common cases. |
723 // | 725 // |
724 // Used for LOG(INFO): Implied are: | 726 // Used for LOG(INFO): Implied are: |
725 // severity = LOG_INFO, ctr = 0 | 727 // severity = LOG_INFO, ctr = 0 |
726 // | 728 // |
727 // Using this constructor instead of the more complex constructor above | 729 // Using this constructor instead of the more complex constructor above |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
785 | 787 |
786 // A non-macro interface to the log facility; (useful | 788 // A non-macro interface to the log facility; (useful |
787 // when the logging level is not a compile-time constant). | 789 // when the logging level is not a compile-time constant). |
788 inline void LogAtLevel(int const log_level, std::string const &msg) { | 790 inline void LogAtLevel(int const log_level, std::string const &msg) { |
789 LogMessage(__FILE__, __LINE__, log_level).stream() << msg; | 791 LogMessage(__FILE__, __LINE__, log_level).stream() << msg; |
790 } | 792 } |
791 | 793 |
792 // This class is used to explicitly ignore values in the conditional | 794 // This class is used to explicitly ignore values in the conditional |
793 // logging macros. This avoids compiler warnings like "value computed | 795 // logging macros. This avoids compiler warnings like "value computed |
794 // is not used" and "statement has no effect". | 796 // is not used" and "statement has no effect". |
795 class LogMessageVoidify { | 797 class BASE_API LogMessageVoidify { |
796 public: | 798 public: |
797 LogMessageVoidify() { } | 799 LogMessageVoidify() { } |
798 // This has to be an operator with a precedence lower than << but | 800 // This has to be an operator with a precedence lower than << but |
799 // higher than ?: | 801 // higher than ?: |
800 void operator&(std::ostream&) { } | 802 void operator&(std::ostream&) { } |
801 }; | 803 }; |
802 | 804 |
803 #if defined(OS_WIN) | 805 #if defined(OS_WIN) |
804 typedef unsigned long SystemErrorCode; | 806 typedef unsigned long SystemErrorCode; |
805 #elif defined(OS_POSIX) | 807 #elif defined(OS_POSIX) |
806 typedef int SystemErrorCode; | 808 typedef int SystemErrorCode; |
807 #endif | 809 #endif |
808 | 810 |
809 // Alias for ::GetLastError() on Windows and errno on POSIX. Avoids having to | 811 // Alias for ::GetLastError() on Windows and errno on POSIX. Avoids having to |
810 // pull in windows.h just for GetLastError() and DWORD. | 812 // pull in windows.h just for GetLastError() and DWORD. |
811 SystemErrorCode GetLastSystemErrorCode(); | 813 BASE_API SystemErrorCode GetLastSystemErrorCode(); |
812 | 814 |
813 #if defined(OS_WIN) | 815 #if defined(OS_WIN) |
814 // Appends a formatted system message of the GetLastError() type. | 816 // Appends a formatted system message of the GetLastError() type. |
815 class Win32ErrorLogMessage { | 817 class BASE_API Win32ErrorLogMessage { |
816 public: | 818 public: |
817 Win32ErrorLogMessage(const char* file, | 819 Win32ErrorLogMessage(const char* file, |
818 int line, | 820 int line, |
819 LogSeverity severity, | 821 LogSeverity severity, |
820 SystemErrorCode err, | 822 SystemErrorCode err, |
821 const char* module); | 823 const char* module); |
822 | 824 |
823 Win32ErrorLogMessage(const char* file, | 825 Win32ErrorLogMessage(const char* file, |
824 int line, | 826 int line, |
825 LogSeverity severity, | 827 LogSeverity severity, |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
857 LogMessage log_message_; | 859 LogMessage log_message_; |
858 | 860 |
859 DISALLOW_COPY_AND_ASSIGN(ErrnoLogMessage); | 861 DISALLOW_COPY_AND_ASSIGN(ErrnoLogMessage); |
860 }; | 862 }; |
861 #endif // OS_WIN | 863 #endif // OS_WIN |
862 | 864 |
863 // Closes the log file explicitly if open. | 865 // Closes the log file explicitly if open. |
864 // NOTE: Since the log file is opened as necessary by the action of logging | 866 // NOTE: Since the log file is opened as necessary by the action of logging |
865 // statements, there's no guarantee that it will stay closed | 867 // statements, there's no guarantee that it will stay closed |
866 // after this call. | 868 // after this call. |
867 void CloseLogFile(); | 869 BASE_API void CloseLogFile(); |
868 | 870 |
869 // Async signal safe logging mechanism. | 871 // Async signal safe logging mechanism. |
870 void RawLog(int level, const char* message); | 872 BASE_API void RawLog(int level, const char* message); |
871 | 873 |
872 #define RAW_LOG(level, message) logging::RawLog(logging::LOG_ ## level, message) | 874 #define RAW_LOG(level, message) logging::RawLog(logging::LOG_ ## level, message) |
873 | 875 |
874 #define RAW_CHECK(condition) \ | 876 #define RAW_CHECK(condition) \ |
875 do { \ | 877 do { \ |
876 if (!(condition)) \ | 878 if (!(condition)) \ |
877 logging::RawLog(logging::LOG_FATAL, "Check failed: " #condition "\n"); \ | 879 logging::RawLog(logging::LOG_FATAL, "Check failed: " #condition "\n"); \ |
878 } while (0) | 880 } while (0) |
879 | 881 |
880 } // namespace logging | 882 } // namespace logging |
881 | 883 |
882 // These functions are provided as a convenience for logging, which is where we | 884 // These functions are provided as a convenience for logging, which is where we |
883 // use streams (it is against Google style to use streams in other places). It | 885 // use streams (it is against Google style to use streams in other places). It |
884 // is designed to allow you to emit non-ASCII Unicode strings to the log file, | 886 // is designed to allow you to emit non-ASCII Unicode strings to the log file, |
885 // which is normally ASCII. It is relatively slow, so try not to use it for | 887 // which is normally ASCII. It is relatively slow, so try not to use it for |
886 // common cases. Non-ASCII characters will be converted to UTF-8 by these | 888 // common cases. Non-ASCII characters will be converted to UTF-8 by these |
887 // operators. | 889 // operators. |
888 std::ostream& operator<<(std::ostream& out, const wchar_t* wstr); | 890 BASE_API std::ostream& operator<<(std::ostream& out, const wchar_t* wstr); |
889 inline std::ostream& operator<<(std::ostream& out, const std::wstring& wstr) { | 891 inline std::ostream& operator<<(std::ostream& out, const std::wstring& wstr) { |
890 return out << wstr.c_str(); | 892 return out << wstr.c_str(); |
891 } | 893 } |
892 | 894 |
893 // The NOTIMPLEMENTED() macro annotates codepaths which have | 895 // The NOTIMPLEMENTED() macro annotates codepaths which have |
894 // not been implemented yet. | 896 // not been implemented yet. |
895 // | 897 // |
896 // The implementation of this macro is controlled by NOTIMPLEMENTED_POLICY: | 898 // The implementation of this macro is controlled by NOTIMPLEMENTED_POLICY: |
897 // 0 -- Do nothing (stripped by compiler) | 899 // 0 -- Do nothing (stripped by compiler) |
898 // 1 -- Warn at compile time | 900 // 1 -- Warn at compile time |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
935 namespace base { | 937 namespace base { |
936 | 938 |
937 class StringPiece; | 939 class StringPiece; |
938 | 940 |
939 // allow StringPiece to be logged (needed for unit testing). | 941 // allow StringPiece to be logged (needed for unit testing). |
940 extern std::ostream& operator<<(std::ostream& o, const StringPiece& piece); | 942 extern std::ostream& operator<<(std::ostream& o, const StringPiece& piece); |
941 | 943 |
942 } // namespace base | 944 } // namespace base |
943 | 945 |
944 #endif // BASE_LOGGING_H_ | 946 #endif // BASE_LOGGING_H_ |
OLD | NEW |