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