brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 1 | #ifndef AOS_COMMON_LOGGING_LOGGING_H_ |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 2 | #define AOS_COMMON_LOGGING_LOGGING_H_ |
| 3 | |
Brian Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 4 | // This file contains the logging client interface. It works with both C and C++ |
| 5 | // code. |
| 6 | |
| 7 | #include <stdio.h> |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 8 | #include <stdint.h> |
Brian Silverman | b067abf | 2013-09-13 22:01:06 -0700 | [diff] [blame] | 9 | #include <stdlib.h> |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 10 | |
| 11 | #ifdef __cplusplus |
| 12 | extern "C" { |
| 13 | #endif |
| 14 | |
| 15 | typedef uint8_t log_level; |
Brian Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 16 | |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 17 | #define DECL_LEVELS \ |
| 18 | DECL_LEVEL(DEBUG, 0); /* stuff that gets printed out every cycle */ \ |
| 19 | DECL_LEVEL(INFO, 1); /* things like PosEdge/NegEdge */ \ |
Brian Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 20 | /* things that might still work if they happen occasionally */ \ |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 21 | DECL_LEVEL(WARNING, 2); \ |
| 22 | /*-1 so that vxworks macro of same name will have same effect if used*/ \ |
| 23 | DECL_LEVEL(ERROR, -1); /* errors */ \ |
Brian Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 24 | /* serious errors. the logging code will terminate the process/task */ \ |
| 25 | DECL_LEVEL(FATAL, 4); \ |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 26 | DECL_LEVEL(LOG_UNKNOWN, 5); /* unknown logging level */ |
Brian Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 27 | #define DECL_LEVEL(name, value) static const log_level name = value; |
Brian Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 28 | DECL_LEVELS; |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 29 | #undef DECL_LEVEL |
| 30 | |
| 31 | #define STRINGIFY(x) TO_STRING(x) |
| 32 | #define TO_STRING(x) #x |
| 33 | |
Brian Silverman | b089388 | 2014-02-10 14:48:30 -0800 | [diff] [blame] | 34 | // Not static const size_t for C code. |
Brian Silverman | c11f3b7 | 2013-03-19 18:32:02 -0700 | [diff] [blame] | 35 | #define LOG_MESSAGE_LEN 400 |
Brian Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 36 | |
| 37 | #ifdef __VXWORKS__ |
| 38 | // We're using ancient glibc, so sticking to just what the syscall can handle is |
| 39 | // probably safer. |
| 40 | #define LOG_PRINTF_FORMAT_TYPE printf |
| 41 | #else |
| 42 | #define LOG_PRINTF_FORMAT_TYPE gnu_printf |
| 43 | #endif |
| 44 | #ifdef __cplusplus |
| 45 | extern "C" { |
| 46 | #endif |
| 47 | // Actually implements the basic logging call. |
| 48 | // Does not check that level is valid. |
| 49 | void log_do(log_level level, const char *format, ...) |
| 50 | __attribute__((format(LOG_PRINTF_FORMAT_TYPE, 2, 3))); |
| 51 | |
| 52 | void log_cork(int line, const char *function, const char *format, ...) |
| 53 | __attribute__((format(LOG_PRINTF_FORMAT_TYPE, 3, 4))); |
| 54 | // Implements the uncork logging call. |
| 55 | void log_uncork(int line, const char *function, log_level level, |
| 56 | const char *file, const char *format, ...) |
| 57 | __attribute__((format(LOG_PRINTF_FORMAT_TYPE, 5, 6))); |
| 58 | #ifdef __cplusplus |
| 59 | } |
| 60 | #endif |
| 61 | |
| 62 | // A magical static const char[] or string literal that communicates the name |
| 63 | // of the enclosing function. |
| 64 | // It's currently using __PRETTY_FUNCTION__ because both GCC and Clang support |
| 65 | // that and it gives nicer results in C++ than the standard __func__ (which |
| 66 | // would also work). |
Brian Silverman | c11f3b7 | 2013-03-19 18:32:02 -0700 | [diff] [blame] | 67 | //#define LOG_CURRENT_FUNCTION __PRETTY_FUNCTION__ |
| 68 | #define LOG_CURRENT_FUNCTION __func__ |
Brian Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 69 | |
Brian Silverman | 8fc7bc9 | 2013-03-13 23:15:47 -0700 | [diff] [blame] | 70 | #undef LOG_SOURCENAME |
| 71 | #define LOG_SOURCENAME __FILE__ |
| 72 | |
Brian Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 73 | // The basic logging call. |
Brian Silverman | d6974f4 | 2014-02-14 13:39:21 -0800 | [diff] [blame] | 74 | #define LOG(level, format, args...) \ |
| 75 | do { \ |
| 76 | log_do(level, LOG_SOURCENAME ": " STRINGIFY(__LINE__) ": %s: " format, \ |
| 77 | LOG_CURRENT_FUNCTION, ##args); \ |
| 78 | /* so that GCC knows that it won't return */ \ |
| 79 | if (level == FATAL) { \ |
| 80 | fprintf(stderr, "log_do(FATAL) fell through!!!!!\n"); \ |
| 81 | printf("see stderr\n"); \ |
| 82 | abort(); \ |
| 83 | } \ |
| 84 | } while (0) |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 85 | |
| 86 | // Allows format to not be a string constant. |
Brian Silverman | d6974f4 | 2014-02-14 13:39:21 -0800 | [diff] [blame] | 87 | #define LOG_DYNAMIC(level, format, args...) \ |
| 88 | do { \ |
| 89 | static char log_buf[LOG_MESSAGE_LEN]; \ |
| 90 | int ret = snprintf(log_buf, sizeof(log_buf), format, ##args); \ |
| 91 | if (ret < 0 || (uintmax_t)ret >= LOG_MESSAGE_LEN) { \ |
| 92 | LOG(ERROR, "next message was too long so not subbing in args\n"); \ |
| 93 | LOG(level, "%s", format); \ |
| 94 | } else { \ |
| 95 | LOG(level, "%s", log_buf); \ |
| 96 | } \ |
| 97 | } while (0) |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 98 | |
Brian Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 99 | // Allows "bottling up" multiple log fragments which can then all be logged in |
| 100 | // one message with LOG_UNCORK. |
| 101 | // Calls from a given thread/task will be grouped together. |
Brian Silverman | d6974f4 | 2014-02-14 13:39:21 -0800 | [diff] [blame] | 102 | #define LOG_CORK(format, args...) \ |
| 103 | do { \ |
| 104 | log_cork(__LINE__, LOG_CURRENT_FUNCTION, format, ##args); \ |
| 105 | } while (0) |
Brian Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 106 | // Actually logs all of the saved up log fragments (including format and args on |
| 107 | // the end). |
Brian Silverman | d6974f4 | 2014-02-14 13:39:21 -0800 | [diff] [blame] | 108 | #define LOG_UNCORK(level, format, args...) \ |
| 109 | do { \ |
| 110 | log_uncork(__LINE__, LOG_CURRENT_FUNCTION, level, LOG_SOURCENAME, format, \ |
| 111 | ##args); \ |
| 112 | } while (0) |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 113 | |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 114 | #ifdef __cplusplus |
| 115 | } |
| 116 | #endif |
| 117 | |
Brian Silverman | b067abf | 2013-09-13 22:01:06 -0700 | [diff] [blame] | 118 | #ifdef __cplusplus |
| 119 | |
| 120 | namespace aos { |
| 121 | |
| 122 | // CHECK* macros, similar to glog |
| 123 | // (<http://google-glog.googlecode.com/svn/trunk/doc/glog.html>)'s, except they |
| 124 | // don't support streaming in extra text. Some of the implementation is borrowed |
| 125 | // from there too. |
| 126 | // They all LOG(FATAL) with a helpful message when the check fails. |
| 127 | // TODO(brians): Replace assert with CHECK |
| 128 | // Portions copyright (c) 1999, Google Inc. |
| 129 | // All rights reserved. |
| 130 | // |
| 131 | // Redistribution and use in source and binary forms, with or without |
| 132 | // modification, are permitted provided that the following conditions are |
| 133 | // met: |
| 134 | // |
| 135 | // * Redistributions of source code must retain the above copyright |
| 136 | // notice, this list of conditions and the following disclaimer. |
| 137 | // * Redistributions in binary form must reproduce the above |
| 138 | // copyright notice, this list of conditions and the following disclaimer |
| 139 | // in the documentation and/or other materials provided with the |
| 140 | // distribution. |
| 141 | // * Neither the name of Google Inc. nor the names of its |
| 142 | // contributors may be used to endorse or promote products derived from |
| 143 | // this software without specific prior written permission. |
| 144 | // |
| 145 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 146 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 147 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 148 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 149 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 150 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 151 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 152 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 153 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 154 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 155 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 156 | |
Brian Silverman | 862b163 | 2013-09-14 15:29:59 -0700 | [diff] [blame] | 157 | // CHECK dies with a fatal error if condition is not true. It is *not* |
| 158 | // controlled by NDEBUG, so the check will be executed regardless of |
| 159 | // compilation mode. Therefore, it is safe to do things like: |
| 160 | // CHECK(fp->Write(x) == 4) |
Brian Silverman | d6974f4 | 2014-02-14 13:39:21 -0800 | [diff] [blame] | 161 | #define CHECK(condition) \ |
| 162 | if (__builtin_expect(!(condition), 0)) { \ |
Brian Silverman | 5d79075 | 2014-01-01 13:25:36 -0800 | [diff] [blame] | 163 | LOG(FATAL, "CHECK(%s) failed\n", #condition); \ |
Brian Silverman | 862b163 | 2013-09-14 15:29:59 -0700 | [diff] [blame] | 164 | } |
| 165 | |
Brian Silverman | b067abf | 2013-09-13 22:01:06 -0700 | [diff] [blame] | 166 | // Helper functions for CHECK_OP macro. |
| 167 | // The (int, int) specialization works around the issue that the compiler |
| 168 | // will not instantiate the template version of the function on values of |
| 169 | // unnamed enum type. |
Brian Silverman | d6974f4 | 2014-02-14 13:39:21 -0800 | [diff] [blame] | 170 | #define DEFINE_CHECK_OP_IMPL(name, op) \ |
| 171 | template <typename T1, typename T2> \ |
| 172 | inline void LogImpl##name(const T1 &v1, const T2 &v2, \ |
| 173 | const char *exprtext) { \ |
| 174 | if (!__builtin_expect(v1 op v2, 1)) { \ |
| 175 | LOG(FATAL, "CHECK(%s) failed\n", exprtext); \ |
| 176 | } \ |
| 177 | } \ |
| 178 | inline void LogImpl##name(int v1, int v2, const char *exprtext) { \ |
| 179 | ::aos::LogImpl##name<int, int>(v1, v2, exprtext); \ |
Brian Silverman | b067abf | 2013-09-13 22:01:06 -0700 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | // We use the full name Check_EQ, Check_NE, etc. in case the file including |
| 183 | // base/logging.h provides its own #defines for the simpler names EQ, NE, etc. |
| 184 | // This happens if, for example, those are used as token names in a |
| 185 | // yacc grammar. |
| 186 | DEFINE_CHECK_OP_IMPL(Check_EQ, ==) // Compilation error with CHECK_EQ(NULL, x)? |
| 187 | DEFINE_CHECK_OP_IMPL(Check_NE, !=) // Use CHECK(x == NULL) instead. |
| 188 | DEFINE_CHECK_OP_IMPL(Check_LE, <=) |
| 189 | DEFINE_CHECK_OP_IMPL(Check_LT, < ) |
| 190 | DEFINE_CHECK_OP_IMPL(Check_GE, >=) |
| 191 | DEFINE_CHECK_OP_IMPL(Check_GT, > ) |
| 192 | |
Brian Silverman | d6974f4 | 2014-02-14 13:39:21 -0800 | [diff] [blame] | 193 | #define CHECK_OP(name, op, val1, val2) \ |
Brian Silverman | b067abf | 2013-09-13 22:01:06 -0700 | [diff] [blame] | 194 | ::aos::LogImplCheck##name(val1, val2, \ |
| 195 | STRINGIFY(val1) STRINGIFY(op) STRINGIFY(val2)) |
| 196 | |
| 197 | #define CHECK_EQ(val1, val2) CHECK_OP(_EQ, ==, val1, val2) |
| 198 | #define CHECK_NE(val1, val2) CHECK_OP(_NE, !=, val1, val2) |
| 199 | #define CHECK_LE(val1, val2) CHECK_OP(_LE, <=, val1, val2) |
| 200 | #define CHECK_LT(val1, val2) CHECK_OP(_LT, < , val1, val2) |
| 201 | #define CHECK_GE(val1, val2) CHECK_OP(_GE, >=, val1, val2) |
| 202 | #define CHECK_GT(val1, val2) CHECK_OP(_GT, > , val1, val2) |
| 203 | |
| 204 | // A small helper for CHECK_NOTNULL(). |
| 205 | template <typename T> |
| 206 | inline T* CheckNotNull(const char *value_name, T *t) { |
| 207 | if (t == NULL) { |
| 208 | LOG(FATAL, "'%s' must not be NULL\n", value_name); |
| 209 | } |
| 210 | return t; |
| 211 | } |
| 212 | |
| 213 | // Check that the input is non NULL. This very useful in constructor |
| 214 | // initializer lists. |
Brian Silverman | d6974f4 | 2014-02-14 13:39:21 -0800 | [diff] [blame] | 215 | #define CHECK_NOTNULL(val) ::aos::CheckNotNull(STRINGIFY(val), val) |
Brian Silverman | b067abf | 2013-09-13 22:01:06 -0700 | [diff] [blame] | 216 | |
| 217 | } // namespace aos |
| 218 | |
| 219 | #endif // __cplusplus |
| 220 | |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 221 | #endif |