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
 
std::vector< std::string > anonymous_namespace{Config.cpp}::_fatalConfigOptions
 

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
442{
443 auto const& itr = _configOptions.find(name);
444 bool fatalConfig = false;
445 bool notFound = itr == _configOptions.end();
446 auto envVarName = GetEnvVarName(name);
447 Optional<std::string> envVar = GetEnvFromCache(name, envVarName);
448 if (envVar)
449 {
450 // If showLogs and this key/value pair wasn't found in the currently saved config
451 if (showLogs && (notFound || itr->second != envVar->c_str()))
452 {
453 LOG_INFO("server.loading", "> Config: Found config value '{}' from environment variable '{}'.", name, envVarName);
454 AddKey(name, *envVar, "ENVIRONMENT", false, false);
455 }
456
457 return *envVar;
458 }
459 else if (notFound)
460 {
461 if (showLogs)
462 {
463 for (std::string s : _fatalConfigOptions)
464 if (s == name)
465 {
466 fatalConfig = true;
467 break;
468 }
469
470 if (fatalConfig)
471 LOG_FATAL("server.loading", "> Config:\n\nFATAL ERROR: Missing property {} in config file {}, add \"{} = {}\" to this file or define '{}' as an environment variable.\n\nYour server cannot start without this option!",
472 name, _filename, name, def, envVarName);
473 else
474 LOG_WARN("server.loading", "> Config: Missing property {} in config file {}, add \"{} = {}\" to this file or define '{}' as an environment variable.",
475 name, _filename, name, def, envVarName);
476 }
477
478 return def;
479 }
480
481 return itr->second;
482}
#define LOG_FATAL(filterType__,...)
Definition: Log.h:153
#define LOG_INFO(filterType__,...)
Definition: Log.h:165
#define LOG_WARN(filterType__,...)
Definition: Log.h:161
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:281
std::vector< std::string > _fatalConfigOptions
Definition: Config.cpp:38
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:80
Optional< std::string > GetEnvFromCache(std::string const &configName, std::string const &envVarName)
Definition: Config.cpp:335

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

◆ GetEnvFromCache()

Optional< std::string > GetEnvFromCache ( std::string const &  configName,
std::string const &  envVarName 
)
336{
337 auto foundInCache = _envVarCache.find(envVarName);
338 Optional<std::string> foundInEnv;
339 // If it's not in the cache
340 if (foundInCache == _envVarCache.end())
341 {
342 // Check the env itself
343 foundInEnv = EnvVarForIniKey(configName);
344 if (foundInEnv)
345 {
346 // If it's found in the env, put it in the cache
347 _envVarCache.emplace(envVarName, *foundInEnv);
348 }
349 // Return the result of checking env
350 return foundInEnv;
351 }
352
353 return foundInCache->second;
354}
Optional< std::string > EnvVarForIniKey(std::string const &key)
Definition: Config.cpp:286
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().