Add and use GetEnvironmentVariableValue (#997)
This commit is contained in:
parent
f5f0988baf
commit
dfed3d7563
|
|
@ -18,7 +18,7 @@ Environment::ExpandEnvironmentVariables(const std::wstring & str)
|
|||
do
|
||||
{
|
||||
expandedStr.resize(requestedSize);
|
||||
requestedSize = ExpandEnvironmentStringsW(str.c_str(), &expandedStr[0], requestedSize);
|
||||
requestedSize = ExpandEnvironmentStringsW(str.c_str(), expandedStr.data(), requestedSize);
|
||||
if (requestedSize == 0)
|
||||
{
|
||||
throw std::system_error(GetLastError(), std::system_category(), "ExpandEnvironmentVariables");
|
||||
|
|
@ -30,3 +30,31 @@ Environment::ExpandEnvironmentVariables(const std::wstring & str)
|
|||
|
||||
return expandedStr;
|
||||
}
|
||||
|
||||
std::optional<std::wstring>
|
||||
Environment::GetEnvironmentVariableValue(const std::wstring & str)
|
||||
{
|
||||
DWORD requestedSize = GetEnvironmentVariableW(str.c_str(), nullptr, 0);
|
||||
if (requestedSize == 0)
|
||||
{
|
||||
if (GetLastError() == ERROR_ENVVAR_NOT_FOUND)
|
||||
{
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
throw std::system_error(GetLastError(), std::system_category(), "GetEnvironmentVariableW");
|
||||
}
|
||||
|
||||
std::wstring expandedStr;
|
||||
do
|
||||
{
|
||||
expandedStr.resize(requestedSize);
|
||||
requestedSize = GetEnvironmentVariableW(str.c_str(), expandedStr.data(), requestedSize);
|
||||
if (requestedSize == 0)
|
||||
{
|
||||
throw std::system_error(GetLastError(), std::system_category(), "ExpandEnvironmentStringsW");
|
||||
}
|
||||
} while (expandedStr.size() != requestedSize + 1);
|
||||
|
||||
return expandedStr;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -4,6 +4,7 @@
|
|||
#pragma once
|
||||
|
||||
#include <string>
|
||||
#include <optional>
|
||||
|
||||
class Environment
|
||||
{
|
||||
|
|
@ -13,5 +14,7 @@ public:
|
|||
|
||||
static
|
||||
std::wstring ExpandEnvironmentVariables(const std::wstring & str);
|
||||
static
|
||||
std::optional<std::wstring> GetEnvironmentVariableValue(const std::wstring & str);
|
||||
};
|
||||
|
||||
|
|
|
|||
|
|
@ -53,7 +53,7 @@ DebugInitialize()
|
|||
|
||||
try
|
||||
{
|
||||
const auto value = std::stoi(Environment::ExpandEnvironmentVariables(L"%ASPNETCORE_MODULE_DEBUG%"));
|
||||
const auto value = std::stoi(Environment::GetEnvironmentVariableValue(L"ASPNETCORE_MODULE_DEBUG").value_or(L"0"));
|
||||
|
||||
if (value >= 1) DEBUG_FLAGS_VAR |= ASPNETCORE_DEBUG_FLAG_ERROR;
|
||||
if (value >= 2) DEBUG_FLAGS_VAR |= ASPNETCORE_DEBUG_FLAG_WARNING;
|
||||
|
|
@ -67,11 +67,11 @@ DebugInitialize()
|
|||
|
||||
try
|
||||
{
|
||||
auto debugOutputFile = Environment::ExpandEnvironmentVariables(L"%ASPNETCORE_MODULE_DEBUG_FILE%");
|
||||
const auto debugOutputFile = Environment::GetEnvironmentVariableValue(L"ASPNETCORE_MODULE_DEBUG_FILE");
|
||||
|
||||
if (!debugOutputFile.empty())
|
||||
if (debugOutputFile.has_value())
|
||||
{
|
||||
g_logFile = CreateFileW(debugOutputFile.c_str(),
|
||||
g_logFile = CreateFileW(debugOutputFile.value().c_str(),
|
||||
(GENERIC_READ | GENERIC_WRITE),
|
||||
(FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE),
|
||||
nullptr,
|
||||
|
|
|
|||
Loading…
Reference in New Issue