1 /* 2 * Copyright (c) 2021 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 FOUNDATION_ACE_FRAMEWORKS_BASE_LOG_DUMP_LOG_H 17 #define FOUNDATION_ACE_FRAMEWORKS_BASE_LOG_DUMP_LOG_H 18 19 #include <cstdio> 20 #include <memory> 21 #include <sstream> 22 #include <vector> 23 24 #include "base/utils/noncopyable.h" 25 #include "base/utils/singleton.h" 26 27 namespace OHOS::Ace { 28 29 class DumpLog : public Singleton<DumpLog> { 30 DECLARE_SINGLETON(DumpLog); 31 32 public: 33 using DumpFile = std::unique_ptr<FILE, decltype(&fclose)>; 34 SetDumpFile(DumpFile && file)35 void SetDumpFile(DumpFile&& file) 36 { 37 dumpFile_ = std::move(file); 38 } GetDumpFile()39 const DumpFile& GetDumpFile() const 40 { 41 return dumpFile_; 42 } 43 44 void Print(int32_t depth, const std::string& className, int32_t childSize); 45 void Print(const std::string& content); 46 void Print(int32_t depth, const std::string& content); 47 48 void PrintToString(int32_t depth, const std::string& className, int32_t childSize, std::vector<std::string>& info); 49 void PrintToString(const std::string& content, std::vector<std::string>& info); 50 void PrintToString(int32_t depth, const std::string& content, std::vector<std::string>& info); 51 52 void Reset(); 53 54 template<typename T> AddDesc(const T && t)55 void AddDesc(const T&& t) 56 { 57 std::stringstream paramStream; 58 paramStream << t << "\n"; 59 description_.push_back(paramStream.str()); 60 } 61 62 template<typename T> AddDesc(const T & t)63 void AddDesc(const T& t) 64 { 65 std::stringstream paramStream; 66 paramStream << t << "\n"; 67 description_.push_back(paramStream.str()); 68 } 69 70 template<typename... Args> AddDesc(Args &&...args)71 void AddDesc(Args&&... args) 72 { 73 std::stringstream paramStream; 74 BuildDesc(paramStream, args...); 75 } 76 77 template<typename T, typename... Args> BuildDesc(std::stringstream & stream,T & t,Args &&...args)78 void BuildDesc(std::stringstream& stream, T& t, Args&&... args) 79 { 80 stream << t << " "; 81 BuildDesc(stream, args...); 82 } 83 84 template<typename T> BuildDesc(std::stringstream & stream,T & t)85 void BuildDesc(std::stringstream& stream, T& t) 86 { 87 stream << t << "\n"; 88 description_.push_back(stream.str()); 89 } 90 91 private: 92 DumpFile dumpFile_ { nullptr, &fclose }; 93 std::vector<std::string> description_; 94 95 ACE_DISALLOW_MOVE(DumpLog); 96 }; 97 98 } // namespace OHOS::Ace 99 100 #endif // FOUNDATION_ACE_FRAMEWORKS_BASE_LOG_DUMP_LOG_H 101