系统:增加系统开关机日志记录服务
authorCaisking <3449750381@qq.com>
Fri, 4 Oct 2024 10:01:28 +0000 (18:01 +0800)
committerCaisking <3449750381@qq.com>
Fri, 4 Oct 2024 10:01:28 +0000 (18:01 +0800)
code/sdk/application/wearable/fd230app/CMakeLists.txt
code/sdk/application/wearable/fd230app/nativelauncher/src/NativeLauncher.cpp
code/sdk/application/wearable/fd230app/nativeui/system/include/SystemLogModel.h [new file with mode: 0644]
code/sdk/application/wearable/fd230app/nativeui/system/include/SystemLogModelC.h [new file with mode: 0644]
code/sdk/application/wearable/fd230app/nativeui/system/src/SystemLogModel.cpp [new file with mode: 0644]
code/sdk/application/wearable/fd230app/nativeui/system/src/SystemPresenter.cpp
code/sdk/drivers/peripheral/utils/custom_utils.c

index e84083a..7ab7208 100644 (file)
@@ -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    
index 0607ad0..2a37a5f 100644 (file)
 #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 (file)
index 0000000..f80b8e3
--- /dev/null
@@ -0,0 +1,51 @@
+#ifndef SystemLog_MODEL_H
+#define SystemLog_MODEL_H
+
+
+#include <cstdint>
+#include <vector>
+
+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<SystemLogData> getLogData();
+
+  size_t getLogSize();
+
+  uint32_t GetCurrentTime();
+
+private:
+  SystemLogModel() = default;
+  ~SystemLogModel() {}
+
+  // 不允许拷贝和赋值
+  SystemLogModel(const SystemLogModel &) = delete;
+  SystemLogModel &operator=(const SystemLogModel &) = delete;
+
+  std::vector<SystemLogData> 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 (file)
index 0000000..48dd636
--- /dev/null
@@ -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 (file)
index 0000000..f0e924e
--- /dev/null
@@ -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 <algorithm>
+#include <chrono>
+#include <cstdlib>
+#include <ctime>
+#include <fstream>
+#include <iostream>
+#include <sstream>
+#include <string>
+#include <sys/time.h>
+#include <thread>
+#include <vector>
+
+#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<int>(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<int &>(data.type) >> data.timestamp) {
+    logData.push_back(data);
+  }
+
+  ifs.close(); // 确保文件关闭
+  WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "Loaded log is ok");
+  return true;
+}
+
+// 获取日志记录
+std::vector<SystemLogData> SystemLogModel::getLogData() { return logData; }
+
+// 获取日志大小
+size_t SystemLogModel::getLogSize() { return logData.size(); }
+
+} // namespace OHOS
\ No newline at end of file
index 25111ea..f78be5f 100644 (file)
@@ -216,6 +216,7 @@ namespace OHOS
 \r
             ShutDownImageAnimatorView::GetInstance()->ShutDownImageAnimator_Start(0);\r
             isShutDownImageAnimatorStart = true;\r
+\r
             return true;\r
         }\r
         else if (strcmp(view.GetViewId(), SHUTDOWN_CHECKOFF) == 0) // 关机取消\r
index 5c54289..6123850 100644 (file)
@@ -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 <dirent.h>
@@ -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",