blob: 6487b641c0794e705782ccac4c622db23e7feb20 [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
Brian Silvermanf665d692013-02-17 22:11:39 -080036#ifdef __cplusplus
37extern "C" {
38#endif
39// Actually implements the basic logging call.
40// Does not check that level is valid.
41void log_do(log_level level, const char *format, ...)
Brian Silvermanf7986142014-04-21 17:42:35 -070042 __attribute__((format(GOOD_PRINTF_FORMAT_TYPE, 2, 3)));
Brian Silvermanf665d692013-02-17 22:11:39 -080043
44void log_cork(int line, const char *function, const char *format, ...)
Brian Silvermanf7986142014-04-21 17:42:35 -070045 __attribute__((format(GOOD_PRINTF_FORMAT_TYPE, 3, 4)));
Brian Silvermanf665d692013-02-17 22:11:39 -080046// Implements the uncork logging call.
47void log_uncork(int line, const char *function, log_level level,
48 const char *file, const char *format, ...)
Brian Silvermanf7986142014-04-21 17:42:35 -070049 __attribute__((format(GOOD_PRINTF_FORMAT_TYPE, 5, 6)));
Brian Silvermanf665d692013-02-17 22:11:39 -080050#ifdef __cplusplus
51}
52#endif
53
54// A magical static const char[] or string literal that communicates the name
55// of the enclosing function.
56// It's currently using __PRETTY_FUNCTION__ because both GCC and Clang support
57// that and it gives nicer results in C++ than the standard __func__ (which
58// would also work).
Brian Silvermanc11f3b72013-03-19 18:32:02 -070059//#define LOG_CURRENT_FUNCTION __PRETTY_FUNCTION__
60#define LOG_CURRENT_FUNCTION __func__
Brian Silvermanf665d692013-02-17 22:11:39 -080061
Brian Silverman8fc7bc92013-03-13 23:15:47 -070062#undef LOG_SOURCENAME
63#define LOG_SOURCENAME __FILE__
64
Brian Silvermanf665d692013-02-17 22:11:39 -080065// The basic logging call.
Brian Silvermand6974f42014-02-14 13:39:21 -080066#define LOG(level, format, args...) \
67 do { \
68 log_do(level, LOG_SOURCENAME ": " STRINGIFY(__LINE__) ": %s: " format, \
69 LOG_CURRENT_FUNCTION, ##args); \
70 /* so that GCC knows that it won't return */ \
71 if (level == FATAL) { \
72 fprintf(stderr, "log_do(FATAL) fell through!!!!!\n"); \
73 printf("see stderr\n"); \
74 abort(); \
75 } \
76 } while (0)
brians343bc112013-02-10 01:53:46 +000077
78// Allows format to not be a string constant.
Brian Silvermand6974f42014-02-14 13:39:21 -080079#define LOG_DYNAMIC(level, format, args...) \
80 do { \
81 static char log_buf[LOG_MESSAGE_LEN]; \
82 int ret = snprintf(log_buf, sizeof(log_buf), format, ##args); \
83 if (ret < 0 || (uintmax_t)ret >= LOG_MESSAGE_LEN) { \
84 LOG(ERROR, "next message was too long so not subbing in args\n"); \
85 LOG(level, "%s", format); \
86 } else { \
87 LOG(level, "%s", log_buf); \
88 } \
89 } while (0)
brians343bc112013-02-10 01:53:46 +000090
Brian Silvermanf665d692013-02-17 22:11:39 -080091// Allows "bottling up" multiple log fragments which can then all be logged in
92// one message with LOG_UNCORK.
93// Calls from a given thread/task will be grouped together.
Brian Silvermand6974f42014-02-14 13:39:21 -080094#define LOG_CORK(format, args...) \
95 do { \
96 log_cork(__LINE__, LOG_CURRENT_FUNCTION, format, ##args); \
97 } while (0)
Brian Silvermanf665d692013-02-17 22:11:39 -080098// Actually logs all of the saved up log fragments (including format and args on
99// the end).
Brian Silvermand6974f42014-02-14 13:39:21 -0800100#define LOG_UNCORK(level, format, args...) \
101 do { \
102 log_uncork(__LINE__, LOG_CURRENT_FUNCTION, level, LOG_SOURCENAME, format, \
103 ##args); \
104 } while (0)
brians343bc112013-02-10 01:53:46 +0000105
brians343bc112013-02-10 01:53:46 +0000106#ifdef __cplusplus
107}
108#endif
109
Brian Silvermanb067abf2013-09-13 22:01:06 -0700110#ifdef __cplusplus
111
112namespace aos {
113
114// CHECK* macros, similar to glog
115// (<http://google-glog.googlecode.com/svn/trunk/doc/glog.html>)'s, except they
116// don't support streaming in extra text. Some of the implementation is borrowed
117// from there too.
118// They all LOG(FATAL) with a helpful message when the check fails.
119// TODO(brians): Replace assert with CHECK
120// Portions copyright (c) 1999, Google Inc.
121// All rights reserved.
122//
123// Redistribution and use in source and binary forms, with or without
124// modification, are permitted provided that the following conditions are
125// met:
126//
127// * Redistributions of source code must retain the above copyright
128// notice, this list of conditions and the following disclaimer.
129// * Redistributions in binary form must reproduce the above
130// copyright notice, this list of conditions and the following disclaimer
131// in the documentation and/or other materials provided with the
132// distribution.
133// * Neither the name of Google Inc. nor the names of its
134// contributors may be used to endorse or promote products derived from
135// this software without specific prior written permission.
136//
137// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
138// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
139// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
140// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
141// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
142// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
143// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
144// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
145// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
146// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
147// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
148
Brian Silverman862b1632013-09-14 15:29:59 -0700149// CHECK dies with a fatal error if condition is not true. It is *not*
150// controlled by NDEBUG, so the check will be executed regardless of
151// compilation mode. Therefore, it is safe to do things like:
152// CHECK(fp->Write(x) == 4)
Brian Silvermand6974f42014-02-14 13:39:21 -0800153#define CHECK(condition) \
154 if (__builtin_expect(!(condition), 0)) { \
Brian Silverman5d790752014-01-01 13:25:36 -0800155 LOG(FATAL, "CHECK(%s) failed\n", #condition); \
Brian Silverman862b1632013-09-14 15:29:59 -0700156 }
157
Brian Silvermanb067abf2013-09-13 22:01:06 -0700158// Helper functions for CHECK_OP macro.
159// The (int, int) specialization works around the issue that the compiler
160// will not instantiate the template version of the function on values of
161// unnamed enum type.
Brian Silvermand6974f42014-02-14 13:39:21 -0800162#define DEFINE_CHECK_OP_IMPL(name, op) \
163 template <typename T1, typename T2> \
164 inline void LogImpl##name(const T1 &v1, const T2 &v2, \
165 const char *exprtext) { \
166 if (!__builtin_expect(v1 op v2, 1)) { \
167 LOG(FATAL, "CHECK(%s) failed\n", exprtext); \
168 } \
169 } \
170 inline void LogImpl##name(int v1, int v2, const char *exprtext) { \
171 ::aos::LogImpl##name<int, int>(v1, v2, exprtext); \
Brian Silvermanb067abf2013-09-13 22:01:06 -0700172 }
173
174// We use the full name Check_EQ, Check_NE, etc. in case the file including
175// base/logging.h provides its own #defines for the simpler names EQ, NE, etc.
176// This happens if, for example, those are used as token names in a
177// yacc grammar.
178DEFINE_CHECK_OP_IMPL(Check_EQ, ==) // Compilation error with CHECK_EQ(NULL, x)?
179DEFINE_CHECK_OP_IMPL(Check_NE, !=) // Use CHECK(x == NULL) instead.
180DEFINE_CHECK_OP_IMPL(Check_LE, <=)
181DEFINE_CHECK_OP_IMPL(Check_LT, < )
182DEFINE_CHECK_OP_IMPL(Check_GE, >=)
183DEFINE_CHECK_OP_IMPL(Check_GT, > )
184
Brian Silvermand6974f42014-02-14 13:39:21 -0800185#define CHECK_OP(name, op, val1, val2) \
Brian Silvermanb067abf2013-09-13 22:01:06 -0700186 ::aos::LogImplCheck##name(val1, val2, \
187 STRINGIFY(val1) STRINGIFY(op) STRINGIFY(val2))
188
189#define CHECK_EQ(val1, val2) CHECK_OP(_EQ, ==, val1, val2)
190#define CHECK_NE(val1, val2) CHECK_OP(_NE, !=, val1, val2)
191#define CHECK_LE(val1, val2) CHECK_OP(_LE, <=, val1, val2)
192#define CHECK_LT(val1, val2) CHECK_OP(_LT, < , val1, val2)
193#define CHECK_GE(val1, val2) CHECK_OP(_GE, >=, val1, val2)
194#define CHECK_GT(val1, val2) CHECK_OP(_GT, > , val1, val2)
195
196// A small helper for CHECK_NOTNULL().
197template <typename T>
198inline T* CheckNotNull(const char *value_name, T *t) {
199 if (t == NULL) {
200 LOG(FATAL, "'%s' must not be NULL\n", value_name);
201 }
202 return t;
203}
204
205// Check that the input is non NULL. This very useful in constructor
206// initializer lists.
Brian Silvermand6974f42014-02-14 13:39:21 -0800207#define CHECK_NOTNULL(val) ::aos::CheckNotNull(STRINGIFY(val), val)
Brian Silvermanb067abf2013-09-13 22:01:06 -0700208
209} // namespace aos
210
211#endif // __cplusplus
212
brians343bc112013-02-10 01:53:46 +0000213#endif