ECArchiverLogger.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /*
  2. * Copyright 2005 - 2016 Zarafa and its licensors
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU Affero General Public License, version 3,
  6. * as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU Affero General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU Affero General Public License
  14. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. *
  16. */
  17. #include <kopano/platform.h>
  18. #include "ECArchiverLogger.h"
  19. namespace KC {
  20. ECArchiverLogger::ECArchiverLogger(ECLogger *lpLogger)
  21. : ECLogger(0)
  22. , m_lpLogger(lpLogger)
  23. {
  24. if (m_lpLogger)
  25. m_lpLogger->AddRef();
  26. }
  27. ECArchiverLogger::~ECArchiverLogger()
  28. {
  29. if (m_lpLogger)
  30. m_lpLogger->Release();
  31. }
  32. tstring ECArchiverLogger::SetUser(tstring strUser)
  33. {
  34. std::swap(strUser, m_strUser);
  35. return strUser;
  36. }
  37. tstring ECArchiverLogger::SetFolder(tstring strFolder)
  38. {
  39. std::swap(strFolder, m_strFolder);
  40. return strFolder;
  41. }
  42. void ECArchiverLogger::Reset()
  43. {
  44. if (m_lpLogger)
  45. m_lpLogger->Reset();
  46. }
  47. void ECArchiverLogger::Log(unsigned int loglevel, const std::string &message)
  48. {
  49. if (m_lpLogger)
  50. m_lpLogger->Log(loglevel, message);
  51. }
  52. void ECArchiverLogger::Log(unsigned int loglevel, const char *format, ...)
  53. {
  54. if (m_lpLogger == NULL || !m_lpLogger->Log(loglevel))
  55. return;
  56. std::string strFormat = CreateFormat(format);
  57. va_list va;
  58. va_start(va, format);
  59. m_lpLogger->LogVA(loglevel, strFormat.c_str(), va);
  60. va_end(va);
  61. }
  62. void ECArchiverLogger::LogVA(unsigned int loglevel, const char *format, va_list& va)
  63. {
  64. if (m_lpLogger == NULL || !m_lpLogger->Log(loglevel))
  65. return;
  66. std::string strFormat = CreateFormat(format);
  67. m_lpLogger->LogVA(loglevel, strFormat.c_str(), va);
  68. }
  69. std::string ECArchiverLogger::CreateFormat(const char *format)
  70. {
  71. char buffer[4096];
  72. int len;
  73. std::string strPrefix;
  74. if (m_strUser.empty())
  75. return strPrefix + format;
  76. if (m_strFolder.empty()) {
  77. len = m_lpLogger->snprintf(buffer, sizeof(buffer), "For '" TSTRING_PRINTF "': ", m_strUser.c_str());
  78. strPrefix = EscapeFormatString(std::string(buffer, len));
  79. } else {
  80. len = m_lpLogger->snprintf(buffer, sizeof(buffer), "For '" TSTRING_PRINTF "' in folder '" TSTRING_PRINTF "': ", m_strUser.c_str(), m_strFolder.c_str());
  81. strPrefix = EscapeFormatString(std::string(buffer, len));
  82. }
  83. return strPrefix + format;
  84. }
  85. std::string ECArchiverLogger::EscapeFormatString(const std::string &strFormat)
  86. {
  87. std::string strEscaped;
  88. strEscaped.reserve(strFormat.length() * 2);
  89. for (auto c : strFormat) {
  90. if (c == '\\')
  91. strEscaped.append("\\\\");
  92. else if (c == '%')
  93. strEscaped.append("%%");
  94. else
  95. strEscaped.append(1, c);
  96. }
  97. return strEscaped;
  98. }
  99. ScopedUserLogging::ScopedUserLogging(ECArchiverLogger *lpLogger, const tstring &strUser)
  100. : m_lpLogger(lpLogger)
  101. , m_strPrevUser(lpLogger->SetUser(strUser))
  102. { }
  103. ScopedUserLogging::~ScopedUserLogging()
  104. {
  105. m_lpLogger->SetUser(m_strPrevUser);
  106. }
  107. ScopedFolderLogging::ScopedFolderLogging(ECArchiverLogger *lpLogger, const tstring &strFolder)
  108. : m_lpLogger(lpLogger)
  109. , m_strPrevFolder(lpLogger->SetFolder(strFolder))
  110. { }
  111. ScopedFolderLogging::~ScopedFolderLogging()
  112. {
  113. m_lpLogger->SetFolder(m_strPrevFolder);
  114. }
  115. } /* namespace */