Redirecting pocketsphinx log to main log

This commit is contained in:
Daniel Wolf 2016-02-29 21:47:36 +01:00
parent 7efea6f56b
commit cdffb56613
1 changed files with 25 additions and 10 deletions

View File

@ -10,6 +10,7 @@
#include <s3types.h> #include <s3types.h>
#include <regex> #include <regex>
#include <gsl_util.h> #include <gsl_util.h>
#include <logging.h>
extern "C" { extern "C" {
#include <pocketsphinx.h> #include <pocketsphinx.h>
@ -111,8 +112,25 @@ void processAudioStream(AudioStream& audioStream16kHzMono, function<void(const v
} while (buffer.size()); } while (buffer.size());
} }
void sphinxErrorCallback(void* user_data, err_lvl_t errorLevel, const char* format, ...) { LogLevel ConvertSphinxErrorLevel(err_lvl_t errorLevel) {
if (errorLevel < ERR_WARN) return; switch (errorLevel) {
case ERR_DEBUG:
case ERR_INFO:
case ERR_INFOCONT:
return LogLevel::Trace;
case ERR_WARN:
return LogLevel::Warning;
case ERR_ERROR:
return LogLevel::Error;
case ERR_FATAL:
return LogLevel::Fatal;
default:
throw invalid_argument("Unknown log level.");
}
}
void sphinxLogCallback(void* user_data, err_lvl_t errorLevel, const char* format, ...) {
UNUSED(user_data);
// Create varArgs list // Create varArgs list
va_list args; va_list args;
@ -133,10 +151,8 @@ void sphinxErrorCallback(void* user_data, err_lvl_t errorLevel, const char* form
string message(chars.data()); string message(chars.data());
boost::algorithm::trim(message); boost::algorithm::trim(message);
// Append message to error string LogLevel logLevel = ConvertSphinxErrorLevel(errorLevel);
string* errorString = static_cast<string*>(user_data); LOG(logLevel) << message;
if (errorString->size() > 0) *errorString += "\n";
*errorString += message;
} }
vector<string> recognizeWords(unique_ptr<AudioStream> audioStream, ps_decoder_t& recognizer, ProgressSink& progressSink) { vector<string> recognizeWords(unique_ptr<AudioStream> audioStream, ps_decoder_t& recognizer, ProgressSink& progressSink) {
@ -286,9 +302,8 @@ map<centiseconds, Phone> detectPhones(
// Discard Pocketsphinx output // Discard Pocketsphinx output
err_set_logfp(nullptr); err_set_logfp(nullptr);
// Collect all Pocketsphinx error messages in a string // Redirect Pocketsphinx output to log
string errorMessage; err_set_callback(sphinxLogCallback, nullptr);
err_set_callback(sphinxErrorCallback, &errorMessage);
try { try {
// Create PocketSphinx configuration // Create PocketSphinx configuration
@ -315,6 +330,6 @@ map<centiseconds, Phone> detectPhones(
return result; return result;
} }
catch (...) { catch (...) {
std::throw_with_nested(runtime_error("Error performing speech recognition via Pocketsphinx. " + errorMessage)); std::throw_with_nested(runtime_error("Error performing speech recognition via Pocketsphinx."));
} }
} }