openMSX
PrinterPortLogger.cc
Go to the documentation of this file.
1 #include "PrinterPortLogger.hh"
2 #include "PlugException.hh"
3 #include "FileException.hh"
4 #include "serialize.hh"
5 
6 namespace openmsx {
7 
9  : logFilenameSetting(
10  commandController, "printerlogfilename",
11  "filename of the file where the printer output is logged to",
12  "printer.log")
13  , toPrint(0) // Initialize to avoid a static analysis (cppcheck) warning.
14  // For correctness it's not strictly needed to initialize
15  // this variable. But understanding why exactly it's not
16  // needed depends on the implementation details of a few
17  // other classes, so let's simplify stuff and just
18  // initialize.
19  , prevStrobe(true)
20 {
21 }
22 
23 bool PrinterPortLogger::getStatus(EmuTime::param /*time*/)
24 {
25  return false; // false = low = ready
26 }
27 
28 void PrinterPortLogger::setStrobe(bool strobe, EmuTime::param /*time*/)
29 {
30  if (file.is_open() && !strobe && prevStrobe) {
31  // falling edge
32  file.write(&toPrint, 1);
33  file.flush(); // optimize when it turns out flushing
34  // every time is too slow
35  }
36  prevStrobe = strobe;
37 }
38 
39 void PrinterPortLogger::writeData(byte data, EmuTime::param /*time*/)
40 {
41  toPrint = data;
42 }
43 
45  Connector& /*connector*/, EmuTime::param /*time*/)
46 {
47  try {
48  file = File(logFilenameSetting.getString(),
50  } catch (FileException& e) {
51  throw PlugException("Couldn't plug printer logger: ",
52  e.getMessage());
53  }
54 }
55 
56 void PrinterPortLogger::unplugHelper(EmuTime::param /*time*/)
57 {
58  file.close();
59 }
60 
61 const std::string& PrinterPortLogger::getName() const
62 {
63  static const std::string name("logger");
64  return name;
65 }
66 
67 std::string_view PrinterPortLogger::getDescription() const
68 {
69  return "Log everything that is sent to the printer port to a "
70  "file. The filename can be set with the "
71  "'printerlogfilename' setting.";
72 }
73 
74 template<typename Archive>
75 void PrinterPortLogger::serialize(Archive& /*ar*/, unsigned /*version*/)
76 {
77  // We don't try to resume logging to the same file.
78  // And to not accidentally loose a previous log, we don't
79  // overwrite that file automatically. So after savestate/loadstate,
80  // you have to replug the PrinterPortLogger
81 }
84 
85 } // namespace openmsx
FileException.hh
openmsx::PrinterPortLogger::unplugHelper
void unplugHelper(EmuTime::param time) override
Definition: PrinterPortLogger.cc:56
openmsx::CommandController
Definition: CommandController.hh:18
serialize.hh
openmsx::File::close
void close()
Close the current file.
Definition: File.cc:78
openmsx::PrinterPortLogger::getDescription
std::string_view getDescription() const override
Description for this pluggable.
Definition: PrinterPortLogger.cc:67
openmsx::PrinterPortLogger
Definition: PrinterPortLogger.hh:13
openmsx::PlugException
Thrown when a plug action fails.
Definition: PlugException.hh:11
PlugException.hh
openmsx::PrinterPortLogger::getStatus
bool getStatus(EmuTime::param time) override
Returns the STATUS signal: false = low = ready, true = high = not ready.
Definition: PrinterPortLogger.cc:23
openmsx::Pluggable
Definition: Pluggable.hh:12
openmsx::File::TRUNCATE
@ TRUNCATE
Definition: File.hh:20
openmsx::FilenameSetting::getString
std::string_view getString() const noexcept
Definition: FilenameSetting.hh:18
openmsx::PrinterPortLogger::getName
const std::string & getName() const override
Name used to identify this pluggable.
Definition: PrinterPortLogger.cc:61
openmsx::File::write
void write(const void *buffer, size_t num)
Write to file.
Definition: File.cc:88
INSTANTIATE_SERIALIZE_METHODS
#define INSTANTIATE_SERIALIZE_METHODS(CLASS)
Definition: serialize.hh:981
openmsx::File::is_open
bool is_open() const
Return true iff this file handle refers to an open file.
Definition: File.hh:61
openmsx::FileException
Definition: FileException.hh:9
openmsx::File::flush
void flush()
Force a write of all buffered data to disk.
Definition: File.cc:123
openmsx::PrinterPortLogger::setStrobe
void setStrobe(bool strobe, EmuTime::param time) override
Sets the strobe signal: false = low, true = high.
Definition: PrinterPortLogger.cc:28
openmsx::PrinterPortLogger::serialize
void serialize(Archive &ar, unsigned version)
Definition: PrinterPortLogger.cc:75
openmsx::PrinterPortLogger::PrinterPortLogger
PrinterPortLogger(CommandController &commandController)
Definition: PrinterPortLogger.cc:8
openmsx::File
Definition: File.hh:16
openmsx::PrinterPortLogger::writeData
void writeData(byte data, EmuTime::param time) override
Sets the data signals.
Definition: PrinterPortLogger.cc:39
openmsx::Connector
Represents something you can plug devices into.
Definition: Connector.hh:21
openmsx::PrinterPortLogger::plugHelper
void plugHelper(Connector &connector, EmuTime::param time) override
Definition: PrinterPortLogger.cc:44
PrinterPortLogger.hh
openmsx::MSXException::getMessage
const std::string & getMessage() const &
Definition: MSXException.hh:23
openmsx
This file implemented 3 utility functions:
Definition: Autofire.cc:5
openmsx::REGISTER_POLYMORPHIC_INITIALIZER
REGISTER_POLYMORPHIC_INITIALIZER(Pluggable, CassettePlayer, "CassettePlayer")