Added structured logging

This commit is contained in:
Daniel Wolf 2016-03-01 21:57:05 +01:00
parent cdffb56613
commit 04ca644cca
7 changed files with 40 additions and 7 deletions

View File

@ -2,6 +2,8 @@
#include <array> #include <array>
#include <boost/log/sinks/unlocked_frontend.hpp> #include <boost/log/sinks/unlocked_frontend.hpp>
#include <boost/log/expressions.hpp> #include <boost/log/expressions.hpp>
#include <centiseconds.h>
#include "tools.h"
using std::string; using std::string;
using std::lock_guard; using std::lock_guard;
@ -64,3 +66,6 @@ boost::shared_ptr<PausableBackendAdapter> initLogging() {
return pausableAdapter; return pausableAdapter;
} }
void logTimedEvent(const string& eventName, centiseconds start, centiseconds end, const string& value) {
LOG_DEBUG << "##" << eventName << "[" << formatDuration(start) << "-" << formatDuration(end) << "]: " << value;
}

View File

@ -8,6 +8,7 @@
#include <vector> #include <vector>
#include <mutex> #include <mutex>
#include <tuple> #include <tuple>
#include "centiseconds.h"
enum class LogLevel { enum class LogLevel {
Trace, Trace,
@ -56,3 +57,5 @@ private:
}; };
boost::shared_ptr<PausableBackendAdapter> initLogging(); boost::shared_ptr<PausableBackendAdapter> initLogging();
void logTimedEvent(const std::string& eventName, centiseconds start, centiseconds end, const std::string& value);

View File

@ -12,6 +12,7 @@
#include "ProgressBar.h" #include "ProgressBar.h"
#include "logging.h" #include "logging.h"
#include <gsl_util.h> #include <gsl_util.h>
#include <tools.h>
using std::exception; using std::exception;
using std::string; using std::string;
@ -41,10 +42,6 @@ unique_ptr<AudioStream> createAudioStream(path filePath) {
} }
} }
string formatDuration(duration<double> seconds) {
return fmt::format("{0:.2f}", seconds.count());
}
ptree createXmlTree(const path& filePath, const map<centiseconds, Phone>& phones, const map<centiseconds, Shape>& shapes) { ptree createXmlTree(const path& filePath, const map<centiseconds, Phone>& phones, const map<centiseconds, Shape>& shapes) {
ptree tree; ptree tree;

View File

@ -1,4 +1,5 @@
#include "mouthAnimation.h" #include "mouthAnimation.h"
#include "logging.h"
using std::map; using std::map;
@ -69,12 +70,18 @@ Shape getShape(Phone phone) {
map<centiseconds, Shape> animate(const map<centiseconds, Phone> &phones) { map<centiseconds, Shape> animate(const map<centiseconds, Phone> &phones) {
map<centiseconds, Shape> shapes; map<centiseconds, Shape> shapes;
Shape lastShape = Shape::Invalid; Shape lastShape = Shape::Invalid;
for (auto it = phones.cbegin(); it != phones.cend(); it++) { for (auto it = phones.cbegin(); it != phones.cend(); ++it) {
Shape shape = getShape(it->second); Shape shape = getShape(it->second);
if (shape != lastShape || next(it) == phones.cend()) { if (shape != lastShape || next(it) == phones.cend()) {
shapes[it->first] = shape; shapes[it->first] = shape;
lastShape = shape; lastShape = shape;
} }
} }
for (auto it = shapes.cbegin(); it != shapes.cend(); ++it) {
if (next(it) == shapes.cend()) break;
logTimedEvent("shape", it->first, next(it)->first, shapeToString(it->second));
}
return shapes; return shapes;
} }

View File

@ -31,6 +31,7 @@ using boost::filesystem::path;
using std::function; using std::function;
using std::regex; using std::regex;
using std::regex_replace; using std::regex_replace;
using std::chrono::duration;
unique_ptr<AudioStream> to16kHzMono(unique_ptr<AudioStream> stream) { unique_ptr<AudioStream> to16kHzMono(unique_ptr<AudioStream> stream) {
// Downmix, if required // Downmix, if required
@ -180,6 +181,10 @@ vector<string> recognizeWords(unique_ptr<AudioStream> audioStream, ps_decoder_t&
for (ps_seg_t* it = ps_seg_iter(&recognizer, &score); it; it = ps_seg_next(it)) { for (ps_seg_t* it = ps_seg_iter(&recognizer, &score); it; it = ps_seg_next(it)) {
const char* word = ps_seg_word(it); const char* word = ps_seg_word(it);
result.push_back(word); result.push_back(word);
int firstFrame, lastFrame;
ps_seg_frames(it, &firstFrame, &lastFrame);
logTimedEvent("word", centiseconds(firstFrame), centiseconds(lastFrame + 1), word);
} }
return result; return result;
@ -288,8 +293,12 @@ map<centiseconds, Phone> getPhoneAlignment(const vector<s3wid_t>& wordIds, uniqu
int duration = phoneEntry->duration; int duration = phoneEntry->duration;
// Add map entries // Add map entries
result[centiseconds(startFrame)] = stringToPhone(phoneName); centiseconds start(startFrame);
result[centiseconds(startFrame + duration)] = Phone::None; result[start] = stringToPhone(phoneName);
centiseconds end(startFrame + duration);
result[end] = Phone::None;
logTimedEvent("phone", start, end, phoneName);
} }
return result; return result;
} }

View File

@ -1 +1,10 @@
#include "tools.h" #include "tools.h"
#include <format.h>
#include <chrono>
using std::string;
using std::chrono::duration;
string formatDuration(duration<double> seconds) {
return fmt::format("{0:.2f}", seconds.count());
}

View File

@ -2,8 +2,11 @@
#include <functional> #include <functional>
#include <memory> #include <memory>
#include <chrono>
#define UNUSED(x) ((void)(x)) #define UNUSED(x) ((void)(x))
template<typename T> template<typename T>
using lambda_unique_ptr = std::unique_ptr<T, std::function<void(T*)>>; using lambda_unique_ptr = std::unique_ptr<T, std::function<void(T*)>>;
std::string formatDuration(std::chrono::duration<double> seconds);