ESR_ReturnCode SR_EventLog_AudioOpen(SR_EventLog* self, const LCHAR* audio_type, size_t sample_rate, size_t sample_size)
{
  SR_EventLogImpl *impl = (SR_EventLogImpl*) self;
  LCHAR *p;

  LSTRCPY(impl->waveformFilename, impl->logFilename);
  p = LSTRSTR(impl->waveformFilename, L(".log"));
  if (p == NULL)
  {
    PLogError(L("ESR_OPEN_ERROR: %s"), impl->waveformFilename);
    return ESR_OPEN_ERROR;
  }
  *p = 0; /* trunc the name */

  psprintf(impl->waveformFilename, L("%s-%04lu.wav"), impl->waveformFilename, (unsigned long) ++impl->waveformCounter);

  impl->waveformFile = pfopen ( impl->waveformFilename, L("wb+") );

  if (impl->waveformFile == NULL)
  {
    PLogError(L("ESR_OPEN_ERROR: %s"), impl->waveformFilename);
    return ESR_OPEN_ERROR;
  }
  impl->waveform_num_bytes = 0;
  impl->waveform_bytes_per_sample = sample_size;
  impl->waveform_sample_rate = sample_rate;
  return writeRiffHeader(self);
}
ESR_ReturnCode SR_GrammarLoad(const LCHAR* grammar, SR_Grammar** self)
{
  SR_Grammar* Interface = NULL;
  SR_GrammarImpl* impl;
  LCHAR* tok;
  ESR_ReturnCode rc;
  LCHAR filename[P_PATH_MAX];
  int addWords;
  
  
  if (self == NULL)
  {
    PLogError(L("ESR_INVALID_ARGUMENT"));
    return ESR_INVALID_ARGUMENT;
  }
  CHKLOG(rc, SR_Grammar_Create(&Interface));
  impl = (SR_GrammarImpl*) Interface;
  
  /**
   * Our filename (referring to the grammar to load, may have associated grammar properties
   * appended to the end of it. We need to split up the properties from the filename
   * example:
   *  recog_nm/namesnnumsSC_dyn,addWords=2000 becomes
   *    filename: recog_nm/namesnnumsSC_dyn
   *    property: addWords=2000
   */
  
  /* init */
  LSTRCPY(filename, grammar);
  addWords = 0;
  
  for (tok = strtok(filename, ","); tok; tok = strtok(NULL, ","))
  {
    if (LSTRSTR(tok, "addWords"))
    {
      addWords = atoi(LSTRCHR(tok, L('=')) + sizeof(LCHAR));
    }
    else if (tok != filename)
    {
      PLogError(L("UNKNOWN grammar load property %s"), tok);
      rc = ESR_INVALID_STATE;
      goto CLEANUP;
    }
  }
  
  /**
   * Based on the filename, determine if you are loading from image or loading from text files.
   * If from image, then the filename will have extension .g2g. If from file, then only a basename
   * will be provided (i.e. without extension)
   */
  
  impl->syntax = CA_AllocateSyntax();
  if (impl->syntax == NULL)
  {
    rc = ESR_OUT_OF_MEMORY;
    goto CLEANUP;
  }
  
  if (LSTRSTR(filename, L(".g2g")))
  {
    /* if the filename ends with .g2g, then we have a binary image */
    if (CA_LoadSyntaxFromImage(impl->syntax, (LCHAR*) filename))
    {
      rc = ESR_READ_ERROR;
      PLogError(L("ESR_READ_ERROR: Problem loading syntax from image"));
      goto CLEANUP;
    }
  }
  else
  {
    if (CA_LoadSyntaxAsExtensible(impl->syntax, (LCHAR*) filename, addWords))
    {
      rc = ESR_READ_ERROR;
      PLogError(L("ESR_READ_ERROR: Problem loading syntax from text"));
      goto CLEANUP;
    }
  }
  
  /*
   * Semantic Graph Loading
   *
   * - it was already created in Grammar_Create()
   * - reuse the same input labels from the recognition context
   * - load knows how to load from base filename or .g2g image
   */
  rc = impl->semgraph->load(impl->semgraph, impl->syntax->synx->olabels, filename, addWords);
  if (rc != ESR_SUCCESS)
  {
    PLogError(L("%s: loading semgraph from image %s"), ESR_rc2str(rc), filename);
    impl->semgraph = NULL;
    goto CLEANUP;
  }
  
  *self = Interface;
  if (impl->eventLog)
  {
    CHKLOG(rc, SR_EventLogTokenPointer_BASIC(impl->eventLog, impl->logLevel, L("igrm"), impl));
    CHKLOG(rc, SR_EventLogToken_BASIC(impl->eventLog, impl->logLevel, L("name"), filename));
    CHKLOG(rc, SR_EventLogEvent_BASIC(impl->eventLog, impl->logLevel, L("ESRldgrm")));
  }
  
  return ESR_SUCCESS;
CLEANUP:
  if (Interface != NULL)
    Interface->destroy(Interface);
  *self = NULL;
  return rc;
}
Example #3
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;
}