RotateStrategy.cpp 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. //
  2. // RotateStrategy.cpp
  3. //
  4. // $Id: //poco/1.4/Foundation/src/RotateStrategy.cpp#1 $
  5. //
  6. // Library: Foundation
  7. // Package: Logging
  8. // Module: FileChannel
  9. //
  10. // Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
  11. // and Contributors.
  12. //
  13. // SPDX-License-Identifier: BSL-1.0
  14. //
  15. #include "Poco/RotateStrategy.h"
  16. #include "Poco/FileStream.h"
  17. #include "Poco/DateTimeParser.h"
  18. #include "Poco/DateTimeFormatter.h"
  19. #include "Poco/DateTimeFormat.h"
  20. namespace Poco {
  21. //
  22. // RotateStrategy
  23. //
  24. RotateStrategy::RotateStrategy()
  25. {
  26. }
  27. RotateStrategy::~RotateStrategy()
  28. {
  29. }
  30. //
  31. // RotateByIntervalStrategy
  32. //
  33. const std::string RotateByIntervalStrategy::ROTATE_TEXT("# Log file created/rotated ");
  34. RotateByIntervalStrategy::RotateByIntervalStrategy(const Timespan& span):
  35. _span(span),
  36. _lastRotate(0)
  37. {
  38. if (span.totalMicroseconds() <= 0) throw InvalidArgumentException("time span must be greater than zero");
  39. }
  40. RotateByIntervalStrategy::~RotateByIntervalStrategy()
  41. {
  42. }
  43. bool RotateByIntervalStrategy::mustRotate(LogFile* pFile)
  44. {
  45. if (_lastRotate == 0 || pFile->size() == 0)
  46. {
  47. if (pFile->size() != 0)
  48. {
  49. Poco::FileInputStream istr(pFile->path());
  50. std::string tag;
  51. std::getline(istr, tag);
  52. if (tag.compare(0, ROTATE_TEXT.size(), ROTATE_TEXT) == 0)
  53. {
  54. std::string timestamp(tag, ROTATE_TEXT.size());
  55. int tzd;
  56. _lastRotate = DateTimeParser::parse(DateTimeFormat::RFC1036_FORMAT, timestamp, tzd).timestamp();
  57. }
  58. else _lastRotate = pFile->creationDate();
  59. }
  60. else
  61. {
  62. _lastRotate.update();
  63. std::string tag(ROTATE_TEXT);
  64. DateTimeFormatter::append(tag, _lastRotate, DateTimeFormat::RFC1036_FORMAT);
  65. pFile->write(tag);
  66. }
  67. }
  68. Timestamp now;
  69. return _span <= now - _lastRotate;
  70. }
  71. //
  72. // RotateBySizeStrategy
  73. //
  74. RotateBySizeStrategy::RotateBySizeStrategy(UInt64 size): _size(size)
  75. {
  76. if (size == 0) throw InvalidArgumentException("size must be greater than zero");
  77. }
  78. RotateBySizeStrategy::~RotateBySizeStrategy()
  79. {
  80. }
  81. bool RotateBySizeStrategy::mustRotate(LogFile* pFile)
  82. {
  83. return pFile->size() >= _size;
  84. }
  85. } // namespace Poco