From ddcf6bb0e05c6a12332f42d16293ca178efcd955 Mon Sep 17 00:00:00 2001 From: wenyang-gitee <13051180828@163.com> Date: Fri, 9 Sep 2022 08:10:07 +0000 Subject: [PATCH] beautify log_display cout Signed-off-by: wenyang-gitee <13051180828@163.com> --- services/hilogtool/log_display.cpp | 83 ++++-------------------------- 1 file changed, 10 insertions(+), 73 deletions(-) diff --git a/services/hilogtool/log_display.cpp b/services/hilogtool/log_display.cpp index 0a773ee..27f4adc 100644 --- a/services/hilogtool/log_display.cpp +++ b/services/hilogtool/log_display.cpp @@ -43,6 +43,7 @@ static constexpr int TP_W = 10; static constexpr int LINES_W = 10; static constexpr int LENGTH_W = 10; static constexpr int DROPPED_W = 10; +static constexpr int STATS_W = 60; static constexpr int FLOAT_PRECSION = 2; static constexpr int MIN2SEC = 60; @@ -141,30 +142,6 @@ static string DurationStr(uint32_t tv_sec, uint32_t tv_nsec) return buffer; } -template -static void SortByLens(vector& v, const T* list, int num) -{ - v.insert(v.begin(), list, list + num); - std::sort(v.begin(), v.end(), [](T& a, T& b) { - return GetTotalLen(a.stats) > GetTotalLen(b.stats); - }); -} - -static void SortDomainList(vector& vd, const DomainStatsRsp* domainList, int num) -{ - SortByLens(vd, domainList, num); -} - -static void SortProcList(vector& vp, const ProcStatsRsp* procList, int num) -{ - SortByLens(vp, procList, num); -} - -static void SortTagList(vector& vt, const TagStatsRsp* tagList, int num) -{ - SortByLens(vt, tagList, num); -} - static void HilogShowDomainStatsInfo(const StatsQueryRsp& rsp) { cout << "Domain Table:" << endl; @@ -178,10 +155,8 @@ static void HilogShowDomainStatsInfo(const StatsQueryRsp& rsp) if (ldStats.dStats == nullptr) { continue; } - vector vd; // sort domain list - SortDomainList(vd, ldStats.dStats, ldStats.domainNum); for (j = 0; j < ldStats.domainNum; j++) { - DomainStatsRsp &dStats = vd[j]; + DomainStatsRsp &dStats = ldStats.dStats[j]; cout << setw(LOGTYPE_W) << LogType2Str(ldStats.type) << colCmd; cout << std::hex << "0x" << setw(DOMAIN_W) << dStats.domain << std::dec << colCmd; cout << setw(TAG_W) << "-" << colCmd; @@ -191,10 +166,8 @@ static void HilogShowDomainStatsInfo(const StatsQueryRsp& rsp) if (dStats.tStats == nullptr) { continue; } - vector vt; // sort tag list - SortTagList(vt, dStats.tStats, dStats.tagNum); for (k = 0; k < dStats.tagNum; k++) { - TagStatsRsp &tStats = vt[k]; + TagStatsRsp &tStats = dStats.tStats[k]; cout << setw(LOGTYPE_W) << LogType2Str(ldStats.type) << colCmd; cout << std::hex << "0x" << setw(DOMAIN_W) << dStats.domain << std::dec << colCmd; cout << setw(TAG_W) << tStats.tag << colCmd; @@ -205,18 +178,6 @@ static void HilogShowDomainStatsInfo(const StatsQueryRsp& rsp) } } -static string GetProcessName(const ProcStatsRsp &pStats) -{ - /* hap process is forked from /system/bin/appspawn, sa process is started by /system/bin/sa_main - the name will be changed after process forking, hilogd holds the original name always, - here we need reconfirm it */ - string name = GetNameByPid(pStats.pid); - if (name == "") { - name = pStats.name; - } - return name.substr(0, PNAME_W - 1); -} - static void HilogShowProcStatsInfo(const StatsQueryRsp& rsp) { cout << "Pid Table:" << endl; @@ -227,14 +188,11 @@ static void HilogShowProcStatsInfo(const StatsQueryRsp& rsp) if (rsp.pStats == nullptr) { return; } - vector vp; // sort process list - SortProcList(vp, rsp.pStats, rsp.procNum); for (i = 0; i < rsp.procNum; i++) { - ProcStatsRsp &pStats = vp[i]; - string name = GetProcessName(pStats); + ProcStatsRsp &pStats = rsp.pStats[i]; cout << setw(LOGTYPE_W) << "-" << colCmd; cout << setw(PID_W) << pStats.pid << colCmd; - cout << setw(PNAME_W) << name << colCmd; + cout << setw(PNAME_W) << pStats.name << colCmd; cout << setw(TAG_W) << "-" << colCmd; PrintStats(pStats.stats); cout << endl; @@ -249,7 +207,7 @@ static void HilogShowProcStatsInfo(const StatsQueryRsp& rsp) } cout << setw(LOGTYPE_W) << LogType2Str(lStats.type) << colCmd; cout << setw(PID_W) << pStats.pid << colCmd; - cout << setw(PNAME_W) << name << colCmd; + cout << setw(PNAME_W) << pStats.name << colCmd; cout << setw(TAG_W) << "-" << colCmd; PrintStats(lStats.stats); cout << endl; @@ -257,13 +215,11 @@ static void HilogShowProcStatsInfo(const StatsQueryRsp& rsp) if (pStats.tStats == nullptr) { continue; } - vector vt; // sort tag list - SortTagList(vt, pStats.tStats, pStats.tagNum); for (j = 0; j < pStats.tagNum; j++) { - TagStatsRsp &tStats = vt[j]; + TagStatsRsp &tStats = pStats.tStats[j]; cout << setw(LOGTYPE_W) << "-" << colCmd; cout << setw(PID_W) << pStats.pid << colCmd; - cout << setw(PNAME_W) << name << colCmd; + cout << setw(PNAME_W) << pStats.name << colCmd; cout << setw(TAG_W) << tStats.tag << colCmd; PrintStats(tStats.stats); cout << endl; @@ -276,28 +232,9 @@ void HilogShowLogStatsInfo(const StatsQueryRsp& rsp) cout << std::left; cout << "Log statistic report (Duration: " << DurationStr(rsp.durationSec, rsp.durationNsec); cout << ", From: " << TimeStr(rsp.tsBeginSec, rsp.tsBeginNsec) << "):" << endl; - uint32_t lines = 0; - uint64_t lens = 0; - for (int i = 0; i < LevelNum; i++) { - lines += rsp.totalLines[i]; - lens += rsp.totalLens[i]; - } - if (lines == 0) { - return; - } - cout << "Total lines: " << lines << ", length: " << Size2Str(lens) << endl; - static const int PERCENT = 100; - for (int i = 0; i < LevelNum; i++) { - string level = LogLevel2Str(static_cast(i + LevelBase)); - cout << level << " lines: " << rsp.totalLines[i]; - cout << "(" << setprecision(FLOAT_PRECSION) << (static_cast(rsp.totalLines[i]*PERCENT)/lines) << "%)"; - cout << ", length: " << Size2Str(rsp.totalLens[i]); - cout << "(" << setprecision(FLOAT_PRECSION) << (static_cast(rsp.totalLens[i]*PERCENT)/lens) << "%)"; - cout<< endl; - } - cout << "---------------------------------------------------------------------" << endl; + cout << setw(STATS_W) << setfill('-') << "-" << endl; HilogShowDomainStatsInfo(rsp); - cout << "---------------------------------------------------------------------" << endl; + cout << setw(STATS_W) << setfill('-') << "-" << endl; HilogShowProcStatsInfo(rsp); } } // namespace HiviewDFX -- Gitee