ECSyncLog.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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 <mutex>
  19. #include <kopano/ecversion.h>
  20. #include <kopano/lockhelper.hpp>
  21. #include "ECSyncLog.h"
  22. #include "ECSyncSettings.h"
  23. #include <kopano/stringutil.h>
  24. #include <kopano/ECLogger.h>
  25. #include <cstdlib>
  26. #include <mapidefs.h>
  27. // for LOG_MAIL
  28. #include <syslog.h>
  29. HRESULT ECSyncLog::GetLogger(ECLogger **lppLogger)
  30. {
  31. HRESULT hr = hrSuccess;
  32. *lppLogger = NULL;
  33. scoped_lock lock(s_hMutex);
  34. if (s_lpLogger == NULL) {
  35. ECSyncSettings *lpSettings = ECSyncSettings::GetInstance();
  36. if (lpSettings->SyncLogEnabled()) {
  37. char dummy[MAX_PATH + 1] = { 0 };
  38. if (GetTempPath(sizeof dummy, dummy) >= sizeof dummy)
  39. dummy[0] = 0x00;
  40. std::string strPath = dummy + std::string("/");
  41. if (lpSettings->ContinuousLogging()) {
  42. time_t now = time(NULL);
  43. strPath += "synclog-";
  44. strPath += stringify(now);
  45. strPath += ".txt.gz";
  46. s_lpLogger = new ECLogger_File(lpSettings->SyncLogLevel(), 1, strPath.c_str(), true);
  47. } else {
  48. strPath += "synclog.txt";
  49. s_lpLogger = new ECLogger_File(lpSettings->SyncLogLevel(), 1, strPath.c_str(), false);
  50. }
  51. s_lpLogger->Log(EC_LOGLEVEL_FATAL, "********************");
  52. s_lpLogger->Log(EC_LOGLEVEL_FATAL, "New sync log session openend (Kopano-" PROJECT_VERSION_CLIENT_STR ")");
  53. s_lpLogger->Log(EC_LOGLEVEL_FATAL, " - Log level: %u", lpSettings->SyncLogLevel());
  54. s_lpLogger->Log(EC_LOGLEVEL_FATAL, " - Sync stream: %s", lpSettings->SyncStreamEnabled() ? "enabled" : "disabled");
  55. s_lpLogger->Log(EC_LOGLEVEL_FATAL, " - Change notifications: %s", lpSettings->ChangeNotificationsEnabled() ? "enabled" : "disabled");
  56. s_lpLogger->Log(EC_LOGLEVEL_FATAL, " - State collector: %s", lpSettings->StateCollectorEnabled() ? "enabled" : "disabled");
  57. s_lpLogger->Log(EC_LOGLEVEL_FATAL, "********************");
  58. }
  59. else {
  60. s_lpLogger = new ECLogger_Null();
  61. }
  62. }
  63. if (!s_lpLogger)
  64. s_lpLogger = new ECLogger_Syslog(EC_LOGLEVEL_DEBUG, "kclibsync", LOG_MAIL);
  65. *lppLogger = s_lpLogger;
  66. s_lpLogger->AddRef();
  67. return hr;
  68. }
  69. HRESULT ECSyncLog::SetLogger(ECLogger *lpLogger)
  70. {
  71. scoped_lock lock(s_hMutex);
  72. if (s_lpLogger)
  73. s_lpLogger->Release();
  74. s_lpLogger = lpLogger;
  75. if (s_lpLogger)
  76. s_lpLogger->AddRef();
  77. return hrSuccess;
  78. }
  79. std::mutex ECSyncLog::s_hMutex;
  80. ECLogger *ECSyncLog::s_lpLogger = NULL;
  81. ECSyncLog::__initializer::~__initializer() {
  82. if (ECSyncLog::s_lpLogger == nullptr)
  83. return;
  84. unsigned ulRef = ECSyncLog::s_lpLogger->Release();
  85. // Make sure all references are released so compressed logs don't get corrupted.
  86. while (ulRef)
  87. ulRef = ECSyncLog::s_lpLogger->Release();
  88. }
  89. ECSyncLog::__initializer ECSyncLog::__i;