log.h 8.3 KB

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