-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
148 lines (130 loc) · 4.53 KB
/
Copy pathmain.cpp
File metadata and controls
148 lines (130 loc) · 4.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#include <iostream>
#include <fstream>
#include <csignal>
#include <unistd.h>
#include <ctime>
#include <sys/stat.h>
#include <direct.h>
#include <vector>
using namespace std;
int step = 0;
vector<std::string>marker = {
"Output: ",
"Warning: ",
"Error: ",
"Progress: "
};
std::string getCurrentTimeString() {
time_t now = time(0);
struct tm tstruct;
char buf[80];
tstruct = *localtime(&now);
strftime(buf, sizeof(buf), "%Y%m%d%H%M%S", &tstruct);
return buf;
}
void logResult(const std::string& message) {
// 检查并创建Logs文件夹
struct stat info;
if (stat("Logs", &info) != 0) {
// 文件夹不存在,创建文件夹
_mkdir("Logs");
}
std::string timeStr = getCurrentTimeString();
std::string fileName = "Logs/solver_" + timeStr + ".log";
std::ofstream logFile(fileName, std::ios::app);
if (logFile.is_open()) {
logFile << message << std::endl;
logFile.close();
} else {
std::cout << marker[2]<<"Unable to open log file: " << fileName << std::endl;
//std::cerr << "Unable to open log file: " << fileName << std::endl;
std::cout.flush();
}
}
void signalHandler(int signal) {
std::cout << marker[0]<<"Interrupt by User." << std::endl;
std::cout.flush();
logResult("Interrupt by User. Stop at Step "+std::to_string(step) + ".");
// sleep(1);
exit(signal);
}
void simulateLongRunningFunction() {
try {
for (int i = 1; i <= 10; ++i) {
std::cout << marker[3] << i << std::endl;
std::cout.flush(); // progress test
std::cout << marker[0] << "Output message catch test: " << i << std::endl;
std::cout.flush(); // output test
std::cout << marker[1] << "Warning message catch test: " << i << std::endl;
std::cout.flush(); // warning test
std::cout << marker[2] << "Error message catch test: " << i << std::endl;
std::cout.flush(); // error test
sleep(1); // 模拟耗时操作
step = i;
}
std::cout << marker[0]<<"Success" << std::endl;
logResult("Success");
}
catch (const std::runtime_error& e) {
std::cout << marker[2]<<"Fail: " << e.what() << std::endl;
std::cout.flush();
logResult(std::string("Fail: ") + e.what()+", Stop at Step "+std::to_string(step)+ ".");
exit(0);
}
}
void readFile(const std::string& path) {
std::ifstream inFile(path);
if (!inFile.is_open()) {
std::cout <<marker[2]<< "Unable to open file: " << path << std::endl;
//std::cerr << "Unable to open file: " << path << std::endl;
logResult("Unable to open file: " + path);
exit(1);
}
inFile.close();
}
int main(int argc, char* argv[]) {
std::string filePath;
bool isAbsolutePath = false;
bool hasFilePath = false;
// 解析命令行参数
if (argc == 3) {
if (strcmp(argv[1], "-d") == 0) {
filePath = argv[2];
isAbsolutePath = true;
hasFilePath = true;
} else if (strcmp(argv[1], "-r") == 0) {
filePath = argv[2];
isAbsolutePath = false;
hasFilePath = true;
} else {
std::cout <<marker[2]<< "Invalid argument. Use -d for absolute path or -r for relative path." << std::endl;
//std::cerr << "Invalid argument. Use -d for absolute path or -r for relative path." << std::endl;
return 1;
}
} else if (argc != 1) {
std::cout <<marker[2]<< "Usage: " << argv[0] << " [-d <absolute_path> | -r <relative_path>]" << std::endl;
//std::cerr << "Usage: " << argv[0] << " [-d <absolute_path> | -r <relative_path>]" << std::endl;
return 1;
}
// 注册信号处理函数
signal(SIGINT, signalHandler);
signal(SIGTERM, signalHandler);
//signal(SIGKILL, signalHandler);
// 如果有指定文件路径,处理路径并读取文件
if (hasFilePath) {
if (!isAbsolutePath) {
char currentPath[FILENAME_MAX];
if (!getcwd(currentPath, sizeof(currentPath))) {
std::cout <<marker[2]<< "Error getting current working directory." << std::endl;
//std::cerr << "Error getting current working directory." << std::endl;
return 1;
}
filePath = std::string(currentPath) + "/" + filePath;
}
readFile(filePath);
}
step = 0;
// 调用模拟耗时长的函数
simulateLongRunningFunction();
return 0;
}