blob: 6682098cf33d4e408a30e63cd7ae4694a7b52a34 [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
7#include <stdio.h>
brians343bc112013-02-10 01:53:46 +00008#include <stdint.h>
Brian Silvermanb067abf2013-09-13 22:01:06 -07009#include <stdlib.h>
Brian Silverman01be0002014-05-10 15:44:38 -070010#include <string.h>
11#include <errno.h>
brians343bc112013-02-10 01:53:46 +000012
John Park33858a32018-09-28 23:05:48 -070013#include "aos/macros.h"
14#include "aos/libc/aos_strerror.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
brians343bc112013-02-10 01:53:46 +000022#define DECL_LEVELS \
23DECL_LEVEL(DEBUG, 0); /* stuff that gets printed out every cycle */ \
24DECL_LEVEL(INFO, 1); /* things like PosEdge/NegEdge */ \
Brian Silvermanf665d692013-02-17 22:11:39 -080025/* things that might still work if they happen occasionally */ \
brians343bc112013-02-10 01:53:46 +000026DECL_LEVEL(WARNING, 2); \
27/*-1 so that vxworks macro of same name will have same effect if used*/ \
28DECL_LEVEL(ERROR, -1); /* errors */ \
Brian Silvermanf665d692013-02-17 22:11:39 -080029/* serious errors. the logging code will terminate the process/task */ \
30DECL_LEVEL(FATAL, 4); \
brians343bc112013-02-10 01:53:46 +000031DECL_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, ...)
Brian Silvermanf7986142014-04-21 17:42:35 -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, ...)
Brian Silvermanf7986142014-04-21 17:42:35 -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, ...)
Brian Silvermanf7986142014-04-21 17:42:35 -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.
Brian Silvermand6974f42014-02-14 13:39:21 -080067#define LOG(level, format, args...) \
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)
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.
81#define PLOG(level, format, args...) PELOG(level, errno, format, ##args)
82
83// Like PLOG except allows specifying an error other than errno.
Brian Silvermanb3efcca2014-06-03 19:47:06 -070084#define PELOG(level, error_in, format, args...) \
85 do { \
86 const int error = error_in; \
87 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.
Brian Silvermand6974f42014-02-14 13:39:21 -080092#define 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 LOG(ERROR, "next message was too long so not subbing in args\n"); \
98 LOG(level, "%s", format); \
99 } else { \
100 LOG(level, "%s", log_buf); \
101 } \
102 } 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.
Brian Silvermand6974f42014-02-14 13:39:21 -0800107#define LOG_CORK(format, args...) \
108 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).
Brian Silvermand6974f42014-02-14 13:39:21 -0800113#define LOG_UNCORK(level, format, args...) \
114 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)
Brian Silvermand6974f42014-02-14 13:39:21 -0800165#define CHECK(condition) \
166 if (__builtin_expect(!(condition), 0)) { \
Brian Silverman5d790752014-01-01 13:25:36 -0800167 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.
Brian Silverman83b71b82014-08-14 23:18:52 -0700174#define DEFINE_CHECK_OP_IMPL(name, op) \
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 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.
195DEFINE_CHECK_OP_IMPL(Check_EQ, ==) // Compilation error with CHECK_EQ(NULL, x)?
196DEFINE_CHECK_OP_IMPL(Check_NE, !=) // Use CHECK(x == NULL) instead.
197DEFINE_CHECK_OP_IMPL(Check_LE, <=)
198DEFINE_CHECK_OP_IMPL(Check_LT, < )
199DEFINE_CHECK_OP_IMPL(Check_GE, >=)
200DEFINE_CHECK_OP_IMPL(Check_GT, > )
201
Brian Silvermand6974f42014-02-14 13:39:21 -0800202#define CHECK_OP(name, op, val1, val2) \
Brian Silvermanb067abf2013-09-13 22:01:06 -0700203 ::aos::LogImplCheck##name(val1, val2, \
204 STRINGIFY(val1) STRINGIFY(op) STRINGIFY(val2))
205
206#define CHECK_EQ(val1, val2) CHECK_OP(_EQ, ==, val1, val2)
207#define CHECK_NE(val1, val2) CHECK_OP(_NE, !=, val1, val2)
208#define CHECK_LE(val1, val2) CHECK_OP(_LE, <=, val1, val2)
209#define CHECK_LT(val1, val2) CHECK_OP(_LT, < , val1, val2)
210#define CHECK_GE(val1, val2) CHECK_OP(_GE, >=, val1, val2)
211#define CHECK_GT(val1, val2) CHECK_OP(_GT, > , val1, val2)
212
213// A small helper for CHECK_NOTNULL().
214template <typename T>
215inline T* CheckNotNull(const char *value_name, T *t) {
216 if (t == NULL) {
217 LOG(FATAL, "'%s' must not be NULL\n", value_name);
218 }
219 return t;
220}
221
222// Check that the input is non NULL. This very useful in constructor
223// initializer lists.
Brian Silvermand6974f42014-02-14 13:39:21 -0800224#define 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)) {
228 PLOG(FATAL, "%s failed", syscall_string);
229 }
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)) {
235 PELOG(FATAL, value, "%s failed", syscall_string);
236 }
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//
243// Example: const int fd = PCHECK(open("/tmp/whatever", O_WRONLY))
Brian Silvermanfe457de2014-05-26 22:04:08 -0700244#define PCHECK(syscall) ::aos::CheckSyscall(STRINGIFY(syscall), syscall)
245
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//
251// Example: PRCHECK(munmap(address, length))
252#define PRCHECK(syscall) ::aos::CheckSyscallReturn(STRINGIFY(syscall), syscall)
253
Brian Silvermanb067abf2013-09-13 22:01:06 -0700254} // namespace aos
255
256#endif // __cplusplus
257
brians343bc112013-02-10 01:53:46 +0000258#endif