123b3eb3cSopenharmony_ci/*
223b3eb3cSopenharmony_ci * Copyright (c) 2021-2022 Huawei Device Co., Ltd.
323b3eb3cSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
423b3eb3cSopenharmony_ci * you may not use this file except in compliance with the License.
523b3eb3cSopenharmony_ci * You may obtain a copy of the License at
623b3eb3cSopenharmony_ci *
723b3eb3cSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
823b3eb3cSopenharmony_ci *
923b3eb3cSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
1023b3eb3cSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
1123b3eb3cSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1223b3eb3cSopenharmony_ci * See the License for the specific language governing permissions and
1323b3eb3cSopenharmony_ci * limitations under the License.
1423b3eb3cSopenharmony_ci */
1523b3eb3cSopenharmony_ci
1623b3eb3cSopenharmony_ci#ifndef FOUNDATION_ACE_FRAMEWORKS_BASE_LOG_DUMP_LOG_H
1723b3eb3cSopenharmony_ci#define FOUNDATION_ACE_FRAMEWORKS_BASE_LOG_DUMP_LOG_H
1823b3eb3cSopenharmony_ci
1923b3eb3cSopenharmony_ci#include <cstdio>
2023b3eb3cSopenharmony_ci#include <memory>
2123b3eb3cSopenharmony_ci#include <sstream>
2223b3eb3cSopenharmony_ci#include <streambuf>
2323b3eb3cSopenharmony_ci#include <string>
2423b3eb3cSopenharmony_ci#include <vector>
2523b3eb3cSopenharmony_ci
2623b3eb3cSopenharmony_ci#include "base/log/log.h"
2723b3eb3cSopenharmony_ci#include "base/utils/noncopyable.h"
2823b3eb3cSopenharmony_ci#include "base/utils/singleton.h"
2923b3eb3cSopenharmony_ci
3023b3eb3cSopenharmony_cinamespace OHOS::Ace {
3123b3eb3cSopenharmony_ci
3223b3eb3cSopenharmony_ciclass ACE_FORCE_EXPORT DumpLog : public Singleton<DumpLog> {
3323b3eb3cSopenharmony_ci    DECLARE_SINGLETON(DumpLog);
3423b3eb3cSopenharmony_ci
3523b3eb3cSopenharmony_cipublic:
3623b3eb3cSopenharmony_ci    class DumpFileBuf : public std::streambuf {
3723b3eb3cSopenharmony_ci    public:
3823b3eb3cSopenharmony_ci        DumpFileBuf(FILE* file) : file_(file) {}
3923b3eb3cSopenharmony_ci        ~DumpFileBuf()
4023b3eb3cSopenharmony_ci        {
4123b3eb3cSopenharmony_ci            if (file_ && fclose(file_)) {
4223b3eb3cSopenharmony_ci                LOGE("DumpFileBuf close file failed!");
4323b3eb3cSopenharmony_ci            }
4423b3eb3cSopenharmony_ci        }
4523b3eb3cSopenharmony_ci
4623b3eb3cSopenharmony_ci        int_type overflow(int_type c) override
4723b3eb3cSopenharmony_ci        {
4823b3eb3cSopenharmony_ci            if (c != EOF) {
4923b3eb3cSopenharmony_ci                if (fwrite(&c, 1, 1, file_) != 1) {
5023b3eb3cSopenharmony_ci                    return EOF;
5123b3eb3cSopenharmony_ci                }
5223b3eb3cSopenharmony_ci            }
5323b3eb3cSopenharmony_ci            return c;
5423b3eb3cSopenharmony_ci        }
5523b3eb3cSopenharmony_ci
5623b3eb3cSopenharmony_ci        std::streamsize xsputn(const char* str, std::streamsize num) override
5723b3eb3cSopenharmony_ci        {
5823b3eb3cSopenharmony_ci            return fwrite(str, 1, num, file_);
5923b3eb3cSopenharmony_ci        }
6023b3eb3cSopenharmony_ci
6123b3eb3cSopenharmony_ci    private:
6223b3eb3cSopenharmony_ci        FILE* file_;
6323b3eb3cSopenharmony_ci    };
6423b3eb3cSopenharmony_ci
6523b3eb3cSopenharmony_ci    class DumpFile : public std::ostream {
6623b3eb3cSopenharmony_ci    public:
6723b3eb3cSopenharmony_ci        DumpFile(FILE* file) : std::ostream(0), buf_(file)
6823b3eb3cSopenharmony_ci        {
6923b3eb3cSopenharmony_ci            rdbuf(&buf_);
7023b3eb3cSopenharmony_ci        }
7123b3eb3cSopenharmony_ci    protected:
7223b3eb3cSopenharmony_ci        DumpFileBuf buf_;
7323b3eb3cSopenharmony_ci    };
7423b3eb3cSopenharmony_ci
7523b3eb3cSopenharmony_ci    void SetDumpFile(DumpFile* file)
7623b3eb3cSopenharmony_ci    {
7723b3eb3cSopenharmony_ci        ostream_.reset(file);
7823b3eb3cSopenharmony_ci    }
7923b3eb3cSopenharmony_ci
8023b3eb3cSopenharmony_ci    void SetDumpFile(std::unique_ptr<std::ostream> file)
8123b3eb3cSopenharmony_ci    {
8223b3eb3cSopenharmony_ci        ostream_ = std::move(file);
8323b3eb3cSopenharmony_ci    }
8423b3eb3cSopenharmony_ci
8523b3eb3cSopenharmony_ci    void SetSeparator(std::string separator)
8623b3eb3cSopenharmony_ci    {
8723b3eb3cSopenharmony_ci        separator_ = separator;
8823b3eb3cSopenharmony_ci    }
8923b3eb3cSopenharmony_ci
9023b3eb3cSopenharmony_ci    const std::unique_ptr<std::ostream>& GetDumpFile() const
9123b3eb3cSopenharmony_ci    {
9223b3eb3cSopenharmony_ci        return ostream_;
9323b3eb3cSopenharmony_ci    }
9423b3eb3cSopenharmony_ci
9523b3eb3cSopenharmony_ci    void Print(int32_t depth, const std::string& className, int32_t childSize);
9623b3eb3cSopenharmony_ci    void Print(const std::string& content);
9723b3eb3cSopenharmony_ci    void Print(int32_t depth, const std::string& content);
9823b3eb3cSopenharmony_ci    void Append(int32_t depth, const std::string& className, int32_t childSize);
9923b3eb3cSopenharmony_ci    bool OutPutBySize();
10023b3eb3cSopenharmony_ci    void OutPutDefault();
10123b3eb3cSopenharmony_ci    void Reset();
10223b3eb3cSopenharmony_ci    void PrintJson(const std::string& content);
10323b3eb3cSopenharmony_ci    void PrintEndDumpInfoNG(bool isElement = false);
10423b3eb3cSopenharmony_ci    std::string GetPrefix(int32_t depth);
10523b3eb3cSopenharmony_ci    std::string FormatDumpInfo(const std::string& str, int32_t depth);
10623b3eb3cSopenharmony_ci    void Append(const std::string& content)
10723b3eb3cSopenharmony_ci    {
10823b3eb3cSopenharmony_ci        result_.append(content);
10923b3eb3cSopenharmony_ci    }
11023b3eb3cSopenharmony_ci
11123b3eb3cSopenharmony_ci    int32_t GetDepth() const
11223b3eb3cSopenharmony_ci    {
11323b3eb3cSopenharmony_ci        return depth_;
11423b3eb3cSopenharmony_ci    }
11523b3eb3cSopenharmony_ci
11623b3eb3cSopenharmony_ci    void SetDepth(int32_t depth)
11723b3eb3cSopenharmony_ci    {
11823b3eb3cSopenharmony_ci        depth_ = depth;
11923b3eb3cSopenharmony_ci    }
12023b3eb3cSopenharmony_ci
12123b3eb3cSopenharmony_ci    template<typename T>
12223b3eb3cSopenharmony_ci    void AddDesc(const T&& t)
12323b3eb3cSopenharmony_ci    {
12423b3eb3cSopenharmony_ci        std::stringstream paramStream;
12523b3eb3cSopenharmony_ci        paramStream << t << "\n";
12623b3eb3cSopenharmony_ci        description_.push_back(paramStream.str());
12723b3eb3cSopenharmony_ci    }
12823b3eb3cSopenharmony_ci
12923b3eb3cSopenharmony_ci    template<typename T>
13023b3eb3cSopenharmony_ci    void AddDesc(const T& t)
13123b3eb3cSopenharmony_ci    {
13223b3eb3cSopenharmony_ci        std::stringstream paramStream;
13323b3eb3cSopenharmony_ci        paramStream << t << "\n";
13423b3eb3cSopenharmony_ci        description_.push_back(paramStream.str());
13523b3eb3cSopenharmony_ci    }
13623b3eb3cSopenharmony_ci
13723b3eb3cSopenharmony_ci    template<typename... Args>
13823b3eb3cSopenharmony_ci    void AddDesc(Args&&... args)
13923b3eb3cSopenharmony_ci    {
14023b3eb3cSopenharmony_ci        std::stringstream paramStream;
14123b3eb3cSopenharmony_ci        BuildDesc(paramStream, args...);
14223b3eb3cSopenharmony_ci    }
14323b3eb3cSopenharmony_ci
14423b3eb3cSopenharmony_ci    template<typename T, typename... Args>
14523b3eb3cSopenharmony_ci    void BuildDesc(std::stringstream& stream, T& t, Args&&... args)
14623b3eb3cSopenharmony_ci    {
14723b3eb3cSopenharmony_ci        stream << t << " ";
14823b3eb3cSopenharmony_ci        BuildDesc(stream, args...);
14923b3eb3cSopenharmony_ci    }
15023b3eb3cSopenharmony_ci
15123b3eb3cSopenharmony_ci    template<typename T>
15223b3eb3cSopenharmony_ci    void BuildDesc(std::stringstream& stream, T& t)
15323b3eb3cSopenharmony_ci    {
15423b3eb3cSopenharmony_ci        stream << t << "\n";
15523b3eb3cSopenharmony_ci        description_.push_back(stream.str());
15623b3eb3cSopenharmony_ci    }
15723b3eb3cSopenharmony_ci
15823b3eb3cSopenharmony_ci    static void ShowDumpHelp(std::vector<std::string>& info);
15923b3eb3cSopenharmony_ci
16023b3eb3cSopenharmony_ci    static const size_t MAX_DUMP_LENGTH = 100000;
16123b3eb3cSopenharmony_ci    static const size_t MIN_JSON_LENGTH = 4;
16223b3eb3cSopenharmony_ci    static const size_t END_POS_TWO = 2;
16323b3eb3cSopenharmony_ci    static const size_t END_POS_THREE = 3;
16423b3eb3cSopenharmony_ci
16523b3eb3cSopenharmony_ciprivate:
16623b3eb3cSopenharmony_ci    std::vector<std::string> description_;
16723b3eb3cSopenharmony_ci    std::unique_ptr<std::ostream> ostream_ { nullptr };
16823b3eb3cSopenharmony_ci    std::string result_;
16923b3eb3cSopenharmony_ci    std::string separator_ = "\n";
17023b3eb3cSopenharmony_ci    int32_t depth_ = -1;
17123b3eb3cSopenharmony_ci    ACE_DISALLOW_MOVE(DumpLog);
17223b3eb3cSopenharmony_ci};
17323b3eb3cSopenharmony_ci
17423b3eb3cSopenharmony_ci} // namespace OHOS::Ace
17523b3eb3cSopenharmony_ci
17623b3eb3cSopenharmony_ci#endif // FOUNDATION_ACE_FRAMEWORKS_BASE_LOG_DUMP_LOG_H
177