From ff14e481e8f4d91f0a0c3cdc7b07261c4a081f22 Mon Sep 17 00:00:00 2001 From: Caisking <3449750381@qq.com> Date: Thu, 17 Oct 2024 11:18:03 +0800 Subject: [PATCH] =?utf8?q?=E7=B3=BB=E7=BB=9F=EF=BC=9A=E5=A2=9E=E5=8A=A0?= =?utf8?q?=E7=B3=BB=E7=BB=9F=E7=A9=BA=E9=97=B2=E5=86=85=E5=AD=98=E6=98=BE?= =?utf8?q?=E7=A4=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- .../nativeui/system/include/SystemLogModel.h | 4 +- .../nativeui/system/src/SystemLogModel.cpp | 50 +++++++++++++++++-- .../debug_output_notification.h | 1 + .../debug_output_notification.cpp | 15 +++++- 4 files changed, 65 insertions(+), 5 deletions(-) diff --git a/code/sdk/application/wearable/fd230app/nativeui/system/include/SystemLogModel.h b/code/sdk/application/wearable/fd230app/nativeui/system/include/SystemLogModel.h index 63022c950..73fd26bc8 100644 --- a/code/sdk/application/wearable/fd230app/nativeui/system/include/SystemLogModel.h +++ b/code/sdk/application/wearable/fd230app/nativeui/system/include/SystemLogModel.h @@ -2,7 +2,7 @@ #define SystemLog_MODEL_H #include -#include +#include namespace OHOS { enum class SystemLogType { POWER_ON = 0, // 开机 @@ -34,6 +34,8 @@ public: uint32_t GetCurrentTime(); + std::string get_system_mem_info(); + private: SystemLogModel() = default; ~SystemLogModel() {} diff --git a/code/sdk/application/wearable/fd230app/nativeui/system/src/SystemLogModel.cpp b/code/sdk/application/wearable/fd230app/nativeui/system/src/SystemLogModel.cpp index 3336cdd2e..a78b27cb4 100644 --- a/code/sdk/application/wearable/fd230app/nativeui/system/src/SystemLogModel.cpp +++ b/code/sdk/application/wearable/fd230app/nativeui/system/src/SystemLogModel.cpp @@ -9,14 +9,17 @@ #include #include #include +#include #include -#include +#include #include #include #include #include +#include #include #include +#include #define SYSTEM_LOG_FILE_PATH "/system/system_log.txt" @@ -107,7 +110,7 @@ bool SystemLogModel::loadLog() { // while (logData.size() > 50) { // logData.pop_front(); // 从前端删除最早的日志 // } - + WEARABLE_LOGI(WEARABLE_LOG_MODULE_APP, "Loaded %d logs from file", logData.size()); @@ -116,10 +119,51 @@ bool SystemLogModel::loadLog() { // 获取日志记录,返回deque const std::deque &SystemLogModel::getLogData() const { - return logData; + return logData; } // 获取日志大小 size_t SystemLogModel::getLogSize() const { return logData.size(); } +//获取系统内存 +/* +[FS INFO]total size=12416KB used size=8114KB free size=4302KB +[FS INFO]/update/: +[FS INFO]total size=12416KB used size=552KB free size=11864KB +[FS INFO]/music/: +[FS INFO]total size=12416KB used size=1270KB free size=11146KB +[FS INFO]/user/: +[FS INFO]total size=91264KB used size=64596KB free size=26668KB +*/ +std::string SystemLogModel::get_system_mem_info() { + struct statfs file_buf = {0}; + std::stringstream result; // 使用 stringstream 来构建返回的字符串 + const std::vector volume_names = {"/system/", "/update/", + "/music/", "/user/"}; + + for (const auto &volume : volume_names) { + // 获取文件系统状态 + if (statfs(volume.c_str(), &file_buf) != 0) { + result << "Error retrieving info for " << volume << std::endl; + continue; + } + + // 计算总大小、已用大小和自由空间 + uint64_t total_size = + static_cast(file_buf.f_blocks) * file_buf.f_bsize; + uint64_t free_size = + static_cast(file_buf.f_bfree) * file_buf.f_bsize; + uint64_t used_size = total_size - free_size; + + // 按照所需格式输出 + result << "[" << volume << "]" << std::endl; + result << "Total size=" << total_size / 1024 << "KB " << std::endl; + result << "Used size=" << used_size / 1024 << "KB " << std::endl; + result << "Free size=" << free_size / 1024 << "KB" << std::endl; + } + + // 返回所有分区的自由空间信息,如果没有信息,则返回说明信息 + return result.str().empty() ? "No volumes found or accessible." + : result.str(); +} } // namespace OHOS \ No newline at end of file diff --git a/code/sdk/application/wearable/service/msg_center/include/notify/debug_output_notification/debug_output_notification.h b/code/sdk/application/wearable/service/msg_center/include/notify/debug_output_notification/debug_output_notification.h index 71d0de3d7..7995ef5cb 100644 --- a/code/sdk/application/wearable/service/msg_center/include/notify/debug_output_notification/debug_output_notification.h +++ b/code/sdk/application/wearable/service/msg_center/include/notify/debug_output_notification/debug_output_notification.h @@ -40,6 +40,7 @@ namespace OHOS std::string get_battery_info(void); std::string get_shutdown_info(void); std::string get_sleep_time(void); + std::string get_free_memory(void); private: DebugOutputViewEventListener(); diff --git a/code/sdk/application/wearable/service/msg_center/src/notify/debug_output_notification/debug_output_notification.cpp b/code/sdk/application/wearable/service/msg_center/src/notify/debug_output_notification/debug_output_notification.cpp index 62a3ae994..12a9d4943 100644 --- a/code/sdk/application/wearable/service/msg_center/src/notify/debug_output_notification/debug_output_notification.cpp +++ b/code/sdk/application/wearable/service/msg_center/src/notify/debug_output_notification/debug_output_notification.cpp @@ -88,6 +88,8 @@ namespace OHOS debugOutputItemInfo[index++] = {6, "shutdown_file_list"}; //睡眠数据 debugOutputItemInfo[index++] = {7, "sleep_time"}; + //系统空闲内存 + debugOutputItemInfo[index++] = {8, "system_free_memory"}; usedLen = index; /* -------------------------------- Add data -------------------------------- */ for (uint8_t i = 0; i < usedLen; i++) @@ -313,8 +315,8 @@ result += "advdata:" + (getCurrentWatchadvdata()) + " "; return oss.str(); } + //获取死机列表 static const char *SHUTDOWN_PATH = "/user/tar"; - std::string DebugOutputViewEventListener::get_shutdown_info() { std::ostringstream oss; @@ -353,6 +355,14 @@ result += "advdata:" + (getCurrentWatchadvdata()) + " "; return result; } + //获取系统free内存 + std::string DebugOutputViewEventListener::get_free_memory() + { + auto logModel = SystemLogModel::GetInstance(); + std::string result = logModel->get_system_mem_info(); + return result; + } + bool DebugOutputViewEventListener::OnClick(OHOS::UIView &view, const OHOS::ClickEvent &event) { std::string viewId = view.GetViewId(); @@ -411,6 +421,9 @@ result += "advdata:" + (getCurrentWatchadvdata()) + " "; case 7: str = get_sleep_time(); break; + case 8: + str = get_free_memory(); + break; default: str = "--"; break; -- 2.17.1