AzerothCore 3.3.5a
OpenSource WoW Emulator
Loading...
Searching...
No Matches
Config.cpp File Reference
#include "Config.h"
#include "Log.h"
#include "StringConvert.h"
#include "StringFormat.h"
#include "Tokenize.h"
#include "Util.h"
#include <cstdlib>
#include <fstream>
#include <mutex>
#include <unordered_map>

Go to the source code of this file.

Namespaces

namespace  anonymous_namespace{Config.cpp}
 

Macros

#define TEMPLATE_CONFIG_OPTION(__typename)    template __typename ConfigMgr::GetOption<__typename>(std::string const& name, __typename const& def, bool showLogs /*= true*/) const;
 

Functions

bool anonymous_namespace{Config.cpp}::IsAppConfig (std::string_view fileName)
 
bool anonymous_namespace{Config.cpp}::IsLoggingSystemOptions (std::string_view optionName)
 
template<typename Format , typename... Args>
void anonymous_namespace{Config.cpp}::PrintError (std::string_view filename, Format &&fmt, Args &&... args)
 
void anonymous_namespace{Config.cpp}::AddKey (std::string const &optionName, std::string const &optionKey, std::string_view fileName, bool isOptional, bool isReload)
 
bool anonymous_namespace{Config.cpp}::ParseFile (std::string const &file, bool isOptional, bool isReload)
 
bool anonymous_namespace{Config.cpp}::LoadFile (std::string const &file, bool isOptional, bool isReload)
 
std::string anonymous_namespace{Config.cpp}::IniKeyToEnvVarKey (std::string const &key)
 
std::string anonymous_namespace{Config.cpp}::GetEnvVarName (std::string const &configName)
 
Optional< std::string > anonymous_namespace{Config.cpp}::EnvVarForIniKey (std::string const &key)
 
Optional< std::string > GetEnvFromCache (std::string const &configName, std::string const &envVarName)
 
template<>
std::string ConfigMgr::GetValueDefault< std::string > (std::string const &name, std::string const &def, bool showLogs) const
 

Variables

std::string anonymous_namespace{Config.cpp}::_filename
 
std::vector< std::string > anonymous_namespace{Config.cpp}::_additonalFiles
 
std::vector< std::string > anonymous_namespace{Config.cpp}::_args
 
std::unordered_map< std::string, std::string > anonymous_namespace{Config.cpp}::_configOptions
 
std::unordered_map< std::string, std::string > anonymous_namespace{Config.cpp}::_envVarCache
 
std::mutex anonymous_namespace{Config.cpp}::_configLock
 

Macro Definition Documentation

◆ TEMPLATE_CONFIG_OPTION

#define TEMPLATE_CONFIG_OPTION (   __typename)     template __typename ConfigMgr::GetOption<__typename>(std::string const& name, __typename const& def, bool showLogs /*= true*/) const;

Function Documentation

◆ ConfigMgr::GetValueDefault< std::string >()

template<>
std::string ConfigMgr::GetValueDefault< std::string > ( std::string const &  name,
std::string const &  def,
bool  showLogs 
) const
436{
437 auto const& itr = _configOptions.find(name);
438 bool notFound = itr == _configOptions.end();
439 auto envVarName = GetEnvVarName(name);
440 Optional<std::string> envVar = GetEnvFromCache(name, envVarName);
441 if (envVar)
442 {
443 // If showLogs and this key/value pair wasn't found in the currently saved config
444 if (showLogs && (notFound || itr->second != envVar->c_str()))
445 {
446 LOG_INFO("server.loading", "> Config: Found config value '{}' from environment variable '{}'.", name, envVarName);
447 AddKey(name, *envVar, "ENVIRONMENT", false, false);
448 }
449
450 return *envVar;
451 }
452 else if (notFound)
453 {
454 if (showLogs)
455 {
456 LOG_ERROR("server.loading", "> Config: Missing property {} in config file {}, add \"{} = {}\" to this file or define '{}' as an environment variable.",
457 name, _filename, name, def, envVarName);
458 }
459
460 return def;
461 }
462
463 return itr->second;
464}
#define LOG_INFO(filterType__,...)
Definition: Log.h:165
#define LOG_ERROR(filterType__,...)
Definition: Log.h:157
std::optional< T > Optional
Optional helper class to wrap optional values within.
Definition: Optional.h:24
std::string GetEnvVarName(std::string const &configName)
Definition: Config.cpp:287
std::unordered_map< std::string, std::string > _configOptions
Definition: Config.cpp:34
std::string _filename
Definition: Config.cpp:31
void AddKey(std::string const &optionName, std::string const &optionKey, std::string_view fileName, bool isOptional, bool isReload)
Definition: Config.cpp:72
Optional< std::string > GetEnvFromCache(std::string const &configName, std::string const &envVarName)
Definition: Config.cpp:341

References anonymous_namespace{Config.cpp}::_configOptions, anonymous_namespace{Config.cpp}::_filename, anonymous_namespace{Config.cpp}::AddKey(), GetEnvFromCache(), anonymous_namespace{Config.cpp}::GetEnvVarName(), LOG_ERROR, and LOG_INFO.

◆ GetEnvFromCache()

Optional< std::string > GetEnvFromCache ( std::string const &  configName,
std::string const &  envVarName 
)
342{
343 auto foundInCache = _envVarCache.find(envVarName);
344 Optional<std::string> foundInEnv;
345 // If it's not in the cache
346 if (foundInCache == _envVarCache.end())
347 {
348 // Check the env itself
349 foundInEnv = EnvVarForIniKey(configName);
350 if (foundInEnv)
351 {
352 // If it's found in the env, put it in the cache
353 _envVarCache.emplace(envVarName, *foundInEnv);
354 }
355 // Return the result of checking env
356 return foundInEnv;
357 }
358
359 return foundInCache->second;
360}
Optional< std::string > EnvVarForIniKey(std::string const &key)
Definition: Config.cpp:292
std::unordered_map< std::string, std::string > _envVarCache
Definition: Config.cpp:35

References anonymous_namespace{Config.cpp}::_envVarCache, and anonymous_namespace{Config.cpp}::EnvVarForIniKey().

Referenced by ConfigMgr::GetValueDefault< std::string >(), and ConfigMgr::GetValueDefault().