void print_word_token(srec* rec, wtokenID wtoken_index, char* msg)
{
  bigcostdata cost, cost_for_word;
  char *p = "NULL";
  word_token* wtoken = &rec->word_token_array[wtoken_index];

  PLogMessage ( msg );
  if (wtoken->word < rec->context->olabels->num_words)
    p = rec->context->olabels->words[wtoken->word];
  ASSERT(rec->accumulated_cost_offset[ wtoken->end_time] != 0);
  cost = wtoken->cost + rec->accumulated_cost_offset[wtoken->end_time];
  if (wtoken->backtrace != MAXwtokenID)
  {
    word_token* next_wtoken = &rec->word_token_array[wtoken->backtrace];
    cost_for_word = cost - next_wtoken->cost - rec->accumulated_cost_offset[next_wtoken->end_time];
  }
  else
  {
    cost_for_word = cost;
  }
  printf("wtoken %d W%i %s cost=%d/%d/%d time=%d/%d node=%d", wtoken_index,
         wtoken->word, p, wtoken->cost, cost, cost_for_word, wtoken->end_time, WORD_TOKEN_GET_WD_ETIME(wtoken), wtoken->end_node);
  pfflush(PSTDOUT);
  print_word_token_backtrace(rec, wtoken->backtrace, "\n");
}
/**
 * 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);
}
ESR_ReturnCode SR_EventLog_Destroy(SR_EventLog* self)
{
  SR_EventLogImpl* impl = (SR_EventLogImpl*) self;
  ESR_ReturnCode rc;

  if (impl->logFile_state == FILE_OK)
  {
    pfflush(impl->logFile);

    pfclose(impl->logFile);
    impl->logFile = NULL;
    impl->logFile_state = NO_FILE;
  }
  CHKLOG(rc, ESR_SessionRemoveProperty(L("eventlog")));
  FREE(impl);
  return ESR_SUCCESS;
CLEANUP:
  return rc;
}
ESR_ReturnCode logIt(SR_EventLogImpl *impl, LCHAR* evtt, LCHAR* log_record, size_t* writtenSize)
{
  struct tm *ct, ct_r;
  LCHAR header[128], header2[64];
  PTimeStamp timestamp;
  const size_t sizeof_LCHAR = sizeof(LCHAR);
  const LCHAR* bar = "|";
  const LCHAR* nl = "\n";
  size_t i, len;
  const LCHAR* toWrite[5];

  toWrite[0] = header;
  toWrite[1] = bar;
  toWrite[2] = evtt;
  toWrite[3] = log_record;
  toWrite[4] = nl;

  ct = &ct_r;
  memset(ct, 0, sizeof(struct tm));

  switch (impl->logFile_state)
  {
    case FILE_OK:
    case SPACE_SETTING:
      PTimeStampSet(&timestamp);
      ct = localtime_r(&timestamp.secs, &ct_r);

      sprintf(header, "TIME=%04d%02d%02d%02d%02d%02d%03d",
              ct->tm_year + 1900, ct->tm_mon + 1, ct->tm_mday, ct->tm_hour,
              ct->tm_min, ct->tm_sec, timestamp.msecs);
      quote_delimiter(header, 128);

      sprintf(header2, "CHAN=%s", L("0")); /* default is channel 0 in ESR */
      quote_delimiter(header2, 128);

      LSTRCAT(header, bar);
      LSTRCAT(header, header2);

      /* write the header,bar,evtt, and record */
      for (*writtenSize = 0, i = 0; i < 5; i++)
      {
        len = LSTRLEN(toWrite[i]);
        if (pfwrite(toWrite[i], sizeof_LCHAR, len, impl->logFile))
          *writtenSize += len;
      }

      if (*writtenSize <= 0)
      {
        PLogError(L("Could not write to log file; logging halted"));
        impl->logFile_state = FILE_ERROR;
        break;
      }
      else
      {
        pfflush(impl->logFile);
      }

      break;

      /* If couldn't open file or error previously, just return */
    case UNINITIALIZED:
    case NO_FILE:
    case FILE_ERROR:
    case SEEK_ERROR:
    default:
      return ESR_INVALID_STATE;

  }

  return ESR_SUCCESS;
}
Exemple #5
0
void PStreamFile::flush()
{
    pfflush(m_file);
}
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;
}
static ESR_ReturnCode FileLoggerFlush(PLogger *self)
{
  FileLogger *p = STATIC_CAST(self, FileLogger, base);
  return pfflush(p->fp) == 0 ? ESR_SUCCESS : ESR_FATAL_ERROR;
}