1.1 --- a/DJMFix.cpp Mon Jan 04 13:38:08 2021 +0100
1.2 +++ b/DJMFix.cpp Mon Jan 04 15:45:12 2021 +0100
1.3 @@ -15,6 +15,7 @@
1.4 * along with this program. If not, see <http://www.gnu.org/licenses/>.
1.5 */
1.6 #include <iostream>
1.7 +#include <sstream>
1.8 #include <iomanip>
1.9 #include <thread>
1.10 #include <mutex>
1.11 @@ -27,11 +28,13 @@
1.12
1.13 namespace djmfix {
1.14
1.15 +using L = djmfix::logging::Level;
1.16 using Bytes = std::vector<uint8_t>;
1.17
1.18 class DJMFixImpl : public DJMFix {
1.19 private:
1.20 MidiSender* midiSender;
1.21 + djmfix::logging::Logger* logger;
1.22 std::thread keepAliveThread;
1.23 std::recursive_mutex midiMutex;
1.24 std::atomic<bool> running{false};
1.25 @@ -41,7 +44,7 @@
1.26
1.27 void run() {
1.28 while (!stopped) {
1.29 - std::cerr << "DJMFixImpl::run()" << std::endl; // TODO: do not mess STDIO
1.30 + logger->log(L::FINE, "DJMFixImpl::run()");
1.31 if (sendKeepAlive) send({0xf0, 0x00, 0x40, 0x05, 0x00, 0x00, 0x00, 0x17, 0x00, 0x50, 0x01, 0xf7});
1.32 std::this_thread::sleep_for(std::chrono::milliseconds(200));
1.33 }
1.34 @@ -117,18 +120,21 @@
1.35
1.36 public:
1.37
1.38 + DJMFixImpl(djmfix::logging::Logger* logger) : logger(logger ? logger : djmfix::logging::blackhole()) {
1.39 + }
1.40 +
1.41 virtual ~DJMFixImpl() override {
1.42 - std::cerr << "~DJMFixImpl()" << std::endl; // TODO: do not mess STDIO
1.43 + logger->log(L::FINE, "~DJMFixImpl()");
1.44 if (running) stop();
1.45 }
1.46
1.47 void setMidiSender(MidiSender* midiSender) {
1.48 - std::cerr << "DJMFixImpl::setMidiSender()" << std::endl; // TODO: do not mess STDIO
1.49 + logger->log(L::FINE, "DJMFixImpl::setMidiSender()");
1.50 this->midiSender = midiSender;
1.51 }
1.52
1.53 virtual void receive(const MidiMessage& midiMessage) override {
1.54 - std::cerr << "DJMFixImpl::receive(): size = " << midiMessage.size() << " data = " << toString(midiMessage) << std::endl; // TODO: do not mess STDIO
1.55 + logger->log(L::INFO, "received message: size = " + std::to_string(midiMessage.size()) + " data = " + toString(midiMessage));
1.56 std::lock_guard<std::recursive_mutex> lock(midiMutex);
1.57
1.58
1.59 @@ -139,7 +145,7 @@
1.60 seed2 = Bytes(midiMessage.begin() + 45, midiMessage.begin() + 45 + 8);
1.61 hash1 = normalize(hash1);
1.62 seed2 = normalize(seed2);
1.63 - std::cerr << "DJMFixImpl::receive(): got message with hash1 = " << toString(hash1) << " and seed2 = " << toString(seed2) << std::endl; // TODO: do not mess STDIO
1.64 + logger->log(L::INFO, "got message with hash1 = " + toString(hash1) + " and seed2 = " + toString(seed2));
1.65
1.66 Bytes seed0 = {0x68, 0x01, 0x31, 0xFB};
1.67 Bytes seed1 = {0x29, 0x00, 0x00, 0x00, 0x23, 0x48, 0x00, 0x00};
1.68 @@ -147,19 +153,20 @@
1.69 Bytes hash1check = toBytes(fnv32hash(concat(seed1, xOR(seed0, seed2))));
1.70
1.71 if (equals(hash1, hash1check)) {
1.72 - std::cerr << "DJMFixImpl::receive(): hash1 verification: OK" << std::endl; // TODO: do not mess STDIO
1.73 + logger->log(L::INFO, "hash1 verification: OK");
1.74 Bytes hash2 = toBytes(fnv32hash(concat(seed2, xOR(seed0, seed2))));
1.75 send(concat({0xf0, 0x00, 0x40, 0x05, 0x00, 0x00, 0x00, 0x17, 0x00, 0x14, 0x38, 0x01, 0x0b, 0x50, 0x69, 0x6f, 0x6e, 0x65, 0x65, 0x72, 0x44, 0x4a, 0x02, 0x0b, 0x72, 0x65, 0x6b, 0x6f, 0x72, 0x64, 0x62, 0x6f, 0x78, 0x04, 0x0a}, concat(denormalize(hash2),{0x05, 0x16, 0x05, 0x09, 0x0b, 0x05, 0x04, 0x0b, 0x0f, 0x0e, 0x0e, 0x04, 0x04, 0x0a, 0x05, 0x0a, 0x0c, 0x08, 0x0e, 0x04, 0x0c, 0x05, 0xf7})));
1.76 } else {
1.77 - std::cerr
1.78 - << "DJMFixImpl::receive(): hash1 verification failed: "
1.79 + std::stringstream logMessage;
1.80 + logMessage
1.81 + << "hash1 verification failed: "
1.82 << " midiMessage = " << toString(midiMessage)
1.83 << " seed0 = " << toString(seed0)
1.84 << " seed1 = " << toString(seed1)
1.85 << " seed2 = " << toString(seed2)
1.86 << " hash1 = " << toString(hash1)
1.87 - << " hash1check = " << toString(hash1check)
1.88 - << std::endl;
1.89 + << " hash1check = " << toString(hash1check);
1.90 + logger->log(L::SEVERE, logMessage.str());
1.91 // TODO: graceful death
1.92 }
1.93 } else if (midiMessage.size() == 12 && midiMessage[9] == 0x15) {
1.94 @@ -169,7 +176,7 @@
1.95 }
1.96
1.97 void start() override {
1.98 - std::cerr << "DJMFixImpl::start()" << std::endl; // TODO: do not mess STDIO
1.99 + logger->log(L::FINE, "DJMFixImpl::start()");
1.100 if (midiSender == nullptr) throw std::logic_error("need a midiSender when starting DJMFix");
1.101
1.102 // TODO: methods for parsing and constructing messages from parts (TLV)
1.103 @@ -184,12 +191,12 @@
1.104 stopped = true;
1.105 keepAliveThread.join();
1.106 running = false;
1.107 - std::cerr << "DJMFixImpl::stop()" << std::endl; // TODO: do not mess STDIO
1.108 + logger->log(L::FINE, "DJMFixImpl::stop()");
1.109 }
1.110 };
1.111
1.112 -DJMFix* create() {
1.113 - return new DJMFixImpl();
1.114 +DJMFix* create(djmfix::logging::Logger* logger) {
1.115 + return new DJMFixImpl(logger);
1.116 }
1.117
1.118 }