/
PIDFile.h
78 lines (61 loc) · 1.74 KB
/
PIDFile.h
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
#pragma once
#include "wxutil/dialog/MessageBox.h"
#include "settings/PreferenceSystem.h"
#include "modulesystem/ModuleRegistry.h"
#include "os/file.h"
#include "string/replace.h"
#define PID_FILENAME "darkradiant.pid"
namespace applog {
/**
* greebo: This is a scoped object which creates a .pid file on construction
* and removes it on destruction. On program crash, the file doesn't
* get erased, which is detected during the next application startup.
*/
class PIDFile
{
std::string _filename;
public:
PIDFile(const std::string& filename)
{
module::ModuleRegistry& registry = module::ModuleRegistry::Instance();
_filename = registry.getApplicationContext().getSettingsPath() + filename;
FILE* pid = fopen(_filename.c_str(), "r");
// Check for an existing radiant.pid file
if (pid != NULL)
{
fclose(pid);
removePIDFile();
#ifndef _DEBUG
fs::path path = registry.getApplicationContext().getSettingsPath();
path /= "darkradiant.log";
std::string logPath = path.string();
string::replace_all(logPath, "\\\\", "\\");
string::replace_all(logPath, "//", "/");
std::string msg("Radiant failed to start properly the last time it was run.\n");
msg += "If this is happening again, you might want to check the log file in\n";
msg += logPath;
wxutil::Messagebox box("DarkRadiant - Startup Failure", msg, ui::IDialog::MESSAGE_CONFIRM);
box.run();
#endif
}
// create a primary .pid for global init run
pid = fopen(_filename.c_str(), "w");
if (pid)
{
fclose(pid);
}
};
~PIDFile()
{
removePIDFile();
}
private:
void removePIDFile()
{
if (remove(_filename.c_str()) == -1)
{
wxutil::Messagebox::ShowError("WARNING: Could not delete " + _filename);
}
}
};
} // namespace applog