LogToFile.h 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. // David Eberly, Geometric Tools, Redmond WA 98052
  2. // Copyright (c) 1998-2020
  3. // Distributed under the Boost Software License, Version 1.0.
  4. // https://www.boost.org/LICENSE_1_0.txt
  5. // https://www.geometrictools.com/License/Boost/LICENSE_1_0.txt
  6. // Version: 4.0.2019.08.13
  7. #pragma once
  8. #include <Mathematics/Logger.h>
  9. #include <fstream>
  10. namespace WwiseGTE
  11. {
  12. class LogToFile : public Logger::Listener
  13. {
  14. public:
  15. LogToFile(std::string const& filename, int flags)
  16. :
  17. Logger::Listener(flags),
  18. mFilename(filename)
  19. {
  20. std::ofstream logFile(filename);
  21. if (logFile)
  22. {
  23. // This clears the file contents from any previous runs.
  24. logFile.close();
  25. }
  26. else
  27. {
  28. // The file cannot be opened. Use a null string for Report to
  29. // know not to attempt opening the file for append.
  30. mFilename = "";
  31. }
  32. }
  33. private:
  34. virtual void Report(std::string const& message)
  35. {
  36. if (mFilename != "")
  37. {
  38. // Open for append.
  39. std::ofstream logFile(mFilename, std::ios_base::out | std::ios_base::app);
  40. if (logFile)
  41. {
  42. logFile << message.c_str();
  43. logFile.close();
  44. }
  45. else
  46. {
  47. // The file cannot be opened. Use a null string for
  48. // Report not to attempt opening the file for append on
  49. // the next call.
  50. mFilename = "";
  51. }
  52. }
  53. }
  54. std::string mFilename;
  55. };
  56. }