backend.cpp 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. #include "backend.h"
  2. #include <util/generic/vector.h>
  3. #include <util/system/mutex.h>
  4. #include <util/generic/singleton.h>
  5. #include <util/generic/yexception.h>
  6. namespace {
  7. class TGlobalLogsStorage {
  8. private:
  9. TVector<TLogBackend*> Backends;
  10. TMutex Mutex;
  11. public:
  12. void Register(TLogBackend* backend) {
  13. TGuard<TMutex> g(Mutex);
  14. Backends.push_back(backend);
  15. }
  16. void UnRegister(TLogBackend* backend) {
  17. TGuard<TMutex> g(Mutex);
  18. for (ui32 i = 0; i < Backends.size(); ++i) {
  19. if (Backends[i] == backend) {
  20. Backends.erase(Backends.begin() + i);
  21. return;
  22. }
  23. }
  24. Y_ABORT("Incorrect pointer for log backend");
  25. }
  26. void Reopen(bool flush) {
  27. TGuard<TMutex> g(Mutex);
  28. for (auto& b : Backends) {
  29. if (typeid(*b) == typeid(TLogBackend)) {
  30. continue;
  31. }
  32. if (flush) {
  33. b->ReopenLog();
  34. } else {
  35. b->ReopenLogNoFlush();
  36. }
  37. }
  38. }
  39. };
  40. }
  41. template <>
  42. class TSingletonTraits<TGlobalLogsStorage> {
  43. public:
  44. static const size_t Priority = 50;
  45. };
  46. ELogPriority TLogBackend::FiltrationLevel() const {
  47. return LOG_MAX_PRIORITY;
  48. }
  49. TLogBackend::TLogBackend() noexcept {
  50. Singleton<TGlobalLogsStorage>()->Register(this);
  51. }
  52. TLogBackend::~TLogBackend() {
  53. Singleton<TGlobalLogsStorage>()->UnRegister(this);
  54. }
  55. void TLogBackend::ReopenLogNoFlush() {
  56. ReopenLog();
  57. }
  58. void TLogBackend::ReopenAllBackends(bool flush) {
  59. Singleton<TGlobalLogsStorage>()->Reopen(flush);
  60. }
  61. size_t TLogBackend::QueueSize() const {
  62. ythrow yexception() << "Not implemented.";
  63. }