-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.cpp
More file actions
222 lines (181 loc) · 4.15 KB
/
Copy pathMain.cpp
File metadata and controls
222 lines (181 loc) · 4.15 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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
#include <iostream>
#include <sstream>
#include <vector>
#include <string>
#include <chrono>
#include <array>
#include <unordered_map>
#include "Log.h"
#include "Windows.h"
struct Options
{
std::string executablePath;
std::vector<std::string> executableArgs;
bool useSuffixes = false;
bool useAnotherConsole = false;
std::array<std::string, 4> suffixes { "ms", "s", "m", "h" };
std::array<int, 4> dividers { 1000, 60, 60, 1 };
};
struct TimeValue
{
double value;
std::string suffix;
};
auto retrieveArgs(int argc, const char* argv[])
{
std::vector<std::string> args(argc);
for (int i = 0; i < argc; i++)
{
args[i] = std::string(argv[i]);
}
return args;
}
auto parseArgs(const std::vector<std::string>& args)
{
Options opt;
bool asExecutableArgs = false;
for (auto i = 1; i < args.size(); i++)
{
const auto& arg = args[i];
if (arg.empty()) continue;
if (asExecutableArgs)
{
opt.executableArgs.push_back(arg);
continue;
}
if (arg.front() == '-')
{
if (arg == "--")
{
asExecutableArgs = true;
continue;
}
else if (arg == "-h")
{
opt.useSuffixes = true;
}
else if (arg == "-n")
{
opt.useAnotherConsole = true;
}
else
{
Log::warning() << "Unknown parameter " << arg << "\n";
}
}
else
{
if (opt.executablePath.empty())
{
opt.executablePath = arg;
}
else
{
Log::warning() << "Ambiguous executable paths. The last one will be used\n";
}
}
}
return opt;
}
auto* getCommandLine(const std::string& executable, const std::vector<std::string>& args)
{
std::ostringstream stream;
stream << "\"" << executable << "\" ";
for (const auto& a : args)
stream << "\"" << a << "\" ";
auto string = stream.str();
char* buffer = new char[string.size() + 1];
for (int i = 0; i < string.size(); i++)
buffer[i] = string[i];
buffer[string.size()] = 0;
return buffer;
}
auto getCurrentTimeMills()
{
return std::chrono::duration_cast<std::chrono::milliseconds>(
std::chrono::system_clock::now()
.time_since_epoch());
}
TimeValue convertTime(const Options& opt, uint64_t ms)
{
double value = double(ms);
if (!opt.useSuffixes)
return { value, opt.suffixes.front() };
for (int i = 0; i < opt.suffixes.size(); i++)
{
if (value < opt.dividers[i])
{
return { value, opt.suffixes[i] };
}
value /= opt.dividers[i];
}
}
auto createProcess(const Options& opt)
{
auto* commandLine = getCommandLine(opt.executablePath, opt.executableArgs);
PROCESS_INFORMATION processInfo{};
STARTUPINFOA startUpInfo{};
Log::info() << "Starting " << commandLine << "\n";
if (!opt.useAnotherConsole)
{
Log::info() << std::string(25, '=') << "\n";
}
startUpInfo.cb = sizeof(startUpInfo);
auto start = getCurrentTimeMills();
auto processAttrs = 0;
if (opt.useAnotherConsole)
{
processAttrs |= CREATE_NEW_CONSOLE;
}
auto createResult = CreateProcessA(nullptr,
commandLine,
nullptr, nullptr, false,
processAttrs, nullptr, nullptr,
&startUpInfo, &processInfo);
if (createResult)
{
if (opt.useAnotherConsole)
{
while (WaitForSingleObject(processInfo.hProcess, 100) == WAIT_TIMEOUT)
{
auto current = getCurrentTimeMills();
auto delta = current - start;
auto time = convertTime(opt, delta.count());
std::cout << "Execution time: " << time.value << time.suffix << "\r";
}
}
else
{
WaitForSingleObject(processInfo.hProcess, INFINITE);
}
}
else
{
Log::error() << "Failed to create new process with executable " << opt.executablePath << "\n";
Log::winError(GetLastError()) << "\n";
}
auto end = getCurrentTimeMills();
delete[] commandLine;
CloseHandle(processInfo.hThread);
CloseHandle(processInfo.hProcess);
return end - start;
}
int main(int argc, const char* argv[])
{
auto args = retrieveArgs(argc, argv);
auto opt = parseArgs(args);
if (opt.executablePath.empty())
{
Log::error() << "Executable path must be specified\n";
return 1;
}
auto delta = createProcess(opt);
if (!opt.useAnotherConsole)
{
Log::info() << std::string(25, '=') << "\n";
}
Log::info() << "Process has been stopped\n";
auto tv = convertTime(opt, delta.count());
Log::info() << tv.value << tv.suffix << "\n";
return 0;
}