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