forked from jeremy-rifkin/cpptrace
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprogram_name.hpp
More file actions
102 lines (87 loc) · 2.52 KB
/
program_name.hpp
File metadata and controls
102 lines (87 loc) · 2.52 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
#ifndef PROGRAM_NAME_HPP
#define PROGRAM_NAME_HPP
#include <mutex>
#include <string>
#include "platform/platform.hpp"
#if IS_WINDOWS
#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif
#include <windows.h>
#define CPPTRACE_MAX_PATH MAX_PATH
CPPTRACE_BEGIN_NAMESPACE
namespace detail {
inline const char* program_name() {
static std::mutex mutex;
const std::lock_guard<std::mutex> lock(mutex);
static std::string name;
static bool did_init = false;
static bool valid = false;
if(!did_init) {
did_init = true;
char buffer[MAX_PATH + 1];
int res = GetModuleFileNameA(nullptr, buffer, MAX_PATH);
if(res) {
name = buffer;
valid = true;
}
}
return valid && !name.empty() ? name.c_str() : nullptr;
}
}
CPPTRACE_END_NAMESPACE
#elif IS_APPLE
#include <cstdint>
#include <mach-o/dyld.h>
#include <sys/syslimits.h>
#define CPPTRACE_MAX_PATH CPPTRACE_PATH_MAX
CPPTRACE_BEGIN_NAMESPACE
namespace detail {
inline const char* program_name() {
static std::mutex mutex;
const std::lock_guard<std::mutex> lock(mutex);
static std::string name;
static bool did_init = false;
static bool valid = false;
if(!did_init) {
did_init = true;
char buffer[CPPTRACE_PATH_MAX + 1];
std::uint32_t bufferSize = sizeof buffer;
if(_NSGetExecutablePath(buffer, &bufferSize) == 0) {
name.assign(buffer, bufferSize);
valid = true;
}
}
return valid && !name.empty() ? name.c_str() : nullptr;
}
}
CPPTRACE_END_NAMESPACE
#elif IS_LINUX
#include <sys/types.h>
#include <unistd.h>
#define CPPTRACE_MAX_PATH CPPTRACE_PATH_MAX
CPPTRACE_BEGIN_NAMESPACE
namespace detail {
inline const char* program_name() {
static std::mutex mutex;
const std::lock_guard<std::mutex> lock(mutex);
static std::string name;
static bool did_init = false;
static bool valid = false;
if(!did_init) {
did_init = true;
char buffer[CPPTRACE_PATH_MAX + 1];
const ssize_t size = readlink("/proc/self/exe", buffer, CPPTRACE_PATH_MAX);
if(size == -1) {
return nullptr;
}
buffer[size] = 0;
name = buffer;
valid = true;
}
return valid && !name.empty() ? name.c_str() : nullptr;
}
}
CPPTRACE_END_NAMESPACE
#endif
#endif