1,使用
int main() {
while(pipeline_run_flag) {
system("top -s -b -n 1 |grep -E \"Cpu|KiB Mem |KiB Swap\" > log/sys_state.txt");
write_system_state_to_log("log/sys_state.txt");
usleep(1000*1000);
}
}
2,函数实现
void write_system_state_to_log(std::string filename)
{
std::ifstream fp;
std::string rtline, szLine;
std::string total_t, free_t, used_t, buffer_t;
std::string::size_type pos1, pos2;
fp.open(filename);
if (!fp.is_open()) {
LOG(ERROR) << "Error: can not find file: " << filename;
}
else {
LOG(WARNING