summaryrefslogtreecommitdiff
path: root/portable/src/plog.c
blob: b5f07ae1206577554e92cdd6b22e2cb4b20e14de (plain)
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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
/*---------------------------------------------------------------------------*
 *  plog.c  *
 *                                                                           *
 *  Copyright 2007, 2008 Nuance Communciations, Inc.                               *
 *                                                                           *
 *  Licensed under the Apache License, Version 2.0 (the 'License');          *
 *  you may not use this file except in compliance with the License.         *
 *                                                                           *
 *  You may obtain a copy of the License at                                  *
 *      http://www.apache.org/licenses/LICENSE-2.0                           *
 *                                                                           *
 *  Unless required by applicable law or agreed to in writing, software      *
 *  distributed under the License is distributed on an 'AS IS' BASIS,        *
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * 
 *  See the License for the specific language governing permissions and      *
 *  limitations under the License.                                           *
 *                                                                           *
 *---------------------------------------------------------------------------*/


#include <stdio.h>
#include <stdarg.h>
#include "PFileSystem.h"
#include "ptypes.h"
#include "plog.h"
#include "pmemory.h"
#include "pstdio.h"
#include "ptimestamp.h"
#include "passert.h"
#ifdef USE_STACKTRACE
#include "PStackTrace.h"
#endif

#ifdef USE_THREAD
#include "ptrd.h"
#include "pmutex.h"
#endif


#if defined (ANDROID)
#if defined (HAVE_ANDROID_OS)
#define LOG_TAG "Srec" 
#include <utils/Log.h>
#endif
#endif

#include "phashtable.h"

#define MTAG __FILE__

#define FILTER_MSG_1		"ESR_BUFFER_OVERFLOW"
#define FILTER_MSG_1_SIZE	( sizeof ( FILTER_MSG_1 ) - 1 )

#define FILTER_MSG_2		"ESR_NO_MATCH_ERROR"
#define FILTER_MSG_2_SIZE	( sizeof ( FILTER_MSG_2 ) - 1 )

static unsigned int GlogLevel = 0;
static PLogger *Glogger = NULL;
static LOG_OUTPUT_FORMAT GlogFormat = LOG_OUTPUT_FORMAT_MODULE_NAME |
                                      LOG_OUTPUT_FORMAT_DATE_TIME;
/**
 * Used to detect endless recursion where the PLog module calls itself.
 */
static ESR_BOOL locked = ESR_FALSE;
#ifdef USE_THREAD

static PtrdMutex* Gmutex = NULL;
#endif

typedef struct FileLogger_t
{
  PLogger base;
  PFile* fp;
}
FileLogger;

/**
 * Prints and formats a message to the log.
 *
 * @param self the PLogger.
 *
 * @param format the format string specifying the next arguments (a la
 * printf).
 *
 * @param args variable argument list.
 *
 * @return The number of bytes written to the PLogger or -1 if an error
 * occurs.
 */
static ESR_ReturnCode FileLoggerPrintf(PLogger *self, const LCHAR *format, ...)
{
  FileLogger *p = STATIC_CAST(self, FileLogger, base);
  ESR_ReturnCode rc;
  va_list args;
  
  va_start(args, format);
  rc = pvfprintf(p->fp, format, args);
  va_end(args);
  return rc;
}

static ESR_ReturnCode FileLoggerFlush(PLogger *self)
{
  FileLogger *p = STATIC_CAST(self, FileLogger, base);
  return pfflush(p->fp) == 0 ? ESR_SUCCESS : ESR_FATAL_ERROR;
}


/**
 * Destroys the logger.  This function is responsible to deallocate any
 * resources used by the logger.  In particular, if buffering is internally
 * used, it needs to flush the buffer.
 */
static void FileLoggerDestroy(PLogger *self)
{
  FileLogger *p = STATIC_CAST(self, FileLogger, base);
  pfflush(p->fp);
  
  if (p->fp != PSTDERR && p->fp != PSTDOUT)
    pfclose(p->fp);
  FREE(p);
}

static ESR_ReturnCode createPFileLogger(PFile* fp, PLogger** logger)
{
  FileLogger* fileLogger;
  
  if (fp == NULL)
    return ESR_INVALID_ARGUMENT;
  fileLogger = NEW(FileLogger, MTAG);
  if (fileLogger == NULL)
    return ESR_OUT_OF_MEMORY;
    
  fileLogger->base.printf = FileLoggerPrintf;
  fileLogger->base.flush = FileLoggerFlush;
  fileLogger->base.destroy = FileLoggerDestroy;
  fileLogger->fp = fp;
  
  *logger = &fileLogger->base;
  return ESR_SUCCESS;
}

/**
 * Initializes the LOG library.  This function must be called before any
 * logging can take place.
 *
 * @param logger The logger to be used to output the messages.  If NULL, then
 * logging goes to PSTDERR.  @param logLevel The level of logging requested.
 *
 * @return ESR_SUCCESS if success, anything else if an error occurs.
 *
 */
ESR_ReturnCode PLogInit(PLogger *logger, unsigned int logLevel)
{
  ESR_ReturnCode rc = ESR_SUCCESS;
  
  if (Glogger != NULL)
    return ESR_INVALID_STATE;
    
  GlogLevel = logLevel;
  
#ifdef USE_THREAD
  if ((rc = PtrdMutexCreate(&Gmutex)) != ESR_SUCCESS)
    return rc;
#endif
    
  if (logger != NULL)
    Glogger = logger;
  else
  {
    rc = createPFileLogger(PSTDERR, &Glogger);
    if (rc != ESR_SUCCESS)
      goto CLEANUP;
  }
  
  return rc;
CLEANUP:
#ifdef USE_THREAD
  if (Gmutex != NULL)
  {
    PtrdMutexDestroy(Gmutex);
    Gmutex = NULL;
  }
#endif
  return rc;
}

ESR_ReturnCode PLogIsInitialized(ESR_BOOL* isInit)
{
  if (isInit == NULL)
    return ESR_INVALID_STATE;
  *isInit = Glogger != NULL;
  return ESR_SUCCESS;
}

ESR_ReturnCode PLogIsLocked(ESR_BOOL* isLocked)
{
  if (isLocked == NULL)
    return ESR_INVALID_STATE;
  *isLocked = locked;
  return ESR_SUCCESS;
}

/**
 * Shutdowns the LOG library.  Once this function is called, no logging activity can be performed.
 * Also, the logger that was given to pLogInit is destroyed.
 *
 * @return ESR_SUCCESS if success, anything else if an error occurs.
 *
 */
ESR_ReturnCode PLogShutdown()
{
  ESR_ReturnCode rc = ESR_SUCCESS;
  
  if (Glogger == NULL)
    return ESR_INVALID_STATE;
    
#ifdef USE_THREAD
  if ((rc = PtrdMutexDestroy(Gmutex)) != ESR_SUCCESS)
    return rc;
  Gmutex = NULL;
#endif
  
  if (Glogger->flush != NULL)
    Glogger->flush(Glogger);
  Glogger->destroy(Glogger);
  Glogger = NULL;
  return rc;
}

ESR_ReturnCode PLogGetLevel(unsigned int *logLevel)
{
  if (Glogger == NULL)
    return ESR_INVALID_STATE;
  if (logLevel == NULL)
    return ESR_INVALID_ARGUMENT;
    
  *logLevel = GlogLevel;
  return ESR_SUCCESS;
}

ESR_ReturnCode PLogSetLevel(unsigned int logLevel)
{
  if (Glogger == NULL)
    return ESR_INVALID_STATE;
    
  GlogLevel = logLevel;
  return ESR_SUCCESS;
}

#define TIME_BUF_SIZE 24
#define TIME_FORMAT L("%Y/%m/%d %H:%M:%S")
#define PLOG_PANIC(x, rc) \
  do \
  { \
    { \
      pfprintf(PSTDERR, L("[%s:%d] %s failed with %s\n"), __FILE__, __LINE__, x, ESR_rc2str(rc)); \
      pfflush(PSTDERR); \
    } \
  } while (0)

static ESR_ReturnCode logIt(const LCHAR *format, va_list args, ESR_BOOL showStackTrace)
{
  ESR_ReturnCode rc = ESR_SUCCESS;
  ESR_ReturnCode flushRC = ESR_SUCCESS;
#ifdef USE_STACKTRACE
#define BUFFER_SIZE P_MAX_STACKTRACE + 2000
#else
#define BUFFER_SIZE 2000
#endif
  LCHAR buffer[BUFFER_SIZE] = L("");

  // TODO: Remove once logging subsystem supports "warn" level
  if (strstr(format, "ESR_BUFFER_OVERFLOW")==format)
    return ESR_SUCCESS;
  
#ifdef USE_STACKTRACE
  if (Glogger == NULL)
  {
    /*
     * There are three possible scenerios for why logging would occur although the PLog module
     * is uninitialized:
     *
     * 1) The code fails before PLog is initialized (perhaps in other portable components)
     * 2) The user forgets to initialize the PLog module
     * 3) The code fails after PLog is uninitialized (on shutdown)
     *
     * We do our best by logging any errors but this might result in the memory leak of
     * the PStackTrace module in case 3.
     */
    rc = PStackTraceCreate();
    if (rc != ESR_SUCCESS)
    {
      PLOG_PANIC(L("PStackTraceCreate"), rc);
      goto CLEANUP;
    }
  }
  else
  {
#ifdef USE_THREAD
    rc = PtrdMutexLock(Gmutex);
    if (rc != ESR_SUCCESS)
      return rc;
#endif
  }
  if (locked)
    return ESR_INVALID_STATE;
  locked = ESR_TRUE;
  
  if (GlogFormat & LOG_OUTPUT_FORMAT_DATE_TIME)
  {
    PTimeStamp now;
    struct tm* loctime;
    LCHAR timeStr[TIME_BUF_SIZE];
    size_t timeStrSize;
    
    PTimeStampSet(&now);
    loctime = localtime(&now.secs);
    timeStrSize = LSTRFTIME(timeStr, TIME_BUF_SIZE, TIME_FORMAT, loctime);
    passert(timeStrSize == (TIME_BUF_SIZE - 5));
    psprintf(timeStr + (TIME_BUF_SIZE - 5), ".%03hu", now.msecs);
    
    psprintf(buffer + LSTRLEN(buffer), L("%s|"), timeStr);
    passert(LSTRLEN(buffer) < BUFFER_SIZE);
  }
  
  if (GlogFormat & LOG_OUTPUT_FORMAT_THREAD_ID)
  {
    rc = psprintf(buffer + LSTRLEN(buffer), L("trd=%u|"), PtrdGetCurrentThreadId());
    passert(LSTRLEN(buffer) < BUFFER_SIZE);
  }
  
  if (GlogFormat & LOG_OUTPUT_FORMAT_MODULE_NAME && showStackTrace)
  {
    size_t len = P_MAX_STACKTRACE;
    LCHAR text[P_MAX_STACKTRACE];
    LCHAR* index;
    size_t i;
    
    rc = PStackTraceGetValue((LCHAR*) & text, &len);
    if (rc == ESR_SUCCESS)
    {
      for (i = 0; i < 2; ++i)
      {
        rc = PStackTracePopLevel((LCHAR*) & text);
        if (rc != ESR_SUCCESS)
        {
          PLOG_PANIC(L("PStackTracePopLevel"), rc);
          goto CLEANUP;
        }
      }
      index = text;
      while (index)
      {
        index = LSTRSTR(index, L(" at\n"));
        if (index != NULL)
        {
          *(index + 1) = L('<');
          *(index + 2) = L('-');
          *(index + 3) = L(' ');
        }
      }
    }
    else if (rc == ESR_NOT_SUPPORTED)
      LSTRCPY(text, L(""));
    else if (rc != ESR_SUCCESS)
    {
      PLOG_PANIC(L("PStackTraceGetValue"), rc);
      goto CLEANUP;
    }
    rc = psprintf(buffer + LSTRLEN(buffer), L("Module=%s|"), text);
    passert(LSTRLEN(buffer) < BUFFER_SIZE);
  }
  
  pvsprintf(buffer + LSTRLEN(buffer), format, args);
#else
  pvsprintf(buffer + LSTRLEN(buffer), format, args);
#endif
  passert(LSTRLEN(buffer) < BUFFER_SIZE);
  
  psprintf(buffer + LSTRLEN(buffer), L("\n"));
  passert(LSTRLEN(buffer) < BUFFER_SIZE);
  
  if (Glogger != NULL)
  {
    rc = Glogger->printf(Glogger, L("%s"), buffer);
    if (rc != ESR_SUCCESS)
      goto CLEANUP;
    flushRC = Glogger->flush(Glogger);
  }
  else
  {
    /* We need to log but the logging module is disabled or is locked so we output to stderr instead */
    {
      pfprintf(PSTDERR, L("%s"), buffer);
      pfflush(PSTDERR);
    }
  }
  locked = ESR_FALSE;
#ifdef USE_THREAD
  PtrdMutexUnlock(Gmutex);
#endif
  return flushRC;
CLEANUP:
  if (Glogger != NULL && Glogger->flush != NULL)
    flushRC = Glogger->flush(Glogger);
  locked = ESR_FALSE;
#ifdef USE_THREAD
  PtrdMutexUnlock(Gmutex);
#endif
  return rc != ESR_SUCCESS ? rc : flushRC;
}

/**
 * Conditionally PLogs a message.  The message is logged only if module is enabled.
 *
 * @param msg The message format specification (ala printf).
 * @return ESR_SUCCESS if success, anything else if an error occurs.
 */
ESR_ReturnCode PLogMessage(const char* msg, ...)
{
  va_list args;
  ESR_ReturnCode rc;
#if USE_STACKTRACE
  size_t depth;
#endif
  
#if defined (ANDROID)
#if defined (HAVE_ANDROID_OS)
  return ( ESR_SUCCESS );/* Get rid of this for phone device */
#endif
#endif

  if (Glogger == NULL)
    return ESR_INVALID_STATE;
#ifdef USE_STACKTRACE
  return ESR_SUCCESS;
  rc = PStackTraceGetDepth(&depth);
  
  if (rc == ESR_NOT_SUPPORTED)
  {
    /* Debugging symbols are missing */
    return ESR_SUCCESS;
  }
  else if (rc != ESR_SUCCESS)
  {
    pfprintf(PSTDERR, L("PStackTraceGetDepth"), ESR_rc2str(rc));
    goto CLEANUP;
  }
  
  /* Remove PLogMessage() from depth */
  --depth;
  if (GlogLevel < depth)
    return ESR_SUCCESS;
#endif
    
  va_start(args, msg);
  rc = logIt(msg, args, ESR_FALSE);
  va_end(args);
  return rc;
#if USE_STACKTRACE
CLEANUP:
  return rc;
#endif
}


/**
 * Unconditionally logs an error message.
 *
 * @param msg The message format specification (ala printf).
 * @return ESR_SUCCESS if success, anything else if an error occurs.
 */
ESR_ReturnCode PLogError(const char* msg, ...)
{
  va_list args;
  ESR_ReturnCode rc;
#if defined (ANDROID)
#if defined (HAVE_ANDROID_OS)
  char log_text [2048];
#endif
#endif

  va_start(args, msg);
#if defined (ANDROID)
#if defined (HAVE_ANDROID_OS)
  pvsprintf ( log_text, msg, args);
/* We need to disable some error messages because they are frequently not
 * errors but due to sloppy use of some functions. This prevents us from
 * getting flooded with bad error messages. SteveR
 */
  if ( ( strncmp ( log_text, FILTER_MSG_1, FILTER_MSG_1_SIZE ) != 0 ) &&
    ( strncmp ( log_text, FILTER_MSG_2, FILTER_MSG_2_SIZE ) != 0 ) )
  {
    ALOGE ("%s", log_text );
  }
  rc = 0;
#else
  rc = logIt(msg, args, ESR_TRUE);
#endif
#else
  rc = logIt(msg, args, ESR_TRUE);
#endif
  va_end(args);
  
  return rc;
}



ESR_ReturnCode PLogCreateFileLogger(PFile* file, PLogger **logger)
{
  if (logger == NULL || file == NULL)
    return ESR_INVALID_ARGUMENT;
    
  return createPFileLogger(file, logger);
}

/**
 * Creates a logger that logs to a circular file.
 *
 * @param filename The name of the file to be created.
 * @param maxsize The maximum number of bytes that the file may have.
 * @param logger logger handle receiving the created logger.
 */
ESR_ReturnCode PLogCreateCircularFileLogger(const LCHAR *filename,
    unsigned int maxsize,
    PLogger **logger)
{
  return ESR_NOT_SUPPORTED;
}


ESR_ReturnCode PLogSetFormat(LOG_OUTPUT_FORMAT format)
{
  GlogFormat = format;
  return ESR_SUCCESS;
}