blob: c71ee460738ecccaebcc55e93f3fba8a2f8582f6 [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.
74#define LOG(level, format, args...) 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 Silvermanf665d692013-02-17 22:11:39 -080086#define LOG_DYNAMIC(level, format, args...) do { \
brians343bc112013-02-10 01:53:46 +000087 static char log_buf[LOG_MESSAGE_LEN]; \
88 int ret = snprintf(log_buf, sizeof(log_buf), format, ##args); \
Brian Silvermanf665d692013-02-17 22:11:39 -080089 if (ret < 0 || (uintmax_t)ret >= LOG_MESSAGE_LEN) { \
90 LOG(ERROR, "next message was too long so not subbing in args\n"); \
brians343bc112013-02-10 01:53:46 +000091 LOG(level, "%s", format); \
92 }else{ \
93 LOG(level, "%s", log_buf); \
94 } \
Brian Silvermanf665d692013-02-17 22:11:39 -080095} while (0)
brians343bc112013-02-10 01:53:46 +000096
Brian Silvermanf665d692013-02-17 22:11:39 -080097// Allows "bottling up" multiple log fragments which can then all be logged in
98// one message with LOG_UNCORK.
99// Calls from a given thread/task will be grouped together.
100#define LOG_CORK(format, args...) do { \
101 log_cork(__LINE__, LOG_CURRENT_FUNCTION, format, ##args); \
102} while (0)
103// Actually logs all of the saved up log fragments (including format and args on
104// the end).
105#define LOG_UNCORK(level, format, args...) do { \
106 log_uncork(__LINE__, LOG_CURRENT_FUNCTION, level, LOG_SOURCENAME, \
107 format, ##args); \
108} while (0)
brians343bc112013-02-10 01:53:46 +0000109
brians343bc112013-02-10 01:53:46 +0000110#ifdef __cplusplus
111}
112#endif
113
Brian Silvermanb067abf2013-09-13 22:01:06 -0700114#ifdef __cplusplus
115
116namespace aos {
117
118// CHECK* macros, similar to glog
119// (<http://google-glog.googlecode.com/svn/trunk/doc/glog.html>)'s, except they
120// don't support streaming in extra text. Some of the implementation is borrowed
121// from there too.
122// They all LOG(FATAL) with a helpful message when the check fails.
123// TODO(brians): Replace assert with CHECK
124// Portions copyright (c) 1999, Google Inc.
125// All rights reserved.
126//
127// Redistribution and use in source and binary forms, with or without
128// modification, are permitted provided that the following conditions are
129// met:
130//
131// * Redistributions of source code must retain the above copyright
132// notice, this list of conditions and the following disclaimer.
133// * Redistributions in binary form must reproduce the above
134// copyright notice, this list of conditions and the following disclaimer
135// in the documentation and/or other materials provided with the
136// distribution.
137// * Neither the name of Google Inc. nor the names of its
138// contributors may be used to endorse or promote products derived from
139// this software without specific prior written permission.
140//
141// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
142// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
143// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
144// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
145// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
146// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
147// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
148// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
149// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
150// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
151// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
152
Brian Silverman862b1632013-09-14 15:29:59 -0700153// CHECK dies with a fatal error if condition is not true. It is *not*
154// controlled by NDEBUG, so the check will be executed regardless of
155// compilation mode. Therefore, it is safe to do things like:
156// CHECK(fp->Write(x) == 4)
157#define CHECK(condition) \
158 if (__builtin_expect(!(condition), 0)) { \
Brian Silverman5d790752014-01-01 13:25:36 -0800159 LOG(FATAL, "CHECK(%s) failed\n", #condition); \
Brian Silverman862b1632013-09-14 15:29:59 -0700160 }
161
Brian Silvermanb067abf2013-09-13 22:01:06 -0700162// Helper functions for CHECK_OP macro.
163// The (int, int) specialization works around the issue that the compiler
164// will not instantiate the template version of the function on values of
165// unnamed enum type.
166#define DEFINE_CHECK_OP_IMPL(name, op) \
167 template <typename T1, typename T2> \
168 inline void LogImpl##name(const T1& v1, const T2& v2, \
169 const char* exprtext) { \
170 if (!__builtin_expect(v1 op v2, 1)) { \
171 LOG(FATAL, "CHECK(%s) failed\n", exprtext); \
172 } \
173 } \
174 inline void LogImpl##name(int v1, int v2, const char* exprtext) { \
175 ::aos::LogImpl##name<int, int>(v1, v2, exprtext); \
176 }
177
178// We use the full name Check_EQ, Check_NE, etc. in case the file including
179// base/logging.h provides its own #defines for the simpler names EQ, NE, etc.
180// This happens if, for example, those are used as token names in a
181// yacc grammar.
182DEFINE_CHECK_OP_IMPL(Check_EQ, ==) // Compilation error with CHECK_EQ(NULL, x)?
183DEFINE_CHECK_OP_IMPL(Check_NE, !=) // Use CHECK(x == NULL) instead.
184DEFINE_CHECK_OP_IMPL(Check_LE, <=)
185DEFINE_CHECK_OP_IMPL(Check_LT, < )
186DEFINE_CHECK_OP_IMPL(Check_GE, >=)
187DEFINE_CHECK_OP_IMPL(Check_GT, > )
188
189#define CHECK_OP(name, op, val1, val2) \
190 ::aos::LogImplCheck##name(val1, val2, \
191 STRINGIFY(val1) STRINGIFY(op) STRINGIFY(val2))
192
193#define CHECK_EQ(val1, val2) CHECK_OP(_EQ, ==, val1, val2)
194#define CHECK_NE(val1, val2) CHECK_OP(_NE, !=, val1, val2)
195#define CHECK_LE(val1, val2) CHECK_OP(_LE, <=, val1, val2)
196#define CHECK_LT(val1, val2) CHECK_OP(_LT, < , val1, val2)
197#define CHECK_GE(val1, val2) CHECK_OP(_GE, >=, val1, val2)
198#define CHECK_GT(val1, val2) CHECK_OP(_GT, > , val1, val2)
199
200// A small helper for CHECK_NOTNULL().
201template <typename T>
202inline T* CheckNotNull(const char *value_name, T *t) {
203 if (t == NULL) {
204 LOG(FATAL, "'%s' must not be NULL\n", value_name);
205 }
206 return t;
207}
208
209// Check that the input is non NULL. This very useful in constructor
210// initializer lists.
211#define CHECK_NOTNULL(val) \
212 ::aos::CheckNotNull(STRINGIFY(val), val)
213
214} // namespace aos
215
216#endif // __cplusplus
217
brians343bc112013-02-10 01:53:46 +0000218#endif