log_util.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. /* Copyright (c) 2011-2014, 2020 The Linux Foundation. All rights reserved.
  2. *
  3. * Redistribution and use in source and binary forms, with or without
  4. * modification, are permitted provided that the following conditions are
  5. * met:
  6. * * Redistributions of source code must retain the above copyright
  7. * notice, this list of conditions and the following disclaimer.
  8. * * Redistributions in binary form must reproduce the above
  9. * copyright notice, this list of conditions and the following
  10. * disclaimer in the documentation and/or other materials provided
  11. * with the distribution.
  12. * * Neither the name of The Linux Foundation nor the names of its
  13. * contributors may be used to endorse or promote products derived
  14. * from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
  17. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  18. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
  19. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
  20. * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  21. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  22. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
  23. * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  24. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
  25. * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
  26. * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27. *
  28. */
  29. #ifndef __LOG_UTIL_H__
  30. #define __LOG_UTIL_H__
  31. #include <stdbool.h>
  32. #include <loc_pla.h>
  33. #if defined (USE_ANDROID_LOGGING) || defined (ANDROID)
  34. // Android and LE targets with logcat support
  35. #include <utils/Log.h>
  36. #include <unistd.h>
  37. #include <sys/syscall.h>
  38. #elif defined (USE_GLIB)
  39. // LE targets with no logcat support
  40. #include <stdio.h>
  41. #include <string.h>
  42. #include <sys/types.h>
  43. #include <sys/time.h>
  44. #include <unistd.h>
  45. #include <sys/syscall.h>
  46. #ifndef LOG_TAG
  47. #define LOG_TAG "GPS_UTILS"
  48. #endif /* LOG_TAG */
  49. // LE targets with no logcat support
  50. #if defined(FEATURE_EXTERNAL_AP) || defined(USE_SYSLOG_LOGGING)
  51. #include <syslog.h>
  52. #define ALOGE(...) syslog(LOG_ERR, "LOC_LOGE: " __VA_ARGS__);
  53. #define ALOGW(...) syslog(LOG_WARNING, "LOC_LOGW: " __VA_ARGS__);
  54. #define ALOGI(...) syslog(LOG_NOTICE, "LOC_LOGI: " __VA_ARGS__);
  55. #define ALOGD(...) syslog(LOG_DEBUG, "LOC_LOGD: " __VA_ARGS__);
  56. #define ALOGV(...) syslog(LOG_NOTICE, "LOC_LOGV: " __VA_ARGS__);
  57. #else /* FEATURE_EXTERNAL_AP */
  58. #define TS_PRINTF(format, x...) \
  59. { \
  60. struct timeval tv; \
  61. struct timezone tz; \
  62. int hh, mm, ss; \
  63. gettimeofday(&tv, &tz); \
  64. hh = tv.tv_sec/3600%24; \
  65. mm = (tv.tv_sec%3600)/60; \
  66. ss = tv.tv_sec%60; \
  67. fprintf(stdout,"%02d:%02d:%02d.%06ld]" format "\n", hh, mm, ss, tv.tv_usec, ##x); \
  68. }
  69. #define ALOGE(format, x...) TS_PRINTF("E/%s (%d): " format , LOG_TAG, getpid(), ##x)
  70. #define ALOGW(format, x...) TS_PRINTF("W/%s (%d): " format , LOG_TAG, getpid(), ##x)
  71. #define ALOGI(format, x...) TS_PRINTF("I/%s (%d): " format , LOG_TAG, getpid(), ##x)
  72. #define ALOGD(format, x...) TS_PRINTF("D/%s (%d): " format , LOG_TAG, getpid(), ##x)
  73. #define ALOGV(format, x...) TS_PRINTF("V/%s (%d): " format , LOG_TAG, getpid(), ##x)
  74. #endif /* FEATURE_EXTERNAL_AP */
  75. #endif /* #if defined (USE_ANDROID_LOGGING) || defined (ANDROID) */
  76. #ifdef __cplusplus
  77. extern "C"
  78. {
  79. #endif
  80. /*=============================================================================
  81. *
  82. * LOC LOGGER TYPE DECLARATION
  83. *
  84. *============================================================================*/
  85. /* LOC LOGGER */
  86. typedef struct loc_logger_s
  87. {
  88. unsigned long DEBUG_LEVEL;
  89. unsigned long TIMESTAMP;
  90. bool LOG_BUFFER_ENABLE;
  91. } loc_logger_s_type;
  92. /*=============================================================================
  93. *
  94. * EXTERNAL DATA
  95. *
  96. *============================================================================*/
  97. // Logging Improvements
  98. extern const char *loc_logger_boolStr[];
  99. extern loc_logger_s_type loc_logger;
  100. extern const char *boolStr[];
  101. extern const char VOID_RET[];
  102. extern const char FROM_AFW[];
  103. extern const char TO_MODEM[];
  104. extern const char FROM_MODEM[];
  105. extern const char TO_AFW[];
  106. extern const char EXIT_TAG[];
  107. extern const char ENTRY_TAG[];
  108. extern const char EXIT_ERROR_TAG[];
  109. #define BUILD_TYPE_PROP_NA 0
  110. #define BUILD_TYPE_PROP_USER 1
  111. #define BUILD_TYPE_PROP_USERDEBUG 2
  112. #define BUILD_TYPE_PROP_INVALID 3
  113. extern int build_type_prop;
  114. /*=============================================================================
  115. *
  116. * MODULE EXPORTED FUNCTIONS
  117. *
  118. *============================================================================*/
  119. inline static void loc_logger_init(unsigned long debug, unsigned long timestamp)
  120. {
  121. loc_logger.DEBUG_LEVEL = debug;
  122. if (BUILD_TYPE_PROP_NA == build_type_prop) {
  123. char value[PROPERTY_VALUE_MAX] = "NA";
  124. property_get("ro.build.type", value, "userdebug");
  125. if (0 == strcmp(value, "user")) {
  126. build_type_prop = BUILD_TYPE_PROP_USER;
  127. } else if (0 == strcmp(value, "userdebug")) {
  128. build_type_prop = BUILD_TYPE_PROP_USERDEBUG;
  129. } else {
  130. build_type_prop = BUILD_TYPE_PROP_INVALID;
  131. }
  132. }
  133. if (BUILD_TYPE_PROP_USER == build_type_prop) {
  134. // force user builds to 2 or less
  135. if (loc_logger.DEBUG_LEVEL > 2) {
  136. loc_logger.DEBUG_LEVEL = 2;
  137. }
  138. }
  139. loc_logger.TIMESTAMP = timestamp;
  140. }
  141. inline static void log_buffer_init(bool enabled) {
  142. loc_logger.LOG_BUFFER_ENABLE = enabled;
  143. }
  144. extern void log_tag_level_map_init();
  145. extern int get_tag_log_level(const char* tag);
  146. extern char* get_timestamp(char* str, unsigned long buf_size);
  147. extern void log_buffer_insert(char *str, unsigned long buf_size, int level);
  148. /*=============================================================================
  149. *
  150. * LOGGING BUFFER MACROS
  151. *
  152. *============================================================================*/
  153. #ifndef LOG_NDEBUG
  154. #define LOG_NDEBUG 0
  155. #endif
  156. #define TOTAL_LOG_LEVELS 5
  157. #define LOGGING_BUFFER_MAX_LEN 1024
  158. #define IF_LOG_BUFFER_ENABLE if (loc_logger.LOG_BUFFER_ENABLE)
  159. #define INSERT_BUFFER(flag, level, format, x...) \
  160. { \
  161. IF_LOG_BUFFER_ENABLE { \
  162. if (flag == 0) { \
  163. char timestr[32]; \
  164. get_timestamp(timestr, sizeof(timestr)); \
  165. char log_str[LOGGING_BUFFER_MAX_LEN]; \
  166. snprintf(log_str, LOGGING_BUFFER_MAX_LEN, "%s %d %ld %s :" format "\n", \
  167. timestr, getpid(), syscall(SYS_gettid), LOG_TAG==NULL ? "": LOG_TAG, ##x);\
  168. log_buffer_insert(log_str, sizeof(log_str), level); \
  169. } \
  170. } \
  171. }
  172. #ifndef DEBUG_DMN_LOC_API
  173. /* LOGGING MACROS */
  174. /*loc_logger.DEBUG_LEVEL is initialized to 0xff in loc_cfg.cpp
  175. if that value remains unchanged, it means gps.conf did not
  176. provide a value and we default to the initial value to use
  177. Android's logging levels*/
  178. /* Tag based logging control MACROS */
  179. /* The logic is like this:
  180. * 1, LOCAL_LOG_LEVEL is defined as a static variable in log_util.h,
  181. * then all source files which includes log_util.h will have its own LOCAL_LOG_LEVEL variable;
  182. * 2, For each source file,
  183. * 2.1, First time when LOC_LOG* is invoked(its LOCAL_LOG_LEVEL == -1),
  184. * Set the tag based log level according to the <tag, level> map;
  185. * If this tag isn't found in map, set local debug level as global loc_logger.DEBUG_LEVEL;
  186. * 2.2, If not the first time, use its LOCAL_LOG_LEVEL as the debug level of this tag.
  187. */
  188. static int LOCAL_LOG_LEVEL = -1;
  189. #define IF_LOC_LOG(x) \
  190. if (((LOCAL_LOG_LEVEL == -1 && (LOCAL_LOG_LEVEL = get_tag_log_level(LOG_TAG)) >= x) ||\
  191. LOCAL_LOG_LEVEL >= x) && LOCAL_LOG_LEVEL <= 5)
  192. #define IF_LOC_LOGE IF_LOC_LOG(1)
  193. #define IF_LOC_LOGW IF_LOC_LOG(2)
  194. #define IF_LOC_LOGI IF_LOC_LOG(3)
  195. #define IF_LOC_LOGD IF_LOC_LOG(4)
  196. #define IF_LOC_LOGV IF_LOC_LOG(5)
  197. #define LOC_LOGE(...) IF_LOC_LOGE { ALOGE(__VA_ARGS__); INSERT_BUFFER(LOG_NDEBUG, 0, __VA_ARGS__);}
  198. #define LOC_LOGW(...) IF_LOC_LOGW { ALOGW(__VA_ARGS__); INSERT_BUFFER(LOG_NDEBUG, 1, __VA_ARGS__);}
  199. #define LOC_LOGI(...) IF_LOC_LOGI { ALOGI(__VA_ARGS__); INSERT_BUFFER(LOG_NDEBUG, 2, __VA_ARGS__);}
  200. #define LOC_LOGD(...) IF_LOC_LOGD { ALOGD(__VA_ARGS__); INSERT_BUFFER(LOG_NDEBUG, 3, __VA_ARGS__);}
  201. #define LOC_LOGV(...) IF_LOC_LOGV { ALOGV(__VA_ARGS__); INSERT_BUFFER(LOG_NDEBUG, 4, __VA_ARGS__);}
  202. #else /* DEBUG_DMN_LOC_API */
  203. #define LOC_LOGE(...) ALOGE(__VA_ARGS__)
  204. #define LOC_LOGW(...) ALOGW(__VA_ARGS__)
  205. #define LOC_LOGI(...) ALOGI(__VA_ARGS__)
  206. #define LOC_LOGD(...) ALOGD(__VA_ARGS__)
  207. #define LOC_LOGV(...) ALOGV(__VA_ARGS__)
  208. #endif /* DEBUG_DMN_LOC_API */
  209. /*=============================================================================
  210. *
  211. * LOGGING IMPROVEMENT MACROS
  212. *
  213. *============================================================================*/
  214. #define LOG_(LOC_LOG, ID, WHAT, SPEC, VAL) \
  215. do { \
  216. if (loc_logger.TIMESTAMP) { \
  217. char ts[32]; \
  218. LOC_LOG("[%s] %s %s line %d " #SPEC, \
  219. get_timestamp(ts, sizeof(ts)), ID, WHAT, __LINE__, VAL); \
  220. } else { \
  221. LOC_LOG("%s %s line %d " #SPEC, \
  222. ID, WHAT, __LINE__, VAL); \
  223. } \
  224. } while(0)
  225. #define LOC_LOG_HEAD(fmt) "%s:%d] " fmt
  226. #define LOC_LOGv(fmt,...) LOC_LOGV(LOC_LOG_HEAD(fmt), __FUNCTION__, __LINE__, ##__VA_ARGS__)
  227. #define LOC_LOGw(fmt,...) LOC_LOGW(LOC_LOG_HEAD(fmt), __FUNCTION__, __LINE__, ##__VA_ARGS__)
  228. #define LOC_LOGi(fmt,...) LOC_LOGI(LOC_LOG_HEAD(fmt), __FUNCTION__, __LINE__, ##__VA_ARGS__)
  229. #define LOC_LOGd(fmt,...) LOC_LOGD(LOC_LOG_HEAD(fmt), __FUNCTION__, __LINE__, ##__VA_ARGS__)
  230. #define LOC_LOGe(fmt,...) LOC_LOGE(LOC_LOG_HEAD(fmt), __FUNCTION__, __LINE__, ##__VA_ARGS__)
  231. #define LOG_I(ID, WHAT, SPEC, VAL) LOG_(LOC_LOGI, ID, WHAT, SPEC, VAL)
  232. #define LOG_V(ID, WHAT, SPEC, VAL) LOG_(LOC_LOGV, ID, WHAT, SPEC, VAL)
  233. #define LOG_E(ID, WHAT, SPEC, VAL) LOG_(LOC_LOGE, ID, WHAT, SPEC, VAL)
  234. #define LOG_D(ID, WHAT, SPEC, VAL) LOG_(LOC_LOGD, ID, WHAT, SPEC, VAL)
  235. #define ENTRY_LOG() LOG_V(ENTRY_TAG, __FUNCTION__, %s, "")
  236. #define EXIT_LOG(SPEC, VAL) LOG_V(EXIT_TAG, __FUNCTION__, SPEC, VAL)
  237. #define EXIT_LOG_WITH_ERROR(SPEC, VAL) \
  238. if (VAL != 0) { \
  239. LOG_E(EXIT_ERROR_TAG, __FUNCTION__, SPEC, VAL); \
  240. } else { \
  241. LOG_V(EXIT_TAG, __FUNCTION__, SPEC, VAL); \
  242. }
  243. // Used for logging callflow from Android Framework
  244. #define ENTRY_LOG_CALLFLOW() LOG_I(FROM_AFW, __FUNCTION__, %s, "")
  245. // Used for logging callflow to Modem
  246. #define EXIT_LOG_CALLFLOW(SPEC, VAL) LOG_I(TO_MODEM, __FUNCTION__, SPEC, VAL)
  247. // Used for logging callflow from Modem(TO_MODEM, __FUNCTION__, %s, "")
  248. #define MODEM_LOG_CALLFLOW(SPEC, VAL) LOG_I(FROM_MODEM, __FUNCTION__, SPEC, VAL)
  249. // Used for logging high frequency callflow from Modem(TO_MODEM, __FUNCTION__, %s, "")
  250. #define MODEM_LOG_CALLFLOW_DEBUG(SPEC, VAL) LOG_D(FROM_MODEM, __FUNCTION__, SPEC, VAL)
  251. // Used for logging callflow to Android Framework
  252. #define CALLBACK_LOG_CALLFLOW(CB, SPEC, VAL) LOG_I(TO_AFW, CB, SPEC, VAL)
  253. #ifdef __cplusplus
  254. }
  255. #endif
  256. #endif // __LOG_UTIL_H__