-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStdTest.inl
More file actions
144 lines (118 loc) · 4.19 KB
/
StdTest.inl
File metadata and controls
144 lines (118 loc) · 4.19 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
/**
* \file StdTest.inl
* \brief test, using C library and STL
*/
namespace stdtest
{
//-------------------------------------------------------------------------------------------------
inline
Report::Report(
const char *a_filePath, ///< file path
const long int a_fileLine, ///< file line number
const char *a_functionName, ///< function name
const char *a_expression ///< expression as string
) :
_filePath {a_filePath},
_fileLine {a_fileLine},
_functionName {a_functionName},
_expression {a_expression}
{
}
//-------------------------------------------------------------------------------------------------
inline void
Report::print() const
{
const int errorCode = errno; // Last error - must be called 1-st !!!
const std::string &errorMsg = std::strerror(errorCode);
const std::string &module = _modulePath();
const std::string &dateTime = _currentDateTime();
std::cout
<< "\n"
<< "-------------------- STD_TEST --------------------" << "\n"
<< " Module: " << module << "\n"
<< " Expression: " << _expression << "\n"
<< " File: " << _filePath << "\n"
<< " Function: " << _functionName << "\n"
<< " Line: " << _fileLine << "\n"
<< " Last error: " << errorCode << " - " << errorMsg << "\n"
<< " Date time: " << dateTime << "\n"
<< "--------------------------------------------------" << std::endl;
}
//-------------------------------------------------------------------------------------------------
inline std::string
Report::_currentDateTime() const
{
std::string sRv;
constexpr std::size_t buffSize {80};
char buff[buffSize + 1] {};
const time_t timeNow = std::time(nullptr);
const std::tm *timeLocal = std::localtime(&timeNow);
if (timeLocal == nullptr) {
return {};
}
size_t uiRv = std::strftime(buff, buffSize, "%Y-%m-%d.%X", timeLocal);
if (uiRv == 0) {
return {};
}
sRv.assign(&buff[0], uiRv);
return sRv;
}
//-------------------------------------------------------------------------------------------------
inline std::string
Report::_modulePath() const
{
std::string sRv;
#if defined(_WIN32)
sRv.resize(MAX_PATH);
DWORD stored = ::GetModuleFileName(nullptr, &sRv.at(0), static_cast<DWORD>( sRv.size() ));
if (stored == 0UL) {
return "<unknown>";
}
sRv.resize(stored);
#elif defined(__linux__)
const std::string procFile = "/proc/" + std::to_string(::getpid()) + "/exe";
int readed {- 1};
std::string filePath(PATH_MAX, '\0');
for ( ; ; ) {
readed = ::readlink(procFile.c_str(), &filePath.at(0), filePath.size());
if (readed == - 1) {
return "<unknown>";
}
if (filePath.size() > static_cast<size_t>(readed)) {
break;
}
filePath.resize(filePath.size() * 2);
}
filePath.resize( static_cast<size_t>(readed) );
sRv.assign(
std::find(filePath.crbegin(), filePath.crend(), '/').base(),
filePath.cend());
#elif defined(__FreeBSD__)
constexpr u_int mibSize {4};
int mib[mibSize] {CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, - 1};
char buff[PATH_MAX + 1] {};
std::size_t buffSize {sizeof(buff) - 1};
int iRv = ::sysctl(mib, mibSize, buff, &buffSize, nullptr, 0U);
if (iRv == - 1) {
return "<unknown>";
}
sRv.assign(buff);
#elif defined(__APPLE__)
constexpr auto buffSize = static_cast<std::uint32_t>(PATH_MAX);
std::string buff(buffSize + 1, {});
int iRv = ::_NSGetExecutablePath(&buff[0], &buffSize);
if (iRv != 0) {
buff.resize(buffSize);
iRv = ::_NSGetExecutablePath(&buff[0], &buffSize);
if (iRv != 0) {
return "<unknown>";
}
}
sRv = buff.c_str(); // Trim '\0'
#else
sRv = "<unknown>";
#endif
return sRv;
}
//-------------------------------------------------------------------------------------------------
} // stdtest