1 /* Copyright (c) 2011-2014 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 
30 #ifndef __LOG_UTIL_H__
31 #define __LOG_UTIL_H__
32 
33 #include <stdbool.h>
34 #include <loc_pla.h>
35 
36 #if defined (USE_ANDROID_LOGGING) || defined (ANDROID)
37 // Android and LE targets with logcat support
38 #include <utils/Log.h>
39 #include <unistd.h>
40 #include <sys/syscall.h>
41 
42 #elif defined (USE_GLIB)
43 // LE targets with no logcat support
44 #include <stdio.h>
45 #include <string.h>
46 #include <sys/types.h>
47 #include <sys/time.h>
48 #include <unistd.h>
49 #include <sys/syscall.h>
50 
51 #ifndef LOG_TAG
52 #define LOG_TAG "GPS_UTILS"
53 #endif /* LOG_TAG */
54 
55 // LE targets with no logcat support
56 #ifdef FEATURE_EXTERNAL_AP
57 #include <syslog.h>
58 #define ALOGE(...) syslog(LOG_ERR,     "LOC_LOGE: " __VA_ARGS__);
59 #define ALOGW(...) syslog(LOG_WARNING, "LOC_LOGW: " __VA_ARGS__);
60 #define ALOGI(...) syslog(LOG_NOTICE,  "LOC_LOGI: " __VA_ARGS__);
61 #define ALOGD(...) syslog(LOG_DEBUG,   "LOC_LOGD: " __VA_ARGS__);
62 #define ALOGV(...) syslog(LOG_NOTICE,  "LOC_LOGV: " __VA_ARGS__);
63 #else /* FEATURE_EXTERNAL_AP */
64 #define TS_PRINTF(format, x...)                                  \
65 {                                                                \
66     struct timeval tv;                                           \
67     struct timezone tz;                                          \
68     int hh, mm, ss;                                              \
69     gettimeofday(&tv, &tz);                                      \
70     hh = tv.tv_sec/3600%24;                                      \
71     mm = (tv.tv_sec%3600)/60;                                    \
72     ss = tv.tv_sec%60;                                           \
73     fprintf(stdout,"%02d:%02d:%02d.%06ld]" format "\n", hh, mm, ss, tv.tv_usec, ##x);    \
74 }
75 
76 #define ALOGE(format, x...) TS_PRINTF("E/%s (%d): " format , LOG_TAG, getpid(), ##x)
77 #define ALOGW(format, x...) TS_PRINTF("W/%s (%d): " format , LOG_TAG, getpid(), ##x)
78 #define ALOGI(format, x...) TS_PRINTF("I/%s (%d): " format , LOG_TAG, getpid(), ##x)
79 #define ALOGD(format, x...) TS_PRINTF("D/%s (%d): " format , LOG_TAG, getpid(), ##x)
80 #define ALOGV(format, x...) TS_PRINTF("V/%s (%d): " format , LOG_TAG, getpid(), ##x)
81 #endif /* FEATURE_EXTERNAL_AP */
82 
83 #endif /* #if defined (USE_ANDROID_LOGGING) || defined (ANDROID) */
84 
85 #ifdef __cplusplus
86 extern "C"
87 {
88 #endif
89 /*=============================================================================
90  *
91  *                         LOC LOGGER TYPE DECLARATION
92  *
93  *============================================================================*/
94 /* LOC LOGGER */
95 typedef struct loc_logger_s
96 {
97   unsigned long  DEBUG_LEVEL;
98   unsigned long  TIMESTAMP;
99   bool           LOG_BUFFER_ENABLE;
100 } loc_logger_s_type;
101 
102 
103 /*=============================================================================
104  *
105  *                               EXTERNAL DATA
106  *
107  *============================================================================*/
108 
109 // Logging Improvements
110 extern const char *loc_logger_boolStr[];
111 
112 extern loc_logger_s_type loc_logger;
113 extern const char *boolStr[];
114 extern const char VOID_RET[];
115 extern const char FROM_AFW[];
116 extern const char TO_MODEM[];
117 extern const char FROM_MODEM[];
118 extern const char TO_AFW[];
119 extern const char EXIT_TAG[];
120 extern const char ENTRY_TAG[];
121 extern const char EXIT_ERROR_TAG[];
122 
123 #define BUILD_TYPE_PROP_NA 0
124 #define BUILD_TYPE_PROP_USER 1
125 #define BUILD_TYPE_PROP_USERDEBUG 2
126 #define BUILD_TYPE_PROP_INVALID 3
127 extern int build_type_prop;
128 
129 /*=============================================================================
130  *
131  *                        MODULE EXPORTED FUNCTIONS
132  *
133  *============================================================================*/
loc_logger_init(unsigned long debug,unsigned long timestamp)134 inline void loc_logger_init(unsigned long debug, unsigned long timestamp)
135 {
136     loc_logger.DEBUG_LEVEL = debug;
137 
138     if (BUILD_TYPE_PROP_NA == build_type_prop) {
139         char value[PROPERTY_VALUE_MAX] = "NA";
140         property_get("ro.build.type", value, "userdebug");
141         if (0 == strcmp(value, "user")) {
142             build_type_prop = BUILD_TYPE_PROP_USER;
143         } else if (0 == strcmp(value, "userdebug")) {
144             build_type_prop = BUILD_TYPE_PROP_USERDEBUG;
145         } else {
146             build_type_prop = BUILD_TYPE_PROP_INVALID;
147         }
148     }
149 
150     if (BUILD_TYPE_PROP_USER == build_type_prop) {
151         // force user builds to 2 or less
152         if (loc_logger.DEBUG_LEVEL > 2) {
153             loc_logger.DEBUG_LEVEL = 2;
154         }
155      }
156 
157     loc_logger.TIMESTAMP = timestamp;
158 }
159 
log_buffer_init(bool enabled)160 inline void log_buffer_init(bool enabled) {
161     loc_logger.LOG_BUFFER_ENABLE = enabled;
162 }
163 
164 extern char* get_timestamp(char* str, unsigned long buf_size);
165 extern void log_buffer_insert(char *str, unsigned long buf_size, int level);
166 
167 /*=============================================================================
168  *
169  *                          LOGGING BUFFER MACROS
170  *
171  *============================================================================*/
172 #ifndef LOG_NDEBUG
173 #define LOG_NDEBUG 0
174 #endif
175 #define TOTAL_LOG_LEVELS 5
176 #define LOGGING_BUFFER_MAX_LEN 1024
177 #define IF_LOG_BUFFER_ENABLE if (loc_logger.LOG_BUFFER_ENABLE)
178 #define INSERT_BUFFER(flag, level, format, x...)                                              \
179 {                                                                                             \
180     IF_LOG_BUFFER_ENABLE {                                                                    \
181         if (flag == 0) {                                                                      \
182             char timestr[32];                                                                 \
183             get_timestamp(timestr, sizeof(timestr));                                          \
184             char log_str[LOGGING_BUFFER_MAX_LEN];                                             \
185             snprintf(log_str, LOGGING_BUFFER_MAX_LEN, "%s %d %ld %s :" format "\n",           \
186                     timestr, getpid(), syscall(SYS_gettid), LOG_TAG==NULL ? "": LOG_TAG, ##x);\
187             log_buffer_insert(log_str, sizeof(log_str), level);                               \
188         }                                                                                     \
189     }                                                                                         \
190 }
191 
192 #ifndef DEBUG_DMN_LOC_API
193 
194 /* LOGGING MACROS */
195 /*loc_logger.DEBUG_LEVEL is initialized to 0xff in loc_cfg.cpp
196   if that value remains unchanged, it means gps.conf did not
197   provide a value and we default to the initial value to use
198   Android's logging levels*/
199 #define IF_LOC_LOGE if((loc_logger.DEBUG_LEVEL >= 1) && (loc_logger.DEBUG_LEVEL <= 5))
200 #define IF_LOC_LOGW if((loc_logger.DEBUG_LEVEL >= 2) && (loc_logger.DEBUG_LEVEL <= 5))
201 #define IF_LOC_LOGI if((loc_logger.DEBUG_LEVEL >= 3) && (loc_logger.DEBUG_LEVEL <= 5))
202 #define IF_LOC_LOGD if((loc_logger.DEBUG_LEVEL >= 4) && (loc_logger.DEBUG_LEVEL <= 5))
203 #define IF_LOC_LOGV if((loc_logger.DEBUG_LEVEL >= 5) && (loc_logger.DEBUG_LEVEL <= 5))
204 
205 #define LOC_LOGE(...) IF_LOC_LOGE { ALOGE(__VA_ARGS__); INSERT_BUFFER(LOG_NDEBUG, 0, __VA_ARGS__);}
206 #define LOC_LOGW(...) IF_LOC_LOGW { ALOGW(__VA_ARGS__); INSERT_BUFFER(LOG_NDEBUG, 1, __VA_ARGS__);}
207 #define LOC_LOGI(...) IF_LOC_LOGI { ALOGI(__VA_ARGS__); INSERT_BUFFER(LOG_NDEBUG, 2, __VA_ARGS__);}
208 #define LOC_LOGD(...) IF_LOC_LOGD { ALOGD(__VA_ARGS__); INSERT_BUFFER(LOG_NDEBUG, 3, __VA_ARGS__);}
209 #define LOC_LOGV(...) IF_LOC_LOGV { ALOGV(__VA_ARGS__); INSERT_BUFFER(LOG_NDEBUG, 4, __VA_ARGS__);}
210 
211 #else /* DEBUG_DMN_LOC_API */
212 
213 #define LOC_LOGE(...) ALOGE(__VA_ARGS__)
214 #define LOC_LOGW(...) ALOGW(__VA_ARGS__)
215 #define LOC_LOGI(...) ALOGI(__VA_ARGS__)
216 #define LOC_LOGD(...) ALOGD(__VA_ARGS__)
217 #define LOC_LOGV(...) ALOGV(__VA_ARGS__)
218 
219 #endif /* DEBUG_DMN_LOC_API */
220 
221 /*=============================================================================
222  *
223  *                          LOGGING IMPROVEMENT MACROS
224  *
225  *============================================================================*/
226 #define LOG_(LOC_LOG, ID, WHAT, SPEC, VAL)                                    \
227     do {                                                                      \
228         if (loc_logger.TIMESTAMP) {                                           \
229             char ts[32];                                                      \
230             LOC_LOG("[%s] %s %s line %d " #SPEC,                              \
231                      get_timestamp(ts, sizeof(ts)), ID, WHAT, __LINE__, VAL); \
232         } else {                                                              \
233             LOC_LOG("%s %s line %d " #SPEC,                                   \
234                      ID, WHAT, __LINE__, VAL);                                \
235         }                                                                     \
236     } while(0)
237 
238 #define LOC_LOG_HEAD(fmt) "%s:%d] " fmt
239 #define LOC_LOGv(fmt,...) LOC_LOGV(LOC_LOG_HEAD(fmt), __FUNCTION__, __LINE__, ##__VA_ARGS__)
240 #define LOC_LOGw(fmt,...) LOC_LOGW(LOC_LOG_HEAD(fmt), __FUNCTION__, __LINE__, ##__VA_ARGS__)
241 #define LOC_LOGi(fmt,...) LOC_LOGI(LOC_LOG_HEAD(fmt), __FUNCTION__, __LINE__, ##__VA_ARGS__)
242 #define LOC_LOGd(fmt,...) LOC_LOGD(LOC_LOG_HEAD(fmt), __FUNCTION__, __LINE__, ##__VA_ARGS__)
243 #define LOC_LOGe(fmt,...) LOC_LOGE(LOC_LOG_HEAD(fmt), __FUNCTION__, __LINE__, ##__VA_ARGS__)
244 
245 #define LOG_I(ID, WHAT, SPEC, VAL) LOG_(LOC_LOGI, ID, WHAT, SPEC, VAL)
246 #define LOG_V(ID, WHAT, SPEC, VAL) LOG_(LOC_LOGV, ID, WHAT, SPEC, VAL)
247 #define LOG_E(ID, WHAT, SPEC, VAL) LOG_(LOC_LOGE, ID, WHAT, SPEC, VAL)
248 #define LOG_D(ID, WHAT, SPEC, VAL) LOG_(LOC_LOGD, ID, WHAT, SPEC, VAL)
249 
250 #define ENTRY_LOG() LOG_V(ENTRY_TAG, __FUNCTION__, %s, "")
251 #define EXIT_LOG(SPEC, VAL) LOG_V(EXIT_TAG, __FUNCTION__, SPEC, VAL)
252 #define EXIT_LOG_WITH_ERROR(SPEC, VAL)                       \
253     if (VAL != 0) {                                          \
254         LOG_E(EXIT_ERROR_TAG, __FUNCTION__, SPEC, VAL);          \
255     } else {                                                 \
256         LOG_V(EXIT_TAG, __FUNCTION__, SPEC, VAL);                \
257     }
258 
259 
260 // Used for logging callflow from Android Framework
261 #define ENTRY_LOG_CALLFLOW() LOG_I(FROM_AFW, __FUNCTION__, %s, "")
262 // Used for logging callflow to Modem
263 #define EXIT_LOG_CALLFLOW(SPEC, VAL) LOG_I(TO_MODEM, __FUNCTION__, SPEC, VAL)
264 // Used for logging callflow from Modem(TO_MODEM, __FUNCTION__, %s, "")
265 #define MODEM_LOG_CALLFLOW(SPEC, VAL) LOG_I(FROM_MODEM, __FUNCTION__, SPEC, VAL)
266 // Used for logging high frequency callflow from Modem(TO_MODEM, __FUNCTION__, %s, "")
267 #define MODEM_LOG_CALLFLOW_DEBUG(SPEC, VAL) LOG_D(FROM_MODEM, __FUNCTION__, SPEC, VAL)
268 // Used for logging callflow to Android Framework
269 #define CALLBACK_LOG_CALLFLOW(CB, SPEC, VAL) LOG_I(TO_AFW, CB, SPEC, VAL)
270 
271 #ifdef __cplusplus
272 }
273 #endif
274 
275 #endif // __LOG_UTIL_H__
276