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