LogFailSafe.hpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /*##############################################################################
  2. HPCC SYSTEMS software Copyright (C) 2012 HPCC Systems.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. ############################################################################## */
  13. #pragma warning (disable : 4786)
  14. #ifndef _LOGFAILSAFE_HPP__
  15. #define _LOGFAILSAFE_HPP__
  16. #include "jlib.hpp"
  17. #include "jstring.hpp"
  18. #include "jutil.hpp" // for StringArray
  19. #include "loggingcommon.hpp"
  20. #include "LogSerializer.hpp"
  21. const unsigned long DEFAULT_SAFE_ROLLOVER_REQ_THRESHOLD = 500000L;
  22. const char* const PropSafeRolloverThreshold = "SafeRolloverThreshold";
  23. const char* const PropFailSafeLogsDir = "FailSafeLogsDir";
  24. const char* const DefaultFailSafeLogsDir = "./FailSafeLogs";
  25. interface ILogFailSafe : IInterface
  26. {
  27. virtual void Add(const char*, const StringBuffer& strContents)=0;//
  28. virtual void Add(const char*,IInterface& pIn)=0;
  29. virtual StringBuffer& GenerateGUID(StringBuffer& GUID,const char* seed="") = 0;
  30. virtual void AddACK(const char* GUID)=0;
  31. virtual void RollCurrentLog()=0;
  32. virtual void RollOldLogs()=0;
  33. virtual bool FindOldLogs() = 0;
  34. virtual void LoadOldLogs(StringArray& oldLogData) = 0;
  35. virtual void SplitLogRecord(const char* requestStr,StringBuffer& GUID, StringBuffer& Cache)=0;
  36. virtual void SafeRollover() = 0;
  37. virtual void RolloverAllLogs()=0;//
  38. virtual bool PopPendingLogRecord(StringBuffer& GUID, StringBuffer& cache) = 0;//
  39. virtual bool canRollCurrentLog() = 0;
  40. };
  41. extern LOGGINGCOMMON_API ILogFailSafe* createFailSafeLogger(IPropertyTree* cfg, const char* logType="");
  42. extern LOGGINGCOMMON_API ILogFailSafe* createFailSafeLogger(IPropertyTree* cfg, const char* pszService, const char* logType="");
  43. class CLogFailSafe : implements ILogFailSafe, public CInterface
  44. {
  45. CLogSerializer m_Added;
  46. CLogSerializer m_Cleared;
  47. static CLogFailSafe* m_Instance;
  48. StringBuffer m_LogType;
  49. StringArray m_UnsentLogs;
  50. StringBuffer m_logsdir;
  51. StringBuffer m_LogService;//
  52. CriticalSection m_critSec;//
  53. GuidMap m_PendingLogs;//
  54. unsigned long safeRolloverReqThreshold = DEFAULT_SAFE_ROLLOVER_REQ_THRESHOLD;
  55. unsigned long safeRolloverSizeThreshold = 0;
  56. void readCfg(IPropertyTree* cfg);
  57. void readSafeRolloverThresholdCfg(StringBuffer& safeRolloverThreshold);
  58. void createNew(const char* logType);
  59. void loadFailed(const char* logType);
  60. StringBuffer& getReceiveFileName(const char* sendFileName, StringBuffer& recieveName);
  61. StringBuffer& ExtractFileName(const char* fileName,StringBuffer& FileName);
  62. void loadPendingLogReqsFromExistingLogFiles();//
  63. void generateNewFileNames(StringBuffer& sendingFile, StringBuffer& receivingFile);//
  64. public:
  65. IMPLEMENT_IINTERFACE;
  66. CLogFailSafe();
  67. CLogFailSafe(IPropertyTree* cfg, const char* logType);
  68. CLogFailSafe(IPropertyTree* cfg, const char* pszService, const char* logType);
  69. virtual ~CLogFailSafe();
  70. StringBuffer& GenerateGUID(StringBuffer& GUID,const char* seed="");
  71. virtual void Add(const char*, const StringBuffer& strContents);//
  72. virtual void Add(const char*,IInterface& pIn);
  73. virtual void AddACK(const char* GUID);
  74. virtual void RollCurrentLog();
  75. virtual void RollOldLogs();
  76. virtual bool FindOldLogs();
  77. virtual void LoadOldLogs(StringArray& oldLogData);
  78. virtual void SplitLogRecord(const char* requestStr,StringBuffer& GUID, StringBuffer& Cache);
  79. virtual void SafeRollover();
  80. virtual void RolloverAllLogs();//
  81. virtual bool PopPendingLogRecord(StringBuffer& GUID, StringBuffer& cache);//
  82. virtual bool canRollCurrentLog() { return m_Added.getItemCount() == m_Cleared.getItemCount(); };
  83. };
  84. #endif // !_LOGFAILSAFE_HPP__