Class Configurations

Synopsis

#include <src/easylogging++.h>

class Configurations : public base::utils::RegistryWithPred<Configuration, Configuration::Predicate>

Description

Thread-safe Configuration repository.

@detail This repository represents configurations for all the levels and configuration type mapped to a value.

Mentioned in

Inheritance

Ancestors: RegistryWithPred

Classes

ParserParser used internally to parse configurations from file or text.

Methods

Configurations overloadDefault constructor with empty repository.
Configurations overloadConstructor used to set configurations using configuration file.
~Configurations
clearClears repository so that all the configurations are unset.
configurationFileGets configuration file used in parsing this configurations.
get
hasConfiguration overloadDetermines whether or not specified configuration type exists in the repository.
hasConfiguration overloadDetermines whether or not specified configuration type exists for specified level.
parseFromFileParses configuration from file.
parseFromTextParse configurations from configuration string.
set overloadSets value of configuration for specified level.
set overloadSets single configuration based on other single configuration.
setFromBaseSets configuration based-off an existing configurations.
setGloballySets configuration for all levels.
setRemainingToDefaultLets you set the remaining configurations to default.
setToDefaultSets configurations to "factory based" configurations.

Source

Lines 1733-1896 in src/easylogging++.h.

class Configurations : public base::utils::RegistryWithPred<Configuration, Configuration::Predicate> {
 public:
  /// @brief Default constructor with empty repository
  Configurations(void);

  /// @brief Constructor used to set configurations using configuration file.
  /// @param configurationFile Full path to configuration file
  /// @param useDefaultsForRemaining Lets you set the remaining configurations to default.
  /// @param base If provided, this configuration will be based off existing repository that this argument is pointing to.
  /// @see parseFromFile(const std::string&, Configurations* base)
  /// @see setRemainingToDefault()
  Configurations(const std::string& configurationFile, bool useDefaultsForRemaining = true,
                 Configurations* base = nullptr);

  virtual ~Configurations(void) {
  }

  /// @brief Parses configuration from file.
  /// @param configurationFile Full path to configuration file
  /// @param base Configurations to base new configuration repository off. This value is used when you want to use
  ///        existing Configurations to base all the values and then set rest of configuration via configuration file.
  /// @return True if successfully parsed, false otherwise. You may define 'ELPP_DEBUG_ASSERT_FAILURE' to make sure you
  ///         do not proceed without successful parse.
  bool parseFromFile(const std::string& configurationFile, Configurations* base = nullptr);

  /// @brief Parse configurations from configuration string.
  ///
  /// @detail This configuration string has same syntax as configuration file contents. Make sure all the necessary
  /// new line characters are provided.
  /// @param base Configurations to base new configuration repository off. This value is used when you want to use
  ///        existing Configurations to base all the values and then set rest of configuration via configuration text.
  /// @return True if successfully parsed, false otherwise. You may define 'ELPP_DEBUG_ASSERT_FAILURE' to make sure you
  ///         do not proceed without successful parse.
  bool parseFromText(const std::string& configurationsString, Configurations* base = nullptr);

  /// @brief Sets configuration based-off an existing configurations.
  /// @param base Pointer to existing configurations.
  void setFromBase(Configurations* base);

  /// @brief Determines whether or not specified configuration type exists in the repository.
  ///
  /// @detail Returns as soon as first level is found.
  /// @param configurationType Type of configuration to check existence for.
  bool hasConfiguration(ConfigurationType configurationType);

  /// @brief Determines whether or not specified configuration type exists for specified level
  /// @param level Level to check
  /// @param configurationType Type of configuration to check existence for.
  bool hasConfiguration(Level level, ConfigurationType configurationType);

  /// @brief Sets value of configuration for specified level.
  ///
  /// @detail Any existing configuration for specified level will be replaced. Also note that configuration types
  /// ConfigurationType::SubsecondPrecision and ConfigurationType::PerformanceTracking will be ignored if not set for
  /// Level::Global because these configurations are not dependant on level.
  /// @param level Level to set configuration for (el::Level).
  /// @param configurationType Type of configuration (el::ConfigurationType)
  /// @param value A string based value. Regardless of what the data type of configuration is, it will always be string
  /// from users' point of view. This is then parsed later to be used internally.
  /// @see Configuration::setValue(const std::string& value)
  /// @see el::Level
  /// @see el::ConfigurationType
  void set(Level level, ConfigurationType configurationType, const std::string& value);

  /// @brief Sets single configuration based on other single configuration.
  /// @see set(Level level, ConfigurationType configurationType, const std::string& value)
  void set(Configuration* conf);

  inline Configuration* get(Level level, ConfigurationType configurationType) {
    base::threading::ScopedLock scopedLock(lock());
    return RegistryWithPred<Configuration, Configuration::Predicate>::get(level, configurationType);
  }

  /// @brief Sets configuration for all levels.
  /// @param configurationType Type of configuration
  /// @param value String based value
  /// @see Configurations::set(Level level, ConfigurationType configurationType, const std::string& value)
  inline void setGlobally(ConfigurationType configurationType, const std::string& value) {
    setGlobally(configurationType, value, false);
  }

  /// @brief Clears repository so that all the configurations are unset
  inline void clear(void) {
    base::threading::ScopedLock scopedLock(lock());
    unregisterAll();
  }

  /// @brief Gets configuration file used in parsing this configurations.
  ///
  /// @detail If this repository was set manually or by text this returns empty string.
  inline const std::string& configurationFile(void) const {
    return m_configurationFile;
  }

  /// @brief Sets configurations to "factory based" configurations.
  void setToDefault(void);

  /// @brief Lets you set the remaining configurations to default.
  ///
  /// @detail By remaining, it means that the level/type a configuration does not exist for.
  /// This function is useful when you want to minimize chances of failures, e.g, if you have a configuration file that sets
  /// configuration for all the configurations except for Enabled or not, we use this so that ENABLED is set to default i.e,
  /// true. If you dont do this explicitly (either by calling this function or by using second param in Constructor
  /// and try to access a value, an error is thrown
  void setRemainingToDefault(void);

  /// @brief Parser used internally to parse configurations from file or text.
  ///
  /// @detail This class makes use of base::utils::Str.
  /// You should not need this unless you are working on some tool for Easylogging++
  class Parser : base::StaticClass {
   public:
    /// @brief Parses configuration from file.
    /// @param configurationFile Full path to configuration file
    /// @param sender Sender configurations pointer. Usually 'this' is used from calling class
    /// @param base Configurations to base new configuration repository off. This value is used when you want to use
    ///        existing Configurations to base all the values and then set rest of configuration via configuration file.
    /// @return True if successfully parsed, false otherwise. You may define '_STOP_ON_FIRSTELPP_ASSERTION' to make sure you
    ///         do not proceed without successful parse.
    static bool parseFromFile(const std::string& configurationFile, Configurations* sender,
                              Configurations* base = nullptr);

    /// @brief Parse configurations from configuration string.
    ///
    /// @detail This configuration string has same syntax as configuration file contents. Make sure all the necessary
    /// new line characters are provided. You may define '_STOP_ON_FIRSTELPP_ASSERTION' to make sure you
    /// do not proceed without successful parse (This is recommended)
    /// @param configurationsString the configuration in plain text format
    /// @param sender Sender configurations pointer. Usually 'this' is used from calling class
    /// @param base Configurations to base new configuration repository off. This value is used when you want to use
    ///        existing Configurations to base all the values and then set rest of configuration via configuration text.
    /// @return True if successfully parsed, false otherwise.
    static bool parseFromText(const std::string& configurationsString, Configurations* sender,
                              Configurations* base = nullptr);

   private:
    friend class el::Loggers;
    static void ignoreComments(std::string* line);
    static bool isLevel(const std::string& line);
    static bool isComment(const std::string& line);
    static inline bool isConfig(const std::string& line);
    static bool parseLine(std::string* line, std::string* currConfigStr, std::string* currLevelStr, Level* currLevel,
                          Configurations* conf);
  };

 private:
  std::string m_configurationFile;
  bool m_isFromFile;
  friend class el::Loggers;

  /// @brief Unsafely sets configuration if does not already exist
  void unsafeSetIfNotExist(Level level, ConfigurationType configurationType, const std::string& value);

  /// @brief Thread unsafe set
  void unsafeSet(Level level, ConfigurationType configurationType, const std::string& value);

  /// @brief Sets configurations for all levels including Level::Global if includeGlobalLevel is true
  /// @see Configurations::setGlobally(ConfigurationType configurationType, const std::string& value)
  void setGlobally(ConfigurationType configurationType, const std::string& value, bool includeGlobalLevel);

  /// @brief Sets configurations (Unsafely) for all levels including Level::Global if includeGlobalLevel is true
  /// @see Configurations::setGlobally(ConfigurationType configurationType, const std::string& value)
  void unsafeSetGlobally(ConfigurationType configurationType, const std::string& value, bool includeGlobalLevel);
};





Add Discussion

Log in to comment