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 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
78 // controlled by NDEBUG, so the check will be executed regardless of | 78 // controlled by NDEBUG, so the check will be executed regardless of |
79 // compilation mode. Therefore, it is safe to do things like: | 79 // compilation mode. Therefore, it is safe to do things like: |
80 // CHECK(fp->Write(x) == 4) | 80 // CHECK(fp->Write(x) == 4) |
81 // Note we use write instead of printf/puts to avoid the risk we'll | 81 // Note we use write instead of printf/puts to avoid the risk we'll |
82 // call malloc(). | 82 // call malloc(). |
83 #define CHECK(condition) \ | 83 #define CHECK(condition) \ |
84 do { \ | 84 do { \ |
85 if (!(condition)) { \ | 85 if (!(condition)) { \ |
86 WRITE_TO_STDERR("Check failed: " #condition "\n", \ | 86 WRITE_TO_STDERR("Check failed: " #condition "\n", \ |
87 sizeof("Check failed: " #condition "\n")-1); \ | 87 sizeof("Check failed: " #condition "\n")-1); \ |
88 abort(); \ | 88 exit(1); \ |
89 } \ | 89 } \ |
90 } while (0) | 90 } while (0) |
91 | 91 |
92 // This takes a message to print. The name is historical. | 92 // This takes a message to print. The name is historical. |
93 #define RAW_CHECK(condition, message) \ | 93 #define RAW_CHECK(condition, message) \ |
94 do { \ | 94 do { \ |
95 if (!(condition)) { \ | 95 if (!(condition)) { \ |
96 WRITE_TO_STDERR("Check failed: " #condition ": " message "\n", \ | 96 WRITE_TO_STDERR("Check failed: " #condition ": " message "\n", \ |
97 sizeof("Check failed: " #condition ": " message "\n")-1);\ | 97 sizeof("Check failed: " #condition ": " message "\n")-1);\ |
98 abort(); \ | 98 exit(1); \ |
99 } \ | 99 } \ |
100 } while (0) | 100 } while (0) |
101 | 101 |
102 // This is like RAW_CHECK, but only in debug-mode | 102 // This is like RAW_CHECK, but only in debug-mode |
103 #ifdef NDEBUG | 103 #ifdef NDEBUG |
104 enum { DEBUG_MODE = 0 }; | 104 enum { DEBUG_MODE = 0 }; |
105 #define RAW_DCHECK(condition, message) | 105 #define RAW_DCHECK(condition, message) |
106 #else | 106 #else |
107 enum { DEBUG_MODE = 1 }; | 107 enum { DEBUG_MODE = 1 }; |
108 #define RAW_DCHECK(condition, message) RAW_CHECK(condition, message) | 108 #define RAW_DCHECK(condition, message) RAW_CHECK(condition, message) |
109 #endif | 109 #endif |
110 | 110 |
111 // This prints errno as well. Note we use write instead of printf/puts to | 111 // This prints errno as well. Note we use write instead of printf/puts to |
112 // avoid the risk we'll call malloc(). | 112 // avoid the risk we'll call malloc(). |
113 #define PCHECK(condition) \ | 113 #define PCHECK(condition) \ |
114 do { \ | 114 do { \ |
115 if (!(condition)) { \ | 115 if (!(condition)) { \ |
116 const int err_no = errno; \ | 116 const int err_no = errno; \ |
117 WRITE_TO_STDERR("Check failed: " #condition ": ", \ | 117 WRITE_TO_STDERR("Check failed: " #condition ": ", \ |
118 sizeof("Check failed: " #condition ": ")-1); \ | 118 sizeof("Check failed: " #condition ": ")-1); \ |
119 WRITE_TO_STDERR(strerror(err_no), strlen(strerror(err_no))); \ | 119 WRITE_TO_STDERR(strerror(err_no), strlen(strerror(err_no))); \ |
120 WRITE_TO_STDERR("\n", sizeof("\n")-1); \ | 120 WRITE_TO_STDERR("\n", sizeof("\n")-1); \ |
121 abort(); \ | 121 exit(1); \ |
122 } \ | 122 } \ |
123 } while (0) | 123 } while (0) |
124 | 124 |
125 // Helper macro for binary operators; prints the two values on error | 125 // 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 | 126 // Don't use this macro directly in your code, use CHECK_EQ et al below |
127 | 127 |
128 // WARNING: These don't compile correctly if one of the arguments is a pointer | 128 // 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 | 129 // and the other is NULL. To work around this, simply static_cast NULL to the |
130 // type of the desired pointer. | 130 // type of the desired pointer. |
131 | 131 |
132 // TODO(jandrews): Also print the values in case of failure. Requires some | 132 // TODO(jandrews): Also print the values in case of failure. Requires some |
133 // sort of type-sensitive ToString() function. | 133 // sort of type-sensitive ToString() function. |
134 #define CHECK_OP(op, val1, val2) \ | 134 #define CHECK_OP(op, val1, val2) \ |
135 do { \ | 135 do { \ |
136 if (!((val1) op (val2))) { \ | 136 if (!((val1) op (val2))) { \ |
137 fprintf(stderr, "Check failed: %s %s %s\n", #val1, #op, #val2); \ | 137 fprintf(stderr, "Check failed: %s %s %s\n", #val1, #op, #val2); \ |
138 abort(); \ | 138 exit(1); \ |
139 } \ | 139 } \ |
140 } while (0) | 140 } while (0) |
141 | 141 |
142 #define CHECK_EQ(val1, val2) CHECK_OP(==, val1, val2) | 142 #define CHECK_EQ(val1, val2) CHECK_OP(==, val1, val2) |
143 #define CHECK_NE(val1, val2) CHECK_OP(!=, val1, val2) | 143 #define CHECK_NE(val1, val2) CHECK_OP(!=, val1, val2) |
144 #define CHECK_LE(val1, val2) CHECK_OP(<=, val1, val2) | 144 #define CHECK_LE(val1, val2) CHECK_OP(<=, val1, val2) |
145 #define CHECK_LT(val1, val2) CHECK_OP(< , val1, val2) | 145 #define CHECK_LT(val1, val2) CHECK_OP(< , val1, val2) |
146 #define CHECK_GE(val1, val2) CHECK_OP(>=, val1, val2) | 146 #define CHECK_GE(val1, val2) CHECK_OP(>=, val1, val2) |
147 #define CHECK_GT(val1, val2) CHECK_OP(> , val1, val2) | 147 #define CHECK_GT(val1, val2) CHECK_OP(> , val1, val2) |
148 | 148 |
(...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
249 #else | 249 #else |
250 typedef int RawFD; | 250 typedef int RawFD; |
251 const RawFD kIllegalRawFD = -1; // what open returns if it fails | 251 const RawFD kIllegalRawFD = -1; // what open returns if it fails |
252 #endif // defined(_WIN32) || defined(__CYGWIN__) || defined(__CYGWIN32__) | 252 #endif // defined(_WIN32) || defined(__CYGWIN__) || defined(__CYGWIN32__) |
253 | 253 |
254 RawFD RawOpenForWriting(const char* filename); // uses default permissions | 254 RawFD RawOpenForWriting(const char* filename); // uses default permissions |
255 void RawWrite(RawFD fd, const char* buf, size_t len); | 255 void RawWrite(RawFD fd, const char* buf, size_t len); |
256 void RawClose(RawFD fd); | 256 void RawClose(RawFD fd); |
257 | 257 |
258 #endif // _LOGGING_H_ | 258 #endif // _LOGGING_H_ |
OLD | NEW |