blob: c54e4340862efad4a7eedb672de16694312712d2 [file] [log] [blame]
brians343bc112013-02-10 01:53:46 +00001#ifndef AOS_COMMON_LOGGING_LOGGING_H_
brians343bc112013-02-10 01:53:46 +00002#define AOS_COMMON_LOGGING_LOGGING_H_
3
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
Brian Silvermana7234c62014-03-24 20:23:25 -070013#include "aos/common/macros.h"
Brian Silvermanaf784862014-05-13 08:14:55 -070014#include "aos/common/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 Silvermanb0893882014-02-10 14:48:30 -080036// Not static const size_t for C code.
Brian Silvermanc11f3b72013-03-19 18:32:02 -070037#define LOG_MESSAGE_LEN 400
Brian Silvermanf665d692013-02-17 22:11:39 -080038
Brian Silvermanf665d692013-02-17 22:11:39 -080039#ifdef __cplusplus
40extern "C" {
41#endif
Brian Silverman01be0002014-05-10 15:44:38 -070042
Brian Silvermanf665d692013-02-17 22:11:39 -080043// Actually implements the basic logging call.
44// Does not check that level is valid.
45void log_do(log_level level, const char *format, ...)
Brian Silvermanf7986142014-04-21 17:42:35 -070046 __attribute__((format(GOOD_PRINTF_FORMAT_TYPE, 2, 3)));
Brian Silvermanf665d692013-02-17 22:11:39 -080047
48void log_cork(int line, const char *function, const char *format, ...)
Brian Silvermanf7986142014-04-21 17:42:35 -070049 __attribute__((format(GOOD_PRINTF_FORMAT_TYPE, 3, 4)));
Brian Silvermanf665d692013-02-17 22:11:39 -080050// Implements the uncork logging call.
51void log_uncork(int line, const char *function, log_level level,
52 const char *file, const char *format, ...)
Brian Silvermanf7986142014-04-21 17:42:35 -070053 __attribute__((format(GOOD_PRINTF_FORMAT_TYPE, 5, 6)));
Brian Silverman01be0002014-05-10 15:44:38 -070054
Brian Silvermanf665d692013-02-17 22:11:39 -080055#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 Silvermanc11f3b72013-03-19 18:32:02 -070064//#define LOG_CURRENT_FUNCTION __PRETTY_FUNCTION__
65#define LOG_CURRENT_FUNCTION __func__
Brian Silvermanf665d692013-02-17 22:11:39 -080066
Brian Silverman8fc7bc92013-03-13 23:15:47 -070067#define LOG_SOURCENAME __FILE__
68
Brian Silvermanf665d692013-02-17 22:11:39 -080069// The basic logging call.
Brian Silvermand6974f42014-02-14 13:39:21 -080070#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)
brians343bc112013-02-10 01:53:46 +000081
Brian Silverman01be0002014-05-10 15:44:38 -070082// 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
brians343bc112013-02-10 01:53:46 +000093// Allows format to not be a string constant.
Brian Silvermand6974f42014-02-14 13:39:21 -080094#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)
brians343bc112013-02-10 01:53:46 +0000105
Brian Silvermanf665d692013-02-17 22:11:39 -0800106// 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 Silvermand6974f42014-02-14 13:39:21 -0800109#define LOG_CORK(format, args...) \
110 do { \
111 log_cork(__LINE__, LOG_CURRENT_FUNCTION, format, ##args); \
112 } while (0)
Brian Silvermanf665d692013-02-17 22:11:39 -0800113// Actually logs all of the saved up log fragments (including format and args on
114// the end).
Brian Silvermand6974f42014-02-14 13:39:21 -0800115#define LOG_UNCORK(level, format, args...) \
116 do { \
117 log_uncork(__LINE__, LOG_CURRENT_FUNCTION, level, LOG_SOURCENAME, format, \
118 ##args); \
119 } while (0)
brians343bc112013-02-10 01:53:46 +0000120
brians343bc112013-02-10 01:53:46 +0000121#ifdef __cplusplus
122}
123#endif
124
Brian Silvermanb067abf2013-09-13 22:01:06 -0700125#ifdef __cplusplus
126
127namespace 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.
Brian Silvermanb067abf2013-09-13 22:01:06 -0700134// Portions copyright (c) 1999, Google Inc.
135// All rights reserved.
136//
137// Redistribution and use in source and binary forms, with or without
138// modification, are permitted provided that the following conditions are
139// met:
140//
141// * Redistributions of source code must retain the above copyright
142// notice, this list of conditions and the following disclaimer.
143// * Redistributions in binary form must reproduce the above
144// copyright notice, this list of conditions and the following disclaimer
145// in the documentation and/or other materials provided with the
146// distribution.
147// * Neither the name of Google Inc. nor the names of its
148// contributors may be used to endorse or promote products derived from
149// this software without specific prior written permission.
150//
151// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
152// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
153// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
154// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
155// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
156// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
157// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
158// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
159// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
160// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
161// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
162
Brian Silverman862b1632013-09-14 15:29:59 -0700163// CHECK dies with a fatal error if condition is not true. It is *not*
164// controlled by NDEBUG, so the check will be executed regardless of
165// compilation mode. Therefore, it is safe to do things like:
166// CHECK(fp->Write(x) == 4)
Brian Silvermand6974f42014-02-14 13:39:21 -0800167#define CHECK(condition) \
168 if (__builtin_expect(!(condition), 0)) { \
Brian Silverman5d790752014-01-01 13:25:36 -0800169 LOG(FATAL, "CHECK(%s) failed\n", #condition); \
Brian Silverman862b1632013-09-14 15:29:59 -0700170 }
171
Brian Silvermanb067abf2013-09-13 22:01:06 -0700172// Helper functions for CHECK_OP macro.
173// The (int, int) specialization works around the issue that the compiler
174// will not instantiate the template version of the function on values of
175// unnamed enum type.
Brian Silvermand6974f42014-02-14 13:39:21 -0800176#define DEFINE_CHECK_OP_IMPL(name, op) \
177 template <typename T1, typename T2> \
178 inline void LogImpl##name(const T1 &v1, const T2 &v2, \
179 const char *exprtext) { \
180 if (!__builtin_expect(v1 op v2, 1)) { \
181 LOG(FATAL, "CHECK(%s) failed\n", exprtext); \
182 } \
183 } \
184 inline void LogImpl##name(int v1, int v2, const char *exprtext) { \
185 ::aos::LogImpl##name<int, int>(v1, v2, exprtext); \
Brian Silvermanb067abf2013-09-13 22:01:06 -0700186 }
187
188// We use the full name Check_EQ, Check_NE, etc. in case the file including
189// base/logging.h provides its own #defines for the simpler names EQ, NE, etc.
190// This happens if, for example, those are used as token names in a
191// yacc grammar.
192DEFINE_CHECK_OP_IMPL(Check_EQ, ==) // Compilation error with CHECK_EQ(NULL, x)?
193DEFINE_CHECK_OP_IMPL(Check_NE, !=) // Use CHECK(x == NULL) instead.
194DEFINE_CHECK_OP_IMPL(Check_LE, <=)
195DEFINE_CHECK_OP_IMPL(Check_LT, < )
196DEFINE_CHECK_OP_IMPL(Check_GE, >=)
197DEFINE_CHECK_OP_IMPL(Check_GT, > )
198
Brian Silvermand6974f42014-02-14 13:39:21 -0800199#define CHECK_OP(name, op, val1, val2) \
Brian Silvermanb067abf2013-09-13 22:01:06 -0700200 ::aos::LogImplCheck##name(val1, val2, \
201 STRINGIFY(val1) STRINGIFY(op) STRINGIFY(val2))
202
203#define CHECK_EQ(val1, val2) CHECK_OP(_EQ, ==, val1, val2)
204#define CHECK_NE(val1, val2) CHECK_OP(_NE, !=, val1, val2)
205#define CHECK_LE(val1, val2) CHECK_OP(_LE, <=, val1, val2)
206#define CHECK_LT(val1, val2) CHECK_OP(_LT, < , val1, val2)
207#define CHECK_GE(val1, val2) CHECK_OP(_GE, >=, val1, val2)
208#define CHECK_GT(val1, val2) CHECK_OP(_GT, > , val1, val2)
209
210// A small helper for CHECK_NOTNULL().
211template <typename T>
212inline T* CheckNotNull(const char *value_name, T *t) {
213 if (t == NULL) {
214 LOG(FATAL, "'%s' must not be NULL\n", value_name);
215 }
216 return t;
217}
218
219// Check that the input is non NULL. This very useful in constructor
220// initializer lists.
Brian Silvermand6974f42014-02-14 13:39:21 -0800221#define CHECK_NOTNULL(val) ::aos::CheckNotNull(STRINGIFY(val), val)
Brian Silvermanb067abf2013-09-13 22:01:06 -0700222
Brian Silvermanfe457de2014-05-26 22:04:08 -0700223inline int CheckSyscall(const char *syscall_string, int value) {
224 if (__builtin_expect(value == -1, false)) {
225 PLOG(FATAL, "%s failed", syscall_string);
226 }
227 return value;
228}
229
230// Check that syscall does not return -1. If it does, PLOG(FATAL)s. This is
231// useful for quickly checking syscalls where it's not very useful to print out
232// the values of any of the arguments.
233#define PCHECK(syscall) ::aos::CheckSyscall(STRINGIFY(syscall), syscall)
234
Brian Silvermanb067abf2013-09-13 22:01:06 -0700235} // namespace aos
236
237#endif // __cplusplus
238
brians343bc112013-02-10 01:53:46 +0000239#endif