log.h 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. /*
  2. $License:
  3. Copyright (C) 2011 InvenSense Corporation, All Rights Reserved.
  4. $
  5. */
  6. /*
  7. * This file incorporates work covered by the following copyright and
  8. * permission notice:
  9. *
  10. * Copyright (C) 2005 The Android Open Source Project
  11. *
  12. * Licensed under the Apache License, Version 2.0 (the "License");
  13. * you may not use this file except in compliance with the License.
  14. * You may obtain a copy of the License at
  15. *
  16. * http://www.apache.org/licenses/LICENSE-2.0
  17. *
  18. * Unless required by applicable law or agreed to in writing, software
  19. * distributed under the License is distributed on an "AS IS" BASIS,
  20. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  21. * See the License for the specific language governing permissions and
  22. * limitations under the License.
  23. */
  24. /*
  25. * C/C++ logging functions. See the logging documentation for API details.
  26. *
  27. * We'd like these to be available from C code (in case we import some from
  28. * somewhere), so this has a C interface.
  29. *
  30. * The output will be correct when the log file is shared between multiple
  31. * threads and/or multiple processes so long as the operating system
  32. * supports O_APPEND. These calls have mutex-protected data structures
  33. * and so are NOT reentrant. Do not use MPL_LOG in a signal handler.
  34. */
  35. #ifndef _LIBS_CUTILS_MPL_LOG_H
  36. #define _LIBS_CUTILS_MPL_LOG_H
  37. #include <stdlib.h>
  38. #include <stdarg.h>
  39. #ifdef ANDROID
  40. #ifdef NDK_BUILD
  41. #include "log_macros.h"
  42. #else
  43. #include <utils/Log.h> /* For the LOG macro */
  44. #endif
  45. #endif
  46. #ifdef __KERNEL__
  47. #include <linux/kernel.h>
  48. #endif
  49. #ifdef __cplusplus
  50. extern "C" {
  51. #endif
  52. #if defined ANDROID_JELLYBEAN
  53. #define LOG ALOG
  54. #define LOG_ERRROR ANDROID_LOG_ERROR
  55. #endif
  56. /* --------------------------------------------------------------------- */
  57. /*
  58. * Normally we strip MPL_LOGV (VERBOSE messages) from release builds.
  59. * You can modify this (for example with "#define MPL_LOG_NDEBUG 0"
  60. * at the top of your source file) to change that behavior.
  61. */
  62. #ifndef MPL_LOG_NDEBUG
  63. #ifdef NDEBUG
  64. #define MPL_LOG_NDEBUG 1
  65. #else
  66. #define MPL_LOG_NDEBUG 0
  67. #endif
  68. #endif
  69. #ifdef __KERNEL__
  70. #define MPL_LOG_UNKNOWN MPL_LOG_VERBOSE
  71. #define MPL_LOG_DEFAULT KERN_DEFAULT
  72. #define MPL_LOG_VERBOSE KERN_CONT
  73. #define MPL_LOG_DEBUG KERN_NOTICE
  74. #define MPL_LOG_INFO KERN_INFO
  75. #define MPL_LOG_WARN KERN_WARNING
  76. #define MPL_LOG_ERROR KERN_ERR
  77. #define MPL_LOG_SILENT MPL_LOG_VERBOSE
  78. #else
  79. /* Based off the log priorities in android
  80. /system/core/include/android/log.h */
  81. #define MPL_LOG_UNKNOWN (0)
  82. #define MPL_LOG_DEFAULT (1)
  83. #define MPL_LOG_VERBOSE (2)
  84. #define MPL_LOG_DEBUG (3)
  85. #define MPL_LOG_INFO (4)
  86. #define MPL_LOG_WARN (5)
  87. #define MPL_LOG_ERROR (6)
  88. #define MPL_LOG_SILENT (8)
  89. #endif
  90. /*
  91. * This is the local tag used for the following simplified
  92. * logging macros. You can change this preprocessor definition
  93. * before using the other macros to change the tag.
  94. */
  95. #ifndef MPL_LOG_TAG
  96. #ifdef __KERNEL__
  97. #define MPL_LOG_TAG
  98. #else
  99. #define MPL_LOG_TAG NULL
  100. #endif
  101. #endif
  102. /* --------------------------------------------------------------------- */
  103. /*
  104. * Simplified macro to send a verbose log message using the current MPL_LOG_TAG.
  105. */
  106. #ifndef MPL_LOGV
  107. #if MPL_LOG_NDEBUG
  108. #ifdef _WIN32
  109. #define MPL_LOGV(fmt, ...) \
  110. do { \
  111. __pragma (warning(suppress : 4127 )) \
  112. if (0) \
  113. MPL_LOG(LOG_VERBOSE, MPL_LOG_TAG, fmt, ##__VA_ARGS__);\
  114. __pragma (warning(suppress : 4127 )) \
  115. } while (0)
  116. #else
  117. #define MPL_LOGV(fmt, ...) \
  118. do { \
  119. if (0) \
  120. MPL_LOG(LOG_VERBOSE, MPL_LOG_TAG, fmt, ##__VA_ARGS__);\
  121. } while (0)
  122. #endif
  123. #else
  124. #define MPL_LOGV(fmt, ...) MPL_LOG(LOG_VERBOSE, MPL_LOG_TAG, fmt, ##__VA_ARGS__)
  125. #endif
  126. #endif
  127. #ifndef CONDITION
  128. #define CONDITION(cond) ((cond) != 0)
  129. #endif
  130. #ifndef MPL_LOGV_IF
  131. #if MPL_LOG_NDEBUG
  132. #define MPL_LOGV_IF(cond, fmt, ...) \
  133. do { if (0) MPL_LOG(fmt, ##__VA_ARGS__); } while (0)
  134. #else
  135. #define MPL_LOGV_IF(cond, fmt, ...) \
  136. ((CONDITION(cond)) \
  137. ? MPL_LOG(LOG_VERBOSE, MPL_LOG_TAG, fmt, ##__VA_ARGS__) \
  138. : (void)0)
  139. #endif
  140. #endif
  141. /*
  142. * Simplified macro to send a debug log message using the current MPL_LOG_TAG.
  143. */
  144. #ifndef MPL_LOGD
  145. #define MPL_LOGD(fmt, ...) MPL_LOG(LOG_DEBUG, MPL_LOG_TAG, fmt, ##__VA_ARGS__)
  146. #endif
  147. #ifndef MPL_LOGD_IF
  148. #define MPL_LOGD_IF(cond, fmt, ...) \
  149. ((CONDITION(cond)) \
  150. ? MPL_LOG(LOG_DEBUG, MPL_LOG_TAG, fmt, ##__VA_ARGS__) \
  151. : (void)0)
  152. #endif
  153. /*
  154. * Simplified macro to send an info log message using the current MPL_LOG_TAG.
  155. */
  156. #ifndef MPL_LOGI
  157. #ifdef __KERNEL__
  158. #define MPL_LOGI(fmt, ...) pr_info(KERN_INFO MPL_LOG_TAG fmt, ##__VA_ARGS__)
  159. #else
  160. #define MPL_LOGI(fmt, ...) MPL_LOG(LOG_INFO, MPL_LOG_TAG, fmt, ##__VA_ARGS__)
  161. #endif
  162. #endif
  163. #ifndef MPL_LOGI_IF
  164. #define MPL_LOGI_IF(cond, fmt, ...) \
  165. ((CONDITION(cond)) \
  166. ? MPL_LOG(LOG_INFO, MPL_LOG_TAG, fmt, ##__VA_ARGS__) \
  167. : (void)0)
  168. #endif
  169. /*
  170. * Simplified macro to send a warning log message using the current MPL_LOG_TAG.
  171. */
  172. #ifndef MPL_LOGW
  173. #ifdef __KERNEL__
  174. #define MPL_LOGW(fmt, ...) printk(KERN_WARNING MPL_LOG_TAG fmt, ##__VA_ARGS__)
  175. #else
  176. #define MPL_LOGW(fmt, ...) MPL_LOG(LOG_WARN, MPL_LOG_TAG, fmt, ##__VA_ARGS__)
  177. #endif
  178. #endif
  179. #ifndef MPL_LOGW_IF
  180. #define MPL_LOGW_IF(cond, fmt, ...) \
  181. ((CONDITION(cond)) \
  182. ? MPL_LOG(LOG_WARN, MPL_LOG_TAG, fmt, ##__VA_ARGS__) \
  183. : (void)0)
  184. #endif
  185. /*
  186. * Simplified macro to send an error log message using the current MPL_LOG_TAG.
  187. */
  188. #ifndef MPL_LOGE
  189. #ifdef __KERNEL__
  190. #define MPL_LOGE(fmt, ...) printk(KERN_ERR MPL_LOG_TAG fmt, ##__VA_ARGS__)
  191. #else
  192. #define MPL_LOGE(fmt, ...) MPL_LOG(LOG_ERROR, MPL_LOG_TAG, fmt, ##__VA_ARGS__)
  193. #endif
  194. #endif
  195. #ifndef MPL_LOGE_IF
  196. #define MPL_LOGE_IF(cond, fmt, ...) \
  197. ((CONDITION(cond)) \
  198. ? MPL_LOG(LOG_ERROR, MPL_LOG_TAG, fmt, ##__VA_ARGS__) \
  199. : (void)0)
  200. #endif
  201. /* --------------------------------------------------------------------- */
  202. /*
  203. * Log a fatal error. If the given condition fails, this stops program
  204. * execution like a normal assertion, but also generating the given message.
  205. * It is NOT stripped from release builds. Note that the condition test
  206. * is -inverted- from the normal assert() semantics.
  207. */
  208. #define MPL_LOG_ALWAYS_FATAL_IF(cond, fmt, ...) \
  209. ((CONDITION(cond)) \
  210. ? ((void)android_printAssert(#cond, MPL_LOG_TAG, \
  211. fmt, ##__VA_ARGS__)) \
  212. : (void)0)
  213. #define MPL_LOG_ALWAYS_FATAL(fmt, ...) \
  214. (((void)android_printAssert(NULL, MPL_LOG_TAG, fmt, ##__VA_ARGS__)))
  215. /*
  216. * Versions of MPL_LOG_ALWAYS_FATAL_IF and MPL_LOG_ALWAYS_FATAL that
  217. * are stripped out of release builds.
  218. */
  219. #if MPL_LOG_NDEBUG
  220. #define MPL_LOG_FATAL_IF(cond, fmt, ...) \
  221. do { \
  222. if (0) \
  223. MPL_LOG_ALWAYS_FATAL_IF(cond, fmt, ##__VA_ARGS__); \
  224. } while (0)
  225. #define MPL_LOG_FATAL(fmt, ...) \
  226. do { \
  227. if (0) \
  228. MPL_LOG_ALWAYS_FATAL(fmt, ##__VA_ARGS__) \
  229. } while (0)
  230. #else
  231. #define MPL_LOG_FATAL_IF(cond, fmt, ...) \
  232. MPL_LOG_ALWAYS_FATAL_IF(cond, fmt, ##__VA_ARGS__)
  233. #define MPL_LOG_FATAL(fmt, ...) \
  234. MPL_LOG_ALWAYS_FATAL(fmt, ##__VA_ARGS__)
  235. #endif
  236. /*
  237. * Assertion that generates a log message when the assertion fails.
  238. * Stripped out of release builds. Uses the current MPL_LOG_TAG.
  239. */
  240. #define MPL_LOG_ASSERT(cond, fmt, ...) \
  241. MPL_LOG_FATAL_IF(!(cond), fmt, ##__VA_ARGS__)
  242. /* --------------------------------------------------------------------- */
  243. /*
  244. * Basic log message macro.
  245. *
  246. * Example:
  247. * MPL_LOG(MPL_LOG_WARN, NULL, "Failed with error %d", errno);
  248. *
  249. * The second argument may be NULL or "" to indicate the "global" tag.
  250. */
  251. #ifndef MPL_LOG
  252. #ifdef REMOVE_LOGGING
  253. #define MPL_LOG(priority, tag, fmt, ...) do {} while (0)
  254. #else
  255. #define MPL_LOG(priority, tag, fmt, ...) \
  256. MPL_LOG_PRI(priority, tag, fmt, ##__VA_ARGS__)
  257. #endif
  258. #endif
  259. /*
  260. * Log macro that allows you to specify a number for the priority.
  261. */
  262. #ifndef MPL_LOG_PRI
  263. #ifdef ANDROID
  264. #define MPL_LOG_PRI(priority, tag, fmt, ...) \
  265. LOG(priority, tag, fmt, ##__VA_ARGS__)
  266. #elif defined __KERNEL__
  267. #define MPL_LOG_PRI(priority, tag, fmt, ...) \
  268. pr_debug(MPL_##priority tag fmt, ##__VA_ARGS__)
  269. #else
  270. #define MPL_LOG_PRI(priority, tag, fmt, ...) \
  271. _MLPrintLog(MPL_##priority, tag, fmt, ##__VA_ARGS__)
  272. #endif
  273. #endif
  274. /*
  275. * Log macro that allows you to pass in a varargs ("args" is a va_list).
  276. */
  277. #ifndef MPL_LOG_PRI_VA
  278. #ifdef ANDROID
  279. #define MPL_LOG_PRI_VA(priority, tag, fmt, args) \
  280. android_vprintLog(priority, NULL, tag, fmt, args)
  281. #elif defined __KERNEL__
  282. /* not allowed in the Kernel because there is no dev_dbg that takes a va_list */
  283. #else
  284. #define MPL_LOG_PRI_VA(priority, tag, fmt, args) \
  285. _MLPrintVaLog(priority, NULL, tag, fmt, args)
  286. #endif
  287. #endif
  288. /* --------------------------------------------------------------------- */
  289. /*
  290. * ===========================================================================
  291. *
  292. * The stuff in the rest of this file should not be used directly.
  293. */
  294. #ifndef ANDROID
  295. int _MLPrintLog(int priority, const char *tag, const char *fmt, ...);
  296. int _MLPrintVaLog(int priority, const char *tag, const char *fmt, va_list args);
  297. /* Final implementation of actual writing to a character device */
  298. int _MLWriteLog(const char *buf, int buflen);
  299. #endif
  300. static inline void __print_result_location(int result,
  301. const char *file,
  302. const char *func, int line)
  303. {
  304. MPL_LOGE("%s|%s|%d returning %d\n", file, func, line, result);
  305. }
  306. #ifdef _WIN32
  307. /* The pragma removes warning about expression being constant */
  308. #define LOG_RESULT_LOCATION(condition) \
  309. do { \
  310. __print_result_location((int)(condition), __FILE__, \
  311. __func__, __LINE__); \
  312. __pragma (warning(suppress : 4127 )) \
  313. } while (0)
  314. #else
  315. #define LOG_RESULT_LOCATION(condition) \
  316. do { \
  317. __print_result_location((int)(condition), __FILE__, \
  318. __func__, __LINE__); \
  319. } while (0)
  320. #endif
  321. #define INV_ERROR_CHECK(r_1329) \
  322. if (r_1329) { \
  323. LOG_RESULT_LOCATION(r_1329); \
  324. return r_1329; \
  325. }
  326. #ifdef __cplusplus
  327. }
  328. #endif
  329. #endif /* _LIBS_CUTILS_MPL_LOG_H */