fork download
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4. #include <memory>
  5. using namespace std;
  6.  
  7.  
  8. class Logger
  9. {
  10. private:
  11. static unique_ptr<Logger> loggerPtr;
  12. std::fstream fs;
  13.  
  14. //To ensure no one else can instantiate Logger.
  15. Logger();
  16.  
  17. public:
  18. enum MessageType
  19. {
  20. ERROR,
  21. WARNING,
  22. INFO
  23. };
  24.  
  25. static Logger* Instance();
  26. void LogFunc(std::string msg, MessageType type);
  27. ~Logger() { cout << "YES IT IS CLOSED !!"<<endl; }
  28.  
  29. friend unique_ptr<Logger> make_unique<Logger>();
  30. };
  31.  
  32. unique_ptr<Logger> Logger::loggerPtr{};
  33.  
  34. Logger* Logger::Instance()
  35. {
  36. if (!loggerPtr)
  37. {
  38. loggerPtr = make_unique<Logger>();
  39. }
  40.  
  41. return loggerPtr.get();
  42. }
  43.  
  44. Logger::Logger()
  45. {
  46. fs.open("docker.log", std::fstream::in | std::fstream::out | std::fstream::app);
  47. }
  48.  
  49. void Logger::LogFunc(std::string msg, MessageType type)
  50. {
  51. std::cout << msg;
  52.  
  53. switch (type)
  54. {
  55. case ERROR:
  56. fs << msg;
  57. break;
  58. case WARNING:
  59. fs << msg;
  60. break;
  61. case INFO:
  62. fs << msg;
  63. break;
  64. }
  65. }
  66.  
  67.  
  68.  
  69. int main() {
  70. Logger::Instance()->LogFunc("Hello.", Logger::INFO);
  71.  
  72. std::fstream fs;
  73. fs.open("docker_test.log", std::fstream::in | std::fstream::out | std::fstream::app);
  74. fs << "Why does this work?!";
  75.  
  76. system("pwd");
  77.  
  78. return 0;
  79. }
Success #stdin #stdout 0s 3416KB
stdin
Standard input is empty
stdout
/home/mjc1Kl
Hello.YES IT IS CLOSED !!