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#include "base/log/dump_log.h" 1723b3eb3cSopenharmony_ci 1823b3eb3cSopenharmony_ci#include <fstream> 1923b3eb3cSopenharmony_ci#include "core/common/ace_application_info.h" 2023b3eb3cSopenharmony_ci 2123b3eb3cSopenharmony_cinamespace OHOS::Ace { 2223b3eb3cSopenharmony_ci 2323b3eb3cSopenharmony_ciDumpLog::DumpLog() = default; 2423b3eb3cSopenharmony_ciDumpLog::~DumpLog() = default; 2523b3eb3cSopenharmony_ci 2623b3eb3cSopenharmony_civoid DumpLog::Print(int32_t depth, const std::string& className, int32_t childSize) 2723b3eb3cSopenharmony_ci{ 2823b3eb3cSopenharmony_ci if (!ostream_ || !ostream_->good()) { 2923b3eb3cSopenharmony_ci return; 3023b3eb3cSopenharmony_ci } 3123b3eb3cSopenharmony_ci std::string space = " "; 3223b3eb3cSopenharmony_ci for (int32_t i = 0; i < depth; ++i) { 3323b3eb3cSopenharmony_ci ostream_->write(space.c_str(), space.length()); 3423b3eb3cSopenharmony_ci } 3523b3eb3cSopenharmony_ci ostream_->write(space.c_str(), space.length()); 3623b3eb3cSopenharmony_ci std::string data = "|-> "; 3723b3eb3cSopenharmony_ci data.append(className); 3823b3eb3cSopenharmony_ci data.append(" childSize:" + std::to_string(childSize)); 3923b3eb3cSopenharmony_ci data.append("\n"); 4023b3eb3cSopenharmony_ci ostream_->write(data.c_str(), data.length()); 4123b3eb3cSopenharmony_ci for (auto& desc : description_) { 4223b3eb3cSopenharmony_ci for (int32_t i = 0; i < depth; ++i) { 4323b3eb3cSopenharmony_ci ostream_->write(space.c_str(), space.length()); 4423b3eb3cSopenharmony_ci } 4523b3eb3cSopenharmony_ci std::string data = ""; 4623b3eb3cSopenharmony_ci if (childSize == 0) { 4723b3eb3cSopenharmony_ci data = " "; 4823b3eb3cSopenharmony_ci } else { 4923b3eb3cSopenharmony_ci data = " | "; 5023b3eb3cSopenharmony_ci } 5123b3eb3cSopenharmony_ci data.append(desc); 5223b3eb3cSopenharmony_ci ostream_->write(data.c_str(), data.length()); 5323b3eb3cSopenharmony_ci } 5423b3eb3cSopenharmony_ci ostream_->flush(); 5523b3eb3cSopenharmony_ci description_.clear(); 5623b3eb3cSopenharmony_ci description_.shrink_to_fit(); 5723b3eb3cSopenharmony_ci} 5823b3eb3cSopenharmony_ci 5923b3eb3cSopenharmony_civoid DumpLog::Print(const std::string& content) 6023b3eb3cSopenharmony_ci{ 6123b3eb3cSopenharmony_ci Print(0, content); 6223b3eb3cSopenharmony_ci} 6323b3eb3cSopenharmony_ci 6423b3eb3cSopenharmony_civoid DumpLog::Print(int32_t depth, const std::string& content) 6523b3eb3cSopenharmony_ci{ 6623b3eb3cSopenharmony_ci std::string space = " "; 6723b3eb3cSopenharmony_ci for (int32_t i = 0; i < depth; ++i) { 6823b3eb3cSopenharmony_ci ostream_->write(space.c_str(), space.length()); 6923b3eb3cSopenharmony_ci } 7023b3eb3cSopenharmony_ci std::string data = content + separator_; 7123b3eb3cSopenharmony_ci ostream_->write(data.c_str(), data.length()); 7223b3eb3cSopenharmony_ci} 7323b3eb3cSopenharmony_ci 7423b3eb3cSopenharmony_civoid DumpLog::Reset() 7523b3eb3cSopenharmony_ci{ 7623b3eb3cSopenharmony_ci ostream_.reset(); 7723b3eb3cSopenharmony_ci} 7823b3eb3cSopenharmony_ci 7923b3eb3cSopenharmony_civoid DumpLog::ShowDumpHelp(std::vector<std::string>& info) 8023b3eb3cSopenharmony_ci{ 8123b3eb3cSopenharmony_ci info.emplace_back(" -element |show element tree"); 8223b3eb3cSopenharmony_ci info.emplace_back(" -render |show render tree"); 8323b3eb3cSopenharmony_ci info.emplace_back(" -inspector |show inspector tree"); 8423b3eb3cSopenharmony_ci info.emplace_back(" -frontend |show path and components count of current page"); 8523b3eb3cSopenharmony_ci info.emplace_back(" -navigation |show navigation path stack"); 8623b3eb3cSopenharmony_ci} 8723b3eb3cSopenharmony_ci 8823b3eb3cSopenharmony_civoid DumpLog::Append(int32_t depth, const std::string& className, int32_t childSize) 8923b3eb3cSopenharmony_ci{ 9023b3eb3cSopenharmony_ci for (int32_t i = 0; i < depth; ++i) { 9123b3eb3cSopenharmony_ci result_.append(" "); 9223b3eb3cSopenharmony_ci } 9323b3eb3cSopenharmony_ci result_.append("|-> "); 9423b3eb3cSopenharmony_ci result_.append(className); 9523b3eb3cSopenharmony_ci result_.append(" childSize:" + std::to_string(childSize)); 9623b3eb3cSopenharmony_ci result_.append("\n"); 9723b3eb3cSopenharmony_ci for (auto& desc : description_) { 9823b3eb3cSopenharmony_ci for (int32_t i = 0; i < depth; ++i) { 9923b3eb3cSopenharmony_ci result_.append(" "); 10023b3eb3cSopenharmony_ci } 10123b3eb3cSopenharmony_ci if (childSize == 0) { 10223b3eb3cSopenharmony_ci result_.append(" "); 10323b3eb3cSopenharmony_ci } else { 10423b3eb3cSopenharmony_ci result_.append(" | "); 10523b3eb3cSopenharmony_ci } 10623b3eb3cSopenharmony_ci result_.append(desc); 10723b3eb3cSopenharmony_ci } 10823b3eb3cSopenharmony_ci description_.clear(); 10923b3eb3cSopenharmony_ci description_.shrink_to_fit(); 11023b3eb3cSopenharmony_ci} 11123b3eb3cSopenharmony_ci 11223b3eb3cSopenharmony_cibool DumpLog::OutPutBySize() 11323b3eb3cSopenharmony_ci{ 11423b3eb3cSopenharmony_ci if (!ostream_->good()) { 11523b3eb3cSopenharmony_ci result_.clear(); 11623b3eb3cSopenharmony_ci return false; 11723b3eb3cSopenharmony_ci } 11823b3eb3cSopenharmony_ci // if current result size > max size, dump will output as file 11923b3eb3cSopenharmony_ci if (result_.size() + 1 > DumpLog::MAX_DUMP_LENGTH) { 12023b3eb3cSopenharmony_ci auto dumpFilePath = AceApplicationInfo::GetInstance().GetDataFileDirPath() + "/arkui.dump"; 12123b3eb3cSopenharmony_ci std::unique_ptr<std::ostream> ostream = std::make_unique<std::ofstream>(dumpFilePath); 12223b3eb3cSopenharmony_ci if (!ostream) { 12323b3eb3cSopenharmony_ci result_.clear(); 12423b3eb3cSopenharmony_ci result_.append("Dump output failed,please try again"); 12523b3eb3cSopenharmony_ci ostream_->write(result_.c_str(), result_.length()); 12623b3eb3cSopenharmony_ci result_.clear(); 12723b3eb3cSopenharmony_ci } 12823b3eb3cSopenharmony_ci CHECK_NULL_RETURN(ostream, false); 12923b3eb3cSopenharmony_ci DumpLog::GetInstance().SetDumpFile(std::move(ostream)); 13023b3eb3cSopenharmony_ci } 13123b3eb3cSopenharmony_ci ostream_->write(result_.c_str(), result_.length()); 13223b3eb3cSopenharmony_ci result_.clear(); 13323b3eb3cSopenharmony_ci ostream_->flush(); 13423b3eb3cSopenharmony_ci return true; 13523b3eb3cSopenharmony_ci} 13623b3eb3cSopenharmony_ci 13723b3eb3cSopenharmony_civoid DumpLog::OutPutDefault() 13823b3eb3cSopenharmony_ci{ 13923b3eb3cSopenharmony_ci if (!ostream_ || !ostream_->good()) { 14023b3eb3cSopenharmony_ci result_.clear(); 14123b3eb3cSopenharmony_ci return; 14223b3eb3cSopenharmony_ci } 14323b3eb3cSopenharmony_ci ostream_->write(result_.c_str(), result_.length()); 14423b3eb3cSopenharmony_ci result_.clear(); 14523b3eb3cSopenharmony_ci ostream_->flush(); 14623b3eb3cSopenharmony_ci} 14723b3eb3cSopenharmony_ci 14823b3eb3cSopenharmony_civoid DumpLog::PrintJson(const std::string& content) 14923b3eb3cSopenharmony_ci{ 15023b3eb3cSopenharmony_ci if (!ostream_->good()) { 15123b3eb3cSopenharmony_ci return; 15223b3eb3cSopenharmony_ci } 15323b3eb3cSopenharmony_ci ostream_->write(content.c_str(), content.length()); 15423b3eb3cSopenharmony_ci ostream_->flush(); 15523b3eb3cSopenharmony_ci} 15623b3eb3cSopenharmony_ci 15723b3eb3cSopenharmony_civoid DumpLog::PrintEndDumpInfoNG(bool isElement) 15823b3eb3cSopenharmony_ci{ 15923b3eb3cSopenharmony_ci int32_t depth = GetDepth() + DumpLog::END_POS_TWO; 16023b3eb3cSopenharmony_ci std::string result; 16123b3eb3cSopenharmony_ci for (int32_t i = 0; i < depth; ++i) { 16223b3eb3cSopenharmony_ci result.append("}"); 16323b3eb3cSopenharmony_ci } 16423b3eb3cSopenharmony_ci if (isElement) { 16523b3eb3cSopenharmony_ci Append(result); 16623b3eb3cSopenharmony_ci } else { 16723b3eb3cSopenharmony_ci PrintJson(result); 16823b3eb3cSopenharmony_ci } 16923b3eb3cSopenharmony_ci} 17023b3eb3cSopenharmony_ci 17123b3eb3cSopenharmony_cistd::string DumpLog::GetPrefix(int32_t depth) 17223b3eb3cSopenharmony_ci{ 17323b3eb3cSopenharmony_ci std::string prefix = ""; 17423b3eb3cSopenharmony_ci if (depth > 0) { 17523b3eb3cSopenharmony_ci int32_t lastDepth = GetDepth(); 17623b3eb3cSopenharmony_ci if (depth == lastDepth) { 17723b3eb3cSopenharmony_ci prefix.append("},"); 17823b3eb3cSopenharmony_ci } else if (depth > lastDepth) { 17923b3eb3cSopenharmony_ci prefix = ","; 18023b3eb3cSopenharmony_ci } else { 18123b3eb3cSopenharmony_ci int32_t diff = lastDepth - depth + 1; 18223b3eb3cSopenharmony_ci prefix.assign(diff, '}').append(","); 18323b3eb3cSopenharmony_ci } 18423b3eb3cSopenharmony_ci } 18523b3eb3cSopenharmony_ci SetDepth(depth); 18623b3eb3cSopenharmony_ci return prefix; 18723b3eb3cSopenharmony_ci} 18823b3eb3cSopenharmony_ci 18923b3eb3cSopenharmony_cistd::string DumpLog::FormatDumpInfo(const std::string& str, int32_t depth) 19023b3eb3cSopenharmony_ci{ 19123b3eb3cSopenharmony_ci if (str.length() > DumpLog::MIN_JSON_LENGTH) { 19223b3eb3cSopenharmony_ci if (depth == 0) { 19323b3eb3cSopenharmony_ci return str.substr(0, str.length() - DumpLog::END_POS_TWO); 19423b3eb3cSopenharmony_ci } 19523b3eb3cSopenharmony_ci return str.substr(1, str.length() - DumpLog::END_POS_THREE); 19623b3eb3cSopenharmony_ci } 19723b3eb3cSopenharmony_ci return str; 19823b3eb3cSopenharmony_ci} 19923b3eb3cSopenharmony_ci} // namespace OHOS::Ace 200