-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpal.h
137 lines (114 loc) · 3.06 KB
/
pal.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
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
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/
#ifndef OTEL_CLR_PROFILER_PAL_H_
#define OTEL_CLR_PROFILER_PAL_H_
#ifdef _WIN32
#include "windows.h"
#include <filesystem>
#include <process.h>
#else
#include <fstream>
#include <unistd.h>
#include <dlfcn.h>
#endif
#if MACOS
#include <libproc.h>
#endif
#include "environment_variables.h"
#include "string.h" // NOLINT
#include "util.h"
#ifdef _WIN32
#define DIR_SEPARATOR WStr('\\')
#define ENV_VAR_PATH_SEPARATOR WStr(';')
#else
#define DIR_SEPARATOR WStr('/')
#define ENV_VAR_PATH_SEPARATOR WStr(':')
#endif
namespace trace
{
template <class TLoggerPolicy>
inline WSTRING GetOpenTelemetryLogFilePath(const std::string& file_name_suffix)
{
const auto file_name = TLoggerPolicy::file_name + file_name_suffix + ".log";
WSTRING directory = GetEnvironmentValue(environment::log_directory);
if (directory.length() > 0)
{
return directory + DIR_SEPARATOR + ToWSTRING(file_name);
}
#ifdef _WIN32
std::filesystem::path program_data_path;
program_data_path = GetEnvironmentValue(WStr("PROGRAMDATA"));
if (program_data_path.empty())
{
program_data_path = WStr(R"(C:\ProgramData)");
}
// on Windows WSTRING == wstring
return (program_data_path / TLoggerPolicy::folder_path / file_name).wstring();
#else
return ToWSTRING("/var/log/opentelemetry/dotnet/" + file_name);
#endif
}
inline WSTRING GetCurrentProcessName()
{
#ifdef _WIN32
const DWORD length = 260;
WCHAR buffer[length]{};
const DWORD len = GetModuleFileName(nullptr, buffer, length);
const WSTRING current_process_path(buffer);
return std::filesystem::path(current_process_path).filename();
#elif MACOS
const int length = 260;
char* buffer = new char[length];
proc_name(getpid(), buffer, length);
return ToWSTRING(std::string(buffer));
#else
std::fstream comm("/proc/self/comm");
std::string name;
std::getline(comm, name);
return ToWSTRING(name);
#endif
}
inline int GetPID()
{
#ifdef _WIN32
return _getpid();
#else
return getpid();
#endif
}
inline WSTRING GetCurrentModuleFileName()
{
static WSTRING moduleFileName = EmptyWStr;
if (moduleFileName != EmptyWStr)
{
// use cached version
return moduleFileName;
}
#ifdef _WIN32
HMODULE hModule;
if (GetModuleHandleExW(GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT | GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS,
(LPCTSTR) GetCurrentModuleFileName,
&hModule))
{
WCHAR lpFileName[1024];
DWORD lpFileNameLength = GetModuleFileNameW(hModule, lpFileName, 1024);
if (lpFileNameLength > 0)
{
moduleFileName = WSTRING(lpFileName, lpFileNameLength);
return moduleFileName;
}
}
#else
Dl_info info;
if (dladdr((void*)GetCurrentModuleFileName, &info))
{
moduleFileName = ToWSTRING(ToString(info.dli_fname));
return moduleFileName;
}
#endif
return EmptyWStr;
}
} // namespace trace
#endif // OTEL_CLR_PROFILER_PAL_H_