栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 系统运维 > 运维 > Linux

嵌入式linux学习笔记-- linux 开机总时间记录的一种方案

Linux 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

嵌入式linux学习笔记-- linux 开机总时间记录的一种方案

产品的一个功能就是在某一个界面上显示产品的总开机时间,之前一直担心因为频繁写一个块导致的EMMC 写坏,今天放下了这个思想的负担 重新尝试简单但是方便的,定时更新写入的逻辑,嗯 真香,真简单!!!! 时间计算的精度可能不是很高 还会出现丢失一些时间,不过这个应该也是无关紧要的,这个数据主要是给客户看 产品已经运行了多久了,大概评估寿命的? 不是很清楚具体这个参数的意义。

直接上代码:

// uptime.h
#ifndef _UP_TIME_H_
#define _UP_TIME_H_
#include 
#include 
#include 
#include 
#include 
#include 
#define filePathToUptime "./uptimeFile"

class upTime
{
public:
    ~upTime(){
        std::cout<<"destructor called!"<thd.joinable())
        {
            this->thd.join();
        }
    }
    void init(const char *path,uint16_t saveGap = 60){
        this->time_toSave = saveGap;
        snprintf(this->filePath,300,"%s",path);
        this->thd = std::thread(calcUptimeProcess,this);
        this->thd.detach();
    }
    upTime(const upTime&)=delete;
    upTime& operator=(const upTime&)=delete;
    static upTime& get_instance(){
        static upTime instance;
        return instance;
    }
private:
    upTime(){
        printf("[%s] constructer enter .....  n",__FUNCTION__);        
    }
    static void calcUptimeProcess(void* __this){
        upTime * _this = (upTime *) __this;
        int ret = _this->loadFile(_this->filePath,&_this->time_sec);        
        printf("[%s] enter _this->time_sec = %dn",__FUNCTION__,_this->time_sec);  
        _this->time_lastSaved  = _this->time_sec;
        while(1){
            usleep(1000000);
            _this->time_sec ++;
            if(_this->time_sec - _this->time_lastSaved >= _this->time_toSave){
                ret = _this->saveFile(_this->filePath,_this->time_sec);
                if(ret == 0)
                    _this->time_lastSaved = _this->time_sec;
            }
        }
    }
    int loadFile(const char *path,uint32_t *time)
    {        
        printf("[%s] enter ...n",__FUNCTION__);
        std::ifstream storedTime(path); 
        if (!storedTime.is_open()) 
        { 
            *time = 0;
            return -1;
        } 
        std::string temp;
        if(getline(storedTime,temp))
        {
            *time = atoi(temp.c_str());
            return 0;
        }
        return -1;
    }
    int saveFile(const char *path,uint32_t time)
    {        
        printf("[%s] enter ... time = %dn",__FUNCTION__,time);
        std::ofstream outfile(path, std::ios::trunc);
        outfile << std::to_string(time);
        return outfile.is_open() ? 0 : -1;
    }
    char filePath[300];
    uint32_t time_sec;      // 开机的时间,按照秒来计算的  0xffffffff / (60 * 24 * 365) 大约8000年
    uint32_t time_toSave;   // 存储间隔 例如 10s 存储一次
    uint32_t time_lastSaved;// 上次进行保存的时间
    std::thread thd;
};

#endif
#include "uptime.h"
int main(int argc, char ** argv)
{
    upTime::get_instance().init("./123");
    while(1){
        usleep(1000000);
        printf("...n");
    }
    return 0;
}

编译和运行指令, 因为需要使用多线程的库 所以需要链接 pthread

g++ uptime.cpp -lpthread && ./a.out

1024 程序猿节日快乐!

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/345643.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号