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 SetSeparator(std::string separator)85 void SetSeparator(std::string separator) 86 { 87 separator_ = separator; 88 } 89 GetDumpFile()90 const std::unique_ptr<std::ostream>& GetDumpFile() const 91 { 92 return ostream_; 93 } 94 95 void Print(int32_t depth, const std::string& className, int32_t childSize); 96 void Print(const std::string& content); 97 void Print(int32_t depth, const std::string& content); 98 void Append(int32_t depth, const std::string& className, int32_t childSize); 99 bool OutPutBySize(); 100 void OutPutDefault(); 101 void Reset(); 102 103 template<typename T> AddDesc(const T && t)104 void AddDesc(const T&& t) 105 { 106 std::stringstream paramStream; 107 paramStream << t << "\n"; 108 description_.push_back(paramStream.str()); 109 } 110 111 template<typename T> AddDesc(const T & t)112 void AddDesc(const T& t) 113 { 114 std::stringstream paramStream; 115 paramStream << t << "\n"; 116 description_.push_back(paramStream.str()); 117 } 118 119 template<typename... Args> AddDesc(Args &&...args)120 void AddDesc(Args&&... args) 121 { 122 std::stringstream paramStream; 123 BuildDesc(paramStream, args...); 124 } 125 126 template<typename T, typename... Args> BuildDesc(std::stringstream & stream,T & t,Args &&...args)127 void BuildDesc(std::stringstream& stream, T& t, Args&&... args) 128 { 129 stream << t << " "; 130 BuildDesc(stream, args...); 131 } 132 133 template<typename T> BuildDesc(std::stringstream & stream,T & t)134 void BuildDesc(std::stringstream& stream, T& t) 135 { 136 stream << t << "\n"; 137 description_.push_back(stream.str()); 138 } 139 140 static void ShowDumpHelp(std::vector<std::string>& info); 141 142 static const size_t MAX_DUMP_LENGTH = 100000; 143 144 private: 145 std::vector<std::string> description_; 146 std::unique_ptr<std::ostream> ostream_ { nullptr }; 147 std::string result_; 148 std::string separator_ = "\n"; 149 ACE_DISALLOW_MOVE(DumpLog); 150 }; 151 152 } // namespace OHOS::Ace 153 154 #endif // FOUNDATION_ACE_FRAMEWORKS_BASE_LOG_DUMP_LOG_H 155