fork download
  1. /**
  2.  * Run logging and then make core dump with not flushed log messages.
  3. */
  4. #include <iostream>
  5. #include <string>
  6. #include <sstream>
  7. #include <cstdlib>
  8.  
  9. using namespace std;
  10.  
  11.  
  12. //! Пишем в буфер и флашим когда размер буфера > flushSize_
  13. class PseudoLogger {
  14. private:
  15. string buffer_;
  16. const string cookie_;
  17. size_t flushSize_ = 50u;
  18.  
  19. PseudoLogger():cookie_("my cookie") {
  20. resetBuffer();
  21. }
  22.  
  23. void resetBuffer() {
  24. buffer_ = cookie_;
  25. }
  26.  
  27. void flush() {
  28. cout << buffer_.substr(cookie_.size());
  29. resetBuffer();
  30. }
  31. public:
  32. ~PseudoLogger() {
  33. flush();
  34. }
  35.  
  36. static PseudoLogger& getInstance() {
  37. static PseudoLogger logger;
  38. return logger;
  39. }
  40.  
  41. void Log(string message) {
  42. if (message.size() > flushSize_)
  43. message.resize(flushSize_);
  44.  
  45. buffer_ += message;
  46. buffer_ += string("\n");
  47.  
  48. if (buffer_.size() > flushSize_)
  49. flush();
  50. }
  51. };
  52.  
  53.  
  54. int main(int argc, char** argv) {
  55. PseudoLogger& logger = PseudoLogger::getInstance();
  56.  
  57. for (int i=1; i<20; ++i) {
  58. std::stringstream out;
  59. out << i;
  60. logger.Log(string("test message ") + out.str());
  61. }
  62.  
  63. abort();
  64.  
  65. return 0;
  66. }
  67.  
Not running #stdin #stdout 0s 0KB
stdin
Standard input is empty
stdout

Standard output is empty