Make this a default implementation of Logger rather than a special case in Log

This commit is contained in:
Chris Cannam
2022-06-23 09:52:23 +01:00
parent b318fb4e8b
commit 9845e4bb38
5 changed files with 53 additions and 43 deletions

View File

@@ -383,8 +383,8 @@ public:
* Note that although the supplied logger gets to decide what to * Note that although the supplied logger gets to decide what to
* do with log messages, the separately-set debug level (see * do with log messages, the separately-set debug level (see
* setDebugLevel() and setDefaultDebugLevel()) still determines * setDebugLevel() and setDefaultDebugLevel()) still determines
* whether any given debug message is generated and sent to the * whether any given debug message is sent to the logger in the
* logger in the first place. * first place.
*/ */
RubberBandStretcher(size_t sampleRate, RubberBandStretcher(size_t sampleRate,
size_t channels, size_t channels,

View File

@@ -24,6 +24,8 @@
#include "faster/R2Stretcher.h" #include "faster/R2Stretcher.h"
#include "finer/R3Stretcher.h" #include "finer/R3Stretcher.h"
#include <iostream>
namespace RubberBand { namespace RubberBand {
class RubberBandStretcher::Impl class RubberBandStretcher::Impl
@@ -31,6 +33,26 @@ class RubberBandStretcher::Impl
R2Stretcher *m_r2; R2Stretcher *m_r2;
R3Stretcher *m_r3; 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<RubberBandStretcher::Logger> logger) { Log makeRBLog(std::shared_ptr<RubberBandStretcher::Logger> logger) {
if (logger) { if (logger) {
return Log( return Log(
@@ -45,7 +67,8 @@ class RubberBandStretcher::Impl
} }
); );
} else { } else {
return Log::makeCoutLog(); return makeRBLog(std::shared_ptr<RubberBandStretcher::Logger>
(new CerrLogger()));
} }
} }

View File

@@ -28,27 +28,4 @@ namespace RubberBand
int Log::m_defaultDebugLevel = 0; 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);
}
);
}
} }

View File

@@ -39,19 +39,10 @@ public:
m_log2(_log2), m_log2(_log2),
m_debugLevel(m_defaultDebugLevel) { } m_debugLevel(m_defaultDebugLevel) { }
Log(const Log &other) : Log(const Log &other) =default;
m_log0(other.m_log0), Log(Log &&other) =default;
m_log1(other.m_log1), Log &operator=(const Log &other) =default;
m_log2(other.m_log2), Log &operator=(Log &&other) =default;
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;
}
void setDebugLevel(int level) { m_debugLevel = level; } void setDebugLevel(int level) { m_debugLevel = level; }
int getDebugLevel() const { return m_debugLevel; } int getDebugLevel() const { return m_debugLevel; }
@@ -68,8 +59,6 @@ public:
if (level <= m_debugLevel) m_log2(message, arg0, arg1); if (level <= m_debugLevel) m_log2(message, arg0, arg1);
} }
static Log makeCoutLog();
private: private:
std::function<void(const char *)> m_log0; std::function<void(const char *)> m_log0;
std::function<void(const char *, double)> m_log1; std::function<void(const char *, double)> m_log1;

View File

@@ -454,6 +454,28 @@ RubberBandVampPlugin::Impl::processOffline(const float *const *inputBuffers,
return FeatureSet(); 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::FeatureSet
RubberBandVampPlugin::Impl::getRemainingFeaturesOffline() RubberBandVampPlugin::Impl::getRemainingFeaturesOffline()
{ {
@@ -464,8 +486,7 @@ RubberBandVampPlugin::Impl::getRemainingFeaturesOffline()
int rate = m_sampleRate; int rate = m_sampleRate;
RubberBand::StretchCalculator sc RubberBand::StretchCalculator sc
(rate, m_stretcher->getInputIncrement(), true, (rate, m_stretcher->getInputIncrement(), true, makeCerrLog());
RubberBand::Log::makeCoutLog());
size_t inputIncrement = m_stretcher->getInputIncrement(); size_t inputIncrement = m_stretcher->getInputIncrement();
std::vector<int> outputIncrements = m_stretcher->getOutputIncrements(); std::vector<int> outputIncrements = m_stretcher->getOutputIncrements();