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 
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 
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() const90     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     void PrintJson(const std::string& content);
103     void PrintEndDumpInfoNG(bool isElement = false);
104     std::string GetPrefix(int32_t depth);
105     std::string FormatDumpInfo(const std::string& str, int32_t depth);
Append(const std::string& content)106     void Append(const std::string& content)
107     {
108         result_.append(content);
109     }
110 
GetDepth() const111     int32_t GetDepth() const
112     {
113         return depth_;
114     }
115 
SetDepth(int32_t depth)116     void SetDepth(int32_t depth)
117     {
118         depth_ = depth;
119     }
120 
121     template<typename T>
AddDesc(const T&& t)122     void AddDesc(const T&& t)
123     {
124         std::stringstream paramStream;
125         paramStream << t << "\n";
126         description_.push_back(paramStream.str());
127     }
128 
129     template<typename T>
AddDesc(const T& t)130     void AddDesc(const T& t)
131     {
132         std::stringstream paramStream;
133         paramStream << t << "\n";
134         description_.push_back(paramStream.str());
135     }
136 
137     template<typename... Args>
AddDesc(Args&&.... args)138     void AddDesc(Args&&... args)
139     {
140         std::stringstream paramStream;
141         BuildDesc(paramStream, args...);
142     }
143 
144     template<typename T, typename... Args>
BuildDesc(std::stringstream& stream, T& t, Args&&... args)145     void BuildDesc(std::stringstream& stream, T& t, Args&&... args)
146     {
147         stream << t << " ";
148         BuildDesc(stream, args...);
149     }
150 
151     template<typename T>
BuildDesc(std::stringstream& stream, T& t)152     void BuildDesc(std::stringstream& stream, T& t)
153     {
154         stream << t << "\n";
155         description_.push_back(stream.str());
156     }
157 
158     static void ShowDumpHelp(std::vector<std::string>& info);
159 
160     static const size_t MAX_DUMP_LENGTH = 100000;
161     static const size_t MIN_JSON_LENGTH = 4;
162     static const size_t END_POS_TWO = 2;
163     static const size_t END_POS_THREE = 3;
164 
165 private:
166     std::vector<std::string> description_;
167     std::unique_ptr<std::ostream> ostream_ { nullptr };
168     std::string result_;
169     std::string separator_ = "\n";
170     int32_t depth_ = -1;
171     ACE_DISALLOW_MOVE(DumpLog);
172 };
173 
174 } // namespace OHOS::Ace
175 
176 #endif // FOUNDATION_ACE_FRAMEWORKS_BASE_LOG_DUMP_LOG_H
177