Merge pull request #179 from renning22/master
[platform/upstream/glog.git] / src / glog / raw_logging.h.in
1 // Copyright (c) 2006, Google Inc.
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
6 // met:
7 //
8 //     * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 //     * Redistributions in binary form must reproduce the above
11 // copyright notice, this list of conditions and the following disclaimer
12 // in the documentation and/or other materials provided with the
13 // distribution.
14 //     * Neither the name of Google Inc. nor the names of its
15 // contributors may be used to endorse or promote products derived from
16 // this software without specific prior written permission.
17 //
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 //
30 // Author: Maxim Lifantsev
31 //
32 // Thread-safe logging routines that do not allocate any memory or
33 // acquire any locks, and can therefore be used by low-level memory
34 // allocation and synchronization code.
35
36 #ifndef BASE_RAW_LOGGING_H_
37 #define BASE_RAW_LOGGING_H_
38
39 #include <time.h>
40
41 @ac_google_start_namespace@
42
43 #include "glog/log_severity.h"
44 #include "glog/vlog_is_on.h"
45
46 // Annoying stuff for windows -- makes sure clients can import these functions
47 #ifndef GOOGLE_GLOG_DLL_DECL
48 # if defined(_WIN32) && !defined(__CYGWIN__)
49 #   define GOOGLE_GLOG_DLL_DECL  __declspec(dllimport)
50 # else
51 #   define GOOGLE_GLOG_DLL_DECL
52 # endif
53 #endif
54
55 // This is similar to LOG(severity) << format... and VLOG(level) << format..,
56 // but
57 // * it is to be used ONLY by low-level modules that can't use normal LOG()
58 // * it is desiged to be a low-level logger that does not allocate any
59 //   memory and does not need any locks, hence:
60 // * it logs straight and ONLY to STDERR w/o buffering
61 // * it uses an explicit format and arguments list
62 // * it will silently chop off really long message strings
63 // Usage example:
64 //   RAW_LOG(ERROR, "Failed foo with %i: %s", status, error);
65 //   RAW_VLOG(3, "status is %i", status);
66 // These will print an almost standard log lines like this to stderr only:
67 //   E0821 211317 file.cc:123] RAW: Failed foo with 22: bad_file
68 //   I0821 211317 file.cc:142] RAW: status is 20
69 #define RAW_LOG(severity, ...) \
70   do { \
71     switch (@ac_google_namespace@::GLOG_ ## severity) {  \
72       case 0: \
73         RAW_LOG_INFO(__VA_ARGS__); \
74         break; \
75       case 1: \
76         RAW_LOG_WARNING(__VA_ARGS__); \
77         break; \
78       case 2: \
79         RAW_LOG_ERROR(__VA_ARGS__); \
80         break; \
81       case 3: \
82         RAW_LOG_FATAL(__VA_ARGS__); \
83         break; \
84       default: \
85         break; \
86     } \
87   } while (0)
88
89 // The following STRIP_LOG testing is performed in the header file so that it's
90 // possible to completely compile out the logging code and the log messages.
91 #if STRIP_LOG == 0
92 #define RAW_VLOG(verboselevel, ...) \
93   do { \
94     if (VLOG_IS_ON(verboselevel)) { \
95       RAW_LOG_INFO(__VA_ARGS__); \
96     } \
97   } while (0)
98 #else
99 #define RAW_VLOG(verboselevel, ...) RawLogStub__(0, __VA_ARGS__)
100 #endif // STRIP_LOG == 0
101
102 #if STRIP_LOG == 0
103 #define RAW_LOG_INFO(...) @ac_google_namespace@::RawLog__(@ac_google_namespace@::GLOG_INFO, \
104                                    __FILE__, __LINE__, __VA_ARGS__)
105 #else
106 #define RAW_LOG_INFO(...) @ac_google_namespace@::RawLogStub__(0, __VA_ARGS__)
107 #endif // STRIP_LOG == 0
108
109 #if STRIP_LOG <= 1
110 #define RAW_LOG_WARNING(...) @ac_google_namespace@::RawLog__(@ac_google_namespace@::GLOG_WARNING,   \
111                                       __FILE__, __LINE__, __VA_ARGS__)
112 #else
113 #define RAW_LOG_WARNING(...) @ac_google_namespace@::RawLogStub__(0, __VA_ARGS__)
114 #endif // STRIP_LOG <= 1
115
116 #if STRIP_LOG <= 2
117 #define RAW_LOG_ERROR(...) @ac_google_namespace@::RawLog__(@ac_google_namespace@::GLOG_ERROR,       \
118                                     __FILE__, __LINE__, __VA_ARGS__)
119 #else
120 #define RAW_LOG_ERROR(...) @ac_google_namespace@::RawLogStub__(0, __VA_ARGS__)
121 #endif // STRIP_LOG <= 2
122
123 #if STRIP_LOG <= 3
124 #define RAW_LOG_FATAL(...) @ac_google_namespace@::RawLog__(@ac_google_namespace@::GLOG_FATAL,       \
125                                     __FILE__, __LINE__, __VA_ARGS__)
126 #else
127 #define RAW_LOG_FATAL(...) \
128   do { \
129     @ac_google_namespace@::RawLogStub__(0, __VA_ARGS__);        \
130     exit(1); \
131   } while (0)
132 #endif // STRIP_LOG <= 3
133
134 // Similar to CHECK(condition) << message,
135 // but for low-level modules: we use only RAW_LOG that does not allocate memory.
136 // We do not want to provide args list here to encourage this usage:
137 //   if (!cond)  RAW_LOG(FATAL, "foo ...", hard_to_compute_args);
138 // so that the args are not computed when not needed.
139 #define RAW_CHECK(condition, message)                                   \
140   do {                                                                  \
141     if (!(condition)) {                                                 \
142       RAW_LOG(FATAL, "Check %s failed: %s", #condition, message);       \
143     }                                                                   \
144   } while (0)
145
146 // Debug versions of RAW_LOG and RAW_CHECK
147 #ifndef NDEBUG
148
149 #define RAW_DLOG(severity, ...) RAW_LOG(severity, __VA_ARGS__)
150 #define RAW_DCHECK(condition, message) RAW_CHECK(condition, message)
151
152 #else  // NDEBUG
153
154 #define RAW_DLOG(severity, ...)                                 \
155   while (false)                                                 \
156     RAW_LOG(severity, __VA_ARGS__)
157 #define RAW_DCHECK(condition, message) \
158   while (false) \
159     RAW_CHECK(condition, message)
160
161 #endif  // NDEBUG
162
163 // Stub log function used to work around for unused variable warnings when
164 // building with STRIP_LOG > 0.
165 static inline void RawLogStub__(int /* ignored */, ...) {
166 }
167
168 // Helper function to implement RAW_LOG and RAW_VLOG
169 // Logs format... at "severity" level, reporting it
170 // as called from file:line.
171 // This does not allocate memory or acquire locks.
172 GOOGLE_GLOG_DLL_DECL void RawLog__(LogSeverity severity,
173                                    const char* file,
174                                    int line,
175                                    const char* format, ...)
176    @ac_cv___attribute___printf_4_5@;
177
178 // Hack to propagate time information into this module so that
179 // this module does not have to directly call localtime_r(),
180 // which could allocate memory.
181 GOOGLE_GLOG_DLL_DECL void RawLog__SetLastTime(const struct tm& t, int usecs);
182
183 @ac_google_end_namespace@
184
185 #endif  // BASE_RAW_LOGGING_H_