OLD | NEW |
1 // Copyright (c) 2005, Google Inc. | 1 // Copyright (c) 2005, Google Inc. |
2 // All rights reserved. | 2 // All rights reserved. |
3 // | 3 // |
4 // Redistribution and use in source and binary forms, with or without | 4 // Redistribution and use in source and binary forms, with or without |
5 // modification, are permitted provided that the following conditions are | 5 // modification, are permitted provided that the following conditions are |
6 // met: | 6 // met: |
7 // | 7 // |
8 // * Redistributions of source code must retain the above copyright | 8 // * Redistributions of source code must retain the above copyright |
9 // notice, this list of conditions and the following disclaimer. | 9 // notice, this list of conditions and the following disclaimer. |
10 // * Redistributions in binary form must reproduce the above | 10 // * Redistributions in binary form must reproduce the above |
(...skipping 27 matching lines...) Expand all Loading... |
38 #include <config.h> | 38 #include <config.h> |
39 #include <stdarg.h> | 39 #include <stdarg.h> |
40 #include <stdlib.h> | 40 #include <stdlib.h> |
41 #include <stdio.h> | 41 #include <stdio.h> |
42 #ifdef HAVE_UNISTD_H | 42 #ifdef HAVE_UNISTD_H |
43 #include <unistd.h> // for write() | 43 #include <unistd.h> // for write() |
44 #endif | 44 #endif |
45 #include <string.h> // for strlen(), strcmp() | 45 #include <string.h> // for strlen(), strcmp() |
46 #include <assert.h> | 46 #include <assert.h> |
47 #include <errno.h> // for errno | 47 #include <errno.h> // for errno |
| 48 #include "base/abort.h" |
48 #include "base/commandlineflags.h" | 49 #include "base/commandlineflags.h" |
49 | 50 |
50 // On some systems (like freebsd), we can't call write() at all in a | 51 // On some systems (like freebsd), we can't call write() at all in a |
51 // global constructor, perhaps because errno hasn't been set up. | 52 // global constructor, perhaps because errno hasn't been set up. |
52 // (In windows, we can't call it because it might call malloc.) | 53 // (In windows, we can't call it because it might call malloc.) |
53 // Calling the write syscall is safer (it doesn't set errno), so we | 54 // Calling the write syscall is safer (it doesn't set errno), so we |
54 // prefer that. Note we don't care about errno for logging: we just | 55 // prefer that. Note we don't care about errno for logging: we just |
55 // do logging on a best-effort basis. | 56 // do logging on a best-effort basis. |
56 #if defined(_MSC_VER) | 57 #if defined(_MSC_VER) |
57 #define WRITE_TO_STDERR(buf, len) WriteToStderr(buf, len); // in port.cc | 58 #define WRITE_TO_STDERR(buf, len) WriteToStderr(buf, len); // in port.cc |
(...skipping 20 matching lines...) Expand all Loading... |
78 // controlled by NDEBUG, so the check will be executed regardless of | 79 // controlled by NDEBUG, so the check will be executed regardless of |
79 // compilation mode. Therefore, it is safe to do things like: | 80 // compilation mode. Therefore, it is safe to do things like: |
80 // CHECK(fp->Write(x) == 4) | 81 // CHECK(fp->Write(x) == 4) |
81 // Note we use write instead of printf/puts to avoid the risk we'll | 82 // Note we use write instead of printf/puts to avoid the risk we'll |
82 // call malloc(). | 83 // call malloc(). |
83 #define CHECK(condition) \ | 84 #define CHECK(condition) \ |
84 do { \ | 85 do { \ |
85 if (!(condition)) { \ | 86 if (!(condition)) { \ |
86 WRITE_TO_STDERR("Check failed: " #condition "\n", \ | 87 WRITE_TO_STDERR("Check failed: " #condition "\n", \ |
87 sizeof("Check failed: " #condition "\n")-1); \ | 88 sizeof("Check failed: " #condition "\n")-1); \ |
88 abort(); \ | 89 tcmalloc::Abort(); \ |
89 } \ | 90 } \ |
90 } while (0) | 91 } while (0) |
91 | 92 |
92 // This takes a message to print. The name is historical. | 93 // This takes a message to print. The name is historical. |
93 #define RAW_CHECK(condition, message) \ | 94 #define RAW_CHECK(condition, message) \ |
94 do { \ | 95 do { \ |
95 if (!(condition)) { \ | 96 if (!(condition)) { \ |
96 WRITE_TO_STDERR("Check failed: " #condition ": " message "\n", \ | 97 WRITE_TO_STDERR("Check failed: " #condition ": " message "\n", \ |
97 sizeof("Check failed: " #condition ": " message "\n")-1);\ | 98 sizeof("Check failed: " #condition ": " message "\n")-1);\ |
98 abort(); \ | 99 tcmalloc::Abort(); \ |
99 } \ | 100 } \ |
100 } while (0) | 101 } while (0) |
101 | 102 |
102 // This is like RAW_CHECK, but only in debug-mode | 103 // This is like RAW_CHECK, but only in debug-mode |
103 #ifdef NDEBUG | 104 #ifdef NDEBUG |
104 enum { DEBUG_MODE = 0 }; | 105 enum { DEBUG_MODE = 0 }; |
105 #define RAW_DCHECK(condition, message) | 106 #define RAW_DCHECK(condition, message) |
106 #else | 107 #else |
107 enum { DEBUG_MODE = 1 }; | 108 enum { DEBUG_MODE = 1 }; |
108 #define RAW_DCHECK(condition, message) RAW_CHECK(condition, message) | 109 #define RAW_DCHECK(condition, message) RAW_CHECK(condition, message) |
109 #endif | 110 #endif |
110 | 111 |
111 // This prints errno as well. Note we use write instead of printf/puts to | 112 // This prints errno as well. Note we use write instead of printf/puts to |
112 // avoid the risk we'll call malloc(). | 113 // avoid the risk we'll call malloc(). |
113 #define PCHECK(condition) \ | 114 #define PCHECK(condition) \ |
114 do { \ | 115 do { \ |
115 if (!(condition)) { \ | 116 if (!(condition)) { \ |
116 const int err_no = errno; \ | 117 const int err_no = errno; \ |
117 WRITE_TO_STDERR("Check failed: " #condition ": ", \ | 118 WRITE_TO_STDERR("Check failed: " #condition ": ", \ |
118 sizeof("Check failed: " #condition ": ")-1); \ | 119 sizeof("Check failed: " #condition ": ")-1); \ |
119 WRITE_TO_STDERR(strerror(err_no), strlen(strerror(err_no))); \ | 120 WRITE_TO_STDERR(strerror(err_no), strlen(strerror(err_no))); \ |
120 WRITE_TO_STDERR("\n", sizeof("\n")-1); \ | 121 WRITE_TO_STDERR("\n", sizeof("\n")-1); \ |
121 abort(); \ | 122 tcmalloc::Abort(); \ |
122 } \ | 123 } \ |
123 } while (0) | 124 } while (0) |
124 | 125 |
125 // Helper macro for binary operators; prints the two values on error | 126 // Helper macro for binary operators; prints the two values on error |
126 // Don't use this macro directly in your code, use CHECK_EQ et al below | 127 // Don't use this macro directly in your code, use CHECK_EQ et al below |
127 | 128 |
128 // WARNING: These don't compile correctly if one of the arguments is a pointer | 129 // WARNING: These don't compile correctly if one of the arguments is a pointer |
129 // and the other is NULL. To work around this, simply static_cast NULL to the | 130 // and the other is NULL. To work around this, simply static_cast NULL to the |
130 // type of the desired pointer. | 131 // type of the desired pointer. |
131 | 132 |
132 // TODO(jandrews): Also print the values in case of failure. Requires some | 133 // TODO(jandrews): Also print the values in case of failure. Requires some |
133 // sort of type-sensitive ToString() function. | 134 // sort of type-sensitive ToString() function. |
134 #define CHECK_OP(op, val1, val2) \ | 135 #define CHECK_OP(op, val1, val2) \ |
135 do { \ | 136 do { \ |
136 if (!((val1) op (val2))) { \ | 137 if (!((val1) op (val2))) { \ |
137 fprintf(stderr, "Check failed: %s %s %s\n", #val1, #op, #val2); \ | 138 fprintf(stderr, "Check failed: %s %s %s\n", #val1, #op, #val2); \ |
138 abort(); \ | 139 tcmalloc::Abort(); \ |
139 } \ | 140 } \ |
140 } while (0) | 141 } while (0) |
141 | 142 |
142 #define CHECK_EQ(val1, val2) CHECK_OP(==, val1, val2) | 143 #define CHECK_EQ(val1, val2) CHECK_OP(==, val1, val2) |
143 #define CHECK_NE(val1, val2) CHECK_OP(!=, val1, val2) | 144 #define CHECK_NE(val1, val2) CHECK_OP(!=, val1, val2) |
144 #define CHECK_LE(val1, val2) CHECK_OP(<=, val1, val2) | 145 #define CHECK_LE(val1, val2) CHECK_OP(<=, val1, val2) |
145 #define CHECK_LT(val1, val2) CHECK_OP(< , val1, val2) | 146 #define CHECK_LT(val1, val2) CHECK_OP(< , val1, val2) |
146 #define CHECK_GE(val1, val2) CHECK_OP(>=, val1, val2) | 147 #define CHECK_GE(val1, val2) CHECK_OP(>=, val1, val2) |
147 #define CHECK_GT(val1, val2) CHECK_OP(> , val1, val2) | 148 #define CHECK_GT(val1, val2) CHECK_OP(> , val1, val2) |
148 | 149 |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
197 inline void LogPrintf(int severity, const char* pat, va_list ap) { | 198 inline void LogPrintf(int severity, const char* pat, va_list ap) { |
198 // We write directly to the stderr file descriptor and avoid FILE | 199 // We write directly to the stderr file descriptor and avoid FILE |
199 // buffering because that may invoke malloc() | 200 // buffering because that may invoke malloc() |
200 char buf[1600]; | 201 char buf[1600]; |
201 perftools_vsnprintf(buf, sizeof(buf)-1, pat, ap); | 202 perftools_vsnprintf(buf, sizeof(buf)-1, pat, ap); |
202 if (buf[0] != '\0' && buf[strlen(buf)-1] != '\n') { | 203 if (buf[0] != '\0' && buf[strlen(buf)-1] != '\n') { |
203 assert(strlen(buf)+1 < sizeof(buf)); | 204 assert(strlen(buf)+1 < sizeof(buf)); |
204 strcat(buf, "\n"); | 205 strcat(buf, "\n"); |
205 } | 206 } |
206 WRITE_TO_STDERR(buf, strlen(buf)); | 207 WRITE_TO_STDERR(buf, strlen(buf)); |
207 if ((severity) == FATAL) | 208 if ((severity) == FATAL) { |
208 abort(); // LOG(FATAL) indicates a big problem, so don't run atexit() calls | 209 // LOG(FATAL) indicates a big problem, so don't run atexit() calls |
| 210 tcmalloc::Abort(); |
| 211 } |
209 } | 212 } |
210 | 213 |
211 // Note that since the order of global constructors is unspecified, | 214 // Note that since the order of global constructors is unspecified, |
212 // global code that calls RAW_LOG may execute before FLAGS_verbose is set. | 215 // global code that calls RAW_LOG may execute before FLAGS_verbose is set. |
213 // Such code will run with verbosity == 0 no matter what. | 216 // Such code will run with verbosity == 0 no matter what. |
214 #define VLOG_IS_ON(severity) (FLAGS_verbose >= severity) | 217 #define VLOG_IS_ON(severity) (FLAGS_verbose >= severity) |
215 | 218 |
216 // In a better world, we'd use __VA_ARGS__, but VC++ 7 doesn't support it. | 219 // In a better world, we'd use __VA_ARGS__, but VC++ 7 doesn't support it. |
217 #define LOG_PRINTF(severity, pat) do { \ | 220 #define LOG_PRINTF(severity, pat) do { \ |
218 if (VLOG_IS_ON(severity)) { \ | 221 if (VLOG_IS_ON(severity)) { \ |
(...skipping 30 matching lines...) Expand all Loading... |
249 #else | 252 #else |
250 typedef int RawFD; | 253 typedef int RawFD; |
251 const RawFD kIllegalRawFD = -1; // what open returns if it fails | 254 const RawFD kIllegalRawFD = -1; // what open returns if it fails |
252 #endif // defined(_WIN32) || defined(__CYGWIN__) || defined(__CYGWIN32__) | 255 #endif // defined(_WIN32) || defined(__CYGWIN__) || defined(__CYGWIN32__) |
253 | 256 |
254 RawFD RawOpenForWriting(const char* filename); // uses default permissions | 257 RawFD RawOpenForWriting(const char* filename); // uses default permissions |
255 void RawWrite(RawFD fd, const char* buf, size_t len); | 258 void RawWrite(RawFD fd, const char* buf, size_t len); |
256 void RawClose(RawFD fd); | 259 void RawClose(RawFD fd); |
257 | 260 |
258 #endif // _LOGGING_H_ | 261 #endif // _LOGGING_H_ |
OLD | NEW |