Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(528)

Side by Side Diff: base/logging.h

Issue 7461141: Rename BASE_API to BASE_EXPORT. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: '' Created 9 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « base/linux_util.h ('k') | base/logging_win.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 <cassert> 9 #include <cassert>
10 #include <string> 10 #include <string>
11 #include <cstring> 11 #include <cstring>
12 #include <sstream> 12 #include <sstream>
13 13
14 #include "base/base_api.h" 14 #include "base/base_export.h"
15 #include "base/basictypes.h" 15 #include "base/basictypes.h"
16 #include "build/build_config.h" 16 #include "build/build_config.h"
17 17
18 // 18 //
19 // Optional message capabilities 19 // Optional message capabilities
20 // ----------------------------- 20 // -----------------------------
21 // Assertion failed messages and fatal errors are displayed in a dialog box 21 // Assertion failed messages and fatal errors are displayed in a dialog box
22 // before the application exits. However, running this UI creates a message 22 // before the application exits. However, running this UI creates a message
23 // loop, which causes application messages to be processed and potentially 23 // loop, which causes application messages to be processed and potentially
24 // dispatched to existing application windows. Since the application is in a 24 // dispatched to existing application windows. Since the application is in a
(...skipping 161 matching lines...) Expand 10 before | Expand all | Expand 10 after
186 // or vice versa. 186 // or vice versa.
187 #if NDEBUG 187 #if NDEBUG
188 #define BaseInitLoggingImpl BaseInitLoggingImpl_built_with_NDEBUG 188 #define BaseInitLoggingImpl BaseInitLoggingImpl_built_with_NDEBUG
189 #else 189 #else
190 #define BaseInitLoggingImpl BaseInitLoggingImpl_built_without_NDEBUG 190 #define BaseInitLoggingImpl BaseInitLoggingImpl_built_without_NDEBUG
191 #endif 191 #endif
192 192
193 // Implementation of the InitLogging() method declared below. We use a 193 // Implementation of the InitLogging() method declared below. We use a
194 // more-specific name so we can #define it above without affecting other code 194 // more-specific name so we can #define it above without affecting other code
195 // that has named stuff "InitLogging". 195 // that has named stuff "InitLogging".
196 BASE_API bool BaseInitLoggingImpl(const PathChar* log_file, 196 BASE_EXPORT bool BaseInitLoggingImpl(const PathChar* log_file,
197 LoggingDestination logging_dest, 197 LoggingDestination logging_dest,
198 LogLockingState lock_log, 198 LogLockingState lock_log,
199 OldFileDeletionState delete_old, 199 OldFileDeletionState delete_old,
200 DcheckState dcheck_state); 200 DcheckState dcheck_state);
201 201
202 // Sets the log file name and other global logging state. Calling this function 202 // 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. 203 // 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 204 // 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 205 // 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. 206 // 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. 207 // See the definition of the enums above for descriptions and default values.
208 // 208 //
209 // The default log file is initialized to "debug.log" in the application 209 // The default log file is initialized to "debug.log" in the application
210 // directory. You probably don't want this, especially since the program 210 // directory. You probably don't want this, especially since the program
211 // directory may not be writable on an enduser's system. 211 // directory may not be writable on an enduser's system.
212 inline bool InitLogging(const PathChar* log_file, 212 inline bool InitLogging(const PathChar* log_file,
213 LoggingDestination logging_dest, 213 LoggingDestination logging_dest,
214 LogLockingState lock_log, 214 LogLockingState lock_log,
215 OldFileDeletionState delete_old, 215 OldFileDeletionState delete_old,
216 DcheckState dcheck_state) { 216 DcheckState dcheck_state) {
217 return BaseInitLoggingImpl(log_file, logging_dest, lock_log, 217 return BaseInitLoggingImpl(log_file, logging_dest, lock_log,
218 delete_old, dcheck_state); 218 delete_old, dcheck_state);
219 } 219 }
220 220
221 // Sets the log level. Anything at or above this level will be written to the 221 // Sets the log level. Anything at or above this level will be written to the
222 // log file/displayed to the user (if applicable). Anything below this level 222 // log file/displayed to the user (if applicable). Anything below this level
223 // will be silently ignored. The log level defaults to 0 (everything is logged 223 // will be silently ignored. The log level defaults to 0 (everything is logged
224 // up to level INFO) if this function is not called. 224 // up to level INFO) if this function is not called.
225 // Note that log messages for VLOG(x) are logged at level -x, so setting 225 // Note that log messages for VLOG(x) are logged at level -x, so setting
226 // the min log level to negative values enables verbose logging. 226 // the min log level to negative values enables verbose logging.
227 BASE_API void SetMinLogLevel(int level); 227 BASE_EXPORT void SetMinLogLevel(int level);
228 228
229 // Gets the current log level. 229 // Gets the current log level.
230 BASE_API int GetMinLogLevel(); 230 BASE_EXPORT int GetMinLogLevel();
231 231
232 // Gets the VLOG default verbosity level. 232 // Gets the VLOG default verbosity level.
233 BASE_API int GetVlogVerbosity(); 233 BASE_EXPORT int GetVlogVerbosity();
234 234
235 // Gets the current vlog level for the given file (usually taken from 235 // Gets the current vlog level for the given file (usually taken from
236 // __FILE__). 236 // __FILE__).
237 237
238 // Note that |N| is the size *with* the null terminator. 238 // Note that |N| is the size *with* the null terminator.
239 BASE_API int GetVlogLevelHelper(const char* file_start, size_t N); 239 BASE_EXPORT int GetVlogLevelHelper(const char* file_start, size_t N);
240 240
241 template <size_t N> 241 template <size_t N>
242 int GetVlogLevel(const char (&file)[N]) { 242 int GetVlogLevel(const char (&file)[N]) {
243 return GetVlogLevelHelper(file, N); 243 return GetVlogLevelHelper(file, N);
244 } 244 }
245 245
246 // Sets the common items you want to be prepended to each log message. 246 // Sets the common items you want to be prepended to each log message.
247 // process and thread IDs default to off, the timestamp defaults to on. 247 // process and thread IDs default to off, the timestamp defaults to on.
248 // If this function is not called, logging defaults to writing the timestamp 248 // If this function is not called, logging defaults to writing the timestamp
249 // only. 249 // only.
250 BASE_API void SetLogItems(bool enable_process_id, bool enable_thread_id, 250 BASE_EXPORT void SetLogItems(bool enable_process_id, bool enable_thread_id,
251 bool enable_timestamp, bool enable_tickcount); 251 bool enable_timestamp, bool enable_tickcount);
252 252
253 // Sets whether or not you'd like to see fatal debug messages popped up in 253 // Sets whether or not you'd like to see fatal debug messages popped up in
254 // a dialog box or not. 254 // a dialog box or not.
255 // Dialogs are not shown by default. 255 // Dialogs are not shown by default.
256 BASE_API void SetShowErrorDialogs(bool enable_dialogs); 256 BASE_EXPORT void SetShowErrorDialogs(bool enable_dialogs);
257 257
258 // Sets the Log Assert Handler that will be used to notify of check failures. 258 // Sets the Log Assert Handler that will be used to notify of check failures.
259 // The default handler shows a dialog box and then terminate the process, 259 // The default handler shows a dialog box and then terminate the process,
260 // however clients can use this function to override with their own handling 260 // however clients can use this function to override with their own handling
261 // (e.g. a silent one for Unit Tests) 261 // (e.g. a silent one for Unit Tests)
262 typedef void (*LogAssertHandlerFunction)(const std::string& str); 262 typedef void (*LogAssertHandlerFunction)(const std::string& str);
263 BASE_API void SetLogAssertHandler(LogAssertHandlerFunction handler); 263 BASE_EXPORT void SetLogAssertHandler(LogAssertHandlerFunction handler);
264 264
265 // Sets the Log Report Handler that will be used to notify of check failures 265 // Sets the Log Report Handler that will be used to notify of check failures
266 // in non-debug mode. The default handler shows a dialog box and continues 266 // in non-debug mode. The default handler shows a dialog box and continues
267 // the execution, however clients can use this function to override with their 267 // the execution, however clients can use this function to override with their
268 // own handling. 268 // own handling.
269 typedef void (*LogReportHandlerFunction)(const std::string& str); 269 typedef void (*LogReportHandlerFunction)(const std::string& str);
270 BASE_API void SetLogReportHandler(LogReportHandlerFunction handler); 270 BASE_EXPORT void SetLogReportHandler(LogReportHandlerFunction handler);
271 271
272 // Sets the Log Message Handler that gets passed every log message before 272 // Sets the Log Message Handler that gets passed every log message before
273 // it's sent to other log destinations (if any). 273 // it's sent to other log destinations (if any).
274 // Returns true to signal that it handled the message and the message 274 // Returns true to signal that it handled the message and the message
275 // should not be sent to other log destinations. 275 // should not be sent to other log destinations.
276 typedef bool (*LogMessageHandlerFunction)(int severity, 276 typedef bool (*LogMessageHandlerFunction)(int severity,
277 const char* file, int line, size_t message_start, const std::string& str); 277 const char* file, int line, size_t message_start, const std::string& str);
278 BASE_API void SetLogMessageHandler(LogMessageHandlerFunction handler); 278 BASE_EXPORT void SetLogMessageHandler(LogMessageHandlerFunction handler);
279 BASE_API LogMessageHandlerFunction GetLogMessageHandler(); 279 BASE_EXPORT LogMessageHandlerFunction GetLogMessageHandler();
280 280
281 typedef int LogSeverity; 281 typedef int LogSeverity;
282 const LogSeverity LOG_VERBOSE = -1; // This is level 1 verbosity 282 const LogSeverity LOG_VERBOSE = -1; // This is level 1 verbosity
283 // Note: the log severities are used to index into the array of names, 283 // Note: the log severities are used to index into the array of names,
284 // see log_severity_names. 284 // see log_severity_names.
285 const LogSeverity LOG_INFO = 0; 285 const LogSeverity LOG_INFO = 0;
286 const LogSeverity LOG_WARNING = 1; 286 const LogSeverity LOG_WARNING = 1;
287 const LogSeverity LOG_ERROR = 2; 287 const LogSeverity LOG_ERROR = 2;
288 const LogSeverity LOG_ERROR_REPORT = 3; 288 const LogSeverity LOG_ERROR_REPORT = 3;
289 const LogSeverity LOG_FATAL = 4; 289 const LogSeverity LOG_FATAL = 4;
(...skipping 326 matching lines...) Expand 10 before | Expand all | Expand 10 after
616 // Definitions for DCHECK et al. 616 // Definitions for DCHECK et al.
617 617
618 #if ENABLE_DCHECK 618 #if ENABLE_DCHECK
619 619
620 #if defined(NDEBUG) 620 #if defined(NDEBUG)
621 621
622 #define COMPACT_GOOGLE_LOG_EX_DCHECK(ClassName, ...) \ 622 #define COMPACT_GOOGLE_LOG_EX_DCHECK(ClassName, ...) \
623 COMPACT_GOOGLE_LOG_EX_ERROR_REPORT(ClassName , ##__VA_ARGS__) 623 COMPACT_GOOGLE_LOG_EX_ERROR_REPORT(ClassName , ##__VA_ARGS__)
624 #define COMPACT_GOOGLE_LOG_DCHECK COMPACT_GOOGLE_LOG_ERROR_REPORT 624 #define COMPACT_GOOGLE_LOG_DCHECK COMPACT_GOOGLE_LOG_ERROR_REPORT
625 const LogSeverity LOG_DCHECK = LOG_ERROR_REPORT; 625 const LogSeverity LOG_DCHECK = LOG_ERROR_REPORT;
626 BASE_API extern DcheckState g_dcheck_state; 626 BASE_EXPORT extern DcheckState g_dcheck_state;
627 #define DCHECK_IS_ON() \ 627 #define DCHECK_IS_ON() \
628 ((::logging::g_dcheck_state == \ 628 ((::logging::g_dcheck_state == \
629 ::logging::ENABLE_DCHECK_FOR_NON_OFFICIAL_RELEASE_BUILDS) && \ 629 ::logging::ENABLE_DCHECK_FOR_NON_OFFICIAL_RELEASE_BUILDS) && \
630 LOG_IS_ON(DCHECK)) 630 LOG_IS_ON(DCHECK))
631 631
632 #else // defined(NDEBUG) 632 #else // defined(NDEBUG)
633 633
634 // On a regular debug build, we want to have DCHECKs enabled. 634 // On a regular debug build, we want to have DCHECKs enabled.
635 #define COMPACT_GOOGLE_LOG_EX_DCHECK(ClassName, ...) \ 635 #define COMPACT_GOOGLE_LOG_EX_DCHECK(ClassName, ...) \
636 COMPACT_GOOGLE_LOG_EX_FATAL(ClassName , ##__VA_ARGS__) 636 COMPACT_GOOGLE_LOG_EX_FATAL(ClassName , ##__VA_ARGS__)
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after
715 #define assert(x) DLOG_ASSERT(x) 715 #define assert(x) DLOG_ASSERT(x)
716 716
717 // This class more or less represents a particular log message. You 717 // This class more or less represents a particular log message. You
718 // create an instance of LogMessage and then stream stuff to it. 718 // create an instance of LogMessage and then stream stuff to it.
719 // When you finish streaming to it, ~LogMessage is called and the 719 // When you finish streaming to it, ~LogMessage is called and the
720 // full message gets streamed to the appropriate destination. 720 // full message gets streamed to the appropriate destination.
721 // 721 //
722 // You shouldn't actually use LogMessage's constructor to log things, 722 // You shouldn't actually use LogMessage's constructor to log things,
723 // though. You should use the LOG() macro (and variants thereof) 723 // though. You should use the LOG() macro (and variants thereof)
724 // above. 724 // above.
725 class BASE_API LogMessage { 725 class BASE_EXPORT LogMessage {
726 public: 726 public:
727 LogMessage(const char* file, int line, LogSeverity severity, int ctr); 727 LogMessage(const char* file, int line, LogSeverity severity, int ctr);
728 728
729 // Two special constructors that generate reduced amounts of code at 729 // Two special constructors that generate reduced amounts of code at
730 // LOG call sites for common cases. 730 // LOG call sites for common cases.
731 // 731 //
732 // Used for LOG(INFO): Implied are: 732 // Used for LOG(INFO): Implied are:
733 // severity = LOG_INFO, ctr = 0 733 // severity = LOG_INFO, ctr = 0
734 // 734 //
735 // Using this constructor instead of the more complex constructor above 735 // Using this constructor instead of the more complex constructor above
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after
809 }; 809 };
810 810
811 #if defined(OS_WIN) 811 #if defined(OS_WIN)
812 typedef unsigned long SystemErrorCode; 812 typedef unsigned long SystemErrorCode;
813 #elif defined(OS_POSIX) 813 #elif defined(OS_POSIX)
814 typedef int SystemErrorCode; 814 typedef int SystemErrorCode;
815 #endif 815 #endif
816 816
817 // Alias for ::GetLastError() on Windows and errno on POSIX. Avoids having to 817 // Alias for ::GetLastError() on Windows and errno on POSIX. Avoids having to
818 // pull in windows.h just for GetLastError() and DWORD. 818 // pull in windows.h just for GetLastError() and DWORD.
819 BASE_API SystemErrorCode GetLastSystemErrorCode(); 819 BASE_EXPORT SystemErrorCode GetLastSystemErrorCode();
820 820
821 #if defined(OS_WIN) 821 #if defined(OS_WIN)
822 // Appends a formatted system message of the GetLastError() type. 822 // Appends a formatted system message of the GetLastError() type.
823 class BASE_API Win32ErrorLogMessage { 823 class BASE_EXPORT Win32ErrorLogMessage {
824 public: 824 public:
825 Win32ErrorLogMessage(const char* file, 825 Win32ErrorLogMessage(const char* file,
826 int line, 826 int line,
827 LogSeverity severity, 827 LogSeverity severity,
828 SystemErrorCode err, 828 SystemErrorCode err,
829 const char* module); 829 const char* module);
830 830
831 Win32ErrorLogMessage(const char* file, 831 Win32ErrorLogMessage(const char* file,
832 int line, 832 int line,
833 LogSeverity severity, 833 LogSeverity severity,
834 SystemErrorCode err); 834 SystemErrorCode err);
835 835
836 // Appends the error message before destructing the encapsulated class. 836 // Appends the error message before destructing the encapsulated class.
837 ~Win32ErrorLogMessage(); 837 ~Win32ErrorLogMessage();
838 838
839 std::ostream& stream() { return log_message_.stream(); } 839 std::ostream& stream() { return log_message_.stream(); }
840 840
841 private: 841 private:
842 SystemErrorCode err_; 842 SystemErrorCode err_;
843 // Optional name of the module defining the error. 843 // Optional name of the module defining the error.
844 const char* module_; 844 const char* module_;
845 LogMessage log_message_; 845 LogMessage log_message_;
846 846
847 DISALLOW_COPY_AND_ASSIGN(Win32ErrorLogMessage); 847 DISALLOW_COPY_AND_ASSIGN(Win32ErrorLogMessage);
848 }; 848 };
849 #elif defined(OS_POSIX) 849 #elif defined(OS_POSIX)
850 // Appends a formatted system message of the errno type 850 // Appends a formatted system message of the errno type
851 class BASE_API ErrnoLogMessage { 851 class BASE_EXPORT ErrnoLogMessage {
852 public: 852 public:
853 ErrnoLogMessage(const char* file, 853 ErrnoLogMessage(const char* file,
854 int line, 854 int line,
855 LogSeverity severity, 855 LogSeverity severity,
856 SystemErrorCode err); 856 SystemErrorCode err);
857 857
858 // Appends the error message before destructing the encapsulated class. 858 // Appends the error message before destructing the encapsulated class.
859 ~ErrnoLogMessage(); 859 ~ErrnoLogMessage();
860 860
861 std::ostream& stream() { return log_message_.stream(); } 861 std::ostream& stream() { return log_message_.stream(); }
862 862
863 private: 863 private:
864 SystemErrorCode err_; 864 SystemErrorCode err_;
865 LogMessage log_message_; 865 LogMessage log_message_;
866 866
867 DISALLOW_COPY_AND_ASSIGN(ErrnoLogMessage); 867 DISALLOW_COPY_AND_ASSIGN(ErrnoLogMessage);
868 }; 868 };
869 #endif // OS_WIN 869 #endif // OS_WIN
870 870
871 // Closes the log file explicitly if open. 871 // Closes the log file explicitly if open.
872 // NOTE: Since the log file is opened as necessary by the action of logging 872 // NOTE: Since the log file is opened as necessary by the action of logging
873 // statements, there's no guarantee that it will stay closed 873 // statements, there's no guarantee that it will stay closed
874 // after this call. 874 // after this call.
875 BASE_API void CloseLogFile(); 875 BASE_EXPORT void CloseLogFile();
876 876
877 // Async signal safe logging mechanism. 877 // Async signal safe logging mechanism.
878 BASE_API void RawLog(int level, const char* message); 878 BASE_EXPORT void RawLog(int level, const char* message);
879 879
880 #define RAW_LOG(level, message) logging::RawLog(logging::LOG_ ## level, message) 880 #define RAW_LOG(level, message) logging::RawLog(logging::LOG_ ## level, message)
881 881
882 #define RAW_CHECK(condition) \ 882 #define RAW_CHECK(condition) \
883 do { \ 883 do { \
884 if (!(condition)) \ 884 if (!(condition)) \
885 logging::RawLog(logging::LOG_FATAL, "Check failed: " #condition "\n"); \ 885 logging::RawLog(logging::LOG_FATAL, "Check failed: " #condition "\n"); \
886 } while (0) 886 } while (0)
887 887
888 } // namespace logging 888 } // namespace logging
889 889
890 // These functions are provided as a convenience for logging, which is where we 890 // These functions are provided as a convenience for logging, which is where we
891 // use streams (it is against Google style to use streams in other places). It 891 // use streams (it is against Google style to use streams in other places). It
892 // is designed to allow you to emit non-ASCII Unicode strings to the log file, 892 // is designed to allow you to emit non-ASCII Unicode strings to the log file,
893 // which is normally ASCII. It is relatively slow, so try not to use it for 893 // which is normally ASCII. It is relatively slow, so try not to use it for
894 // common cases. Non-ASCII characters will be converted to UTF-8 by these 894 // common cases. Non-ASCII characters will be converted to UTF-8 by these
895 // operators. 895 // operators.
896 BASE_API std::ostream& operator<<(std::ostream& out, const wchar_t* wstr); 896 BASE_EXPORT std::ostream& operator<<(std::ostream& out, const wchar_t* wstr);
897 inline std::ostream& operator<<(std::ostream& out, const std::wstring& wstr) { 897 inline std::ostream& operator<<(std::ostream& out, const std::wstring& wstr) {
898 return out << wstr.c_str(); 898 return out << wstr.c_str();
899 } 899 }
900 900
901 // The NOTIMPLEMENTED() macro annotates codepaths which have 901 // The NOTIMPLEMENTED() macro annotates codepaths which have
902 // not been implemented yet. 902 // not been implemented yet.
903 // 903 //
904 // The implementation of this macro is controlled by NOTIMPLEMENTED_POLICY: 904 // The implementation of this macro is controlled by NOTIMPLEMENTED_POLICY:
905 // 0 -- Do nothing (stripped by compiler) 905 // 0 -- Do nothing (stripped by compiler)
906 // 1 -- Warn at compile time 906 // 1 -- Warn at compile time
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
938 static int count = 0;\ 938 static int count = 0;\
939 LOG_IF(ERROR, 0 == count++) << NOTIMPLEMENTED_MSG;\ 939 LOG_IF(ERROR, 0 == count++) << NOTIMPLEMENTED_MSG;\
940 } while(0) 940 } while(0)
941 #endif 941 #endif
942 942
943 namespace base { 943 namespace base {
944 944
945 class StringPiece; 945 class StringPiece;
946 946
947 // Allows StringPiece to be logged. 947 // Allows StringPiece to be logged.
948 BASE_API std::ostream& operator<<(std::ostream& o, const StringPiece& piece); 948 BASE_EXPORT std::ostream& operator<<(std::ostream& o, const StringPiece& piece);
949 949
950 } // namespace base 950 } // namespace base
951 951
952 #endif // BASE_LOGGING_H_ 952 #endif // BASE_LOGGING_H_
OLDNEW
« no previous file with comments | « base/linux_util.h ('k') | base/logging_win.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698