1 /* 2 * Copyright (c) 2022-2023 Huawei Device Co., Ltd. 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 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef THERMAL_DFX_H 17 #define THERMAL_DFX_H 18 19 #include <atomic> 20 #include <fstream> 21 #include <map> 22 #include <memory> 23 #include <string> 24 #include <thread> 25 26 #include "nocopyable.h" 27 #include "thermal_hdf_config.h" 28 29 namespace OHOS { 30 namespace HDI { 31 namespace Thermal { 32 namespace V1_1 { 33 class ThermalDfx : public NoCopyable { 34 public: 35 ThermalDfx(); 36 ~ThermalDfx(); 37 38 void Init(); 39 static ThermalDfx& GetInstance(); 40 static void DestroyInstance(); 41 42 private: 43 std::string CanonicalizeSpecPath(const char* src); 44 bool Compress(const std::string& dataFile, const std::string& destFile); 45 void StartThread(); 46 void CreateLogFile(); 47 void ProcessLogInfo(std::string& logFile, bool isEmpty); 48 void WriteToEmptyFile(std::ofstream& wStream, std::string& currentTime); 49 void WriteToFile(std::ofstream& wStream, std::string& currentTime); 50 void CompressFile(); 51 bool PrepareWriteDfxLog(); 52 void LoopingThreadEntry(); 53 std::string GetFileNameIndex(const uint32_t index); 54 int32_t GetIntParameter(const std::string& key, const int32_t def, const int32_t minValue); 55 bool GetBoolParameter(const std::string& key, const bool def); 56 void StopThread(); 57 void WidthWatchCallback(const std::string& value); 58 void IntervalWatchCallback(const std::string& value); 59 void EnableWatchCallback(const std::string& value); 60 static void InfoChangedCallback(const char* key, const char* value, void* context); 61 62 std::atomic_uint8_t width_; 63 std::atomic_uint32_t interval_; 64 std::atomic_bool enable_; 65 std::unique_ptr<std::thread> logThread_ {nullptr}; 66 std::mutex mutex_; 67 static std::mutex mutexInstance_; 68 static std::shared_ptr<ThermalDfx> instance_; 69 }; 70 } // V1_1 71 } // Thermal 72 } // HDI 73 } // OHOS 74 #endif // THERMAL_DFX_H 75