source: XIOS3/trunk/extern/cpptrace/src/program_name.hpp @ 2573

Last change on this file since 2573 was 2573, checked in by ymipsl, 9 months ago

create new external source lib : cpptrace, for statck trace output
YM

File size: 2.5 KB
Line 
1#ifndef PROGRAM_NAME_HPP
2#define PROGRAM_NAME_HPP
3
4#include <mutex>
5#include <string>
6
7#if defined(_WIN32)
8#include <windows.h>
9
10namespace cpptrace {
11    namespace detail {
12        inline std::string program_name() {
13            static std::mutex mutex;
14            const std::lock_guard<std::mutex> lock(mutex);
15            static std::string name;
16            static bool did_init = false;
17            static bool valid = false;
18            if(!did_init) {
19                did_init = true;
20                char buffer[MAX_PATH + 1];
21                int res = GetModuleFileNameA(nullptr, buffer, MAX_PATH);
22                if(res) {
23                    name = buffer;
24                    valid = true;
25                }
26            }
27            return valid && !name.empty() ? name.c_str() : nullptr;
28        }
29    }
30}
31
32#elif defined(__APPLE__)
33
34#include <cstdint>
35#include <mach-o/dyld.h>
36#include <sys/syslimits.h>
37
38namespace cpptrace {
39    namespace detail {
40        inline const char* program_name() {
41            static std::mutex mutex;
42            const std::lock_guard<std::mutex> lock(mutex);
43            static std::string name;
44            static bool did_init = false;
45            static bool valid = false;
46            if(!did_init) {
47                did_init = true;
48                std::uint32_t bufferSize = PATH_MAX + 1;
49                char buffer[bufferSize];
50                if(_NSGetExecutablePath(buffer, &bufferSize) == 0) {
51                    name.assign(buffer, bufferSize);
52                    valid = true;
53                }
54            }
55            return valid && !name.empty() ? name.c_str() : nullptr;
56        }
57    }
58}
59
60#elif defined(__linux__)
61
62#include <linux/limits.h>
63#include <sys/types.h>
64#include <unistd.h>
65
66namespace cpptrace {
67    namespace detail {
68        inline const char* program_name() {
69            static std::mutex mutex;
70            const std::lock_guard<std::mutex> lock(mutex);
71            static std::string name;
72            static bool did_init = false;
73            static bool valid = false;
74            if(!did_init) {
75                did_init = true;
76                char buffer[PATH_MAX + 1];
77                const ssize_t size = readlink("/proc/self/exe", buffer, PATH_MAX);
78                if(size == -1) {
79                    return nullptr;
80                }
81                buffer[size] = 0;
82                name = buffer;
83                valid = true;
84            }
85            return valid && !name.empty() ? name.c_str() : nullptr;
86        }
87    }
88}
89
90#endif
91
92#endif
Note: See TracBrowser for help on using the repository browser.