DJMFix.cpp
branchv_0
changeset 12 15d87fdd6e6c
parent 8 87dfa7c89294
child 13 334b727f7516
--- a/DJMFix.cpp	Mon Jan 04 13:38:08 2021 +0100
+++ b/DJMFix.cpp	Mon Jan 04 15:45:12 2021 +0100
@@ -15,6 +15,7 @@
  * along with this program. If not, see <http://www.gnu.org/licenses/>.
  */
 #include <iostream>
+#include <sstream>
 #include <iomanip>
 #include <thread>
 #include <mutex>
@@ -27,11 +28,13 @@
 
 namespace djmfix {
 
+using L = djmfix::logging::Level;
 using Bytes = std::vector<uint8_t>;
 
 class DJMFixImpl : public DJMFix {
 private:
 	MidiSender* midiSender;
+	djmfix::logging::Logger* logger;
 	std::thread keepAliveThread;
 	std::recursive_mutex midiMutex;
 	std::atomic<bool> running{false};
@@ -41,7 +44,7 @@
 
 	void run() {
 		while (!stopped) {
-			std::cerr << "DJMFixImpl::run()" << std::endl; // TODO: do not mess STDIO
+			logger->log(L::FINE, "DJMFixImpl::run()");
 			if (sendKeepAlive) send({0xf0, 0x00, 0x40, 0x05, 0x00, 0x00, 0x00, 0x17, 0x00, 0x50, 0x01, 0xf7});
 			std::this_thread::sleep_for(std::chrono::milliseconds(200));
 		}
@@ -117,18 +120,21 @@
 
 public:
 
+	DJMFixImpl(djmfix::logging::Logger* logger) : logger(logger ? logger : djmfix::logging::blackhole()) {
+	}
+
 	virtual ~DJMFixImpl() override {
-		std::cerr << "~DJMFixImpl()" << std::endl; // TODO: do not mess STDIO
+		logger->log(L::FINE, "~DJMFixImpl()");
 		if (running) stop();
 	}
 
 	void setMidiSender(MidiSender* midiSender) {
-		std::cerr << "DJMFixImpl::setMidiSender()" << std::endl; // TODO: do not mess STDIO
+		logger->log(L::FINE, "DJMFixImpl::setMidiSender()");
 		this->midiSender = midiSender;
 	}
 
 	virtual void receive(const MidiMessage& midiMessage) override {
-		std::cerr << "DJMFixImpl::receive(): size = " << midiMessage.size() << " data = " << toString(midiMessage) << std::endl; // TODO: do not mess STDIO
+		logger->log(L::INFO, "received message: size = " + std::to_string(midiMessage.size()) + " data = " + toString(midiMessage));
 		std::lock_guard<std::recursive_mutex> lock(midiMutex);
 
 
@@ -139,7 +145,7 @@
 			seed2 = Bytes(midiMessage.begin() + 45, midiMessage.begin() + 45 + 8);
 			hash1 = normalize(hash1);
 			seed2 = normalize(seed2);
-			std::cerr << "DJMFixImpl::receive(): got message with hash1 = " << toString(hash1) << " and seed2 = " << toString(seed2) << std::endl; // TODO: do not mess STDIO
+			logger->log(L::INFO, "got message with hash1 = " + toString(hash1) + " and seed2 = " + toString(seed2));
 
 			Bytes seed0 = {0x68, 0x01, 0x31, 0xFB};
 			Bytes seed1 = {0x29, 0x00, 0x00, 0x00, 0x23, 0x48, 0x00, 0x00};
@@ -147,19 +153,20 @@
 			Bytes hash1check = toBytes(fnv32hash(concat(seed1, xOR(seed0, seed2))));
 
 			if (equals(hash1, hash1check)) {
-				std::cerr << "DJMFixImpl::receive(): hash1 verification: OK" << std::endl; // TODO: do not mess STDIO
+				logger->log(L::INFO, "hash1 verification: OK");
 				Bytes hash2 = toBytes(fnv32hash(concat(seed2, xOR(seed0, seed2))));
 				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})));
 			} else {
-				std::cerr
-						<< "DJMFixImpl::receive(): hash1 verification failed: "
+				std::stringstream logMessage;
+				logMessage
+						<< "hash1 verification failed: "
 						<< " midiMessage = " << toString(midiMessage)
 						<< " seed0 = " << toString(seed0)
 						<< " seed1 = " << toString(seed1)
 						<< " seed2 = " << toString(seed2)
 						<< " hash1 = " << toString(hash1)
-						<< " hash1check = " << toString(hash1check)
-						<< std::endl;
+						<< " hash1check = " << toString(hash1check);
+				logger->log(L::SEVERE, logMessage.str());
 				// TODO: graceful death
 			}
 		} else if (midiMessage.size() == 12 && midiMessage[9] == 0x15) {
@@ -169,7 +176,7 @@
 	}
 
 	void start() override {
-		std::cerr << "DJMFixImpl::start()" << std::endl; // TODO: do not mess STDIO
+		logger->log(L::FINE, "DJMFixImpl::start()");
 		if (midiSender == nullptr) throw std::logic_error("need a midiSender when starting DJMFix");
 
 		// TODO: methods for parsing and constructing messages from parts (TLV)
@@ -184,12 +191,12 @@
 		stopped = true;
 		keepAliveThread.join();
 		running = false;
-		std::cerr << "DJMFixImpl::stop()" << std::endl; // TODO: do not mess STDIO
+		logger->log(L::FINE, "DJMFixImpl::stop()");
 	}
 };
 
-DJMFix* create() {
-	return new DJMFixImpl();
+DJMFix* create(djmfix::logging::Logger* logger) {
+	return new DJMFixImpl(logger);
 }
 
 }