-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
306 lines (303 loc) · 7.83 KB
/
main.cpp
File metadata and controls
306 lines (303 loc) · 7.83 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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
#include <conio.h>
#include <limits>
#include <iostream>
#include <QDir>
#include <QFileInfo>
#include <QThread>
#include <QProcess>
using namespace std;
int main()
{
QDir dir;
QFileInfo fiLog;
QTime tCooldownStart, tNow;
QFile fCfg, fLog;
QDateTime dtLastTick, dtFile;
qint32 i, iCooldownTimer, iArr, iState, iWaitCooldownMin, iRestartCnt, iOldTickThreshold; // states: 0 - normal check, 1 - TaskKill process start, 2 - TaskKill finished/restart
qint64 qi64sec;
unsigned long ulWaitRestartMs;
QString str, sPathLogFile, sFileName, sPathTerminal64;
QStringList slArg;
char sArr[4] = { '-', '\\', '|', '/' };
bool bOK, bWindows = false;
QProcess * proc = NULL;
QObject * parent = nullptr;
char ch;
sFileName = "tkcfg.ini";iCooldownTimer = iRestartCnt = iOldTickThreshold = 0;
fCfg.setFileName( sFileName );
if ( !fCfg.exists() )
{
cout << "Fatal error, config file not found." << endl;
getchar();
return -1;
}
if ( !fCfg.open( QIODevice::ReadOnly ) )
{
cout << "Fatal error, config file cannot be opened." << endl;
getchar();
return -1;
}
QTextStream tsCfg( &fCfg );
sPathLogFile = "X";
sFileName = "";
ulWaitRestartMs = 5000;tCooldownStart = QTime::currentTime();iWaitCooldownMin = 1;
while ( !tsCfg.atEnd() )
{
str = tsCfg.readLine();
if ( str.contains( "PathToLogFile" ) )
{
i = str.indexOf( '=', 0 );
if ( i > -1 )
{
i++;
while ( str[i] == ' ' )
i++;
sPathLogFile = str.mid( i );
}
else
{
cout << "Error in config file - PathToLogFile invalid." << endl;
getchar();
return -1;
}
}
if ( str.contains( "LogFileName" ) )
{
i = str.indexOf( '=', 0 );
if ( i > -1 )
{
i++;
while ( str[i] == ' ' )
i++;
sFileName = str.mid( i );
}
else
{
cout << "Error in config file - LogFileName invalid." << endl;
getchar();
return -1;
}
}
if ( str.contains( "TerminalPath" ) )
{
i = str.indexOf( '=', 0 );
if ( i > -1 )
{
i++;
while ( str[i] == ' ' )
i++;
sPathTerminal64 = str.mid( i );
}
else
{
cout << "Error in config file - TerminalPath invalid." << endl;
getchar();
return -1;
}
}
if ( str.contains( "WaitRestart" ) )
{
i = str.indexOf( '=', 0 );
if ( i > -1 )
{
i++;
while ( str[i] == ' ' )
i++;
str = str.mid( i );ulWaitRestartMs = str.toULong( &bOK );
}
if ( i < 0 || !bOK )
{
cout << "Error in config file - WaitRestart invalid." << endl;
getchar();
return -1;
}
}
if ( str.contains( "WaitCooldown" ) )
{
i = str.indexOf( '=', 0 );
if ( i > -1 )
{
i++;
while ( str[i] == ' ' )
i++;
str = str.mid( i );iWaitCooldownMin = (int) str.toUInt( &bOK );
}
if ( i < 0 || !bOK )
{
cout << "Error in config file - WaitCooldown invalid." << endl;
getchar();
return -1;
}
}
if ( str.contains( "OldTickThreshold" ) )
{
i = str.indexOf( '=', 0 );
if ( i > -1 )
{
i++;
while ( str[i] == ' ' )
i++;
str = str.mid( i );iOldTickThreshold = (int) str.toUInt( &bOK );
}
if ( i < 0 || !bOK )
{
cout << "Error in config file - OldTickThreshold invalid." << endl;
getchar();
return -1;
}
}
}
fCfg.close();
//qInfo() << "C++ Style Info Message";
#if defined _WIN32
bWindows = true;
#endif
if ( !dir.exists( sPathLogFile ) )
{
cout << "Fatal error, directory \"" << sPathLogFile.toLatin1().data() << "\" not found. Check the config file." << endl;
getchar();
return -1;
}
dir.setPath( sPathLogFile );
if ( !dir.setCurrent( sPathLogFile ) )
{
cout << "Fatal error, directory \"" << sPathLogFile.toLatin1().data() << "\" not available. Check the config file." << endl;
getchar();
return -1;
}
//qInfo() << "Checking" << sFileName << "regularly...";
cout << "Checking " << sFileName.toLatin1().data() << " regularly...";
QThread::msleep( 500 );cout << " 3";
QThread::msleep( 500 );cout << " 2";
QThread::msleep( 500 );cout << " 1" << endl;QThread::msleep( 500 );
fiLog.setFile( sPathLogFile + "/" + sFileName );
if ( sFileName.isEmpty() )
{
cout << "Error: log file name empty. Check the config file." << endl;
return -1;
}
start:
iArr = iState = 0;
tNow = QTime::currentTime();//tNow = tNow.addSecs( 65 );
i = tNow.hour() * 60 + tNow.minute();fLog.setFileName(fiLog.absoluteFilePath());
if ( i - ( tCooldownStart.hour() * 60 + tCooldownStart.minute() ) > iWaitCooldownMin ) // won't work correctly around midnight. Should have used QDateTime. I don't care. :)
{
cout << "Cooldown finished." << endl;
iCooldownTimer = 0;
}
if ( !fiLog.exists() && !iCooldownTimer )
{
cout << "Error: file \"" << sFileName.toLatin1().data() << "\" does not exist. Check the config file." << endl;
//qInfo() << "Error: file \"" << sFileName.toLatin1().data() << "\" does not exist."; // extra spaces
//return -1;
}
do
{
if ( kbhit() )
{
ch = getch();
if ( ch == 27 )
{
str = "\nRestart counter so far: " + QString::number( iRestartCnt ) + "\n";
cout << str.toLatin1().data() << "Do you want to quit? Y/N" << endl;
ch = toupper( getch() );
if ( kbhit() ) // clear stdin buffer correctly
{
cin.clear();
cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}
if ( ch == 'Y' )
{
iState = 99;
break;
}
}
}
if ( fiLog.exists() )
{
fiLog.refresh();
dtFile = fiLog.fileTime( QFileDevice::FileModificationTime );
dtLastTick = QDateTime::currentDateTime();
if ( dtFile.isValid() )
{
str = dtFile.time().toString( "hh:mm:ss" );str += ", difference: ";qi64sec = dtLastTick.secsTo(dtFile);
str += QString::number( qi64sec / 60 ) ;str += " minutes.";
if ( qi64sec / 60 < -1 * iOldTickThreshold )
{
str += " Last tick logged too long ago, restart needed.";
cout << "\nFile last modification time: " << str.toLatin1().data() << endl;iState = 1;
}
else
{
if ( bWindows )
system("cls");
cout << sArr[iArr++];// << 0x0d;// << endl; //ANSI escape sequences don't work in Win32 environment :(
if ( iArr == 4 )
iArr = 0;
}
}
else
{
cout << "Error reading log file time - deleting log." << endl;
if ( fLog.exists() )
fLog.remove(); // necessary
}
}
QThread::msleep( 1000 );
} while ( !iState );
if ( iState == 1 ) // kill process
{
if ( proc != NULL )
{
if ( proc->state() == QProcess::Running && !iCooldownTimer )
{
proc->close();
//if ( fLog.exists() )
//fLog.remove(); // not necessary
cout << "Terminal process closed." << endl;
}
}
else if ( !iCooldownTimer )
{
slArg.clear();slArg << "/im" << "terminal64*" << "/f";
QProcess::execute( "Taskkill", slArg );
//cout << "Terminal process killed." << endl;
}
cout << "Waiting for restart..." << endl;
iState = 2;QThread::msleep( ulWaitRestartMs );
}
if ( iState == 2 ) //restart terminal64.exe
{
if ( !iCooldownTimer )
{
str = "\"" + sPathTerminal64 + "/terminal64.exe\"";
if ( proc == NULL )
proc = new QProcess( parent );
if ( proc->state() == QProcess::NotRunning )
{
slArg.clear();
proc->start( str, slArg );iCooldownTimer = 1;iRestartCnt++;
//cout << "Terminal process started." << endl;
tCooldownStart = QTime::currentTime();
}
}
else cout << "Restarted recently. On cooldown." << endl;
goto start;
}
/*if ( proc != NULL )
{
if ( proc->state() == QProcess::Running ) // cannot detect manual closing of terminal
{
cout << "\nTerminal process still running. I guess I'll leave it that way." << endl << "Press enter key to continue." << endl;
getchar();
//proc->close();
}
if ( proc->exitStatus() == QProcess::NormalExit ) // not working correctly. Probably need to connect to 'finished' signal.
{
cout << "\nTerminal process exited." << endl << "Press enter key to continue." << endl;
getchar();
//delete proc;
}
}*/
return 0;
}