From 9845e4bb388a4864b74033de33d95737a5a5a023 Mon Sep 17 00:00:00 2001 From: Chris Cannam Date: Thu, 23 Jun 2022 09:52:23 +0100 Subject: [PATCH] Make this a default implementation of Logger rather than a special case in Log --- rubberband/RubberBandStretcher.h | 4 ++-- src/RubberBandStretcher.cpp | 25 ++++++++++++++++++++++++- src/common/Log.cpp | 23 ----------------------- src/common/Log.h | 19 ++++--------------- vamp/RubberBandVampPlugin.cpp | 25 +++++++++++++++++++++++-- 5 files changed, 53 insertions(+), 43 deletions(-) diff --git a/rubberband/RubberBandStretcher.h b/rubberband/RubberBandStretcher.h index 4be1bd0..b806e48 100644 --- a/rubberband/RubberBandStretcher.h +++ b/rubberband/RubberBandStretcher.h @@ -383,8 +383,8 @@ public: * Note that although the supplied logger gets to decide what to * do with log messages, the separately-set debug level (see * setDebugLevel() and setDefaultDebugLevel()) still determines - * whether any given debug message is generated and sent to the - * logger in the first place. + * whether any given debug message is sent to the logger in the + * first place. */ RubberBandStretcher(size_t sampleRate, size_t channels, diff --git a/src/RubberBandStretcher.cpp b/src/RubberBandStretcher.cpp index 493f40c..2202832 100644 --- a/src/RubberBandStretcher.cpp +++ b/src/RubberBandStretcher.cpp @@ -24,6 +24,8 @@ #include "faster/R2Stretcher.h" #include "finer/R3Stretcher.h" +#include + namespace RubberBand { class RubberBandStretcher::Impl @@ -31,6 +33,26 @@ class RubberBandStretcher::Impl R2Stretcher *m_r2; R3Stretcher *m_r3; + class CerrLogger : public RubberBandStretcher::Logger { + public: + void log(const char *message) override { + std::cerr << "RubberBand: " << message << "\n"; + } + void log(const char *message, double arg0) override { + auto prec = std::cerr.precision(); + std::cerr.precision(10); + std::cerr << "RubberBand: " << message << ": " << arg0 << "\n"; + std::cerr.precision(prec); + } + void log(const char *message, double arg0, double arg1) override { + auto prec = std::cerr.precision(); + std::cerr.precision(10); + std::cerr << "RubberBand: " << message + << ": (" << arg0 << ", " << arg1 << ")" << "\n"; + std::cerr.precision(prec); + } + }; + Log makeRBLog(std::shared_ptr logger) { if (logger) { return Log( @@ -45,7 +67,8 @@ class RubberBandStretcher::Impl } ); } else { - return Log::makeCoutLog(); + return makeRBLog(std::shared_ptr + (new CerrLogger())); } } diff --git a/src/common/Log.cpp b/src/common/Log.cpp index c2c944c..03b3536 100644 --- a/src/common/Log.cpp +++ b/src/common/Log.cpp @@ -28,27 +28,4 @@ namespace RubberBand int Log::m_defaultDebugLevel = 0; -Log -Log::makeCoutLog() -{ - return Log( - [](const char *message) { - std::cout << "RubberBand: " << message << "\n"; - }, - [](const char *message, double arg0) { - auto prec = std::cout.precision(); - std::cout.precision(10); - std::cout << "RubberBand: " << message << ": " << arg0 << "\n"; - std::cout.precision(prec); - }, - [](const char *message, double arg0, double arg1) { - auto prec = std::cout.precision(); - std::cout.precision(10); - std::cout << "RubberBand: " << message - << ": (" << arg0 << ", " << arg1 << ")" << "\n"; - std::cout.precision(prec); - } - ); -} - } diff --git a/src/common/Log.h b/src/common/Log.h index 1a9038c..b78315e 100644 --- a/src/common/Log.h +++ b/src/common/Log.h @@ -39,19 +39,10 @@ public: m_log2(_log2), m_debugLevel(m_defaultDebugLevel) { } - Log(const Log &other) : - m_log0(other.m_log0), - m_log1(other.m_log1), - m_log2(other.m_log2), - m_debugLevel(other.m_debugLevel) { } - - Log &operator=(const Log &other) { - m_log0 = other.m_log0; - m_log1 = other.m_log1; - m_log2 = other.m_log2; - m_debugLevel = other.m_debugLevel; - return *this; - } + Log(const Log &other) =default; + Log(Log &&other) =default; + Log &operator=(const Log &other) =default; + Log &operator=(Log &&other) =default; void setDebugLevel(int level) { m_debugLevel = level; } int getDebugLevel() const { return m_debugLevel; } @@ -68,8 +59,6 @@ public: if (level <= m_debugLevel) m_log2(message, arg0, arg1); } - static Log makeCoutLog(); - private: std::function m_log0; std::function m_log1; diff --git a/vamp/RubberBandVampPlugin.cpp b/vamp/RubberBandVampPlugin.cpp index a00ee1c..028ee81 100644 --- a/vamp/RubberBandVampPlugin.cpp +++ b/vamp/RubberBandVampPlugin.cpp @@ -454,6 +454,28 @@ RubberBandVampPlugin::Impl::processOffline(const float *const *inputBuffers, return FeatureSet(); } +static RubberBand::Log makeCerrLog() +{ + auto log0 = [](const char *message) { + std::cerr << "RubberBand: " << message << "\n"; + }; + auto log1 = [](const char *message, double arg0) { + auto prec = std::cerr.precision(); + std::cerr.precision(10); + std::cerr << "RubberBand: " << message << ": " << arg0 << "\n"; + std::cerr.precision(prec); + }; + auto log2 = [](const char *message, double arg0, double arg1) { + auto prec = std::cerr.precision(); + std::cerr.precision(10); + std::cerr << "RubberBand: " << message + << ": (" << arg0 << ", " << arg1 << ")" << "\n"; + std::cerr.precision(prec); + }; + + return RubberBand::Log(log0, log1, log2); +} + RubberBandVampPlugin::FeatureSet RubberBandVampPlugin::Impl::getRemainingFeaturesOffline() { @@ -464,8 +486,7 @@ RubberBandVampPlugin::Impl::getRemainingFeaturesOffline() int rate = m_sampleRate; RubberBand::StretchCalculator sc - (rate, m_stretcher->getInputIncrement(), true, - RubberBand::Log::makeCoutLog()); + (rate, m_stretcher->getInputIncrement(), true, makeCerrLog()); size_t inputIncrement = m_stretcher->getInputIncrement(); std::vector outputIncrements = m_stretcher->getOutputIncrements();