Added logging to AudioDatabase. Also implemented analysis string checking.

Added logger pointer member to AudioDatabase class

Fixed logger flush on error issue. Implemented analysis string checking in AudioDatabase constructor.

Added std exception handeling and logging in main.
This commit is contained in:
2016-07-11 22:07:49 +01:00
committed by Sam Perry
parent 6cb48863cf
commit 1fd2a35c4b
6 changed files with 117 additions and 27 deletions
+44 -15
View File
@@ -1,45 +1,74 @@
#include <iostream>
#include <string>
#include <list>
#include <set>
#include <boost/algorithm/string.hpp>
#include "logger.h"
using std::string;
using std::cout;
using std::endl;
using std::list;
/*!
* A class that encapsulates a collection of AudioFile objects in order to
* perform analysis and synthesis operations on batches of audio files.
*/
class AudioDatabase {
public:
template<typename Iter>
AudioDatabase(std::string audio_dir, std::string database_dir, Iter it, Iter end);
AudioDatabase(const std::string audio_dir, const std::string database_dir, list<string>& analyses, Logger* log);
private:
std::string database_dir = "";
std::string audio_dir = "";
// Define a set that stores the locations of audiofiles in the database.
std::set<string> audio_file_set;
Logger* log;
};
/*! A function that determines whether a string value is found in the container.
*/
template<typename container>
bool in_array(const string &value, const container &array)
bool in_array(string &value, const container &array)
{
cout <<"Test: " << value << endl;
boost::algorithm::to_upper(value);
return std::find(array.begin(), array.end(), value) != array.end();
}
/*! Check that analysis strings provided are supported by the database object
\param iterator - An iterator pointing to where to begin checking strings are valid.
\param end - An iterator pointing to the point at which to stop analysing strings.
*/
template<typename Iter>
bool check_analyses_valid(Iter iterator, Iter end)
Iter check_analyses_valid(Iter iterator, Iter end)
{
static std::list<string> valid_analyses = {"f0", "RMS"};
static std::list<string> valid_analyses = {
"RMS",
"ZEROX",
"FFT",
"SPCCNTR",
"SPCSPRD",
"SPCFLUX",
"SPCCF",
"SPCFLATNESS",
"F0",
"PEAK",
"CENTROID",
"VARIANCE",
"KURTOSIS",
"SKEWNESS",
"HARM_RATIO"
};
while(iterator != end)
{
if(!in_array(*iterator, valid_analyses)) {
cout << "Nope!" << endl;
return false;
return iterator;
}
++iterator;
}
return true;
}
template<typename Iter>
AudioDatabase::AudioDatabase(string audio_dir, string database_dir, Iter it, Iter end) {
cout << "HELLO?!?" << endl;
check_analyses_valid(it, end);
return iterator;
}
+3
View File
@@ -1,3 +1,5 @@
#ifndef LOGGER_H
#define LOGGER_H
#include <boost/shared_ptr.hpp>
#include <boost/log/sinks.hpp>
#include <boost/log/sources/severity_logger.hpp>
@@ -31,3 +33,4 @@ class Logger {
//Define a logger
boost::log::sources::severity_logger< boost::log::trivial::severity_level > lg;
};
#endif
-2
View File
@@ -16,7 +16,6 @@ ArgumentParser::ArgumentParser() : desc("Allowed options") {
;
}
/*
int ArgumentParser::parseargs(int argc, char** argv) {
po::store(po::command_line_parser(argc, argv).options(desc).positional(positionalOptions).run(), vm);
po::notify(vm);
@@ -28,4 +27,3 @@ int ArgumentParser::parseargs(int argc, char** argv) {
}
return 0;
}
*/
+29
View File
@@ -1 +1,30 @@
#include <string>
#include <list>
#include "AudioDatabase.h"
#include <stdexcept>
#include <set>
using namespace std;
AudioDatabase::AudioDatabase(
const string audio_dir,
const string database_dir,
list<string>& analyses,
Logger* log
)
{
this->log = log;
// Remove duplicate strings from list of analyses.
analyses.unique();
// Check that all analysis strings supplied refer to valid analyses.
list<string>::const_iterator valid = check_analyses_valid(analyses.begin(), analyses.end());
if(valid != analyses.end()) {
string err = "The analysis string supplied to the AudioDatabase constructor is not valid: " + *valid;
throw std::runtime_error(err);
}
this->database_dir = database_dir;
this->audio_dir = audio_dir;
}
+37 -10
View File
@@ -2,17 +2,44 @@
#include "logger.h"
#include "ArgumentParser.h"
#include "AudioDatabase.h"
#include <vector>
#include <list>
#include <string>
using namespace std;
int main(int argc, char** argv) {
Logger log = Logger();
ArgumentParser argparse = ArgumentParser();
//argparse.parseargs(argc, argv);
log.error("My pretty little error!");
vector<string> analyses = {"BLARGH"};
AudioDatabase db = AudioDatabase("./", "./", analyses.begin(), analyses.end());
return 0;
namespace
{
const size_t ERROR_IN_COMMAND_LINE = 1;
const size_t SUCCESS = 0;
const size_t ERROR_UNHANDLED_EXCEPTION = 2;
}
int main(int argc, char** argv) {
// Initialize a logger object to be used for message handeling throughout
// the program
Logger log = Logger();
try
{
// Initialize object to parse arguments supplied by user from command
// line
ArgumentParser argparse = ArgumentParser();
// Parse arguments and exit program if specified (through use of --help
// or -h flag)
if(argparse.parseargs(argc, argv)) {
return ERROR_IN_COMMAND_LINE;
}
list<string> analyses = {"BLARGH"};
AudioDatabase db = AudioDatabase("./", "./", analyses, &log);
}
catch(std::exception& e)
{
string error("Unhandled Exception reached the top of main:\n");
error.append(e.what());
log.error(error);
return ERROR_UNHANDLED_EXCEPTION;
}
return SUCCESS;
}
+4
View File
@@ -87,8 +87,12 @@ void Logger::warning(std::string str) {
void Logger::error(std::string str) {
BOOST_LOG_SEV(lg, logging::trivial::error) << str;
Logger::console_sink->flush();
Logger::file_sink->flush();
}
void Logger::fatal(std::string str) {
BOOST_LOG_SEV(lg, logging::trivial::fatal) << str;
Logger::console_sink->flush();
Logger::file_sink->flush();
}