log.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568
  1. /*
  2. * Copyright (C) 2005-2014 The Android Open Source Project
  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 LOG in a signal handler.
  26. //
  27. #ifndef COCOS_CUTILS_LOG_H
  28. #define COCOS_CUTILS_LOG_H
  29. #include <stdarg.h>
  30. #include <stdio.h>
  31. #include <sys/types.h>
  32. #include <time.h>
  33. #include <unistd.h>
  34. #include <android/log.h>
  35. #ifdef __cplusplus
  36. extern "C" {
  37. #endif
  38. // ---------------------------------------------------------------------
  39. /*
  40. * Normally we strip ALOGV (VERBOSE messages) from release builds.
  41. * You can modify this (for example with "#define LOG_NDEBUG 0"
  42. * at the top of your source file) to change that behavior.
  43. */
  44. #ifndef LOG_NDEBUG
  45. #if defined(COCOS2D_DEBUG) && COCOS2D_DEBUG > 0
  46. #define LOG_NDEBUG 0
  47. #else
  48. #define LOG_NDEBUG 1
  49. #endif
  50. #endif
  51. /*
  52. * This is the local tag used for the following simplified
  53. * logging macros. You can change this preprocessor definition
  54. * before using the other macros to change the tag.
  55. */
  56. #ifndef LOG_TAG
  57. #define LOG_TAG NULL
  58. #endif
  59. // ---------------------------------------------------------------------
  60. #ifndef __predict_false
  61. #define __predict_false(exp) __builtin_expect((exp) != 0, 0)
  62. #endif
  63. /*
  64. * -DLINT_RLOG in sources that you want to enforce that all logging
  65. * goes to the radio log buffer. If any logging goes to any of the other
  66. * log buffers, there will be a compile or link error to highlight the
  67. * problem. This is not a replacement for a full audit of the code since
  68. * this only catches compiled code, not ifdef'd debug code. Options to
  69. * defining this, either temporarily to do a spot check, or permanently
  70. * to enforce, in all the communications trees; We have hopes to ensure
  71. * that by supplying just the radio log buffer that the communications
  72. * teams will have their one-stop shop for triaging issues.
  73. */
  74. #ifndef LINT_RLOG
  75. /*
  76. * Simplified macro to send a verbose log message using the current LOG_TAG.
  77. */
  78. #ifndef ALOGV
  79. #define __ALOGV(...) ((void)ALOG(LOG_VERBOSE, LOG_TAG, __VA_ARGS__))
  80. #if LOG_NDEBUG
  81. #define ALOGV(...) do { if (0) { __ALOGV(__VA_ARGS__); } } while (0)
  82. #else
  83. #define ALOGV(...) __ALOGV(__VA_ARGS__)
  84. #endif
  85. #endif
  86. #ifndef ALOGV_IF
  87. #if LOG_NDEBUG
  88. #define ALOGV_IF(cond, ...) ((void)0)
  89. #else
  90. #define ALOGV_IF(cond, ...) \
  91. ( (__predict_false(cond)) \
  92. ? ((void)ALOG(LOG_VERBOSE, LOG_TAG, __VA_ARGS__)) \
  93. : (void)0 )
  94. #endif
  95. #endif
  96. /*
  97. * Simplified macro to send a debug log message using the current LOG_TAG.
  98. */
  99. #ifndef ALOGD
  100. #define ALOGD(...) ((void)ALOG(LOG_DEBUG, LOG_TAG, __VA_ARGS__))
  101. #endif
  102. #ifndef ALOGD_IF
  103. #define ALOGD_IF(cond, ...) \
  104. ( (__predict_false(cond)) \
  105. ? ((void)ALOG(LOG_DEBUG, LOG_TAG, __VA_ARGS__)) \
  106. : (void)0 )
  107. #endif
  108. /*
  109. * Simplified macro to send an info log message using the current LOG_TAG.
  110. */
  111. #ifndef ALOGI
  112. #define ALOGI(...) ((void)ALOG(LOG_INFO, LOG_TAG, __VA_ARGS__))
  113. #endif
  114. #ifndef ALOGI_IF
  115. #define ALOGI_IF(cond, ...) \
  116. ( (__predict_false(cond)) \
  117. ? ((void)ALOG(LOG_INFO, LOG_TAG, __VA_ARGS__)) \
  118. : (void)0 )
  119. #endif
  120. /*
  121. * Simplified macro to send a warning log message using the current LOG_TAG.
  122. */
  123. #ifndef ALOGW
  124. #define ALOGW(...) ((void)ALOG(LOG_WARN, LOG_TAG, __VA_ARGS__))
  125. #endif
  126. #ifndef ALOGW_IF
  127. #define ALOGW_IF(cond, ...) \
  128. ( (__predict_false(cond)) \
  129. ? ((void)ALOG(LOG_WARN, LOG_TAG, __VA_ARGS__)) \
  130. : (void)0 )
  131. #endif
  132. /*
  133. * Simplified macro to send an error log message using the current LOG_TAG.
  134. */
  135. #ifndef ALOGE
  136. #define ALOGE(...) ((void)ALOG(LOG_ERROR, LOG_TAG, __VA_ARGS__))
  137. #endif
  138. #ifndef ALOGE_IF
  139. #define ALOGE_IF(cond, ...) \
  140. ( (__predict_false(cond)) \
  141. ? ((void)ALOG(LOG_ERROR, LOG_TAG, __VA_ARGS__)) \
  142. : (void)0 )
  143. #endif
  144. // ---------------------------------------------------------------------
  145. /*
  146. * Conditional based on whether the current LOG_TAG is enabled at
  147. * verbose priority.
  148. */
  149. #ifndef IF_ALOGV
  150. #if LOG_NDEBUG
  151. #define IF_ALOGV() if (false)
  152. #else
  153. #define IF_ALOGV() IF_ALOG(LOG_VERBOSE, LOG_TAG)
  154. #endif
  155. #endif
  156. /*
  157. * Conditional based on whether the current LOG_TAG is enabled at
  158. * debug priority.
  159. */
  160. #ifndef IF_ALOGD
  161. #define IF_ALOGD() IF_ALOG(LOG_DEBUG, LOG_TAG)
  162. #endif
  163. /*
  164. * Conditional based on whether the current LOG_TAG is enabled at
  165. * info priority.
  166. */
  167. #ifndef IF_ALOGI
  168. #define IF_ALOGI() IF_ALOG(LOG_INFO, LOG_TAG)
  169. #endif
  170. /*
  171. * Conditional based on whether the current LOG_TAG is enabled at
  172. * warn priority.
  173. */
  174. #ifndef IF_ALOGW
  175. #define IF_ALOGW() IF_ALOG(LOG_WARN, LOG_TAG)
  176. #endif
  177. /*
  178. * Conditional based on whether the current LOG_TAG is enabled at
  179. * error priority.
  180. */
  181. #ifndef IF_ALOGE
  182. #define IF_ALOGE() IF_ALOG(LOG_ERROR, LOG_TAG)
  183. #endif
  184. // ---------------------------------------------------------------------
  185. /*
  186. * Simplified macro to send a verbose system log message using the current LOG_TAG.
  187. */
  188. #ifndef SLOGV
  189. #define __SLOGV(...) \
  190. ((void)__android_log_buf_print(LOG_ID_SYSTEM, ANDROID_LOG_VERBOSE, LOG_TAG, __VA_ARGS__))
  191. #if LOG_NDEBUG
  192. #define SLOGV(...) do { if (0) { __SLOGV(__VA_ARGS__); } } while (0)
  193. #else
  194. #define SLOGV(...) __SLOGV(__VA_ARGS__)
  195. #endif
  196. #endif
  197. #ifndef SLOGV_IF
  198. #if LOG_NDEBUG
  199. #define SLOGV_IF(cond, ...) ((void)0)
  200. #else
  201. #define SLOGV_IF(cond, ...) \
  202. ( (__predict_false(cond)) \
  203. ? ((void)__android_log_buf_print(LOG_ID_SYSTEM, ANDROID_LOG_VERBOSE, LOG_TAG, __VA_ARGS__)) \
  204. : (void)0 )
  205. #endif
  206. #endif
  207. /*
  208. * Simplified macro to send a debug system log message using the current LOG_TAG.
  209. */
  210. #ifndef SLOGD
  211. #define SLOGD(...) \
  212. ((void)__android_log_buf_print(LOG_ID_SYSTEM, ANDROID_LOG_DEBUG, LOG_TAG, __VA_ARGS__))
  213. #endif
  214. #ifndef SLOGD_IF
  215. #define SLOGD_IF(cond, ...) \
  216. ( (__predict_false(cond)) \
  217. ? ((void)__android_log_buf_print(LOG_ID_SYSTEM, ANDROID_LOG_DEBUG, LOG_TAG, __VA_ARGS__)) \
  218. : (void)0 )
  219. #endif
  220. /*
  221. * Simplified macro to send an info system log message using the current LOG_TAG.
  222. */
  223. #ifndef SLOGI
  224. #define SLOGI(...) \
  225. ((void)__android_log_buf_print(LOG_ID_SYSTEM, ANDROID_LOG_INFO, LOG_TAG, __VA_ARGS__))
  226. #endif
  227. #ifndef SLOGI_IF
  228. #define SLOGI_IF(cond, ...) \
  229. ( (__predict_false(cond)) \
  230. ? ((void)__android_log_buf_print(LOG_ID_SYSTEM, ANDROID_LOG_INFO, LOG_TAG, __VA_ARGS__)) \
  231. : (void)0 )
  232. #endif
  233. /*
  234. * Simplified macro to send a warning system log message using the current LOG_TAG.
  235. */
  236. #ifndef SLOGW
  237. #define SLOGW(...) \
  238. ((void)__android_log_buf_print(LOG_ID_SYSTEM, ANDROID_LOG_WARN, LOG_TAG, __VA_ARGS__))
  239. #endif
  240. #ifndef SLOGW_IF
  241. #define SLOGW_IF(cond, ...) \
  242. ( (__predict_false(cond)) \
  243. ? ((void)__android_log_buf_print(LOG_ID_SYSTEM, ANDROID_LOG_WARN, LOG_TAG, __VA_ARGS__)) \
  244. : (void)0 )
  245. #endif
  246. /*
  247. * Simplified macro to send an error system log message using the current LOG_TAG.
  248. */
  249. #ifndef SLOGE
  250. #define SLOGE(...) \
  251. ((void)__android_log_buf_print(LOG_ID_SYSTEM, ANDROID_LOG_ERROR, LOG_TAG, __VA_ARGS__))
  252. #endif
  253. #ifndef SLOGE_IF
  254. #define SLOGE_IF(cond, ...) \
  255. ( (__predict_false(cond)) \
  256. ? ((void)__android_log_buf_print(LOG_ID_SYSTEM, ANDROID_LOG_ERROR, LOG_TAG, __VA_ARGS__)) \
  257. : (void)0 )
  258. #endif
  259. #endif /* !LINT_RLOG */
  260. // ---------------------------------------------------------------------
  261. /*
  262. * Simplified macro to send a verbose radio log message using the current LOG_TAG.
  263. */
  264. #ifndef RLOGV
  265. #define __RLOGV(...) \
  266. ((void)__android_log_buf_print(LOG_ID_RADIO, ANDROID_LOG_VERBOSE, LOG_TAG, __VA_ARGS__))
  267. #if LOG_NDEBUG
  268. #define RLOGV(...) do { if (0) { __RLOGV(__VA_ARGS__); } } while (0)
  269. #else
  270. #define RLOGV(...) __RLOGV(__VA_ARGS__)
  271. #endif
  272. #endif
  273. #ifndef RLOGV_IF
  274. #if LOG_NDEBUG
  275. #define RLOGV_IF(cond, ...) ((void)0)
  276. #else
  277. #define RLOGV_IF(cond, ...) \
  278. ( (__predict_false(cond)) \
  279. ? ((void)__android_log_buf_print(LOG_ID_RADIO, ANDROID_LOG_VERBOSE, LOG_TAG, __VA_ARGS__)) \
  280. : (void)0 )
  281. #endif
  282. #endif
  283. /*
  284. * Simplified macro to send a debug radio log message using the current LOG_TAG.
  285. */
  286. #ifndef RLOGD
  287. #define RLOGD(...) \
  288. ((void)__android_log_buf_print(LOG_ID_RADIO, ANDROID_LOG_DEBUG, LOG_TAG, __VA_ARGS__))
  289. #endif
  290. #ifndef RLOGD_IF
  291. #define RLOGD_IF(cond, ...) \
  292. ( (__predict_false(cond)) \
  293. ? ((void)__android_log_buf_print(LOG_ID_RADIO, ANDROID_LOG_DEBUG, LOG_TAG, __VA_ARGS__)) \
  294. : (void)0 )
  295. #endif
  296. /*
  297. * Simplified macro to send an info radio log message using the current LOG_TAG.
  298. */
  299. #ifndef RLOGI
  300. #define RLOGI(...) \
  301. ((void)__android_log_buf_print(LOG_ID_RADIO, ANDROID_LOG_INFO, LOG_TAG, __VA_ARGS__))
  302. #endif
  303. #ifndef RLOGI_IF
  304. #define RLOGI_IF(cond, ...) \
  305. ( (__predict_false(cond)) \
  306. ? ((void)__android_log_buf_print(LOG_ID_RADIO, ANDROID_LOG_INFO, LOG_TAG, __VA_ARGS__)) \
  307. : (void)0 )
  308. #endif
  309. /*
  310. * Simplified macro to send a warning radio log message using the current LOG_TAG.
  311. */
  312. #ifndef RLOGW
  313. #define RLOGW(...) \
  314. ((void)__android_log_buf_print(LOG_ID_RADIO, ANDROID_LOG_WARN, LOG_TAG, __VA_ARGS__))
  315. #endif
  316. #ifndef RLOGW_IF
  317. #define RLOGW_IF(cond, ...) \
  318. ( (__predict_false(cond)) \
  319. ? ((void)__android_log_buf_print(LOG_ID_RADIO, ANDROID_LOG_WARN, LOG_TAG, __VA_ARGS__)) \
  320. : (void)0 )
  321. #endif
  322. /*
  323. * Simplified macro to send an error radio log message using the current LOG_TAG.
  324. */
  325. #ifndef RLOGE
  326. #define RLOGE(...) \
  327. ((void)__android_log_buf_print(LOG_ID_RADIO, ANDROID_LOG_ERROR, LOG_TAG, __VA_ARGS__))
  328. #endif
  329. #ifndef RLOGE_IF
  330. #define RLOGE_IF(cond, ...) \
  331. ( (__predict_false(cond)) \
  332. ? ((void)__android_log_buf_print(LOG_ID_RADIO, ANDROID_LOG_ERROR, LOG_TAG, __VA_ARGS__)) \
  333. : (void)0 )
  334. #endif
  335. // ---------------------------------------------------------------------
  336. /*
  337. * Log a fatal error. If the given condition fails, this stops program
  338. * execution like a normal assertion, but also generating the given message.
  339. * It is NOT stripped from release builds. Note that the condition test
  340. * is -inverted- from the normal assert() semantics.
  341. */
  342. #ifndef LOG_ALWAYS_FATAL_IF
  343. #define LOG_ALWAYS_FATAL_IF(cond, ...) \
  344. ( (__predict_false(cond)) \
  345. ? ((void)android_printAssert(#cond, LOG_TAG, ## __VA_ARGS__)) \
  346. : (void)0 )
  347. #endif
  348. #ifndef LOG_ALWAYS_FATAL
  349. #define LOG_ALWAYS_FATAL(...) \
  350. ( ((void)android_printAssert(NULL, LOG_TAG, ## __VA_ARGS__)) )
  351. #endif
  352. /*
  353. * Versions of LOG_ALWAYS_FATAL_IF and LOG_ALWAYS_FATAL that
  354. * are stripped out of release builds.
  355. */
  356. #if LOG_NDEBUG
  357. #ifndef LOG_FATAL_IF
  358. #define LOG_FATAL_IF(cond, ...) ((void)0)
  359. #endif
  360. #ifndef LOG_FATAL
  361. #define LOG_FATAL(...) ((void)0)
  362. #endif
  363. #else
  364. #ifndef LOG_FATAL_IF
  365. #define LOG_FATAL_IF(cond, ...) LOG_ALWAYS_FATAL_IF(cond, ## __VA_ARGS__)
  366. #endif
  367. #ifndef LOG_FATAL
  368. #define LOG_FATAL(...) LOG_ALWAYS_FATAL(__VA_ARGS__)
  369. #endif
  370. #endif
  371. /*
  372. * Assertion that generates a log message when the assertion fails.
  373. * Stripped out of release builds. Uses the current LOG_TAG.
  374. */
  375. #ifndef ALOG_ASSERT
  376. #define ALOG_ASSERT(cond, ...) LOG_FATAL_IF(!(cond), ## __VA_ARGS__)
  377. //#define ALOG_ASSERT(cond) LOG_FATAL_IF(!(cond), "Assertion failed: " #cond)
  378. #endif
  379. // ---------------------------------------------------------------------
  380. /*
  381. * Basic log message macro.
  382. *
  383. * Example:
  384. * ALOG(LOG_WARN, NULL, "Failed with error %d", errno);
  385. *
  386. * The second argument may be NULL or "" to indicate the "global" tag.
  387. */
  388. #ifndef ALOG
  389. #define ALOG(priority, tag, ...) \
  390. LOG_PRI(ANDROID_##priority, tag, __VA_ARGS__)
  391. #endif
  392. /*
  393. * Log macro that allows you to specify a number for the priority.
  394. */
  395. #ifndef LOG_PRI
  396. #define LOG_PRI(priority, tag, ...) \
  397. android_printLog(priority, tag, __VA_ARGS__)
  398. #endif
  399. /*
  400. * Log macro that allows you to pass in a varargs ("args" is a va_list).
  401. */
  402. #ifndef LOG_PRI_VA
  403. #define LOG_PRI_VA(priority, tag, fmt, args) \
  404. android_vprintLog(priority, NULL, tag, fmt, args)
  405. #endif
  406. /*
  407. * Conditional given a desired logging priority and tag.
  408. */
  409. #ifndef IF_ALOG
  410. #define IF_ALOG(priority, tag) \
  411. if (android_testLog(ANDROID_##priority, tag))
  412. #endif
  413. // ---------------------------------------------------------------------
  414. /*
  415. * ===========================================================================
  416. *
  417. * The stuff in the rest of this file should not be used directly.
  418. */
  419. #define android_printLog(prio, tag, ...) \
  420. __android_log_print(prio, tag, __VA_ARGS__)
  421. #define android_vprintLog(prio, cond, tag, ...) \
  422. __android_log_vprint(prio, tag, __VA_ARGS__)
  423. /* XXX Macros to work around syntax errors in places where format string
  424. * arg is not passed to ALOG_ASSERT, LOG_ALWAYS_FATAL or LOG_ALWAYS_FATAL_IF
  425. * (happens only in debug builds).
  426. */
  427. /* Returns 2nd arg. Used to substitute default value if caller's vararg list
  428. * is empty.
  429. */
  430. #define __android_second(dummy, second, ...) second
  431. /* If passed multiple args, returns ',' followed by all but 1st arg, otherwise
  432. * returns nothing.
  433. */
  434. #define __android_rest(first, ...) , ## __VA_ARGS__
  435. #define android_printAssert(cond, tag, ...) \
  436. __android_log_assert(cond, tag, \
  437. __android_second(0, ## __VA_ARGS__, NULL) __android_rest(__VA_ARGS__))
  438. #define android_writeLog(prio, tag, text) \
  439. __android_log_write(prio, tag, text)
  440. #define android_bWriteLog(tag, payload, len) \
  441. __android_log_bwrite(tag, payload, len)
  442. #define android_btWriteLog(tag, type, payload, len) \
  443. __android_log_btwrite(tag, type, payload, len)
  444. #define android_errorWriteLog(tag, subTag) \
  445. __android_log_error_write(tag, subTag, -1, NULL, 0)
  446. #define android_errorWriteWithInfoLog(tag, subTag, uid, data, dataLen) \
  447. __android_log_error_write(tag, subTag, uid, data, dataLen)
  448. /*
  449. * IF_ALOG uses android_testLog, but IF_ALOG can be overridden.
  450. * android_testLog will remain constant in its purpose as a wrapper
  451. * for Android logging filter policy, and can be subject to
  452. * change. It can be reused by the developers that override
  453. * IF_ALOG as a convenient means to reimplement their policy
  454. * over Android.
  455. */
  456. #if LOG_NDEBUG /* Production */
  457. #define android_testLog(prio, tag) \
  458. (__android_log_is_loggable(prio, tag, ANDROID_LOG_DEBUG) != 0)
  459. #else
  460. #define android_testLog(prio, tag) \
  461. (__android_log_is_loggable(prio, tag, ANDROID_LOG_VERBOSE) != 0)
  462. #endif
  463. /*
  464. * Use the per-tag properties "log.tag.<tagname>" to generate a runtime
  465. * result of non-zero to expose a log. prio is ANDROID_LOG_VERBOSE to
  466. * ANDROID_LOG_FATAL. default_prio if no property. Undefined behavior if
  467. * any other value.
  468. */
  469. int __android_log_is_loggable(int prio, const char *tag, int default_prio);
  470. int __android_log_security(); /* Device Owner is present */
  471. int __android_log_error_write(int tag, const char *subTag, int32_t uid, const char *data,
  472. uint32_t dataLen);
  473. /*
  474. * Send a simple string to the log.
  475. */
  476. int __android_log_buf_write(int bufID, int prio, const char *tag, const char *text);
  477. int __android_log_buf_print(int bufID, int prio, const char *tag, const char *fmt, ...)
  478. #if defined(__GNUC__)
  479. __attribute__((__format__(printf, 4, 5)))
  480. #endif
  481. ;
  482. #ifdef __cplusplus
  483. }
  484. #endif
  485. #endif /* COCOS_CUTILS_LOG_H */