blob: bd9b1038fb312d9d6c0c2822d106087756a931aa [file] [log] [blame]
John Park33858a32018-09-28 23:05:48 -07001#ifndef AOS_LOGGING_LOGGING_H_
2#define AOS_LOGGING_LOGGING_H_
brians343bc112013-02-10 01:53:46 +00003
Brian Silvermanf665d692013-02-17 22:11:39 -08004// This file contains the logging client interface. It works with both C and C++
5// code.
6
Austin Schuhf257f3c2019-10-27 21:00:43 -07007#include <errno.h>
brians343bc112013-02-10 01:53:46 +00008#include <stdint.h>
Austin Schuhf257f3c2019-10-27 21:00:43 -07009#include <stdio.h>
Brian Silvermanb067abf2013-09-13 22:01:06 -070010#include <stdlib.h>
Brian Silverman01be0002014-05-10 15:44:38 -070011#include <string.h>
brians343bc112013-02-10 01:53:46 +000012
John Park33858a32018-09-28 23:05:48 -070013#include "aos/libc/aos_strerror.h"
Austin Schuhf257f3c2019-10-27 21:00:43 -070014#include "aos/macros.h"
Brian Silvermana7234c62014-03-24 20:23:25 -070015
brians343bc112013-02-10 01:53:46 +000016#ifdef __cplusplus
17extern "C" {
18#endif
19
20typedef uint8_t log_level;
Brian Silvermanf665d692013-02-17 22:11:39 -080021
Austin Schuhf257f3c2019-10-27 21:00:43 -070022#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 Silvermanf665d692013-02-17 22:11:39 -080032#define DECL_LEVEL(name, value) static const log_level name = value;
Brian Silvermanf665d692013-02-17 22:11:39 -080033DECL_LEVELS;
brians343bc112013-02-10 01:53:46 +000034#undef DECL_LEVEL
35
Brian Silvermanf665d692013-02-17 22:11:39 -080036#ifdef __cplusplus
37extern "C" {
38#endif
Brian Silverman01be0002014-05-10 15:44:38 -070039
Brian Silvermanf665d692013-02-17 22:11:39 -080040// Actually implements the basic logging call.
41// Does not check that level is valid.
42void log_do(log_level level, const char *format, ...)
Austin Schuhf257f3c2019-10-27 21:00:43 -070043 __attribute__((format(GOOD_PRINTF_FORMAT_TYPE, 2, 3)));
Brian Silvermanf665d692013-02-17 22:11:39 -080044
45void log_cork(int line, const char *function, const char *format, ...)
Austin Schuhf257f3c2019-10-27 21:00:43 -070046 __attribute__((format(GOOD_PRINTF_FORMAT_TYPE, 3, 4)));
Brian Silvermanf665d692013-02-17 22:11:39 -080047// Implements the uncork logging call.
48void log_uncork(int line, const char *function, log_level level,
49 const char *file, const char *format, ...)
Austin Schuhf257f3c2019-10-27 21:00:43 -070050 __attribute__((format(GOOD_PRINTF_FORMAT_TYPE, 5, 6)));
Brian Silverman01be0002014-05-10 15:44:38 -070051
Brian Silvermanf665d692013-02-17 22:11:39 -080052#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 Silvermanc11f3b72013-03-19 18:32:02 -070061//#define LOG_CURRENT_FUNCTION __PRETTY_FUNCTION__
62#define LOG_CURRENT_FUNCTION __func__
Brian Silvermanf665d692013-02-17 22:11:39 -080063
Brian Silverman8fc7bc92013-03-13 23:15:47 -070064#define LOG_SOURCENAME __FILE__
65
Brian Silvermanf665d692013-02-17 22:11:39 -080066// The basic logging call.
Austin Schuhf257f3c2019-10-27 21:00:43 -070067#define AOS_LOG(level, format, args...) \
Brian Silvermand6974f42014-02-14 13:39:21 -080068 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)
brians343bc112013-02-10 01:53:46 +000078
Brian Silvermanb3efcca2014-06-03 19:47:06 -070079// Same as LOG except appends " due to %d (%s)\n" (formatted with errno and
Brian Silverman01be0002014-05-10 15:44:38 -070080// aos_strerror(errno)) to the message.
Austin Schuhf257f3c2019-10-27 21:00:43 -070081#define AOS_PLOG(level, format, args...) AOS_PELOG(level, errno, format, ##args)
Brian Silverman01be0002014-05-10 15:44:38 -070082
83// Like PLOG except allows specifying an error other than errno.
Austin Schuhf257f3c2019-10-27 21:00:43 -070084#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 Silverman01be0002014-05-10 15:44:38 -070089 } while (0);
90
brians343bc112013-02-10 01:53:46 +000091// Allows format to not be a string constant.
Austin Schuhf257f3c2019-10-27 21:00:43 -070092#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 Silvermand6974f42014-02-14 13:39:21 -0800102 } while (0)
brians343bc112013-02-10 01:53:46 +0000103
Brian Silvermanf665d692013-02-17 22:11:39 -0800104// 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 Schuhf257f3c2019-10-27 21:00:43 -0700107#define AOS_LOG_CORK(format, args...) \
Brian Silvermand6974f42014-02-14 13:39:21 -0800108 do { \
109 log_cork(__LINE__, LOG_CURRENT_FUNCTION, format, ##args); \
110 } while (0)
Brian Silvermanf665d692013-02-17 22:11:39 -0800111// Actually logs all of the saved up log fragments (including format and args on
112// the end).
Austin Schuhf257f3c2019-10-27 21:00:43 -0700113#define AOS_LOG_UNCORK(level, format, args...) \
Brian Silvermand6974f42014-02-14 13:39:21 -0800114 do { \
115 log_uncork(__LINE__, LOG_CURRENT_FUNCTION, level, LOG_SOURCENAME, format, \
116 ##args); \
117 } while (0)
brians343bc112013-02-10 01:53:46 +0000118
brians343bc112013-02-10 01:53:46 +0000119#ifdef __cplusplus
120}
121#endif
122
Brian Silvermanb067abf2013-09-13 22:01:06 -0700123#ifdef __cplusplus
124
125namespace 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 Silvermanb067abf2013-09-13 22:01:06 -0700132// 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 Silverman862b1632013-09-14 15:29:59 -0700161// 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 Schuhf257f3c2019-10-27 21:00:43 -0700165#define AOS_CHECK(condition) \
166 if (__builtin_expect(!(condition), 0)) { \
167 AOS_LOG(FATAL, "CHECK(%s) failed\n", #condition); \
Brian Silverman862b1632013-09-14 15:29:59 -0700168 }
169
Brian Silvermanb067abf2013-09-13 22:01:06 -0700170// 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 Schuhf257f3c2019-10-27 21:00:43 -0700174#define AOS_DEFINE_CHECK_OP_IMPL(name, op) \
Brian Silverman83b71b82014-08-14 23:18:52 -0700175 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 Silvermanb067abf2013-09-13 22:01:06 -0700189 }
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 Schuhf257f3c2019-10-27 21:00:43 -0700195AOS_DEFINE_CHECK_OP_IMPL(Check_EQ, ==) // Compilation error with CHECK_EQ(NULL, x)?
196AOS_DEFINE_CHECK_OP_IMPL(Check_NE, !=) // Use CHECK(x == NULL) instead.
197AOS_DEFINE_CHECK_OP_IMPL(Check_LE, <=)
198AOS_DEFINE_CHECK_OP_IMPL(Check_LT, <)
199AOS_DEFINE_CHECK_OP_IMPL(Check_GE, >=)
200AOS_DEFINE_CHECK_OP_IMPL(Check_GT, >)
Brian Silvermanb067abf2013-09-13 22:01:06 -0700201
Austin Schuhf257f3c2019-10-27 21:00:43 -0700202#define AOS_CHECK_OP(name, op, val1, val2) \
203 ::aos::LogImplCheck##name(val1, val2, \
Brian Silvermanb067abf2013-09-13 22:01:06 -0700204 STRINGIFY(val1) STRINGIFY(op) STRINGIFY(val2))
205
Austin Schuhf257f3c2019-10-27 21:00:43 -0700206#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 Silvermanb067abf2013-09-13 22:01:06 -0700212
213// A small helper for CHECK_NOTNULL().
214template <typename T>
Austin Schuhf257f3c2019-10-27 21:00:43 -0700215inline T *CheckNotNull(const char *value_name, T *t) {
Brian Silvermanb067abf2013-09-13 22:01:06 -0700216 if (t == NULL) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700217 AOS_LOG(FATAL, "'%s' must not be NULL\n", value_name);
Brian Silvermanb067abf2013-09-13 22:01:06 -0700218 }
219 return t;
220}
221
222// Check that the input is non NULL. This very useful in constructor
223// initializer lists.
Austin Schuhf257f3c2019-10-27 21:00:43 -0700224#define AOS_CHECK_NOTNULL(val) ::aos::CheckNotNull(STRINGIFY(val), val)
Brian Silvermanb067abf2013-09-13 22:01:06 -0700225
Brian Silvermanfe457de2014-05-26 22:04:08 -0700226inline int CheckSyscall(const char *syscall_string, int value) {
227 if (__builtin_expect(value == -1, false)) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700228 AOS_PLOG(FATAL, "%s failed", syscall_string);
Brian Silvermanfe457de2014-05-26 22:04:08 -0700229 }
230 return value;
231}
232
Brian Silvermanb20e06a2014-09-03 12:09:24 -0400233inline void CheckSyscallReturn(const char *syscall_string, int value) {
234 if (__builtin_expect(value != 0, false)) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700235 AOS_PELOG(FATAL, value, "%s failed", syscall_string);
Brian Silvermanb20e06a2014-09-03 12:09:24 -0400236 }
237}
238
Brian Silvermanfe457de2014-05-26 22:04:08 -0700239// 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 Silvermanb20e06a2014-09-03 12:09:24 -0400241// the values of any of the arguments. Returns the result otherwise.
242//
Austin Schuhf257f3c2019-10-27 21:00:43 -0700243// Example: const int fd = AOS_PCHECK(open("/tmp/whatever", O_WRONLY))
244#define AOS_PCHECK(syscall) ::aos::CheckSyscall(STRINGIFY(syscall), syscall)
Brian Silvermanfe457de2014-05-26 22:04:08 -0700245
Brian Silvermanb20e06a2014-09-03 12:09:24 -0400246// 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 Schuhf257f3c2019-10-27 21:00:43 -0700251// Example: AOS_PRCHECK(munmap(address, length))
252#define AOS_PRCHECK(syscall) \
253 ::aos::CheckSyscallReturn(STRINGIFY(syscall), syscall)
Brian Silvermanb20e06a2014-09-03 12:09:24 -0400254
Brian Silvermanb067abf2013-09-13 22:01:06 -0700255} // namespace aos
256
257#endif // __cplusplus
258
brians343bc112013-02-10 01:53:46 +0000259#endif