FormattingChannel.cpp 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. //
  2. // FormattingChannel.cpp
  3. //
  4. // $Id: //poco/1.4/Foundation/src/FormattingChannel.cpp#1 $
  5. //
  6. // Library: Foundation
  7. // Package: Logging
  8. // Module: Formatter
  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/FormattingChannel.h"
  16. #include "Poco/Formatter.h"
  17. #include "Poco/Message.h"
  18. #include "Poco/LoggingRegistry.h"
  19. namespace Poco {
  20. FormattingChannel::FormattingChannel():
  21. _pFormatter(0),
  22. _pChannel(0)
  23. {
  24. }
  25. FormattingChannel::FormattingChannel(Formatter* pFormatter):
  26. _pFormatter(pFormatter),
  27. _pChannel(0)
  28. {
  29. if (_pFormatter) _pFormatter->duplicate();
  30. }
  31. FormattingChannel::FormattingChannel(Formatter* pFormatter, Channel* pChannel):
  32. _pFormatter(pFormatter),
  33. _pChannel(pChannel)
  34. {
  35. if (_pFormatter) _pFormatter->duplicate();
  36. if (_pChannel) _pChannel->duplicate();
  37. }
  38. FormattingChannel::~FormattingChannel()
  39. {
  40. if (_pChannel) _pChannel->release();
  41. if (_pFormatter) _pFormatter->release();
  42. }
  43. void FormattingChannel::setFormatter(Formatter* pFormatter)
  44. {
  45. if (_pFormatter) _pFormatter->release();
  46. _pFormatter = pFormatter;
  47. if (_pFormatter) _pFormatter->duplicate();
  48. }
  49. Formatter* FormattingChannel::getFormatter() const
  50. {
  51. return _pFormatter;
  52. }
  53. void FormattingChannel::setChannel(Channel* pChannel)
  54. {
  55. if (_pChannel) _pChannel->release();
  56. _pChannel = pChannel;
  57. if (_pChannel) _pChannel->duplicate();
  58. }
  59. Channel* FormattingChannel::getChannel() const
  60. {
  61. return _pChannel;
  62. }
  63. void FormattingChannel::log(const Message& msg)
  64. {
  65. if (_pChannel)
  66. {
  67. if (_pFormatter)
  68. {
  69. std::string text;
  70. _pFormatter->format(msg, text);
  71. _pChannel->log(Message(msg, text));
  72. }
  73. else
  74. {
  75. _pChannel->log(msg);
  76. }
  77. }
  78. }
  79. void FormattingChannel::setProperty(const std::string& name, const std::string& value)
  80. {
  81. if (name == "channel")
  82. setChannel(LoggingRegistry::defaultRegistry().channelForName(value));
  83. else if (name == "formatter")
  84. setFormatter(LoggingRegistry::defaultRegistry().formatterForName(value));
  85. else if (_pChannel)
  86. _pChannel->setProperty(name, value);
  87. }
  88. void FormattingChannel::open()
  89. {
  90. if (_pChannel)
  91. _pChannel->open();
  92. }
  93. void FormattingChannel::close()
  94. {
  95. if (_pChannel)
  96. _pChannel->close();
  97. }
  98. } // namespace Poco