2015-09-28 19:19:39 +00:00
|
|
|
#include <iostream>
|
2015-11-25 21:00:24 +00:00
|
|
|
#include <format.h>
|
2015-12-29 10:44:55 +00:00
|
|
|
#include <tclap/CmdLine.h>
|
2015-12-29 15:26:01 +00:00
|
|
|
#include "appInfo.h"
|
2016-01-06 19:45:04 +00:00
|
|
|
#include "NiceCmdLineOutput.h"
|
2016-01-08 09:53:35 +00:00
|
|
|
#include "ProgressBar.h"
|
2016-02-29 20:00:38 +00:00
|
|
|
#include "logging.h"
|
|
|
|
#include <gsl_util.h>
|
2016-04-12 18:49:06 +00:00
|
|
|
#include "Exporter.h"
|
2016-05-02 18:31:59 +00:00
|
|
|
#include "ContinuousTimeline.h"
|
2016-06-03 19:07:49 +00:00
|
|
|
#include <boost/filesystem/operations.hpp>
|
|
|
|
#include "stringTools.h"
|
2016-06-14 15:38:11 +00:00
|
|
|
#include <boost/range/adaptor/transformed.hpp>
|
2016-07-20 18:42:27 +00:00
|
|
|
#include <boost/filesystem/fstream.hpp>
|
2016-08-11 08:29:01 +00:00
|
|
|
#include "parallel.h"
|
2016-11-15 20:56:39 +00:00
|
|
|
#include "exceptions.h"
|
|
|
|
#include "textFiles.h"
|
|
|
|
#include "rhubarbLib.h"
|
2015-09-28 19:19:39 +00:00
|
|
|
|
2015-11-19 17:32:14 +00:00
|
|
|
using std::exception;
|
|
|
|
using std::string;
|
2016-06-03 19:07:49 +00:00
|
|
|
using std::u32string;
|
2016-03-08 21:59:44 +00:00
|
|
|
using std::vector;
|
2015-11-19 17:32:14 +00:00
|
|
|
using std::unique_ptr;
|
2016-04-12 18:49:06 +00:00
|
|
|
using std::make_unique;
|
2016-04-13 20:37:39 +00:00
|
|
|
using std::shared_ptr;
|
|
|
|
using std::make_shared;
|
2015-11-25 21:00:24 +00:00
|
|
|
using std::map;
|
|
|
|
using std::chrono::duration;
|
|
|
|
using std::chrono::duration_cast;
|
|
|
|
using boost::filesystem::path;
|
2016-06-14 15:38:11 +00:00
|
|
|
using boost::adaptors::transformed;
|
2015-11-19 17:32:14 +00:00
|
|
|
|
2016-03-08 21:59:44 +00:00
|
|
|
namespace tclap = TCLAP;
|
|
|
|
|
|
|
|
// Tell TCLAP how to handle our types
|
2016-02-09 21:08:11 +00:00
|
|
|
namespace TCLAP {
|
|
|
|
template<>
|
2016-04-13 20:37:39 +00:00
|
|
|
struct ArgTraits<logging::Level> {
|
2016-03-08 21:59:44 +00:00
|
|
|
typedef ValueLike ValueCategory;
|
2016-02-09 21:08:11 +00:00
|
|
|
};
|
2016-04-12 18:49:06 +00:00
|
|
|
template<>
|
|
|
|
struct ArgTraits<ExportFormat> {
|
|
|
|
typedef ValueLike ValueCategory;
|
|
|
|
};
|
2016-02-09 21:08:11 +00:00
|
|
|
}
|
|
|
|
|
2016-04-13 20:37:39 +00:00
|
|
|
shared_ptr<logging::PausableSink> addPausableStdErrSink(logging::Level minLevel) {
|
|
|
|
auto stdErrSink = make_shared<logging::StdErrSink>(make_shared<logging::SimpleConsoleFormatter>());
|
|
|
|
auto pausableSink = make_shared<logging::PausableSink>(stdErrSink);
|
|
|
|
auto levelFilter = make_shared<logging::LevelFilter>(pausableSink, minLevel);
|
|
|
|
logging::addSink(levelFilter);
|
|
|
|
return pausableSink;
|
|
|
|
}
|
|
|
|
|
|
|
|
void addFileSink(path path, logging::Level minLevel) {
|
|
|
|
auto file = make_shared<boost::filesystem::ofstream>();
|
|
|
|
file->exceptions(std::ifstream::failbit | std::ifstream::badbit);
|
|
|
|
file->open(path);
|
|
|
|
auto FileSink = make_shared<logging::StreamSink>(file, make_shared<logging::SimpleFileFormatter>());
|
|
|
|
auto levelFilter = make_shared<logging::LevelFilter>(FileSink, minLevel);
|
|
|
|
logging::addSink(levelFilter);
|
|
|
|
}
|
|
|
|
|
2016-11-15 20:56:39 +00:00
|
|
|
unique_ptr<Exporter> createExporter(ExportFormat exportFormat) {
|
|
|
|
switch (exportFormat) {
|
|
|
|
case ExportFormat::TSV:
|
|
|
|
return make_unique<TSVExporter>();
|
|
|
|
case ExportFormat::XML:
|
|
|
|
return make_unique<XMLExporter>();
|
|
|
|
case ExportFormat::JSON:
|
|
|
|
return make_unique<JSONExporter>();
|
|
|
|
default:
|
|
|
|
throw std::runtime_error("Unknown export format.");
|
2016-06-03 19:07:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-19 17:32:14 +00:00
|
|
|
int main(int argc, char *argv[]) {
|
2016-08-04 19:02:40 +00:00
|
|
|
#ifdef _DEBUG
|
2016-04-13 20:37:39 +00:00
|
|
|
auto pausableStderrSink = addPausableStdErrSink(logging::Level::Warn);
|
2016-03-08 21:59:44 +00:00
|
|
|
pausableStderrSink->pause();
|
2016-08-04 19:02:40 +00:00
|
|
|
#endif
|
2016-02-29 20:00:38 +00:00
|
|
|
|
2015-12-29 10:44:55 +00:00
|
|
|
// Define command-line parameters
|
|
|
|
const char argumentValueSeparator = ' ';
|
2016-03-08 21:59:44 +00:00
|
|
|
tclap::CmdLine cmd(appName, argumentValueSeparator, appVersion);
|
2015-12-29 10:44:55 +00:00
|
|
|
cmd.setExceptionHandling(false);
|
2016-01-06 19:45:04 +00:00
|
|
|
cmd.setOutput(new NiceCmdLineOutput());
|
2016-04-14 20:14:20 +00:00
|
|
|
auto logLevels = vector<logging::Level>(logging::LevelConverter::get().getValues());
|
2016-04-13 20:37:39 +00:00
|
|
|
tclap::ValuesConstraint<logging::Level> logLevelConstraint(logLevels);
|
|
|
|
tclap::ValueArg<logging::Level> logLevel("", "logLevel", "The minimum log level to log", false, logging::Level::Debug, &logLevelConstraint, cmd);
|
2016-03-08 21:59:44 +00:00
|
|
|
tclap::ValueArg<string> logFileName("", "logFile", "The log file path.", false, string(), "string", cmd);
|
2016-08-11 08:29:01 +00:00
|
|
|
tclap::ValueArg<int> maxThreadCount("", "threads", "The maximum number of worker threads to use.", false, getProcessorCoreCount(), "number", cmd);
|
2016-06-03 19:07:49 +00:00
|
|
|
tclap::ValueArg<string> dialogFile("d", "dialogFile", "A file containing the text of the dialog.", false, string(), "string", cmd);
|
2016-04-14 20:14:20 +00:00
|
|
|
auto exportFormats = vector<ExportFormat>(ExportFormatConverter::get().getValues());
|
2016-04-12 18:49:06 +00:00
|
|
|
tclap::ValuesConstraint<ExportFormat> exportFormatConstraint(exportFormats);
|
|
|
|
tclap::ValueArg<ExportFormat> exportFormat("f", "exportFormat", "The export format.", false, ExportFormat::TSV, &exportFormatConstraint, cmd);
|
2016-03-08 21:59:44 +00:00
|
|
|
tclap::UnlabeledValueArg<string> inputFileName("inputFile", "The input file. Must be a sound file in WAVE format.", true, "", "string", cmd);
|
2015-12-29 10:44:55 +00:00
|
|
|
|
2016-01-06 19:45:04 +00:00
|
|
|
try {
|
2016-02-29 20:00:38 +00:00
|
|
|
auto resumeLogging = gsl::finally([&]() {
|
|
|
|
std::cerr << std::endl << std::endl;
|
2016-08-04 19:02:40 +00:00
|
|
|
#ifdef _DEBUG
|
2016-03-08 21:59:44 +00:00
|
|
|
pausableStderrSink->resume();
|
2016-08-04 19:02:40 +00:00
|
|
|
#endif
|
2016-02-29 20:00:38 +00:00
|
|
|
std::cerr << std::endl;
|
|
|
|
});
|
|
|
|
|
2015-12-29 10:44:55 +00:00
|
|
|
// Parse command line
|
|
|
|
cmd.parse(argc, argv);
|
2016-08-11 08:29:01 +00:00
|
|
|
if (maxThreadCount.getValue() < 1) {
|
|
|
|
throw std::runtime_error("Thread count must be 1 or higher.");
|
|
|
|
}
|
2015-11-19 20:17:35 +00:00
|
|
|
|
2016-03-08 21:59:44 +00:00
|
|
|
// Set up log file
|
|
|
|
if (logFileName.isSet()) {
|
|
|
|
addFileSink(path(logFileName.getValue()), logLevel.getValue());
|
|
|
|
}
|
|
|
|
|
2016-06-14 15:38:11 +00:00
|
|
|
logging::infoFormat("Application startup. Command line: {}", join(
|
|
|
|
vector<char*>(argv, argv + argc) | transformed([](char* arg) { return fmt::format("\"{}\"", arg); }), " "));
|
|
|
|
|
2016-11-15 20:56:39 +00:00
|
|
|
std::cerr << "Processing input file. ";
|
|
|
|
ContinuousTimeline<Shape> animation(TimeRange::zero(), Shape::X);
|
2016-01-08 09:53:35 +00:00
|
|
|
{
|
|
|
|
ProgressBar progressBar;
|
2016-11-15 20:56:39 +00:00
|
|
|
|
|
|
|
// Animate the recording
|
|
|
|
animation = animateWaveFile(
|
|
|
|
inputFileName.getValue(),
|
|
|
|
dialogFile.isSet() ? readUTF8File(path(dialogFile.getValue())) : boost::optional<u32string>(),
|
2016-08-11 08:29:01 +00:00
|
|
|
maxThreadCount.getValue(),
|
2016-02-09 21:08:11 +00:00
|
|
|
progressBar);
|
2016-01-08 09:53:35 +00:00
|
|
|
}
|
2016-11-15 20:56:39 +00:00
|
|
|
std::cerr << "Done." << std::endl << std::endl;
|
2015-11-19 17:32:14 +00:00
|
|
|
|
2016-11-15 20:56:39 +00:00
|
|
|
// Export animation
|
|
|
|
unique_ptr<Exporter> exporter = createExporter(exportFormat.getValue());
|
|
|
|
exporter->exportShapes(path(inputFileName.getValue()), animation, std::cout);
|
2016-01-08 09:53:35 +00:00
|
|
|
|
2016-06-14 15:38:11 +00:00
|
|
|
logging::info("Exiting application normally.");
|
2015-11-19 17:32:14 +00:00
|
|
|
|
|
|
|
return 0;
|
2016-03-08 21:59:44 +00:00
|
|
|
} catch (tclap::ArgException& e) {
|
2016-01-06 19:45:04 +00:00
|
|
|
// Error parsing command-line args.
|
|
|
|
cmd.getOutput()->failure(cmd, e);
|
2016-02-29 20:00:38 +00:00
|
|
|
std::cerr << std::endl;
|
2016-06-14 15:38:11 +00:00
|
|
|
logging::error("Invalid command line. Exiting application with error code.");
|
2015-12-29 10:44:55 +00:00
|
|
|
return 1;
|
2016-03-08 21:59:44 +00:00
|
|
|
} catch (tclap::ExitException&) {
|
2016-01-06 19:45:04 +00:00
|
|
|
// A built-in TCLAP command (like --help) has finished. Exit application.
|
2016-02-29 20:00:38 +00:00
|
|
|
std::cerr << std::endl;
|
2016-06-14 15:38:11 +00:00
|
|
|
logging::info("Exiting application after help-like command.");
|
2016-01-06 19:45:04 +00:00
|
|
|
return 0;
|
2015-11-19 17:32:14 +00:00
|
|
|
} catch (const exception& e) {
|
2016-01-06 19:45:04 +00:00
|
|
|
// Generic error
|
2016-06-14 15:38:11 +00:00
|
|
|
string message = getMessage(e);
|
|
|
|
std::cerr << "An error occurred.\n" << message << std::endl;
|
|
|
|
logging::errorFormat("Exiting application with error: {}", message);
|
2015-11-19 17:32:14 +00:00
|
|
|
return 1;
|
|
|
|
}
|
2015-11-25 21:00:24 +00:00
|
|
|
}
|