From bf8ce965b3f09ee38a4ab30c14f0c55abe9f0abe Mon Sep 17 00:00:00 2001 From: dufourj Date: Mon, 13 Mar 2006 20:32:08 +0000 Subject: Config : - getter avec valeur par défaut. Log : - plus besoin de try ; - valeur par défaut du niveau de log en dure (plus besoin de la mettre dans config). --- i/marvin/runtime/rc/config | 2 +- i/marvin/src/config/config.hh | 13 +++++++++++++ i/marvin/src/config/config_data.cc | 13 +++++++++++++ i/marvin/src/config/config_data.hh | 8 ++++++++ i/marvin/src/config/config_data.tcc | 20 ++++++++++++++++++++ i/marvin/src/log/log.cc | 16 ++++------------ 6 files changed, 59 insertions(+), 13 deletions(-) (limited to 'i') diff --git a/i/marvin/runtime/rc/config b/i/marvin/runtime/rc/config index d01fb8a..6be1ca2 100644 --- a/i/marvin/runtime/rc/config +++ b/i/marvin/runtime/rc/config @@ -29,7 +29,7 @@ motor.pStatMotor = 0 ### Log # Default minimum level outputed -log.level.default = "info" +# log.level.default = "debug" # Can be specialized for a specific module # For example, with the module asserv # log.level.asserv = "debug" diff --git a/i/marvin/src/config/config.hh b/i/marvin/src/config/config.hh index ab9cfb7..3034b1a 100644 --- a/i/marvin/src/config/config.hh +++ b/i/marvin/src/config/config.hh @@ -67,11 +67,24 @@ class Config { return configData_.get (id); } + /// Récupère une valeur de configuration, fonction générique avec valeur + /// par défaut pour éviter l'exception en cas d'abscence de valeur. + template + const T &get (const std::string &id, const T &defaut) const + { + return configData_.get (id, defaut); + } /// Récupère une valeur de configuration. const any &get (const std::string &id) const { return configData_.get (id); } + /// Récupère une valeur de configuration avec valeur par défaut pour + /// éviter le renvoie d'exception. + const any &get (const std::string &id, const any &defaut) const + { + return configData_.get (id, defaut); + } /// Get the unique instance of the class static inline Config & getInstance () diff --git a/i/marvin/src/config/config_data.cc b/i/marvin/src/config/config_data.cc index a8721fb..2642e74 100644 --- a/i/marvin/src/config/config_data.cc +++ b/i/marvin/src/config/config_data.cc @@ -53,6 +53,19 @@ ConfigData::get (const std::string &id) const return i->second; } +/// Récupère une valeur de configuration avec une valeur par défaut. Ne +/// renvoye pas d'exception. +const any & +ConfigData::get (const std::string &id, const any &defaut) const +{ + Data::const_iterator i = data_.find (id); + // If not found, return default values + if (i == data_.end ()) + return defaut; + else + return i->second; +} + /// Ajoute une valeur de configuration. VAL prend l'ancienne valeur ou un any /// vide. void diff --git a/i/marvin/src/config/config_data.hh b/i/marvin/src/config/config_data.hh index a0dbacf..8c8bf1f 100644 --- a/i/marvin/src/config/config_data.hh +++ b/i/marvin/src/config/config_data.hh @@ -45,8 +45,16 @@ class ConfigData /// Récupère une valeur de configuration, fonction générique. template const T &get (const std::string &id) const; + /// Récupère une valeur de configuration, fonction générique avec valeur + /// par défaut. Cette fonction ne renvoie d'exception que si il y a un + /// problème de type et non de valeur non existante. + template + const T &get (const std::string &id, const T &defaut) const; /// Récupère une valeur de configuration. const any &get (const std::string &id) const; + /// Récupère une valeur de configuration avec une valeur par défaut. Ne + /// renvoye pas d'exception. + const any &get (const std::string &id, const any &defaut) const; /// Ajoute une valeur de configuration. VAL prend l'ancienne valeur ou un /// any vide. void add (const std::string &id, any &val); diff --git a/i/marvin/src/config/config_data.tcc b/i/marvin/src/config/config_data.tcc index 31b62bc..7e5b905 100644 --- a/i/marvin/src/config/config_data.tcc +++ b/i/marvin/src/config/config_data.tcc @@ -37,6 +37,26 @@ ConfigData::get (const std::string &id) const return *v; } +/// Récupère une valeur de configuration, fonction générique avec valeur +/// par défaut. Cette fonction ne renvoie d'exception que si il y a un +/// problème de type et non de valeur non existante. +template +const T & +ConfigData::get (const std::string &id, const T &defaut) const +{ + // Construct an empty any + any empty; + // Get the value with an empty one by default + const any &a = get (id, empty); + // Empty value returned, not found + if (a.empty ()) + return defaut; + const T *v = any_cast (&a); + if (!v) + throw std::runtime_error (id + ": config item with bad type"); + return *v; +} + /// Ajoute une valeur de configuration, fonction générique. Attention, /// cette fonction est plus couteuse car elle fait une copie de /// la valeur. diff --git a/i/marvin/src/log/log.cc b/i/marvin/src/log/log.cc index ba5faa8..dd2227b 100644 --- a/i/marvin/src/log/log.cc +++ b/i/marvin/src/log/log.cc @@ -37,20 +37,12 @@ Log::Log (const char *module, const char *instance) // Get Config Config &config = Config::getInstance (); // Get default level log - level_ = toLevel (config.get("log.level.default")); + std::string defaultLvl = config.get("log.level.default", + "info"); + level_ = toLevel (defaultLvl); // Get private level if exist - if (module) - { - try - { - level_ = toLevel (config.get(std::string ("log.level.") + module)); - } - catch (std::exception &) - { - // Do nothing if it does not exist - } - } + level_ = toLevel (config.get(std::string ("log.level.") + module, defaultLvl)); } /// Crée un nouveau LogMessage. -- cgit v1.2.3