diff --git a/src/sfizz/Logger.cpp b/src/sfizz/Logger.cpp index 1e3bc157..7de5b20a 100644 --- a/src/sfizz/Logger.cpp +++ b/src/sfizz/Logger.cpp @@ -1,8 +1,10 @@ #include "Logger.h" +#include "Debug.h" #include "absl/algorithm/container.h" #include "ghc/fs_std.hpp" #include #include +#include #include using namespace std::chrono_literals; @@ -36,14 +38,26 @@ void printStatistics(std::vector& data) std::cout << '\n'; } +sfz::Logger::Logger() +{ + keepRunning.test_and_set(); + clearFlag.test_and_set(); + loggingThread = std::thread(&Logger::moveEvents, this); +} + sfz::Logger::~Logger() { keepRunning.clear(); loggingThread.join(); if (!fileTimes.empty()) { - fs::path loadLogPath{ fs::current_path() / "file_times.csv" }; - std::ofstream loadLogFile { loadLogPath.string() }; + std::stringstream fileLogFilename; + fileLogFilename << this << "_" + << prefix + << "_file_log.csv"; + fs::path fileLogPath{ fs::current_path() / fileLogFilename.str() }; + DBG("Logging file times to " << fileLogPath.filename()); + std::ofstream loadLogFile { fileLogPath.string() }; loadLogFile << "WaitDuration,LoadDuration,FileSize,FileName" << '\n'; for (auto& time: fileTimes) loadLogFile << time.waitDuration.count() << ',' @@ -53,7 +67,12 @@ sfz::Logger::~Logger() } if (!callbackTimes.empty()) { - fs::path callbackLogPath{ fs::current_path() / "callback_times.csv" }; + std::stringstream callbackLogFilename; + callbackLogFilename << this << "_" + << prefix + << "_callback_log.csv"; + fs::path callbackLogPath{ fs::current_path() / callbackLogFilename.str() }; + DBG("Logging callback times to " << callbackLogPath.filename()); std::ofstream callbackLogFile { callbackLogPath.string() }; callbackLogFile << "Duration,NumVoices,NumSamples" << '\n'; for (auto& time: callbackTimes) @@ -74,6 +93,17 @@ void sfz::Logger::logFileTime(std::chrono::duration waitDuration, std::c fileTimeQueue.try_push({ waitDuration, loadDuration, fileSize, filename }); } +void sfz::Logger::setPrefix(const std::string& prefix) +{ + this->prefix = prefix; +} + +void sfz::Logger::clear() +{ + clearFlag.clear(); + prefix.clear(); +} + void sfz::Logger::moveEvents() { while(keepRunning.test_and_set()) { @@ -85,6 +115,11 @@ void sfz::Logger::moveEvents() while (fileTimeQueue.try_pop(fileTime)) fileTimes.push_back(fileTime); + if (!clearFlag.test_and_set()) { + fileTimes.clear(); + callbackTimes.clear(); + } + std::this_thread::sleep_for(50ms); } } diff --git a/src/sfizz/Logger.h b/src/sfizz/Logger.h index b6a5c142..2bd1f6bd 100644 --- a/src/sfizz/Logger.h +++ b/src/sfizz/Logger.h @@ -25,6 +25,7 @@ #include "Config.h" #include "atomic_queue/atomic_queue.h" #include +#include #include #include #include "absl/strings/string_view.h" @@ -50,20 +51,26 @@ struct CallbackTime class Logger { public: - Logger() = default; + Logger(); ~Logger(); + void setPrefix(const std::string& prefix); + void clear(); void logCallbackTime(std::chrono::duration duration, int numVoices, int numSamples); void logFileTime(std::chrono::duration waitDuration, std::chrono::duration loadDuration, uint32_t fileSize, absl::string_view filename); private: void moveEvents(); + std::string prefix { "" }; + atomic_queue::AtomicQueue2 callbackTimeQueue; atomic_queue::AtomicQueue2 fileTimeQueue; std::vector callbackTimes; std::vector fileTimes; + std::atomic_flag keepRunning; - std::thread loggingThread { &Logger::moveEvents, this }; + std::atomic_flag clearFlag; + std::thread loggingThread; }; } diff --git a/src/sfizz/Synth.cpp b/src/sfizz/Synth.cpp index 7fd285d4..b146f16e 100644 --- a/src/sfizz/Synth.cpp +++ b/src/sfizz/Synth.cpp @@ -135,6 +135,7 @@ void sfz::Synth::clear() list.clear(); regions.clear(); resources.filePool.clear(); + resources.logger.clear(); numGroups = 0; numMasters = 0; numCurves = 0; @@ -220,7 +221,7 @@ void addEndpointsToVelocityCurve(sfz::Region& region) } } -bool sfz::Synth::loadSfzFile(const fs::path& filename) +bool sfz::Synth::loadSfzFile(const fs::path& file) { AtomicDisabler callbackDisabler { canEnterCallback }; while (inCallback) { @@ -228,7 +229,7 @@ bool sfz::Synth::loadSfzFile(const fs::path& filename) } clear(); - auto parserReturned = sfz::Parser::loadSfzFile(filename); + auto parserReturned = sfz::Parser::loadSfzFile(file); if (!parserReturned) return false; @@ -236,6 +237,7 @@ bool sfz::Synth::loadSfzFile(const fs::path& filename) return false; resources.filePool.setRootDirectory(this->originalDirectory); + resources.logger.setPrefix(file.filename()); auto currentRegion = regions.begin(); auto lastRegion = regions.rbegin();