1 /* 2 * Copyright (c) 2021-2022 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 <streambuf> 23 #include <string> 24 #include <vector> 25 26 #include "base/log/log.h" 27 #include "base/utils/noncopyable.h" 28 #include "base/utils/singleton.h" 29 30 namespace OHOS::Ace { 31 32 class ACE_FORCE_EXPORT DumpLog : public Singleton<DumpLog> { 33 DECLARE_SINGLETON(DumpLog); 34 35 public: 36 class DumpFileBuf : public std::streambuf { 37 public: DumpFileBuf(FILE * file)38 DumpFileBuf(FILE* file) : file_(file) {} ~DumpFileBuf()39 ~DumpFileBuf() 40 { 41 if (file_ && fclose(file_)) { 42 LOGE("DumpFileBuf close file failed!"); 43 } 44 } 45 overflow(int_type c)46 int_type overflow(int_type c) override 47 { 48 if (c != EOF) { 49 if (fwrite(&c, 1, 1, file_) != 1) { 50 return EOF; 51 } 52 } 53 return c; 54 } 55 xsputn(const char * str,std::streamsize num)56 std::streamsize xsputn(const char* str, std::streamsize num) override 57 { 58 return fwrite(str, 1, num, file_); 59 } 60 61 private: 62 FILE* file_; 63 }; 64 65 class DumpFile : public std::ostream { 66 public: DumpFile(FILE * file)67 DumpFile(FILE* file) : std::ostream(0), buf_(file) 68 { 69 rdbuf(&buf_); 70 } 71 protected: 72 DumpFileBuf buf_; 73 }; 74 SetDumpFile(DumpFile * file)75 void SetDumpFile(DumpFile* file) 76 { 77 ostream_.reset(file); 78 } 79 SetDumpFile(std::unique_ptr<std::ostream> file)80 void SetDumpFile(std::unique_ptr<std::ostream> file) 81 { 82 ostream_ = std::move(file); 83 } 84 GetDumpFile()85 const std::unique_ptr<std::ostream>& GetDumpFile() const 86 { 87 return ostream_; 88 } 89 90 void Print(int32_t depth, const std::string& className, int32_t childSize); 91 void Print(const std::string& content); 92 void Print(int32_t depth, const std::string& content); 93 void Append(int32_t depth, const std::string& className, int32_t childSize); 94 bool OutPutBySize(); 95 void OutPutDefault(); 96 void Reset(); 97 98 template<typename T> AddDesc(const T && t)99 void AddDesc(const T&& t) 100 { 101 std::stringstream paramStream; 102 paramStream << t << "\n"; 103 description_.push_back(paramStream.str()); 104 } 105 106 template<typename T> AddDesc(const T & t)107 void AddDesc(const T& t) 108 { 109 std::stringstream paramStream; 110 paramStream << t << "\n"; 111 description_.push_back(paramStream.str()); 112 } 113 114 template<typename... Args> AddDesc(Args &&...args)115 void AddDesc(Args&&... args) 116 { 117 std::stringstream paramStream; 118 BuildDesc(paramStream, args...); 119 } 120 121 template<typename T, typename... Args> BuildDesc(std::stringstream & stream,T & t,Args &&...args)122 void BuildDesc(std::stringstream& stream, T& t, Args&&... args) 123 { 124 stream << t << " "; 125 BuildDesc(stream, args...); 126 } 127 128 template<typename T> BuildDesc(std::stringstream & stream,T & t)129 void BuildDesc(std::stringstream& stream, T& t) 130 { 131 stream << t << "\n"; 132 description_.push_back(stream.str()); 133 } 134 135 static void ShowDumpHelp(std::vector<std::string>& info); 136 137 static const size_t MAX_DUMP_LENGTH = 100000; 138 139 private: 140 std::vector<std::string> description_; 141 std::unique_ptr<std::ostream> ostream_ { nullptr }; 142 std::string result_; 143 ACE_DISALLOW_MOVE(DumpLog); 144 }; 145 146 } // namespace OHOS::Ace 147 148 #endif // FOUNDATION_ACE_FRAMEWORKS_BASE_LOG_DUMP_LOG_H 149