From 2b93362d27f61068354e6ba07408d32d4f8e3dfd Mon Sep 17 00:00:00 2001 From: Caisking <3449750381@qq.com> Date: Fri, 4 Oct 2024 18:01:28 +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=E5=BC=80=E5=85=B3=E6=9C=BA=E6=97=A5=E5=BF=97?= =?utf8?q?=E8=AE=B0=E5=BD=95=E6=9C=8D=E5=8A=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- .../wearable/fd230app/CMakeLists.txt | 3 +- .../nativelauncher/src/NativeLauncher.cpp | 10 ++ .../nativeui/system/include/SystemLogModel.h | 51 ++++++++ .../nativeui/system/include/SystemLogModelC.h | 21 ++++ .../nativeui/system/src/SystemLogModel.cpp | 113 ++++++++++++++++++ .../nativeui/system/src/SystemPresenter.cpp | 1 + .../drivers/peripheral/utils/custom_utils.c | 18 ++- 7 files changed, 215 insertions(+), 2 deletions(-) create mode 100644 code/sdk/application/wearable/fd230app/nativeui/system/include/SystemLogModel.h create mode 100644 code/sdk/application/wearable/fd230app/nativeui/system/include/SystemLogModelC.h create mode 100644 code/sdk/application/wearable/fd230app/nativeui/system/src/SystemLogModel.cpp diff --git a/code/sdk/application/wearable/fd230app/CMakeLists.txt b/code/sdk/application/wearable/fd230app/CMakeLists.txt index e84083a4e..7ab720879 100644 --- a/code/sdk/application/wearable/fd230app/CMakeLists.txt +++ b/code/sdk/application/wearable/fd230app/CMakeLists.txt @@ -389,7 +389,8 @@ set(SOURCES #系统 ${CMAKE_CURRENT_SOURCE_DIR}/nativeui/system/src/SystemPresenter.cpp ${CMAKE_CURRENT_SOURCE_DIR}/nativeui/system/src/SystemView.cpp - ${CMAKE_CURRENT_SOURCE_DIR}/nativeui/system/src/SystemModel.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/nativeui/system/src/SystemModel.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/nativeui/system/src/SystemLogModel.cpp ${CMAKE_CURRENT_SOURCE_DIR}/nativeui/system/src/SystemAboutTheWatchView.cpp ${CMAKE_CURRENT_SOURCE_DIR}/nativeui/system/src/SystemSuperviseView.cpp ${CMAKE_CURRENT_SOURCE_DIR}/nativeui/system/src/Systemupgrade.cpp diff --git a/code/sdk/application/wearable/fd230app/nativelauncher/src/NativeLauncher.cpp b/code/sdk/application/wearable/fd230app/nativelauncher/src/NativeLauncher.cpp index 0607ad0bd..2a37a5fdc 100644 --- a/code/sdk/application/wearable/fd230app/nativelauncher/src/NativeLauncher.cpp +++ b/code/sdk/application/wearable/fd230app/nativelauncher/src/NativeLauncher.cpp @@ -278,6 +278,8 @@ #include "schedule/include/ScheduleData.h" #include "sleepmode/include/SleepModePresenter.h" +#include "system/include/SystemLogModel.h" + #include "ui_service.h" extern "C" void uapi_dev_runtime_set_lock_status(uint8_t status); extern "C" uint8_t uapi_dev_runtime_get_lock_status(void); @@ -566,6 +568,14 @@ namespace OHOS } } } + // 注册开机时间 + SystemLogData systemlog; + systemlog.timestamp = SystemLogModel::GetInstance()->GetCurrentTime(); + systemlog.type = SystemLogType::POWER_ON; + // 先存储日志 + SystemLogModel::GetInstance()->saveLog(systemlog); + // 然后读取日志 + SystemLogModel::GetInstance()->loadLog(); } void NativeLauncher::InitAllNativeSlices(void) diff --git a/code/sdk/application/wearable/fd230app/nativeui/system/include/SystemLogModel.h b/code/sdk/application/wearable/fd230app/nativeui/system/include/SystemLogModel.h new file mode 100644 index 000000000..f80b8e3b1 --- /dev/null +++ b/code/sdk/application/wearable/fd230app/nativeui/system/include/SystemLogModel.h @@ -0,0 +1,51 @@ +#ifndef SystemLog_MODEL_H +#define SystemLog_MODEL_H + + +#include +#include + +namespace OHOS { +enum class SystemLogType { + POWER_ON = 0, // 开机 + POWER_OFF, // 关机 + RESTART, // 重启 + FACTORY_RESET, // 恢复出厂设置 + SOFTWARE_UPDATE, // 更新 +}; + +struct SystemLogData { + SystemLogType type; //类型 + uint32_t timestamp; //时间戳 +}; + +class SystemLogModel { +public: + static SystemLogModel *GetInstance(void); + + // 最多存储50条(超过了删除老的) + bool saveLog(SystemLogData data); + + // 加载数据 + bool loadLog(); + + // 获取日志记录 + std::vector getLogData(); + + size_t getLogSize(); + + uint32_t GetCurrentTime(); + +private: + SystemLogModel() = default; + ~SystemLogModel() {} + + // 不允许拷贝和赋值 + SystemLogModel(const SystemLogModel &) = delete; + SystemLogModel &operator=(const SystemLogModel &) = delete; + + std::vector logData; // 用于存储日志数据 +}; +} // namespace OHOS + +#endif // SystemLog_MODEL_H \ No newline at end of file diff --git a/code/sdk/application/wearable/fd230app/nativeui/system/include/SystemLogModelC.h b/code/sdk/application/wearable/fd230app/nativeui/system/include/SystemLogModelC.h new file mode 100644 index 000000000..48dd636b5 --- /dev/null +++ b/code/sdk/application/wearable/fd230app/nativeui/system/include/SystemLogModelC.h @@ -0,0 +1,21 @@ +#ifndef SYSTEMLOGC_H +#define SYSTEMLOGC_H + +#ifdef __cplusplus +#if __cplusplus +extern "C" +{ +#endif +#endif + +void start_report_log(void); +void stop_report_log(void); +void resume_report_log(void); +void update_version_report_log(void); + +#ifdef __cplusplus +#if __cplusplus +} +#endif +#endif +#endif diff --git a/code/sdk/application/wearable/fd230app/nativeui/system/src/SystemLogModel.cpp b/code/sdk/application/wearable/fd230app/nativeui/system/src/SystemLogModel.cpp new file mode 100644 index 000000000..f0e924e3d --- /dev/null +++ b/code/sdk/application/wearable/fd230app/nativeui/system/src/SystemLogModel.cpp @@ -0,0 +1,113 @@ +#include "system/include/SystemLogModel.h" +#include "cmsis_os.h" +#include "custom_utils.h" +#include "graphic_service.h" +#include "iostream" +#include "securec.h" +#include "system/include/SystemLogModelC.h" +#include "wearable_log.h" +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define SYSTEM_LOG_FILE_PATH "/system/system_log.txt" + +extern "C" { +// 通用日志记录功能 +void report_log(OHOS::SystemLogType type) { + OHOS::SystemLogData systemlog; + systemlog.timestamp = OHOS::SystemLogModel::GetInstance()->GetCurrentTime(); + systemlog.type = type; + + // 存储日志 + OHOS::SystemLogModel::GetInstance()->saveLog(systemlog); +} + +// 关机保存 +void start_report_log(void) { report_log(OHOS::SystemLogType::POWER_OFF); } + +// 重启保存 +void stop_report_log(void) { report_log(OHOS::SystemLogType::RESTART); } + +// 恢复存储保存 +void resume_report_log(void) { report_log(OHOS::SystemLogType::FACTORY_RESET); } + +// 更新版本保存 +void update_version_report_log(void) { + report_log(OHOS::SystemLogType::SOFTWARE_UPDATE); +} +} + +namespace OHOS { +SystemLogModel *SystemLogModel::GetInstance(void) { + static SystemLogModel instance; + return &instance; +} + +uint32_t SystemLogModel::GetCurrentTime() { + uint32_t time; + if (uapi_get_timestampe_ss(&time) != ERRCODE_SUCC) { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "Failed to get timestamp"); + return 0; + } + return time; +} + +// 保存日志 +bool SystemLogModel::saveLog(SystemLogData data) { + // 将新日志添加到日志数据 + logData.push_back(data); + + // 如果超过限制,删除最早的日志 + if (logData.size() > 50) { + logData.erase(logData.begin()); + } + + // 将日志写入文件 + std::ofstream ofs(SYSTEM_LOG_FILE_PATH, std::ios::app); + if (!ofs.is_open()) { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, + "Failed to open log file for writing"); + return false; + } + + ofs << static_cast(data.type) << " " << data.timestamp << std::endl; + return true; +} + +// 加载日志 +bool SystemLogModel::loadLog() { + std::ifstream ifs(SYSTEM_LOG_FILE_PATH); + if (!ifs.is_open()) { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, + "Failed to open log file for reading"); + return false; + } + + logData.clear(); // 清空现有日志 + SystemLogData data; + + while (ifs >> reinterpret_cast(data.type) >> data.timestamp) { + logData.push_back(data); + } + + ifs.close(); // 确保文件关闭 + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "Loaded log is ok"); + return true; +} + +// 获取日志记录 +std::vector SystemLogModel::getLogData() { return logData; } + +// 获取日志大小 +size_t SystemLogModel::getLogSize() { return logData.size(); } + +} // namespace OHOS \ No newline at end of file diff --git a/code/sdk/application/wearable/fd230app/nativeui/system/src/SystemPresenter.cpp b/code/sdk/application/wearable/fd230app/nativeui/system/src/SystemPresenter.cpp index 25111ea9e..f78be5fb5 100644 --- a/code/sdk/application/wearable/fd230app/nativeui/system/src/SystemPresenter.cpp +++ b/code/sdk/application/wearable/fd230app/nativeui/system/src/SystemPresenter.cpp @@ -216,6 +216,7 @@ namespace OHOS ShutDownImageAnimatorView::GetInstance()->ShutDownImageAnimator_Start(0); isShutDownImageAnimatorStart = true; + return true; } else if (strcmp(view.GetViewId(), SHUTDOWN_CHECKOFF) == 0) // 关机取消 diff --git a/code/sdk/drivers/peripheral/utils/custom_utils.c b/code/sdk/drivers/peripheral/utils/custom_utils.c index 5c5428918..612385083 100644 --- a/code/sdk/drivers/peripheral/utils/custom_utils.c +++ b/code/sdk/drivers/peripheral/utils/custom_utils.c @@ -37,6 +37,7 @@ #include "producttest/include/testTPC.h" #include "system/include/SystemPresenterC.h" #include "producttest/include/testPresenterC.h" +#include "system/include/SystemLogModelC.h" #include "ConfigModelC.h" #endif #include @@ -162,16 +163,26 @@ uint32_t zh_set_sn(uint8_t *buf_sn, uint32_t len_sn) extern uint32_t mcu_os_sys_reset(uint8_t *para, uint32_t para_len); extern void uapi_sys_shipmode(uint32_t arg); extern void cm_sync_all_config_to_disk(void); +//关机 void uapi_poweroff_device(void) { //重启前要保存时间 uapi_calendar_store_date(); + + //关机保存 + start_report_log(); + uapi_sys_shipmode(0); } +//重启 void uapi_reboot_device(void) { - //重启前要保存时间 + //重启前要保存时间 uapi_calendar_store_date(); + + //重启保存 + stop_report_log(); + mcu_os_sys_reset(NULL,NULL); } @@ -470,6 +481,8 @@ errcode_t uaip_cmd_update_request(void) printf("uapi_upg_request_upgrade OK!!\r\n"); /* app中下载后直接开始升级流程,升级流程结束后重启 */ uapi_calendar_store_date(); + + update_version_report_log();//版本升级记录 /* IOT设备重启 */ upg_reboot(); } @@ -1985,6 +1998,9 @@ int uapi_save_exc(void) void uapi_restore_factory_setting(void) { + //恢复存储 + resume_report_log(); + char *filebuf[] = {"/system/ConfigModel.json", "/user/userdata", "/system/kvstore", -- 2.17.1