openMSX
PrinterPortLogger.cc
Go to the documentation of this file.
1 #include "PrinterPortLogger.hh"
2 #include "PlugException.hh"
3 #include "FileContext.hh"
4 #include "FileException.hh"
5 #include "serialize.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(userFileContext().resolve(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 std::string_view PrinterPortLogger::getName() const
63 {
64  return "logger";
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
Represents something you can plug devices into.
Definition: Connector.hh:21
void close()
Close the current file.
Definition: File.cc:88
bool is_open() const
Return true iff this file handle refers to an open file.
Definition: File.hh:63
void write(const void *buffer, size_t num)
Write to file.
Definition: File.cc:98
@ TRUNCATE
Definition: File.hh:20
void flush()
Force a write of all buffered data to disk.
Definition: File.cc:133
zstring_view getString() const noexcept
const std::string & getMessage() const &
Definition: MSXException.hh:23
Thrown when a plug action fails.
void unplugHelper(EmuTime::param time) override
void setStrobe(bool strobe, EmuTime::param time) override
Sets the strobe signal: false = low, true = high.
void serialize(Archive &ar, unsigned version)
void plugHelper(Connector &connector, EmuTime::param time) override
PrinterPortLogger(CommandController &commandController)
std::string_view getName() const override
Name used to identify this pluggable.
void writeData(byte data, EmuTime::param time) override
Sets the data signals.
bool getStatus(EmuTime::param time) override
Returns the STATUS signal: false = low = ready, true = high = not ready.
std::string_view getDescription() const override
Description for this pluggable.
This file implemented 3 utility functions:
Definition: Autofire.cc:5
REGISTER_POLYMORPHIC_INITIALIZER(Pluggable, CassettePlayer, "CassettePlayer")
FileContext userFileContext(string_view savePath)
Definition: FileContext.cc:172
#define INSTANTIATE_SERIALIZE_METHODS(CLASS)
Definition: serialize.hh:983