-
Notifications
You must be signed in to change notification settings - Fork 3
/
ubx_log.cpp
executable file
·367 lines (333 loc) · 10 KB
/
ubx_log.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
/*******************************************************************************
*
* Copyright (C) u-blox AG
* u-blox AG, Thalwil, Switzerland
*
* All rights reserved.
*
* Permission to use, copy, modify, and distribute this software for any
* purpose without fee is hereby granted, provided that this entire notice
* is included in all copies of any software which is or includes a copy
* or modification of this software and in all copies of the supporting
* documentation for such software.
*
* THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR IMPLIED
* WARRANTY. IN PARTICULAR, NEITHER THE AUTHOR NOR U-BLOX MAKES ANY
* REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE MERCHANTABILITY
* OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR PURPOSE.
*
*******************************************************************************
*
* Project: PE_ANS
*
******************************************************************************/
/*!
\file
\brief time utility library
Module for time functions
*/
/*******************************************************************************
* $Id: ubx_log.cpp 65270 2013-01-25 08:42:48Z andrea.foni $
******************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <ctype.h>
#include <grp.h>
#include <sys/time.h>
#include "private/android_filesystem_config.h"
#include <cutils/open_memstream.h>
#include <fcntl.h>
#include "std_types.h"
#include "ubx_log.h"
#include "ubxgpsstate.h"
///////////////////////////////////////////////////////////////////////////////
//! Lookup a string from a string table
/*!
\param v : Index into string table
\param l : Pointer to table of strings
\param n : Max number of strings in table
\return : Pointer to string
*/
const char* _strLookup(unsigned int v, const char* const * l, unsigned int n)
{
if ((v >= n) || (l[v] == NULL))
return "UNKNOWN";
else
return l[v];
}
///////////////////////////////////////////////////////////////////////////////
//! Lookup a set of strings
/*! Lookup a set of strings from a string table according to flags,
and assemble into a buffer, a pointer to which is returned to the caller.
\param v : Flags into string table
\param l : Pointer to table of strings
\param n : Max number of strings in table
\return : pointer to buffer of assembled strings
*/
const char* _strLookupX(unsigned int v, const char* const * l, unsigned int n)
{
static char s[LOG_SPECIAL_BUFFER_COUNT][1024];
static int si = -1;
si++;
if (si == LOG_SPECIAL_BUFFER_COUNT)
{
si = 0;
}
char* p = s[si];
p[0] = '\0';
unsigned int i;
for (i = 0; i < n; i ++)
{
if (v & (1<<i))
{
const char* f = (l[i]!=NULL) ? l[i] : "UNKNOWN%02d";
p += sprintf(p, f, i);
*p ++ = '+';
}
}
if (p > s[si])
{
p[-1] = '\0';
}
return s[si];
}
#ifdef SUPL_ENABLED
///////////////////////////////////////////////////////////////////////////////
// CMCC compatible logging of files.
///////////////////////////////////////////////////////////////////////////////
//! Constructor
/*!
\param name : Pointer to log path and filename
\param max : Maximun size (in bytes) of log
*/
CLog::CLog(const char* name, int max, bool verbose)
{
static const char* env = NULL;
if (env == NULL)
{
env = getenv("EXTERNAL_STORAGE");
if (env == NULL)
env = "/mnt/sdcard";
gid_t gids[] = { AID_SDCARD_RW };
if (setgroups(sizeof(gids)/sizeof(gids[0]), gids) != 0)
{
LOGE("CLog::%s cannot get write accress to sdcard %d", __FUNCTION__, errno);
// gps log
env = "/data"; // todo remove this line
}
}
/*
if (setgid(AID_SHELL) != 0)
{
LOGE("CLog::%s cannot set gid %d %d %d", __FUNCTION__, getgid(), getegid(), errno);
}
if (setuid(AID_SHELL) == -1)
{
LOGE("CLog::%s cannot set uid %d %d %d", __FUNCTION__, getuid(), geteuid(), errno);
}
int s = getgroups(0,NULL);
gid_t* g = new gid_t[s];
getgroups(s, g);
LOGD("CLog::%s gids:", __FUNCTION__);
while (s --)
LOGD("CLog::%s gid %d", __FUNCTION__, *g++);
*/
sprintf(m_name, "%s/%s", env, name);
m_max = max;
m_verbose = verbose;
}
///////////////////////////////////////////////////////////////////////////////
//! Format and write to log file
/*!
\param code : Logging code
\param fmt : Pointrer to string containing formatting (as per a printf)
\param ... : Variable parameters to log (as per a printf)
*/
void CLog::write(unsigned int code, const char* fmt, ...)
{
if (!CUbxGpsState::getInstance()->getCmccLogActive())
{
return;
}
char buf[256];
char tmp[32];
int i = sprintf(buf, "[%s] 0x%08X:", timestamp(tmp), code);
va_list args;
va_start(args, fmt);
//lint -e{530} remove Symbol 'args' (line 266) not initialized
i += vsprintf(&buf[i], fmt, args);
va_end (args);
if (m_verbose)
LOGD("CLog::%s file='%s' data='%s' size=%d", __FUNCTION__, m_name, buf, i);
buf[i++] = '\r';
buf[i++] = '\n';
buf[i] = '\0';
writeFile(buf, i);
}
///////////////////////////////////////////////////////////////////////////////
//! Write to log file
/*!
\param pBuf : Pointer to buffer to write
\param len : Number of bytes to write
*/
void CLog::writeFile(const char* pBuf, int len)
{
FILE* pFile = fopen(m_name, "a");
if (pFile == NULL)
{
// failed
LOGE("CLog::%s : Can not open '%s' file : %i", __FUNCTION__, m_name, errno);
return;
}
int size = ftell(pFile);
if (size > m_max)
{
// Max file size reached, so recreate file with zero size
pFile = freopen(m_name, "w", pFile);
LOGW("CLog::%s : file='%s' resize", __FUNCTION__, m_name);
}
if (pFile == NULL)
{
// failed
LOGE("CLog::%s : Can not reopen '%s' file : %i", __FUNCTION__, m_name, errno);
return;
}
fwrite(pBuf, 1, (unsigned int) len, pFile);
fclose(pFile);
}
///////////////////////////////////////////////////////////////////////////////
//! Generate a time stamp for logging
/*!
\param tmp : Pointer to string place time stamp text into
\return : Pointer to string containing time stamp
*/
const char* CLog::timestamp(char* tmp)
{
static char buf[32];
struct timeval tv;
gettimeofday(&tv, NULL);
struct tm st;
gmtime_r(&tv.tv_sec, &st);
sprintf(tmp?tmp:buf, "%04d%02d%02d%02d%02d%02d.%02d",
st.tm_year + 1900, st.tm_mon + 1, st.tm_mday,
st.tm_hour, st.tm_min, st.tm_sec, (int)(tv.tv_usec/10000));
return tmp?tmp:buf;
}
///////////////////////////////////////////////////////////////////////////////
//! Destructor
/*!
*/
CLog::~CLog()
{
}
///////////////////////////////////////////////////////////////////////////////
//! Log text.
/*! Log text, but add a time stamp to the body of the text
\param code : Logging code
\param txt : Pointer to text to log
*/
void CLog::txt(int code, const char* pTxt)
{
char tmp[32];
write((unsigned int) code, "%s %s", timestamp(tmp), pTxt);
}
///////////////////////////////////////////////////////////////////////////////
// Static data
static CLog s_logMessages("SUPL-MESSAGE.LOG", 128*1024, false);
///////////////////////////////////////////////////////////////////////////////
//! Transfer a large memory buffer to the log
/*! Message buffer may be too big to log in one hit, so parse the message
using 0x0A characters to split into multiple lines. Then log each line.
\param pMsg : Pointer to message buffer to log
\param size : Size of buffer
*/
static void renderMemStreamToLog(char* pBuf, size_t size)
{
unsigned int i = 0;
unsigned int start = i;
while(i < size)
{
if (pBuf[i] == 0x0A)
{
pBuf[i] = 0;
if (CUbxGpsState::getInstance()->getSuplMsgToFile())
{
s_logMessages.writeFile(&pBuf[start], (int) (i - start));
s_logMessages.writeFile("\r\n", 2);
}
if(CUbxGpsState::getInstance()->getLogSuplMessages())
{
LOGV("%s", &pBuf[start]);
}
start = i + 1;
}
i++;
}
}
///////////////////////////////////////////////////////////////////////////////
//! Log a separator
/*!
*/
static void logSep(void)
{
char buffer[200];
int len = sprintf(buffer, "================================================================================\n");
renderMemStreamToLog(buffer, (unsigned int) len);
}
///////////////////////////////////////////////////////////////////////////////
//! Decode a Supl message to text and send to log
/*!
\param pMsg : Pointer to Supl message to decode to log
\param incoming : Direction of message
*/
void logSupl(const struct ULP_PDU * pMsg, bool incoming)
{
if (CUbxGpsState::getInstance()->getLogSuplMessages() ||
CUbxGpsState::getInstance()->getSuplMsgToFile())
{
logSep();
char buffer[200];
int len = sprintf(buffer, "%s\n", incoming ? "<------- From Server" : "-------> To Server");
renderMemStreamToLog(buffer, (unsigned int) len);
char* pBuf = NULL;
size_t size = 0;
FILE* pFile = open_memstream(&pBuf, &size); // Supl print message needs a file stream
asn_fprint(pFile, &asn_DEF_ULP_PDU, pMsg);
fclose(pFile);
renderMemStreamToLog(pBuf, size); // Now transfer from memory to Android log
free(pBuf);
logSep();
}
}
///////////////////////////////////////////////////////////////////////////////
//! Decode a RRLP message to text and send to log
/*!
\param pMsg : Pointer to RRLP message to decode to log
*/
void logRRLP(const PDU_t *pRrlpMsg, bool incoming)
{
if (CUbxGpsState::getInstance()->getLogSuplMessages() ||
CUbxGpsState::getInstance()->getSuplMsgToFile())
{
logSep();
char buffer[200];
int len = sprintf(buffer, "RRLP Payload\n");
renderMemStreamToLog(buffer, (unsigned int) len);
len = sprintf(buffer, "%s\n", incoming ? "<------- From Server" : "-------> To Server");
renderMemStreamToLog(buffer, (unsigned int) len);
char* pBuf = NULL;
size_t size = 0;
FILE* pFile = open_memstream(&pBuf, &size); // RRLP print message needs a file stream
asn_fprint(pFile, &asn_DEF_PDU, pRrlpMsg);
fclose(pFile);
renderMemStreamToLog(pBuf, size); // Now transfer from memory to Android log
free(pBuf);
logSep();
}
}
///////////////////////////////////////////////////////////////////////////////
// Instances for each log file
CLog logGps("GPS.LOG", 64*1024);
CLog logAgps("A-GPS.LOG", 32*1024, true);
#endif