SimpleFileChannel.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. //
  2. // SimpleFileChannel.cpp
  3. //
  4. // $Id: //poco/1.4/Foundation/src/SimpleFileChannel.cpp#1 $
  5. //
  6. // Library: Foundation
  7. // Package: Logging
  8. // Module: SimpleFileChannel
  9. //
  10. // Copyright (c) 2005-2006, Applied Informatics Software Engineering GmbH.
  11. // and Contributors.
  12. //
  13. // SPDX-License-Identifier: BSL-1.0
  14. //
  15. #include "Poco/SimpleFileChannel.h"
  16. #include "Poco/LogFile.h"
  17. #include "Poco/File.h"
  18. #include "Poco/Message.h"
  19. #include "Poco/Exception.h"
  20. #include "Poco/Ascii.h"
  21. #include "Poco/String.h"
  22. namespace Poco {
  23. const std::string SimpleFileChannel::PROP_PATH = "path";
  24. const std::string SimpleFileChannel::PROP_SECONDARYPATH = "secondaryPath";
  25. const std::string SimpleFileChannel::PROP_ROTATION = "rotation";
  26. const std::string SimpleFileChannel::PROP_FLUSH = "flush";
  27. SimpleFileChannel::SimpleFileChannel():
  28. _limit(0),
  29. _flush(true),
  30. _pFile(0)
  31. {
  32. }
  33. SimpleFileChannel::SimpleFileChannel(const std::string& path):
  34. _path(path),
  35. _secondaryPath(path + ".0"),
  36. _limit(0),
  37. _flush(true),
  38. _pFile(0)
  39. {
  40. }
  41. SimpleFileChannel::~SimpleFileChannel()
  42. {
  43. try
  44. {
  45. close();
  46. }
  47. catch (...)
  48. {
  49. poco_unexpected();
  50. }
  51. }
  52. void SimpleFileChannel::open()
  53. {
  54. FastMutex::ScopedLock lock(_mutex);
  55. if (!_pFile)
  56. {
  57. File primary(_path);
  58. File secondary(_secondaryPath);
  59. Timestamp pt = primary.exists() ? primary.getLastModified() : 0;
  60. Timestamp st = secondary.exists() ? secondary.getLastModified() : 0;
  61. std::string path;
  62. if (pt >= st)
  63. path = _path;
  64. else
  65. path = _secondaryPath;
  66. _pFile = new LogFile(path);
  67. }
  68. }
  69. void SimpleFileChannel::close()
  70. {
  71. FastMutex::ScopedLock lock(_mutex);
  72. delete _pFile;
  73. _pFile = 0;
  74. }
  75. void SimpleFileChannel::log(const Message& msg)
  76. {
  77. open();
  78. FastMutex::ScopedLock lock(_mutex);
  79. if (_limit > 0 && _pFile->size() >= _limit)
  80. {
  81. rotate();
  82. }
  83. _pFile->write(msg.getText(), _flush);
  84. }
  85. void SimpleFileChannel::setProperty(const std::string& name, const std::string& value)
  86. {
  87. FastMutex::ScopedLock lock(_mutex);
  88. if (name == PROP_PATH)
  89. {
  90. _path = value;
  91. if (_secondaryPath.empty())
  92. _secondaryPath = _path + ".0";
  93. }
  94. else if (name == PROP_SECONDARYPATH)
  95. _secondaryPath = value;
  96. else if (name == PROP_ROTATION)
  97. setRotation(value);
  98. else if (name == PROP_FLUSH)
  99. setFlush(value);
  100. else
  101. Channel::setProperty(name, value);
  102. }
  103. std::string SimpleFileChannel::getProperty(const std::string& name) const
  104. {
  105. if (name == PROP_PATH)
  106. return _path;
  107. else if (name == PROP_SECONDARYPATH)
  108. return _secondaryPath;
  109. else if (name == PROP_ROTATION)
  110. return _rotation;
  111. else if (name == PROP_FLUSH)
  112. return std::string(_flush ? "true" : "false");
  113. else
  114. return Channel::getProperty(name);
  115. }
  116. Timestamp SimpleFileChannel::creationDate() const
  117. {
  118. if (_pFile)
  119. return _pFile->creationDate();
  120. else
  121. return 0;
  122. }
  123. UInt64 SimpleFileChannel::size() const
  124. {
  125. if (_pFile)
  126. return _pFile->size();
  127. else
  128. return 0;
  129. }
  130. const std::string& SimpleFileChannel::path() const
  131. {
  132. return _path;
  133. }
  134. const std::string& SimpleFileChannel::secondaryPath() const
  135. {
  136. return _secondaryPath;
  137. }
  138. void SimpleFileChannel::setRotation(const std::string& rotation)
  139. {
  140. std::string::const_iterator it = rotation.begin();
  141. std::string::const_iterator end = rotation.end();
  142. UInt64 n = 0;
  143. while (it != end && Ascii::isSpace(*it)) ++it;
  144. while (it != end && Ascii::isDigit(*it)) { n *= 10; n += *it++ - '0'; }
  145. while (it != end && Ascii::isSpace(*it)) ++it;
  146. std::string unit;
  147. while (it != end && Ascii::isAlpha(*it)) unit += *it++;
  148. if (unit == "K")
  149. _limit = n*1024;
  150. else if (unit == "M")
  151. _limit = n*1024*1024;
  152. else if (unit.empty())
  153. _limit = n;
  154. else if (unit == "never")
  155. _limit = 0;
  156. else
  157. throw InvalidArgumentException("rotation", rotation);
  158. _rotation = rotation;
  159. }
  160. void SimpleFileChannel::setFlush(const std::string& flush)
  161. {
  162. _flush = icompare(flush, "true") == 0;
  163. }
  164. void SimpleFileChannel::rotate()
  165. {
  166. std::string newPath;
  167. if (_pFile->path() == _path)
  168. newPath = _secondaryPath;
  169. else
  170. newPath = _path;
  171. File f(newPath);
  172. if (f.exists())
  173. {
  174. try
  175. {
  176. f.remove();
  177. }
  178. catch (...)
  179. {
  180. }
  181. }
  182. delete _pFile;
  183. _pFile = new LogFile(newPath);
  184. }
  185. } // namespace Poco