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 #if defined (USE_ANDROID_LOGGING) || defined (ANDROID)
34 // Android and LE targets with logcat support
35 #include <utils/Log.h>
36 
37 #elif defined (USE_GLIB)
38 // LE targets with no logcat support
39 #include <stdio.h>
40 #include <string.h>
41 #include <sys/types.h>
42 #include <sys/time.h>
43 #include <unistd.h>
44 
45 #ifndef LOG_TAG
46 #define LOG_TAG "GPS_UTILS"
47 #endif /* LOG_TAG */
48 
49 // LE targets with no logcat support
50 #ifdef FEATURE_EXTERNAL_AP
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 
70 #define ALOGE(format, x...) TS_PRINTF("E/%s (%d): " format , LOG_TAG, getpid(), ##x)
71 #define ALOGW(format, x...) TS_PRINTF("W/%s (%d): " format , LOG_TAG, getpid(), ##x)
72 #define ALOGI(format, x...) TS_PRINTF("I/%s (%d): " format , LOG_TAG, getpid(), ##x)
73 #define ALOGD(format, x...) TS_PRINTF("D/%s (%d): " format , LOG_TAG, getpid(), ##x)
74 #define ALOGV(format, x...) TS_PRINTF("V/%s (%d): " format , LOG_TAG, getpid(), ##x)
75 #endif /* FEATURE_EXTERNAL_AP */
76 
77 #endif /* #if defined (USE_ANDROID_LOGGING) || defined (ANDROID) */
78 
79 #ifdef __cplusplus
80 extern "C"
81 {
82 #endif
83 /*=============================================================================
84  *
85  *                         LOC LOGGER TYPE DECLARATION
86  *
87  *============================================================================*/
88 /* LOC LOGGER */
89 typedef struct loc_logger_s
90 {
91   unsigned long  DEBUG_LEVEL;
92   unsigned long  TIMESTAMP;
93 } loc_logger_s_type;
94 
95 /*=============================================================================
96  *
97  *                               EXTERNAL DATA
98  *
99  *============================================================================*/
100 extern loc_logger_s_type loc_logger;
101 
102 // Logging Improvements
103 extern const char *loc_logger_boolStr[];
104 
105 extern const char *boolStr[];
106 extern const char VOID_RET[];
107 extern const char FROM_AFW[];
108 extern const char TO_MODEM[];
109 extern const char FROM_MODEM[];
110 extern const char TO_AFW[];
111 extern const char EXIT_TAG[];
112 extern const char ENTRY_TAG[];
113 extern const char EXIT_ERROR_TAG[];
114 
115 /*=============================================================================
116  *
117  *                        MODULE EXPORTED FUNCTIONS
118  *
119  *============================================================================*/
120 extern void loc_logger_init(unsigned long debug, unsigned long timestamp);
121 extern char* get_timestamp(char* str, unsigned long buf_size);
122 
123 #ifndef DEBUG_DMN_LOC_API
124 
125 /* LOGGING MACROS */
126 /*loc_logger.DEBUG_LEVEL is initialized to 0xff in loc_cfg.cpp
127   if that value remains unchanged, it means gps.conf did not
128   provide a value and we default to the initial value to use
129   Android's logging levels*/
130 #define IF_LOC_LOGE if((loc_logger.DEBUG_LEVEL >= 1) && (loc_logger.DEBUG_LEVEL <= 5))
131 #define IF_LOC_LOGW if((loc_logger.DEBUG_LEVEL >= 2) && (loc_logger.DEBUG_LEVEL <= 5))
132 #define IF_LOC_LOGI if((loc_logger.DEBUG_LEVEL >= 3) && (loc_logger.DEBUG_LEVEL <= 5))
133 #define IF_LOC_LOGD if((loc_logger.DEBUG_LEVEL >= 4) && (loc_logger.DEBUG_LEVEL <= 5))
134 #define IF_LOC_LOGV if((loc_logger.DEBUG_LEVEL >= 5) && (loc_logger.DEBUG_LEVEL <= 5))
135 
136 #define LOC_LOGE(...) IF_LOC_LOGE { ALOGE(__VA_ARGS__); }
137 #define LOC_LOGW(...) IF_LOC_LOGW { ALOGW(__VA_ARGS__); }
138 #define LOC_LOGI(...) IF_LOC_LOGI { ALOGI(__VA_ARGS__); }
139 #define LOC_LOGD(...) IF_LOC_LOGD { ALOGD(__VA_ARGS__); }
140 #define LOC_LOGV(...) IF_LOC_LOGV { ALOGV(__VA_ARGS__); }
141 
142 #else /* DEBUG_DMN_LOC_API */
143 
144 #define LOC_LOGE(...) ALOGE(__VA_ARGS__)
145 #define LOC_LOGW(...) ALOGW(__VA_ARGS__)
146 #define LOC_LOGI(...) ALOGI(__VA_ARGS__)
147 #define LOC_LOGD(...) ALOGD(__VA_ARGS__)
148 #define LOC_LOGV(...) ALOGV(__VA_ARGS__)
149 
150 #endif /* DEBUG_DMN_LOC_API */
151 
152 /*=============================================================================
153  *
154  *                          LOGGING IMPROVEMENT MACROS
155  *
156  *============================================================================*/
157 #define LOG_(LOC_LOG, ID, WHAT, SPEC, VAL)                                    \
158     do {                                                                      \
159         if (loc_logger.TIMESTAMP) {                                           \
160             char ts[32];                                                      \
161             LOC_LOG("[%s] %s %s line %d " #SPEC,                              \
162                      get_timestamp(ts, sizeof(ts)), ID, WHAT, __LINE__, VAL); \
163         } else {                                                              \
164             LOC_LOG("%s %s line %d " #SPEC,                                   \
165                      ID, WHAT, __LINE__, VAL);                                \
166         }                                                                     \
167     } while(0)
168 
169 #define LOC_LOG_HEAD(fmt) "%s:%d] " fmt
170 #define LOC_LOGv(fmt,...) LOC_LOGV(LOC_LOG_HEAD(fmt), __FUNCTION__, __LINE__, ##__VA_ARGS__)
171 #define LOC_LOGw(fmt,...) LOC_LOGW(LOC_LOG_HEAD(fmt), __FUNCTION__, __LINE__, ##__VA_ARGS__)
172 #define LOC_LOGi(fmt,...) LOC_LOGI(LOC_LOG_HEAD(fmt), __FUNCTION__, __LINE__, ##__VA_ARGS__)
173 #define LOC_LOGd(fmt,...) LOC_LOGD(LOC_LOG_HEAD(fmt), __FUNCTION__, __LINE__, ##__VA_ARGS__)
174 #define LOC_LOGe(fmt,...) LOC_LOGE(LOC_LOG_HEAD(fmt), __FUNCTION__, __LINE__, ##__VA_ARGS__)
175 
176 #define LOG_I(ID, WHAT, SPEC, VAL) LOG_(LOC_LOGI, ID, WHAT, SPEC, VAL)
177 #define LOG_V(ID, WHAT, SPEC, VAL) LOG_(LOC_LOGV, ID, WHAT, SPEC, VAL)
178 #define LOG_E(ID, WHAT, SPEC, VAL) LOG_(LOC_LOGE, ID, WHAT, SPEC, VAL)
179 
180 #define ENTRY_LOG() LOG_V(ENTRY_TAG, __FUNCTION__, %s, "")
181 #define EXIT_LOG(SPEC, VAL) LOG_V(EXIT_TAG, __FUNCTION__, SPEC, VAL)
182 #define EXIT_LOG_WITH_ERROR(SPEC, VAL)                       \
183     if (VAL != 0) {                                          \
184         LOG_E(EXIT_ERROR_TAG, __FUNCTION__, SPEC, VAL);          \
185     } else {                                                 \
186         LOG_V(EXIT_TAG, __FUNCTION__, SPEC, VAL);                \
187     }
188 
189 
190 // Used for logging callflow from Android Framework
191 #define ENTRY_LOG_CALLFLOW() LOG_I(FROM_AFW, __FUNCTION__, %s, "")
192 // Used for logging callflow to Modem
193 #define EXIT_LOG_CALLFLOW(SPEC, VAL) LOG_I(TO_MODEM, __FUNCTION__, SPEC, VAL)
194 // Used for logging callflow from Modem(TO_MODEM, __FUNCTION__, %s, "")
195 #define MODEM_LOG_CALLFLOW(SPEC, VAL) LOG_I(FROM_MODEM, __FUNCTION__, SPEC, VAL)
196 // Used for logging callflow to Android Framework
197 #define CALLBACK_LOG_CALLFLOW(CB, SPEC, VAL) LOG_I(TO_AFW, CB, SPEC, VAL)
198 
199 #ifdef __cplusplus
200 }
201 #endif
202 
203 #endif // __LOG_UTIL_H__
204