chore: warn when parsing environment variable fails (#3904)

* chore: warn when parsing environment variable fails

* doc: update changelog
This commit is contained in:
Leon Richardt 2022-08-06 13:38:10 +02:00 committed by GitHub
parent ebc7852f9f
commit 2dd37ca210
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 69 additions and 0 deletions

View file

@ -26,6 +26,7 @@
- Minor: Removed total views from the usercard, as Twitch no longer updates the number. (#3792) - Minor: Removed total views from the usercard, as Twitch no longer updates the number. (#3792)
- Minor: Add Quick Switcher item to open a channel in a new popup window. (#3828) - Minor: Add Quick Switcher item to open a channel in a new popup window. (#3828)
- Minor: Reduced GIF frame window from 30ms to 20ms, causing fewer frame skips in animated emotes. (#3886) - Minor: Reduced GIF frame window from 30ms to 20ms, causing fewer frame skips in animated emotes. (#3886)
- Minor: Warn when parsing an environment variable fails. (#3904)
- Bugfix: Fix crash that can occur when closing and quickly reopening a split, then running a command. (#3852) - Bugfix: Fix crash that can occur when closing and quickly reopening a split, then running a command. (#3852)
- Bugfix: Connection to Twitch PubSub now recovers more reliably. (#3643, #3716) - Bugfix: Connection to Twitch PubSub now recovers more reliably. (#3643, #3716)
- Bugfix: Fix crash that can occur when changing channels. (#3799) - Bugfix: Fix crash that can occur when changing channels. (#3799)

View file

@ -332,6 +332,7 @@ set(SOURCE_FILES
util/StreamerMode.hpp util/StreamerMode.hpp
util/Twitch.cpp util/Twitch.cpp
util/Twitch.hpp util/Twitch.hpp
util/TypeName.hpp
util/WindowsHelper.cpp util/WindowsHelper.cpp
util/WindowsHelper.hpp util/WindowsHelper.hpp

View file

@ -1,11 +1,38 @@
#include "common/Env.hpp" #include "common/Env.hpp"
#include "common/QLogging.hpp"
#include "util/TypeName.hpp"
#include <QVariant> #include <QVariant>
namespace chatterino { namespace chatterino {
namespace { namespace {
template <typename T>
void warn(const char *envName, T defaultValue)
{
auto *envString = std::getenv(envName);
if (!envString)
{
// This function is not supposed to be used for non-existant
// environment variables.
return;
}
const auto typeName = QString::fromStdString(
std::string(type_name<decltype(defaultValue)>()));
qCWarning(chatterinoEnv).noquote()
<< QStringLiteral(
"Cannot parse value '%1' of environment variable '%2' "
"as a %3, reverting to default value '%4'")
.arg(envString)
.arg(envName)
.arg(typeName)
.arg(defaultValue);
}
QString readStringEnv(const char *envName, QString defaultValue) QString readStringEnv(const char *envName, QString defaultValue)
{ {
auto envString = std::getenv(envName); auto envString = std::getenv(envName);
@ -28,6 +55,10 @@ namespace {
{ {
return val; return val;
} }
else
{
warn(envName, defaultValue);
}
} }
return defaultValue; return defaultValue;

View file

@ -13,6 +13,7 @@ Q_LOGGING_CATEGORY(chatterinoBttv, "chatterino.bttv", logThreshold);
Q_LOGGING_CATEGORY(chatterinoCache, "chatterino.cache", logThreshold); Q_LOGGING_CATEGORY(chatterinoCache, "chatterino.cache", logThreshold);
Q_LOGGING_CATEGORY(chatterinoCommon, "chatterino.common", logThreshold); Q_LOGGING_CATEGORY(chatterinoCommon, "chatterino.common", logThreshold);
Q_LOGGING_CATEGORY(chatterinoEmoji, "chatterino.emoji", logThreshold); Q_LOGGING_CATEGORY(chatterinoEmoji, "chatterino.emoji", logThreshold);
Q_LOGGING_CATEGORY(chatterinoEnv, "chatterino.env", logThreshold);
Q_LOGGING_CATEGORY(chatterinoFfzemotes, "chatterino.ffzemotes", logThreshold); Q_LOGGING_CATEGORY(chatterinoFfzemotes, "chatterino.ffzemotes", logThreshold);
Q_LOGGING_CATEGORY(chatterinoHelper, "chatterino.helper", logThreshold); Q_LOGGING_CATEGORY(chatterinoHelper, "chatterino.helper", logThreshold);
Q_LOGGING_CATEGORY(chatterinoHotkeys, "chatterino.hotkeys", logThreshold); Q_LOGGING_CATEGORY(chatterinoHotkeys, "chatterino.hotkeys", logThreshold);

View file

@ -9,6 +9,7 @@ Q_DECLARE_LOGGING_CATEGORY(chatterinoBttv);
Q_DECLARE_LOGGING_CATEGORY(chatterinoCache); Q_DECLARE_LOGGING_CATEGORY(chatterinoCache);
Q_DECLARE_LOGGING_CATEGORY(chatterinoCommon); Q_DECLARE_LOGGING_CATEGORY(chatterinoCommon);
Q_DECLARE_LOGGING_CATEGORY(chatterinoEmoji); Q_DECLARE_LOGGING_CATEGORY(chatterinoEmoji);
Q_DECLARE_LOGGING_CATEGORY(chatterinoEnv);
Q_DECLARE_LOGGING_CATEGORY(chatterinoFfzemotes); Q_DECLARE_LOGGING_CATEGORY(chatterinoFfzemotes);
Q_DECLARE_LOGGING_CATEGORY(chatterinoHelper); Q_DECLARE_LOGGING_CATEGORY(chatterinoHelper);
Q_DECLARE_LOGGING_CATEGORY(chatterinoHotkeys); Q_DECLARE_LOGGING_CATEGORY(chatterinoHotkeys);

34
src/util/TypeName.hpp Normal file
View file

@ -0,0 +1,34 @@
#pragma once
#include <string_view>
namespace chatterino {
// Adapted from: https://stackoverflow.com/a/56766138.
// NOTE: Relies on the "magic" prefixes and suffixes. There are implementations
// that attempt to manually detect these (linked in the SO answer above) but
// they seemed too complex for the scope we have here.
template <typename T>
constexpr auto type_name()
{
std::string_view name, prefix, suffix;
#ifdef __clang__
name = __PRETTY_FUNCTION__;
prefix = "auto chatterino::type_name() [T = ";
suffix = "]";
#elif defined(__GNUC__)
name = __PRETTY_FUNCTION__;
prefix = "constexpr auto chatterino::type_name() [with T = ";
suffix = "]";
#elif defined(_MSC_VER)
name = __FUNCSIG__;
prefix = "auto __cdecl chatterino::type_name<";
suffix = ">(void)";
#endif
name.remove_prefix(prefix.size());
name.remove_suffix(suffix.size());
return name;
}
} // namespace chatterino