OXYGENHUB
◆ main openmsx / src / PrinterPortLogger.cc
2.0 KiB · C++ · last changed chore(): initial commit 1 month ago source blame history raw
1
#include "PrinterPortLogger.hh"
2
3
#include "FileContext.hh"
4
#include "FileException.hh"
5
#include "PlugException.hh"
6
#include "serialize.hh"
7
8
namespace openmsx {
9
10
PrinterPortLogger::PrinterPortLogger(CommandController& commandController)
11
	: logFilenameSetting(
12
		commandController, "printerlogfilename",
13
		"filename of the file where the printer output is logged to",
14
		"printer.log")
15
{
16
}
17
18
bool PrinterPortLogger::getStatus(EmuTime /*time*/)
19
{
20
	return false; // false = low = ready
21
}
22
23
void PrinterPortLogger::setStrobe(bool strobe, EmuTime /*time*/)
24
{
25
	if (file.is_open() && !strobe && prevStrobe) {
26
		// falling edge
27
		file.write(std::span{&toPrint, 1});
28
		file.flush(); // optimize when it turns out flushing
29
		               // every time is too slow
30
	}
31
	prevStrobe = strobe;
32
}
33
34
void PrinterPortLogger::writeData(uint8_t data, EmuTime /*time*/)
35
{
36
	toPrint = data;
37
}
38
39
void PrinterPortLogger::plugHelper(
40
		Connector& /*connector*/, EmuTime /*time*/)
41
{
42
	try {
43
		file = File(userFileContext().resolve(logFilenameSetting.getString()),
44
		            File::OpenMode::TRUNCATE);
45
	} catch (FileException& e) {
46
		throw PlugException("Couldn't plug printer logger: ",
47
		                    e.getMessage());
48
	}
49
}
50
51
void PrinterPortLogger::unplugHelper(EmuTime /*time*/)
52
{
53
	file.close();
54
}
55
56
zstring_view PrinterPortLogger::getName() const
57
{
58
	return "logger";
59
}
60
61
zstring_view PrinterPortLogger::getDescription() const
62
{
63
	return "Log everything that is sent to the printer port to a "
64
	       "file. The filename can be set with the "
65
	       "'printerlogfilename' setting.";
66
}
67
68
template<typename Archive>
69
void PrinterPortLogger::serialize(Archive& /*ar*/, unsigned /*version*/)
70
{
71
	// We don't try to resume logging to the same file.
72
	// And to not accidentally loose a previous log, we don't
73
	// overwrite that file automatically. So after savestate/loadstate,
74
	// you have to replug the PrinterPortLogger
75
}
76
INSTANTIATE_SERIALIZE_METHODS(PrinterPortLogger);
77
REGISTER_POLYMORPHIC_INITIALIZER(Pluggable, PrinterPortLogger, "PrinterPortLogger");
78
79
} // namespace openmsx