From 14969afab764d2f93f2cbe6bb654ccbfbc6ae4c1 Mon Sep 17 00:00:00 2001 From: Daniel Wolf Date: Tue, 20 Dec 2016 22:24:50 +0100 Subject: [PATCH] Added --quiet mode --- src/lib/rhubarbLib.cpp | 2 +- src/main.cpp | 63 ++++++++++++++++++++++----------------- src/tools/ProgressBar.cpp | 6 ++-- src/tools/ProgressBar.h | 4 ++- 4 files changed, 43 insertions(+), 32 deletions(-) diff --git a/src/lib/rhubarbLib.cpp b/src/lib/rhubarbLib.cpp index b01b620..6ee8a3d 100644 --- a/src/lib/rhubarbLib.cpp +++ b/src/lib/rhubarbLib.cpp @@ -25,7 +25,7 @@ unique_ptr createWaveAudioClip(path filePath) { try { return std::make_unique(filePath); } catch (...) { - std::throw_with_nested(std::runtime_error(fmt::format("Could not open sound file '{0}'.", filePath.string()))); + std::throw_with_nested(std::runtime_error(fmt::format("Could not open sound file {}.", filePath))); } } diff --git a/src/main.cpp b/src/main.cpp index 4bdb4bb..510ce85 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -20,6 +20,8 @@ #include "TsvExporter.h" #include "XmlExporter.h" #include "JsonExporter.h" +#include +#include using std::exception; using std::string; @@ -80,10 +82,8 @@ unique_ptr createExporter(ExportFormat exportFormat) { } int main(int argc, char *argv[]) { -#ifdef _DEBUG auto pausableStderrSink = addPausableStdErrSink(logging::Level::Warn); pausableStderrSink->pause(); -#endif // Define command-line parameters const char argumentValueSeparator = ' '; @@ -94,6 +94,7 @@ int main(int argc, char *argv[]) { tclap::ValuesConstraint logLevelConstraint(logLevels); tclap::ValueArg logLevel("", "logLevel", "The minimum log level to log", false, logging::Level::Debug, &logLevelConstraint, cmd); tclap::ValueArg logFileName("", "logFile", "The log file path.", false, string(), "string", cmd); + tclap::SwitchArg quietMode("q", "quiet", "Suppresses all output to stderr except for error messages.", cmd, false); tclap::ValueArg maxThreadCount("", "threads", "The maximum number of worker threads to use.", false, getProcessorCoreCount(), "number", cmd); tclap::ValueArg dialogFile("d", "dialogFile", "A file containing the text of the dialog.", false, string(), "string", cmd); auto exportFormats = vector(ExportFormatConverter::get().getValues()); @@ -101,20 +102,24 @@ int main(int argc, char *argv[]) { tclap::ValueArg exportFormat("f", "exportFormat", "The export format.", false, ExportFormat::Tsv, &exportFormatConstraint, cmd); tclap::UnlabeledValueArg inputFileName("inputFile", "The input file. Must be a sound file in WAVE format.", true, "", "string", cmd); + std::ostream* infoStream = &std::cerr; + boost::iostreams::stream nullStream((boost::iostreams::null_sink())); + try { auto resumeLogging = gsl::finally([&]() { - std::cerr << std::endl << std::endl; -#ifdef _DEBUG + *infoStream << std::endl << std::endl; pausableStderrSink->resume(); -#endif - std::cerr << std::endl; }); // Parse command line cmd.parse(argc, argv); + if (quietMode.getValue()) { + infoStream = &nullStream; + } if (maxThreadCount.getValue() < 1) { throw std::runtime_error("Thread count must be 1 or higher."); } + path inputFilePath(inputFileName.getValue()); // Set up log file if (logFileName.isSet()) { @@ -124,43 +129,45 @@ int main(int argc, char *argv[]) { logging::infoFormat("Application startup. Command line: {}", join( vector(argv, argv + argc) | transformed([](char* arg) { return fmt::format("\"{}\"", arg); }), " ")); - std::cerr << "Processing input file. "; - JoiningContinuousTimeline animation(TimeRange::zero(), Shape::X); - { - ProgressBar progressBar; + try { + *infoStream << fmt::format("Generating lip-sync data for {}.", inputFilePath) << std::endl; + *infoStream << "Processing. "; + JoiningContinuousTimeline animation(TimeRange::zero(), Shape::X); + { + ProgressBar progressBar(*infoStream); - // Animate the recording - animation = animateWaveFile( - inputFileName.getValue(), - dialogFile.isSet() ? readUtf8File(path(dialogFile.getValue())) : boost::optional(), - maxThreadCount.getValue(), - progressBar); + // Animate the recording + animation = animateWaveFile( + inputFilePath, + dialogFile.isSet() ? readUtf8File(path(dialogFile.getValue())) : boost::optional(), + maxThreadCount.getValue(), + progressBar); + } + *infoStream << "Done." << std::endl << std::endl; + + // Export animation + unique_ptr exporter = createExporter(exportFormat.getValue()); + exporter->exportShapes(inputFilePath, animation, std::cout); + + logging::info("Exiting application normally."); + } catch (...) { + std::throw_with_nested(std::runtime_error(fmt::format("Error processing file {}.", inputFilePath))); } - std::cerr << "Done." << std::endl << std::endl; - - // Export animation - unique_ptr exporter = createExporter(exportFormat.getValue()); - exporter->exportShapes(path(inputFileName.getValue()), animation, std::cout); - - logging::info("Exiting application normally."); return 0; } catch (tclap::ArgException& e) { // Error parsing command-line args. cmd.getOutput()->failure(cmd, e); - std::cerr << std::endl; - logging::error("Invalid command line. Exiting application with error code."); + logging::error("Invalid command line. Exiting application."); return 1; } catch (tclap::ExitException&) { // A built-in TCLAP command (like --help) has finished. Exit application. - std::cerr << std::endl; logging::info("Exiting application after help-like command."); return 0; } catch (const exception& e) { // Generic error string message = getMessage(e); - std::cerr << "An error occurred.\n" << message << std::endl; - logging::errorFormat("Exiting application with error: {}", message); + logging::fatalFormat("Exiting application with error:\n{}", message); return 1; } } diff --git a/src/tools/ProgressBar.cpp b/src/tools/ProgressBar.cpp index 26a1e7f..79bbfd1 100644 --- a/src/tools/ProgressBar.cpp +++ b/src/tools/ProgressBar.cpp @@ -48,7 +48,9 @@ void ProgressMerger::report() { } } -ProgressBar::ProgressBar() { +ProgressBar::ProgressBar(std::ostream& stream) : + stream(stream) +{ updateLoopFuture = std::async(std::launch::async, &ProgressBar::updateLoop, this); } @@ -108,6 +110,6 @@ void ProgressBar::updateText(const string& text) { output.append(overlapCount, '\b'); } - std::cerr << output; + stream << output; currentText = text; } diff --git a/src/tools/ProgressBar.h b/src/tools/ProgressBar.h index 93332df..8406493 100644 --- a/src/tools/ProgressBar.h +++ b/src/tools/ProgressBar.h @@ -7,6 +7,7 @@ #include #include #include +#include class ProgressSink { public: @@ -43,7 +44,7 @@ private: class ProgressBar : public ProgressSink { public: - ProgressBar(); + ProgressBar(std::ostream& stream = std::cerr); ~ProgressBar(); void reportProgress(double value) override; @@ -55,6 +56,7 @@ private: std::atomic currentProgress { 0 }; std::atomic done { false }; + std::ostream& stream; std::string currentText; int animationIndex = 0; };