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