1 /* Copyright (c) 2011-2012, 2015, 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 #define LOG_NDEBUG 0
31
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <unistd.h>
35 #include <sys/time.h>
36 #include "log_util.h"
37 #include "loc_log.h"
38 #include "msg_q.h"
39 #include <loc_pla.h>
40 #include "LogBuffer.h"
41
42 #define BUFFER_SIZE 120
43
44 // Logging Improvements
45 const char *loc_logger_boolStr[]={"False","True"};
46 const char VOID_RET[] = "None";
47 const char FROM_AFW[] = "===>";
48 const char TO_MODEM[] = "--->";
49 const char FROM_MODEM[] = "<---";
50 const char TO_AFW[] = "<===";
51 const char EXIT_TAG[] = "Exiting";
52 const char ENTRY_TAG[] = "Entering";
53 const char EXIT_ERROR_TAG[] = "Exiting with error";
54
55 int build_type_prop = BUILD_TYPE_PROP_NA;
56
57 /* Logging Mechanism */
58 loc_logger_s_type loc_logger;
59
60 /* Get names from value */
loc_get_name_from_mask(const loc_name_val_s_type table[],size_t table_size,long mask)61 const char* loc_get_name_from_mask(const loc_name_val_s_type table[], size_t table_size, long mask)
62 {
63 size_t i;
64 for (i = 0; i < table_size; i++)
65 {
66 if (table[i].val & (long) mask)
67 {
68 return table[i].name;
69 }
70 }
71 return UNKNOWN_STR;
72 }
73
74 /* Get names from value */
loc_get_name_from_val(const loc_name_val_s_type table[],size_t table_size,long value)75 const char* loc_get_name_from_val(const loc_name_val_s_type table[], size_t table_size, long value)
76 {
77 size_t i;
78 for (i = 0; i < table_size; i++)
79 {
80 if (table[i].val == (long) value)
81 {
82 return table[i].name;
83 }
84 }
85 return UNKNOWN_STR;
86 }
87
88 static const loc_name_val_s_type loc_msg_q_status[] =
89 {
90 NAME_VAL( eMSG_Q_SUCCESS ),
91 NAME_VAL( eMSG_Q_FAILURE_GENERAL ),
92 NAME_VAL( eMSG_Q_INVALID_PARAMETER ),
93 NAME_VAL( eMSG_Q_INVALID_HANDLE ),
94 NAME_VAL( eMSG_Q_UNAVAILABLE_RESOURCE ),
95 NAME_VAL( eMSG_Q_INSUFFICIENT_BUFFER )
96 };
97 static const size_t loc_msg_q_status_num = LOC_TABLE_SIZE(loc_msg_q_status);
98
99 /* Find msg_q status name */
loc_get_msg_q_status(int status)100 const char* loc_get_msg_q_status(int status)
101 {
102 return loc_get_name_from_val(loc_msg_q_status, loc_msg_q_status_num, (long) status);
103 }
104
log_succ_fail_string(int is_succ)105 const char* log_succ_fail_string(int is_succ)
106 {
107 return is_succ? "successful" : "failed";
108 }
109
110 //Target names
111 static const loc_name_val_s_type target_name[] =
112 {
113 NAME_VAL(GNSS_NONE),
114 NAME_VAL(GNSS_MSM),
115 NAME_VAL(GNSS_GSS),
116 NAME_VAL(GNSS_MDM),
117 NAME_VAL(GNSS_AUTO),
118 NAME_VAL(GNSS_UNKNOWN)
119 };
120
121 static const size_t target_name_num = LOC_TABLE_SIZE(target_name);
122
123 /*===========================================================================
124
125 FUNCTION loc_get_target_name
126
127 DESCRIPTION
128 Returns pointer to a string that contains name of the target
129
130 XX:XX:XX.000\0
131
132 RETURN VALUE
133 The target name string
134
135 ===========================================================================*/
loc_get_target_name(unsigned int target)136 const char *loc_get_target_name(unsigned int target)
137 {
138 int index = 0;
139 static char ret[BUFFER_SIZE];
140
141 index = getTargetGnssType(target);
142 if( index < 0 || (unsigned)index >= target_name_num )
143 index = target_name_num - 1;
144
145 if( (target & HAS_SSC) == HAS_SSC ) {
146 snprintf(ret, sizeof(ret), " %s with SSC",
147 loc_get_name_from_val(target_name, target_name_num, (long)index) );
148 }
149 else {
150 snprintf(ret, sizeof(ret), " %s without SSC",
151 loc_get_name_from_val(target_name, target_name_num, (long)index) );
152 }
153 return ret;
154 }
155
156
157 /*===========================================================================
158
159 FUNCTION loc_get_time
160
161 DESCRIPTION
162 Logs a callback event header.
163 The pointer time_string should point to a buffer of at least 13 bytes:
164
165 XX:XX:XX.000\0
166
167 RETURN VALUE
168 The time string
169
170 ===========================================================================*/
loc_get_time(char * time_string,size_t buf_size)171 char *loc_get_time(char *time_string, size_t buf_size)
172 {
173 struct timeval now; /* sec and usec */
174 struct tm now_tm; /* broken-down time */
175 char hms_string[80]; /* HH:MM:SS */
176
177 gettimeofday(&now, NULL);
178 localtime_r(&now.tv_sec, &now_tm);
179
180 strftime(hms_string, sizeof hms_string, "%H:%M:%S", &now_tm);
181 snprintf(time_string, buf_size, "%s.%03d", hms_string, (int) (now.tv_usec / 1000));
182
183 return time_string;
184 }
185
186 /*===========================================================================
187 FUNCTION get_timestamp
188
189 DESCRIPTION
190 Generates a timestamp using the current system time
191
192 DEPENDENCIES
193 N/A
194
195 RETURN VALUE
196 Char pointer to the parameter str
197
198 SIDE EFFECTS
199 N/A
200 ===========================================================================*/
get_timestamp(char * str,unsigned long buf_size)201 char * get_timestamp(char *str, unsigned long buf_size)
202 {
203 struct timeval tv;
204 struct timezone tz;
205 int hh, mm, ss;
206 gettimeofday(&tv, &tz);
207 hh = tv.tv_sec/3600%24;
208 mm = (tv.tv_sec%3600)/60;
209 ss = tv.tv_sec%60;
210 snprintf(str, buf_size, "%02d:%02d:%02d.%06ld", hh, mm, ss, tv.tv_usec);
211 return str;
212 }
213
214 /*===========================================================================
215
216 FUNCTION log_buffer_insert
217
218 DESCRIPTION
219 Insert a log sentence with specific level to the log buffer.
220
221 RETURN VALUE
222 N/A
223
224 ===========================================================================*/
log_buffer_insert(char * str,unsigned long buf_size,int level)225 void log_buffer_insert(char *str, unsigned long buf_size, int level)
226 {
227 timespec tv;
228 clock_gettime(CLOCK_BOOTTIME, &tv);
229 uint64_t elapsedTime = (uint64_t)tv.tv_sec + (uint64_t)tv.tv_nsec/1000000000;
230 string ss = str;
231 loc_util::LogBuffer::getInstance()->append(ss, level, elapsedTime);
232 }
233