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 
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
void plugHelper(Connector &connector, EmuTime::param time) override
string_view getString() const
REGISTER_POLYMORPHIC_INITIALIZER(Pluggable, CassettePlayer, "CassettePlayer")
void writeData(byte data, EmuTime::param time) override
Sets the data signals.
Represents something you can plug devices into.
Definition: Connector.hh:20
const std::string & getMessage() const &
Definition: MSXException.hh:23
void unplugHelper(EmuTime::param time) override
uint8_t byte
8 bit unsigned integer
Definition: openmsx.hh:26
void setStrobe(bool strobe, EmuTime::param time) override
Sets the strobe signal: false = low, true = high.
void serialize(Archive &ar, unsigned version)
bool is_open() const
Return true iff this file handle refers to an open file.
Definition: File.hh:57
void flush()
Force a write of all buffered data to disk.
Definition: File.cc:119
Thanks to enen for testing this on a real cartridge:
Definition: Autofire.cc:5
This class implements a (close approximation) of the std::string_view class.
Definition: string_view.hh:15
const std::string & getName() const override
Name used to identify this pluggable.
#define INSTANTIATE_SERIALIZE_METHODS(CLASS)
Definition: serialize.hh:840
void write(const void *buffer, size_t num)
Write to file.
Definition: File.cc:84
void close()
Close the current file.
Definition: File.cc:74
bool getStatus(EmuTime::param time) override
Returns the STATUS signal: false = low = ready, true = high = not ready.
Thrown when a plug action fails.
string_view getDescription() const override
Description for this pluggable.
PrinterPortLogger(CommandController &commandController)