PrinterPortLogger.cc 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. #include "PrinterPortLogger.hh"
  2. #include "PlugException.hh"
  3. #include "FileException.hh"
  4. #include "serialize.hh"
  5. namespace openmsx {
  6. PrinterPortLogger::PrinterPortLogger(CommandController& commandController)
  7. : logFilenameSetting(
  8. commandController, "printerlogfilename",
  9. "filename of the file where the printer output is logged to",
  10. "printer.log")
  11. , toPrint(0) // Initialize to avoid a static analysis (cppcheck) warning.
  12. // For correctness it's not strictly needed to initialize
  13. // this variable. But understanding why exactly it's not
  14. // needed depends on the implementation details of a few
  15. // other classes, so let's simplify stuff and just
  16. // initialize.
  17. , prevStrobe(true)
  18. {
  19. }
  20. bool PrinterPortLogger::getStatus(EmuTime::param /*time*/)
  21. {
  22. return false; // false = low = ready
  23. }
  24. void PrinterPortLogger::setStrobe(bool strobe, EmuTime::param /*time*/)
  25. {
  26. if (file.is_open() && !strobe && prevStrobe) {
  27. // falling edge
  28. file.write(&toPrint, 1);
  29. file.flush(); // optimize when it turns out flushing
  30. // every time is too slow
  31. }
  32. prevStrobe = strobe;
  33. }
  34. void PrinterPortLogger::writeData(byte data, EmuTime::param /*time*/)
  35. {
  36. toPrint = data;
  37. }
  38. void PrinterPortLogger::plugHelper(
  39. Connector& /*connector*/, EmuTime::param /*time*/)
  40. {
  41. try {
  42. file = File(logFilenameSetting.getString(),
  43. File::TRUNCATE);
  44. } catch (FileException& e) {
  45. throw PlugException("Couldn't plug printer logger: ",
  46. e.getMessage());
  47. }
  48. }
  49. void PrinterPortLogger::unplugHelper(EmuTime::param /*time*/)
  50. {
  51. file.close();
  52. }
  53. const std::string& PrinterPortLogger::getName() const
  54. {
  55. static const std::string name("logger");
  56. return name;
  57. }
  58. std::string_view PrinterPortLogger::getDescription() const
  59. {
  60. return "Log everything that is sent to the printer port to a "
  61. "file. The filename can be set with the "
  62. "'printerlogfilename' setting.";
  63. }
  64. template<typename Archive>
  65. void PrinterPortLogger::serialize(Archive& /*ar*/, unsigned /*version*/)
  66. {
  67. // We don't try to resume logging to the same file.
  68. // And to not accidentally loose a previous log, we don't
  69. // overwrite that file automatically. So after savestate/loadstate,
  70. // you have to replug the PrinterPortLogger
  71. }
  72. INSTANTIATE_SERIALIZE_METHODS(PrinterPortLogger);
  73. REGISTER_POLYMORPHIC_INITIALIZER(Pluggable, PrinterPortLogger, "PrinterPortLogger");
  74. } // namespace openmsx