3 Copyright (C) 2011 InvenSense Corporation, All Rights Reserved.
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
21 * This file incorporates work covered by the following copyright and
24 * Copyright (C) 2005 The Android Open Source Project
26 * Licensed under the Apache License, Version 2.0 (the "License");
27 * you may not use this file except in compliance with the License.
28 * You may obtain a copy of the License at
30 * http://www.apache.org/licenses/LICENSE-2.0
32 * Unless required by applicable law or agreed to in writing, software
33 * distributed under the License is distributed on an "AS IS" BASIS,
34 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
35 * See the License for the specific language governing permissions and
36 * limitations under the License.
40 * C/C++ logging functions. See the logging documentation for API details.
42 * We'd like these to be available from C code (in case we import some from
43 * somewhere), so this has a C interface.
45 * The output will be correct when the log file is shared between multiple
46 * threads and/or multiple processes so long as the operating system
47 * supports O_APPEND. These calls have mutex-protected data structures
48 * and so are NOT reentrant. Do not use MPL_LOG in a signal handler.
50 #ifndef _LIBS_CUTILS_MPL_LOG_H
51 #define _LIBS_CUTILS_MPL_LOG_H
57 #include <linux/kernel.h>
60 /* --------------------------------------------------------------------- */
63 * Normally we strip MPL_LOGV (VERBOSE messages) from release builds.
64 * You can modify this (for example with "#define MPL_LOG_NDEBUG 0"
65 * at the top of your source file) to change that behavior.
67 #ifndef MPL_LOG_NDEBUG
69 #define MPL_LOG_NDEBUG 1
71 #define MPL_LOG_NDEBUG 0
75 #define MPL_LOG_UNKNOWN MPL_LOG_VERBOSE
76 #define MPL_LOG_DEFAULT KERN_DEFAULT
77 #define MPL_LOG_VERBOSE KERN_CONT
78 #define MPL_LOG_DEBUG KERN_NOTICE
79 #define MPL_LOG_INFO KERN_INFO
80 #define MPL_LOG_WARN KERN_WARNING
81 #define MPL_LOG_ERROR KERN_ERR
82 #define MPL_LOG_SILENT MPL_LOG_VERBOSE
87 * This is the local tag used for the following simplified
88 * logging macros. You can change this preprocessor definition
89 * before using the other macros to change the tag.
95 /* --------------------------------------------------------------------- */
98 * Simplified macro to send a verbose log message using the current MPL_LOG_TAG.
102 #define MPL_LOGV(fmt, ...) \
105 MPL_LOG(LOG_VERBOSE, MPL_LOG_TAG, fmt, ##__VA_ARGS__);\
108 #define MPL_LOGV(fmt, ...) MPL_LOG(LOG_VERBOSE, MPL_LOG_TAG, fmt, ##__VA_ARGS__)
113 #define CONDITION(cond) ((cond) != 0)
118 #define MPL_LOGV_IF(cond, fmt, ...) \
119 do { if (0) MPL_LOG(fmt, ##__VA_ARGS__); } while (0)
121 #define MPL_LOGV_IF(cond, fmt, ...) \
123 ? MPL_LOG(LOG_VERBOSE, MPL_LOG_TAG, fmt, ##__VA_ARGS__) \
129 * Simplified macro to send a debug log message using the current MPL_LOG_TAG.
132 #define MPL_LOGD(fmt, ...) MPL_LOG(LOG_DEBUG, MPL_LOG_TAG, fmt, ##__VA_ARGS__)
136 #define MPL_LOGD_IF(cond, fmt, ...) \
138 ? MPL_LOG(LOG_DEBUG, MPL_LOG_TAG, fmt, ##__VA_ARGS__) \
143 * Simplified macro to send an info log message using the current MPL_LOG_TAG.
146 #define MPL_LOGI(fmt, ...) pr_info(KERN_INFO MPL_LOG_TAG fmt, ##__VA_ARGS__)
150 #define MPL_LOGI_IF(cond, fmt, ...) \
152 ? MPL_LOG(LOG_INFO, MPL_LOG_TAG, fmt, ##__VA_ARGS__) \
157 * Simplified macro to send a warning log message using the current MPL_LOG_TAG.
160 #define MPL_LOGW(fmt, ...) printk(KERN_WARNING MPL_LOG_TAG fmt, ##__VA_ARGS__)
164 #define MPL_LOGW_IF(cond, fmt, ...) \
166 ? MPL_LOG(LOG_WARN, MPL_LOG_TAG, fmt, ##__VA_ARGS__) \
171 * Simplified macro to send an error log message using the current MPL_LOG_TAG.
174 #define MPL_LOGE(fmt, ...) printk(KERN_ERR MPL_LOG_TAG fmt, ##__VA_ARGS__)
178 #define MPL_LOGE_IF(cond, fmt, ...) \
180 ? MPL_LOG(LOG_ERROR, MPL_LOG_TAG, fmt, ##__VA_ARGS__) \
184 /* --------------------------------------------------------------------- */
187 * Log a fatal error. If the given condition fails, this stops program
188 * execution like a normal assertion, but also generating the given message.
189 * It is NOT stripped from release builds. Note that the condition test
190 * is -inverted- from the normal assert() semantics.
192 #define MPL_LOG_ALWAYS_FATAL_IF(cond, fmt, ...) \
194 ? ((void)android_printAssert(#cond, MPL_LOG_TAG, \
195 fmt, ##__VA_ARGS__)) \
198 #define MPL_LOG_ALWAYS_FATAL(fmt, ...) \
199 (((void)android_printAssert(NULL, MPL_LOG_TAG, fmt, ##__VA_ARGS__)))
202 * Versions of MPL_LOG_ALWAYS_FATAL_IF and MPL_LOG_ALWAYS_FATAL that
203 * are stripped out of release builds.
206 #define MPL_LOG_FATAL_IF(cond, fmt, ...) \
209 MPL_LOG_ALWAYS_FATAL_IF(cond, fmt, ##__VA_ARGS__); \
211 #define MPL_LOG_FATAL(fmt, ...) \
214 MPL_LOG_ALWAYS_FATAL(fmt, ##__VA_ARGS__) \
217 #define MPL_LOG_FATAL_IF(cond, fmt, ...) \
218 MPL_LOG_ALWAYS_FATAL_IF(cond, fmt, ##__VA_ARGS__)
219 #define MPL_LOG_FATAL(fmt, ...) \
220 MPL_LOG_ALWAYS_FATAL(fmt, ##__VA_ARGS__)
224 * Assertion that generates a log message when the assertion fails.
225 * Stripped out of release builds. Uses the current MPL_LOG_TAG.
227 #define MPL_LOG_ASSERT(cond, fmt, ...) \
228 MPL_LOG_FATAL_IF(!(cond), fmt, ##__VA_ARGS__)
230 /* --------------------------------------------------------------------- */
233 * Basic log message macro.
236 * MPL_LOG(MPL_LOG_WARN, NULL, "Failed with error %d", errno);
238 * The second argument may be NULL or "" to indicate the "global" tag.
241 #define MPL_LOG(priority, tag, fmt, ...) \
242 MPL_LOG_PRI(priority, tag, fmt, ##__VA_ARGS__)
246 * Log macro that allows you to specify a number for the priority.
249 #define MPL_LOG_PRI(priority, tag, fmt, ...) \
250 pr_debug(MPL_##priority tag fmt, ##__VA_ARGS__)
254 * Log macro that allows you to pass in a varargs ("args" is a va_list).
256 #ifndef MPL_LOG_PRI_VA
257 /* not allowed in the Kernel because there is no dev_dbg that takes a va_list */
260 /* --------------------------------------------------------------------- */
263 * ===========================================================================
265 * The stuff in the rest of this file should not be used directly.
268 int _MLPrintLog(int priority, const char *tag, const char *fmt, ...);
269 int _MLPrintVaLog(int priority, const char *tag, const char *fmt, va_list args);
270 /* Final implementation of actual writing to a character device */
271 int _MLWriteLog(const char *buf, int buflen);
273 static inline void __print_result_location(int result,
275 const char *func, int line)
277 MPL_LOGE("%s|%s|%d returning %d\n", file, func, line, result);
280 #define LOG_RESULT_LOCATION(condition) \
282 __print_result_location((int)(condition), __FILE__, \
283 __func__, __LINE__); \
287 #endif /* _LIBS_CUTILS_MPL_LOG_H */