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 #include "memory.hh"
6 
7 namespace openmsx {
8 
10  : logFilenameSetting(
11  commandController, "printerlogfilename",
12  "filename of the file where the printer output is logged to",
13  "printer.log")
14  , toPrint(0) // Initialize to avoid a static analysis (cppcheck) warning.
15  // For correctness it's not strictly needed to initialize
16  // this variable. But understanding why exactly it's not
17  // needed depends on the implementation details of a few
18  // other classes, so let's simplify stuff and just
19  // initialize.
20  , prevStrobe(true)
21 {
22 }
23 
24 bool PrinterPortLogger::getStatus(EmuTime::param /*time*/)
25 {
26  return false; // false = low = ready
27 }
28 
29 void PrinterPortLogger::setStrobe(bool strobe, EmuTime::param /*time*/)
30 {
31  if (file.is_open() && !strobe && prevStrobe) {
32  // falling edge
33  file.write(&toPrint, 1);
34  file.flush(); // optimize when it turns out flushing
35  // every time is too slow
36  }
37  prevStrobe = strobe;
38 }
39 
40 void PrinterPortLogger::writeData(byte data, EmuTime::param /*time*/)
41 {
42  toPrint = data;
43 }
44 
46  Connector& /*connector*/, EmuTime::param /*time*/)
47 {
48  try {
49  file = File(logFilenameSetting.getString(),
51  } catch (FileException& e) {
52  throw PlugException("Couldn't plug printer logger: " +
53  e.getMessage());
54  }
55 }
56 
57 void PrinterPortLogger::unplugHelper(EmuTime::param /*time*/)
58 {
59  file.close();
60 }
61 
62 const std::string& PrinterPortLogger::getName() const
63 {
64  static const std::string name("logger");
65  return name;
66 }
67 
69 {
70  return "Log everything that is sent to the printer port to a "
71  "file. The filename can be set with the "
72  "'printerlogfilename' setting.";
73 }
74 
75 template<typename Archive>
76 void PrinterPortLogger::serialize(Archive& /*ar*/, unsigned /*version*/)
77 {
78  // We don't try to resume logging to the same file.
79  // And to not accidentally loose a previous log, we don't
80  // overwrite that file automatically. So after savestate/loadstate,
81  // you have to replug the PrinterPortLogger
82 }
85 
86 } // namespace openmsx
void plugHelper(Connector &connector, EmuTime::param time) override
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
string_ref getDescription() const override
Description for this pluggable.
void unplugHelper(EmuTime::param time) override
uint8_t byte
8 bit unsigned integer
Definition: openmsx.hh:26
This class implements a subset of the proposal for std::string_ref (proposed for the next c++ standar...
Definition: string_ref.hh:18
void setStrobe(bool strobe, EmuTime::param time) override
Sets the strobe signal: false = low, true = high.
void serialize(Archive &ar, unsigned version)
const std::string & getMessage() const
Definition: MSXException.hh:13
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
const std::string & getName() const override
Name used to identify this pluggable.
#define INSTANTIATE_SERIALIZE_METHODS(CLASS)
Definition: serialize.hh:840
uint8_t * data()
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
string_ref getString() const
bool getStatus(EmuTime::param time) override
Returns the STATUS signal: false = low = ready, true = high = not ready.
Thrown when a plug action fails.
PrinterPortLogger(CommandController &commandController)