fileSink.hpp 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. /*##############################################################################
  2. HPCC SYSTEMS software Copyright (C) 2021 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 once
  14. #include "jmetrics.hpp"
  15. #include "jptree.hpp"
  16. #include "jstring.hpp"
  17. #include "jfile.ipp"
  18. #include "jsem.hpp"
  19. #include <thread>
  20. #include <condition_variable>
  21. using namespace hpccMetrics;
  22. #ifdef FILESINK_EXPORTS
  23. #define FILESINK_API DECL_EXPORT
  24. #else
  25. #define FILESINK_API DECL_IMPORT
  26. #endif
  27. class FILESINK_API FileMetricSink : public MetricSink
  28. {
  29. public:
  30. explicit FileMetricSink(const char *name, const IPropertyTree *pSettingsTree);
  31. ~FileMetricSink() override;
  32. void startCollection(MetricsReporter *pReporter) override;
  33. void stopCollection() override;
  34. protected:
  35. void writeReportHeaderToFile() const;
  36. void writeMeasurementToFile(const std::string &metricName, uint32_t value, const std::string &metricDescription) const;
  37. void collectionThread();
  38. void doStopCollecting();
  39. protected:
  40. StringBuffer fileName;
  41. unsigned collectionPeriodSeconds;
  42. std::thread collectThread;
  43. std::atomic<bool> stopCollectionFlag{false};
  44. bool isCollecting = false;
  45. bool clearFileOnStartCollecting = false;
  46. FILE *fhandle = nullptr;
  47. Semaphore waitSem;
  48. };