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