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

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及以上版本已经支持自动回绕处理,无需额外担心溢出问题。