导读:本期聚焦于小伙伴创作的《C++怎么通过读取系统uptime获取开机总时间并记录运行日志》,敬请观看详情,探索知识的价值。以下视频、文章将为您系统阐述其核心内容与价值。如果您觉得《C++怎么通过读取系统uptime获取开机总时间并记录运行日志》有用,将其分享出去将是对创作者最好的鼓励。

在C++开发场景中,获取系统开机总时间并记录运行日志是常见需求,可用于系统运行时长统计、异常排查时的环境信息记录等。不同操作系统的uptime获取逻辑存在差异,下面分别介绍Linux和Windows系统下的实现方案。

C++怎么通过读取系统uptime获取开机总时间并记录运行日志

Linux系统下获取uptime实现方案

Linux系统中,/proc/uptime文件存储了系统开机以来的总秒数,第一列就是开机时长,单位秒,读取该文件即可快速获取信息。

读取uptime核心逻辑

我们可以通过标准文件流读取/proc/uptime文件,解析第一列的数值得到开机总秒数,再将秒数转换为天、时、分、秒的格式方便阅读。

#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <chrono>
#include <ctime>
#include <iomanip>

// 获取系统开机总秒数
double get_uptime_seconds() {
    std::ifstream uptime_file("/proc/uptime");
    if (!uptime_file.is_open()) {
        std::cerr << "无法打开/proc/uptime文件" << std::endl;
        return -1.0;
    }
    std::string line;
    std::getline(uptime_file, line);
    std::stringstream ss(line);
    double uptime_sec;
    ss >> uptime_sec;
    return uptime_sec;
}

// 将秒数转换为天时分秒格式
std::string format_uptime(double seconds) {
    if (seconds < 0) {
        return "获取失败";
    }
    long long total_sec = static_cast<long long>(seconds);
    int days = total_sec / (24 * 3600);
    total_sec %= (24 * 3600);
    int hours = total_sec / 3600;
    total_sec %= 3600;
    int minutes = total_sec / 60;
    int secs = total_sec % 60;
    std::stringstream fmt_ss;
    fmt_ss << days << "天" << hours << "小时" << minutes << "分钟" << secs << "秒";
    return fmt_ss.str();
}

记录运行日志实现

获取到开机总时间后,我们可以将当前时间、开机总时长等信息格式化,写入到指定的日志文件中,日志按日期命名方便管理。

// 获取当前时间字符串,格式为YYYY-MM-DD HH:MM:SS
std::string get_current_time_str() {
    auto now = std::chrono::system_clock::now();
    std::time_t now_time = std::chrono::system_clock::to_time_t(now);
    std::tm* local_tm = std::localtime(&now_time);
    std::stringstream time_ss;
    time_ss << std::put_time(local_tm, "%Y-%m-%d %H:%M:%S");
    return time_ss.str();
}

// 记录日志到文件,日志文件按日期命名
void write_run_log(const std::string& uptime_str) {
    std::string current_time = get_current_time_str();
    // 提取日期作为日志文件名
    std::string log_date = current_time.substr(0, 10);
    std::string log_file_name = "run_log_" + log_date + ".txt";
    
    std::ofstream log_file(log_file_name, std::ios::app);
    if (!log_file.is_open()) {
        std::cerr << "无法打开日志文件" << std::endl;
        return;
    }
    log_file << "[" << current_time << "] 系统开机总时长: " << uptime_str << std::endl;
    log_file.close();
}

int main() {
    double uptime_sec = get_uptime_seconds();
    std::string uptime_str = format_uptime(uptime_sec);
    std::cout << "系统开机总时长: " << uptime_str << std::endl;
    write_run_log(uptime_str);
    return 0;
}

Windows系统下获取uptime实现方案

Windows系统没有/proc/uptime文件,需要通过系统API获取系统启动时间,再和当前时间计算差值得到开机总时长。

核心API与实现逻辑

我们可以使用GetTickCount64函数获取系统启动以来的毫秒数,也可以查询系统启动时间再计算差值,下面使用GetTickCount64的实现更简洁。

#include <iostream>
#include <windows.h>
#include <string>
#include <sstream>
#include <chrono>
#include <ctime>
#include <iomanip>
#include <fstream>

// 获取系统开机总秒数,Windows下使用GetTickCount64
double get_uptime_seconds_win() {
    ULONGLONG tick_count = GetTickCount64();
    // 转换为秒
    return static_cast<double>(tick_count) / 1000.0;
}

// 格式化开机时长,和Linux版本逻辑一致
std::string format_uptime_win(double seconds) {
    if (seconds < 0) {
        return "获取失败";
    }
    long long total_sec = static_cast<long long>(seconds);
    int days = total_sec / (24 * 3600);
    total_sec %= (24 * 3600);
    int hours = total_sec / 3600;
    total_sec %= 3600;
    int minutes = total_sec / 60;
    int secs = total_sec % 60;
    std::stringstream fmt_ss;
    fmt_ss << days << "天" << hours << "小时" << minutes << "分钟" << secs << "秒";
    return fmt_ss.str();
}

// 获取当前时间字符串,Windows下实现
std::string get_current_time_str_win() {
    auto now = std::chrono::system_clock::now();
    std::time_t now_time = std::chrono::system_clock::to_time_t(now);
    std::tm local_tm;
    localtime_s(&local_tm, &now_time);
    std::stringstream time_ss;
    time_ss << std::put_time(&local_tm, "%Y-%m-%d %H:%M:%S");
    return time_ss.str();
}

// 写入日志文件,和Linux版本逻辑一致
void write_run_log_win(const std::string& uptime_str) {
    std::string current_time = get_current_time_str_win();
    std::string log_date = current_time.substr(0, 10);
    std::string log_file_name = "run_log_" + log_date + ".txt";
    
    std::ofstream log_file(log_file_name, std::ios::app);
    if (!log_file.is_open()) {
        std::cerr << "无法打开日志文件" << std::endl;
        return;
    }
    log_file << "[" << current_time << "] 系统开机总时长: " << uptime_str << std::endl;
    log_file.close();
}

int main() {
    double uptime_sec = get_uptime_seconds_win();
    std::string uptime_str = format_uptime_win(uptime_sec);
    std::cout << "系统开机总时长: " << uptime_str << std::endl;
    write_run_log_win(uptime_str);
    return 0;
}

注意事项

  • Linux系统下需要确保程序有读取/proc/uptime文件的权限,一般普通用户都有该权限。
  • Windows系统下编译时需要链接对应的系统库,使用MSVC编译时无需额外配置,MinGW可能需要添加相关编译选项。
  • 日志文件路径可以根据实际需求修改,避免权限不足导致日志写入失败。
  • 如果系统运行时间超过GetTickCount64的上限(约49.7天),该函数在Windows 10及以上版本已经支持自动回绕处理,无需额外担心溢出问题。

C++uptime运行日志系统开机时间修改时间:2026-07-10 12:21:34

免责声明:​ 已尽一切努力确保本网站所含信息的准确性。网站内容多为原创整理与精心编撰,观点力求客观中立。本站旨在免费分享,内容仅供个人学习、研究或参考使用。若引用了第三方作品,版权归原作者所有。如内容涉及您的权益,请联系我们处理。
内容垂直聚焦
专注技术核心技术栏目,确保每篇文章深度聚焦于实用技能。从代码技巧到架构设计,为用户提供无干扰的纯技术知识沉淀,精准满足专业提升需求。
知识结构清晰
覆盖从开发到部署的全链路。AI、前端、编程、数据库、服务器、建站、系统层层递进,构建清晰学习路径,帮助用户系统化掌握开发与运维所需的核心技术。
深度技术解析
拒绝泛泛而谈,深入技术细节与实践难点。无论是数据库优化还是服务器配置,均结合真实场景与代码示例进行剖析,致力于提供可直接应用于工作的解决方案。
专业领域覆盖
精准对应开发生命周期。从前端界面到后端编程,从数据库操作到服务器运维,形成完整闭环,一站式满足全栈工程师和运维人员的技术需求。
即学即用高效
内容强调实操性,步骤清晰、代码完整。用户可根据教程直接复现和应用于自身项目,显著缩短从学习到实践的距离,快速解决开发中的具体问题。
持续更新保障
专注既定技术方向进行长期、稳定的内容输出。确保各栏目技术文章持续更新迭代,紧跟主流技术发展趋势,为用户提供经久不衰的学习价值。