From d97348dd38c160eeb1a3f5941e6698799e17f9fa Mon Sep 17 00:00:00 2001
From: Thomas Harte <thomas.harte@gmail.com>
Date: Sat, 2 Mar 2019 18:07:05 -0500
Subject: [PATCH] Eliminates dangling uses of `printf`.

---
 Analyser/Dynamic/MultiMachine/MultiMachine.cpp      |  9 ++++-----
 Analyser/Static/Commodore/StaticAnalyser.cpp        |  3 ++-
 Components/6522/6522.hpp                            |  6 +++---
 .../6522/Implementation/6522Implementation.hpp      | 13 ++++++++++---
 Components/9918/9918.cpp                            |  4 ++--
 Machines/CRTMachine.hpp                             |  2 +-
 Machines/Commodore/1540/Implementation/C1540.cpp    |  4 ----
 Machines/Commodore/SerialBus.cpp                    |  7 +++----
 Machines/Commodore/Vic-20/Vic20.cpp                 |  9 +++++----
 Machines/MSX/Cartridges/ASCII16kb.hpp               |  5 ++---
 Machines/MSX/Cartridges/ASCII8kb.hpp                |  5 ++---
 Machines/MSX/Cartridges/Konami.hpp                  |  5 ++---
 Machines/MSX/Cartridges/KonamiWithSCC.hpp           |  5 ++---
 Machines/MSX/MSX.cpp                                | 10 ++++++----
 Machines/MSX/ROMSlotHandler.hpp                     |  4 +++-
 OSBindings/SDL/main.cpp                             |  2 +-
 Outputs/Log.hpp                                     |  2 +-
 Processors/Z80/AllRAM/Z80AllRAM.cpp                 |  1 -
 Storage/Tape/Formats/TZX.cpp                        |  3 ++-
 19 files changed, 51 insertions(+), 48 deletions(-)

diff --git a/Analyser/Dynamic/MultiMachine/MultiMachine.cpp b/Analyser/Dynamic/MultiMachine/MultiMachine.cpp
index bd59331cf..ba6328b31 100644
--- a/Analyser/Dynamic/MultiMachine/MultiMachine.cpp
+++ b/Analyser/Dynamic/MultiMachine/MultiMachine.cpp
@@ -7,6 +7,7 @@
 //
 
 #include "MultiMachine.hpp"
+#include "../../../Outputs/Log.hpp"
 
 #include <algorithm>
 
@@ -74,14 +75,12 @@ bool MultiMachine::would_collapse(const std::vector<std::unique_ptr<DynamicMachi
 
 void MultiMachine::multi_crt_did_run_machines() {
 	std::lock_guard<decltype(machines_mutex_)> machines_lock(machines_mutex_);
-#ifdef DEBUG
+#ifndef NDEBUG
 	for(const auto &machine: machines_) {
 		CRTMachine::Machine *crt = machine->crt_machine();
-		printf("%0.2f ", crt->get_confidence());
-		crt->print_type();
-		printf("; ");
+		LOGNBR(PADHEX(2) << crt->get_confidence() << " " << crt->debug_type() << "; ");
 	}
-	printf("\n");
+	LOGNBR(std::endl);
 #endif
 
 	DynamicMachine *front = machines_.front().get();
diff --git a/Analyser/Static/Commodore/StaticAnalyser.cpp b/Analyser/Static/Commodore/StaticAnalyser.cpp
index c932b8d3a..789db5c0f 100644
--- a/Analyser/Static/Commodore/StaticAnalyser.cpp
+++ b/Analyser/Static/Commodore/StaticAnalyser.cpp
@@ -13,6 +13,7 @@
 #include "Tape.hpp"
 #include "Target.hpp"
 #include "../../../Storage/Cartridge/Encodings/CommodoreROM.hpp"
+#include "../../../Outputs/Log.hpp"
 
 #include <algorithm>
 #include <sstream>
@@ -91,7 +92,7 @@ Analyser::Static::TargetList Analyser::Static::Commodore::GetTargets(const Media
 		// make a first guess based on loading address
 		switch(files.front().starting_address) {
 			default:
-				printf("Starting address %04x?\n", files.front().starting_address);
+				LOG("Unrecognised loading address for Commodore program: " << PADHEX(4) <<  files.front().starting_address);
 			case 0x1001:
 				target->memory_model = Target::MemoryModel::Unexpanded;
 			break;
diff --git a/Components/6522/6522.hpp b/Components/6522/6522.hpp
index 6224d1d29..2369c500c 100644
--- a/Components/6522/6522.hpp
+++ b/Components/6522/6522.hpp
@@ -123,9 +123,9 @@ template <class T> class MOS6522: public MOS6522Base {
 		inline void reevaluate_interrupts();
 };
 
+}
+}
+
 #include "Implementation/6522Implementation.hpp"
 
-}
-}
-
 #endif /* _522_hpp */
diff --git a/Components/6522/Implementation/6522Implementation.hpp b/Components/6522/Implementation/6522Implementation.hpp
index 9230ee3da..4e3074032 100644
--- a/Components/6522/Implementation/6522Implementation.hpp
+++ b/Components/6522/Implementation/6522Implementation.hpp
@@ -6,6 +6,11 @@
 //  Copyright 2017 Thomas Harte. All rights reserved.
 //
 
+#include "../../../Outputs/Log.hpp"
+
+namespace MOS {
+namespace MOS6522 {
+
 template <typename T> void MOS6522<T>::set_register(int address, uint8_t value) {
 	address &= 0xf;
 	switch(address) {
@@ -61,20 +66,19 @@ template <typename T> void MOS6522<T>::set_register(int address, uint8_t value)
 			registers_.auxiliary_control = value;
 		break;
 		case 0xc:
-//			printf("Peripheral control %02x\n", value);
 			registers_.peripheral_control = value;
 
 			// TODO: simplify below; trying to avoid improper logging of unimplemented warnings in input mode
 			if(value & 0x08) {
 				switch(value & 0x0e) {
-					default: printf("Unimplemented control line mode %d\n", (value >> 1)&7);		break;
+					default: 	LOG("Unimplemented control line mode " << int((value >> 1)&7));		break;
 					case 0x0c:	bus_handler_.set_control_line_output(Port::A, Line::Two, false);	break;
 					case 0x0e:	bus_handler_.set_control_line_output(Port::A, Line::Two, true);		break;
 				}
 			}
 			if(value & 0x80) {
 				switch(value & 0xe0) {
-					default: printf("Unimplemented control line mode %d\n", (value >> 5)&7);		break;
+					default: 	LOG("Unimplemented control line mode " << int((value >> 5)&7));		break;
 					case 0xc0:	bus_handler_.set_control_line_output(Port::B, Line::Two, false);	break;
 					case 0xe0:	bus_handler_.set_control_line_output(Port::B, Line::Two, true);		break;
 				}
@@ -157,3 +161,6 @@ template <typename T> void MOS6522<T>::reevaluate_interrupts() {
 		bus_handler_.set_interrupt_status(new_interrupt_status);
 	}
 }
+
+}
+}
diff --git a/Components/9918/9918.cpp b/Components/9918/9918.cpp
index 6818de294..143fa7546 100644
--- a/Components/9918/9918.cpp
+++ b/Components/9918/9918.cpp
@@ -11,6 +11,7 @@
 #include <cassert>
 #include <cstring>
 #include <cstdlib>
+#include "../../Outputs/Log.hpp"
 
 using namespace TI::TMS;
 
@@ -591,7 +592,6 @@ void TMS9918::set_register(int address, uint8_t value) {
 			case 8:
 				if(is_sega_vdp(personality_)) {
 					master_system_.horizontal_scroll = low_write_;
-//					printf("Set to %d at %d, %d\n", low_write_, row_, column_);
 				}
 			break;
 
@@ -608,7 +608,7 @@ void TMS9918::set_register(int address, uint8_t value) {
 			break;
 
 			default:
-//				printf("Unknown TMS write: %d to %d\n", low_write_, value);
+				LOG("Unknown TMS write: " << int(low_write_) << " to " << int(value));
 			break;
 		}
 	} else {
diff --git a/Machines/CRTMachine.hpp b/Machines/CRTMachine.hpp
index 413032bbe..64be17500 100644
--- a/Machines/CRTMachine.hpp
+++ b/Machines/CRTMachine.hpp
@@ -42,7 +42,7 @@ class Machine {
 
 		/// @returns The confidence that this machine is running content it understands.
 		virtual float get_confidence() { return 0.5f; }
-		virtual void print_type() {}
+		virtual std::string debug_type() { return ""; }
 
 		/// Runs the machine for @c duration seconds.
 		virtual void run_for(Time::Seconds duration) {
diff --git a/Machines/Commodore/1540/Implementation/C1540.cpp b/Machines/Commodore/1540/Implementation/C1540.cpp
index 9d1073c77..8fa73ee31 100644
--- a/Machines/Commodore/1540/Implementation/C1540.cpp
+++ b/Machines/Commodore/1540/Implementation/C1540.cpp
@@ -170,7 +170,6 @@ void SerialPortVIA::set_port_output(MOS::MOS6522::Port port, uint8_t value, uint
 			attention_acknowledge_level_ = !(value&0x10);
 			data_level_output_ = (value&0x02);
 
-//			printf("[C1540] %s output is %s\n", StringForLine(::Commodore::Serial::Line::Clock), value ? "high" : "low");
 			serialPort->set_output(::Commodore::Serial::Line::Clock, static_cast<::Commodore::Serial::LineLevel>(!(value&0x08)));
 			update_data_line();
 		}
@@ -178,8 +177,6 @@ void SerialPortVIA::set_port_output(MOS::MOS6522::Port port, uint8_t value, uint
 }
 
 void SerialPortVIA::set_serial_line_state(::Commodore::Serial::Line line, bool value) {
-//	printf("[C1540] %s input is %s\n", StringForLine(line), value ? "high" : "low");
-
 	switch(line) {
 		default: break;
 		case ::Commodore::Serial::Line::Data:		port_b_ = (port_b_ & ~0x01) | (value ? 0x00 : 0x01);		break;
@@ -200,7 +197,6 @@ void SerialPortVIA::set_serial_port(const std::shared_ptr<::Commodore::Serial::P
 void SerialPortVIA::update_data_line() {
 	std::shared_ptr<::Commodore::Serial::Port> serialPort = serial_port_.lock();
 	if(serialPort) {
-//		printf("[C1540] %s output is %s\n", StringForLine(::Commodore::Serial::Line::Data), (!data_level_output_ && (attention_level_input_ != attention_acknowledge_level_)) ? "high" : "low");
 		// "ATN (Attention) is an input on pin 3 of P2 and P3 that is sensed at PB7 and CA1 of UC3 after being inverted by UA1"
 		serialPort->set_output(::Commodore::Serial::Line::Data,
 			static_cast<::Commodore::Serial::LineLevel>(!data_level_output_ && (attention_level_input_ != attention_acknowledge_level_)));
diff --git a/Machines/Commodore/SerialBus.cpp b/Machines/Commodore/SerialBus.cpp
index f9441f39b..7617aa61e 100644
--- a/Machines/Commodore/SerialBus.cpp
+++ b/Machines/Commodore/SerialBus.cpp
@@ -9,6 +9,7 @@
 #include "SerialBus.hpp"
 
 #include <cstdio>
+#include <iostream>
 
 using namespace Commodore::Serial;
 
@@ -49,8 +50,6 @@ void Bus::set_line_output_did_change(Line line) {
 		}
 	}
 
-//	printf("[Bus] %s is %s\n", StringForLine(line), new_line_level ? "high" : "low");
-
 	// post an update only if one occurred
 	if(new_line_level != line_levels_[line]) {
 		line_levels_[line] = new_line_level;
@@ -69,7 +68,7 @@ void Bus::set_line_output_did_change(Line line) {
 void DebugPort::set_input(Line line, LineLevel value) {
 	input_levels_[line] = value;
 
-	printf("[Bus] %s is %s\n", StringForLine(line), value ? "high" : "low");
+	std::cout << "[Bus] " << StringForLine(line) << " is " << (value ? "high" : "low");
 	if(!incoming_count_) {
 		incoming_count_ = (!input_levels_[Line::Clock] && !input_levels_[Line::Data]) ? 8 : 0;
 	} else {
@@ -77,6 +76,6 @@ void DebugPort::set_input(Line line, LineLevel value) {
 			incoming_byte_ = (incoming_byte_ >> 1) | (input_levels_[Line::Data] ? 0x80 : 0x00);
 		}
 		incoming_count_--;
-		if(incoming_count_ == 0) printf("[Bus] Observed %02x\n", incoming_byte_);
+		if(incoming_count_ == 0) std::cout << "[Bus] Observed value " << std::hex << int(incoming_byte_);
 	}
 }
diff --git a/Machines/Commodore/Vic-20/Vic20.cpp b/Machines/Commodore/Vic-20/Vic20.cpp
index 027b0f9b5..1d82bc909 100644
--- a/Machines/Commodore/Vic-20/Vic20.cpp
+++ b/Machines/Commodore/Vic-20/Vic20.cpp
@@ -21,6 +21,7 @@
 #include "../../../Components/6522/6522.hpp"
 
 #include "../../../ClockReceiver/ForceInline.hpp"
+#include "../../../Outputs/Log.hpp"
 
 #include "../../../Storage/Tape/Parsers/Commodore.hpp"
 
@@ -526,12 +527,12 @@ class ConcreteMachine:
 							const uint16_t tape_buffer_pointer = static_cast<uint16_t>(ram_[0xb2]) | static_cast<uint16_t>(ram_[0xb3] << 8);
 							header->serialise(&ram_[tape_buffer_pointer], 0x8000 - tape_buffer_pointer);
 							hold_tape_ = true;
-							printf("Found header\n");
+							LOG("Vic-20: Found header");
 						} else {
 							// no header found, so pretend this hack never interceded
 							tape_->get_tape()->set_offset(tape_position);
 							hold_tape_ = false;
-							printf("Didn't find header\n");
+							LOG("Vic-20: Didn't find header");
 						}
 
 						// clear status and the verify flag
@@ -572,11 +573,11 @@ class ConcreteMachine:
 								m6502_.set_value_of_register(CPU::MOS6502::Register::ProgramCounter, 0xfccf);
 								*value = 0xea;	// i.e. NOP implied
 								hold_tape_ = true;
-								printf("Found data\n");
+								LOG("Vic-20: Found data");
 							} else {
 								tape_->get_tape()->set_offset(tape_position);
 								hold_tape_ = false;
-								printf("Didn't find data\n");
+								LOG("Vic-20: Didn't find data");
 							}
 						}
 					}
diff --git a/Machines/MSX/Cartridges/ASCII16kb.hpp b/Machines/MSX/Cartridges/ASCII16kb.hpp
index 92ff37137..d97de2370 100644
--- a/Machines/MSX/Cartridges/ASCII16kb.hpp
+++ b/Machines/MSX/Cartridges/ASCII16kb.hpp
@@ -20,7 +20,6 @@ class ASCII16kbROMSlotHandler: public ROMSlotHandler {
 			map_(map), slot_(slot) {}
 
 		void write(uint16_t address, uint8_t value, bool pc_is_outside_bios) override {
-//			printf("A16 %04x ", address);
 			switch(address >> 11) {
 				default:
 					if(pc_is_outside_bios) confidence_counter_.add_miss();
@@ -40,8 +39,8 @@ class ASCII16kbROMSlotHandler: public ROMSlotHandler {
 			}
 		}
 
-		virtual void print_type() override {
-			printf("A16");
+		virtual std::string debug_type() override {
+			return "A16";
 		}
 
 	private:
diff --git a/Machines/MSX/Cartridges/ASCII8kb.hpp b/Machines/MSX/Cartridges/ASCII8kb.hpp
index e52a9a63d..125235e74 100644
--- a/Machines/MSX/Cartridges/ASCII8kb.hpp
+++ b/Machines/MSX/Cartridges/ASCII8kb.hpp
@@ -20,7 +20,6 @@ class ASCII8kbROMSlotHandler: public ROMSlotHandler {
 			map_(map), slot_(slot) {}
 
 		void write(uint16_t address, uint8_t value, bool pc_is_outside_bios) override {
-//			printf("A8 %04x ", address);
 			switch(address >> 11) {
 				default:
 					if(pc_is_outside_bios) confidence_counter_.add_miss();
@@ -52,8 +51,8 @@ class ASCII8kbROMSlotHandler: public ROMSlotHandler {
 			}
 		}
 
-		virtual void print_type() override {
-			printf("A8");
+		virtual std::string debug_type() override {
+			return "A8";
 		}
 
 	private:
diff --git a/Machines/MSX/Cartridges/Konami.hpp b/Machines/MSX/Cartridges/Konami.hpp
index 7a91fa365..c738d1622 100644
--- a/Machines/MSX/Cartridges/Konami.hpp
+++ b/Machines/MSX/Cartridges/Konami.hpp
@@ -20,7 +20,6 @@ class KonamiROMSlotHandler: public ROMSlotHandler {
 			map_(map), slot_(slot) {}
 
 		void write(uint16_t address, uint8_t value, bool pc_is_outside_bios) override {
-//			printf("K %04x[%c]\n", address, pc_is_outside_bios ? '.' : '+');
 			switch(address >> 13) {
 				default:
 					if(pc_is_outside_bios) confidence_counter_.add_miss();
@@ -46,8 +45,8 @@ class KonamiROMSlotHandler: public ROMSlotHandler {
 			}
 		}
 
-		virtual void print_type() override {
-			printf("K");
+		virtual std::string debug_type() override {
+			return "K";
 		}
 	private:
 		MSX::MemoryMap &map_;
diff --git a/Machines/MSX/Cartridges/KonamiWithSCC.hpp b/Machines/MSX/Cartridges/KonamiWithSCC.hpp
index a6cbed409..1387c0116 100644
--- a/Machines/MSX/Cartridges/KonamiWithSCC.hpp
+++ b/Machines/MSX/Cartridges/KonamiWithSCC.hpp
@@ -21,7 +21,6 @@ class KonamiWithSCCROMSlotHandler: public ROMSlotHandler {
 			map_(map), slot_(slot), scc_(scc) {}
 
 		void write(uint16_t address, uint8_t value, bool pc_is_outside_bios) override {
-//			printf("KSCC %04x ", address);
 			switch(address >> 11) {
 				default:
 					if(pc_is_outside_bios) confidence_counter_.add_miss();
@@ -76,8 +75,8 @@ class KonamiWithSCCROMSlotHandler: public ROMSlotHandler {
 			return 0xff;
 		}
 
-		virtual void print_type() override {
-			printf("KSCC");
+		virtual std::string debug_type() override {
+			return "KSCC";
 		}
 
 	private:
diff --git a/Machines/MSX/MSX.cpp b/Machines/MSX/MSX.cpp
index dbc38691c..38b5b9b72 100644
--- a/Machines/MSX/MSX.cpp
+++ b/Machines/MSX/MSX.cpp
@@ -38,6 +38,7 @@
 #include "../MediaTarget.hpp"
 #include "../KeyboardMachine.hpp"
 
+#include "../../Outputs/Log.hpp"
 #include "../../Outputs/Speaker/Implementation/CompoundSource.hpp"
 #include "../../Outputs/Speaker/Implementation/LowpassSpeaker.hpp"
 #include "../../Outputs/Speaker/Implementation/SampleSource.hpp"
@@ -295,10 +296,11 @@ class ConcreteMachine:
 			return 0.5f;
 		}
 
-		void print_type() override {
+		std::string debug_type() override {
 			if(memory_slots_[1].handler) {
-				memory_slots_[1].handler->print_type();
+				return "MSX:" + memory_slots_[1].handler->debug_type();
 			}
+			return "MSX";
 		}
 
 		bool insert_media(const Analyser::Static::Media &media) override {
@@ -719,14 +721,14 @@ class ConcreteMachine:
 							// b0-b3: keyboard line
 							machine_.set_keyboard_line(value & 0xf);
 						} break;
-						default: printf("What what what what?\n"); break;
+						default: LOG("Unrecognised: MSX set 8255 output port " << port << " to value " << PADHEX(2) << value); break;
 					}
 				}
 
 				uint8_t get_value(int port) {
 					if(port == 1) {
 						return machine_.read_keyboard();
-					} else printf("What what?\n");
+					} else LOG("MSX attempted to read from 8255 port " << port);
 					return 0xff;
 				}
 
diff --git a/Machines/MSX/ROMSlotHandler.hpp b/Machines/MSX/ROMSlotHandler.hpp
index e0fadbc64..120ce737e 100644
--- a/Machines/MSX/ROMSlotHandler.hpp
+++ b/Machines/MSX/ROMSlotHandler.hpp
@@ -14,6 +14,7 @@
 
 #include <cstddef>
 #include <cstdint>
+#include <string>
 
 /*
 	Design assumptions:
@@ -71,7 +72,8 @@ class ROMSlotHandler {
 			return confidence_counter_.get_confidence();
 		}
 
-		virtual void print_type() {
+		virtual std::string debug_type() {
+			return "";
 		}
 
 	protected:
diff --git a/OSBindings/SDL/main.cpp b/OSBindings/SDL/main.cpp
index fded659fb..d0833e937 100644
--- a/OSBindings/SDL/main.cpp
+++ b/OSBindings/SDL/main.cpp
@@ -456,7 +456,7 @@ int main(int argc, char *argv[]) {
 								400, 300,
 								SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE);
 
-	SDL_GLContext gl_context;
+	SDL_GLContext gl_context = nullptr;
 	if(window) {
 		gl_context = SDL_GL_CreateContext(window);
 	}
diff --git a/Outputs/Log.hpp b/Outputs/Log.hpp
index 9d9538ced..d24d2262b 100644
--- a/Outputs/Log.hpp
+++ b/Outputs/Log.hpp
@@ -19,9 +19,9 @@
 
 #else
 
-#include <iomanip>
 #include <iostream>
 #include <ios>
+#include <iomanip>
 
 #define PADHEX(n) std::hex << std::setw(n) << std::right << std::setfill('0')
 #define PADDEC(n) std::dec << std::setw(n) << std::right << std::setfill('0')
diff --git a/Processors/Z80/AllRAM/Z80AllRAM.cpp b/Processors/Z80/AllRAM/Z80AllRAM.cpp
index fe4bc5083..0da164330 100644
--- a/Processors/Z80/AllRAM/Z80AllRAM.cpp
+++ b/Processors/Z80/AllRAM/Z80AllRAM.cpp
@@ -52,7 +52,6 @@ class ConcreteAllRAMProcessor: public AllRAMProcessor, public BusHandler {
 				break;
 
 				default:
-					printf("???\n");
 				break;
 			}
 
diff --git a/Storage/Tape/Formats/TZX.cpp b/Storage/Tape/Formats/TZX.cpp
index ee8230759..75f6bc9d7 100644
--- a/Storage/Tape/Formats/TZX.cpp
+++ b/Storage/Tape/Formats/TZX.cpp
@@ -9,6 +9,7 @@
 #include "TZX.hpp"
 
 #include "CSW.hpp"
+#include "../../../Outputs/Log.hpp"
 
 using namespace Storage::Tape;
 
@@ -91,7 +92,7 @@ void TZX::get_next_pulses() {
 			default:
 				// In TZX each chunk has a different way of stating or implying its length,
 				// so there is no route past an unimplemented chunk.
-				printf("!!Unknown %02x!!", chunk_id);
+				LOG("Unknown TZX chunk: " << PADHEX(4) << chunk_id);
 				set_is_at_end(true);
 			return;
 		}