int log_config_set(struct log_config* config, const char* key, const char* value);
const char* log_config_get(struct log_config* config, const char* key);
+int log_config_get_int(struct log_config* config, const char* key, int default_val);
+int log_config_get_boolean(struct log_config* config, const char* key, int default_val);
int log_config_read(struct log_config* config);
int log_config_read_file(struct log_config* config, char const* filename);
int log_config_write(struct log_config* config, char const* filename);
#include <unistd.h>
#include <fcntl.h>
#include <logcommon.h>
+#include <limits.h>
/**
* @addtogroup SHARED_FUNCTIONS
}
/**
+ * @brief Convert decimal number string to int
+ * @param[in] str string of decimal number
+ * @param[out] num a place to store the result
+ * @return 0 on success otherwise -errno
+ * @see strtol(3)
+ */
+static int str2num(const char *str, int *num)
+{
+ if (str == NULL)
+ return -EINVAL;
+
+ errno = 0; /* To distinguish success/failure after call */
+ char *endptr;
+ int val = strtol(str, &endptr, 10);
+
+ /* Check for various possible errors */
+ if ((errno == ERANGE && (val == LONG_MAX || val == LONG_MIN))
+ || (errno != 0 && val == 0))
+ return -errno;
+
+ else if (endptr == str)
+ return -EINVAL; /* No digits were found */
+
+ /* If we got here, strtol() successfully parsed a number */
+ *num = val;
+ return 0;
+}
+
+/**
+ * @brief Get a config value converted to int
+ * @details Returns a value with the given key from the given config
+ * @param[in] c The config to work with
+ * @param[in] key The key of the value entry to get
+ * @param[in] default_val Default int value to return when proper value not given in config
+ * @return The value of the entry if such exists, else default value
+ * @see log_config_get
+ */
+int log_config_get_int(struct log_config* c, const char* key, int default_val)
+{
+ int val;
+ return (str2num(log_config_get(c, key), &val) == 0 ? val : default_val);
+}
+
+/**
+ * @brief Get a config value converted to boolean
+ * @details Returns TRUE if config string is non-zero digital number
+ * @param[in] c The config to work with
+ * @param[in] key The key of the value entry to get
+ * @param[in] default_val Default boolean value to return when proper value not given in config
+ * @return The value of the entry if such exists, else default value
+ * @see log_config_get_int
+ */
+int log_config_get_boolean(struct log_config* c, const char* key, int default_val)
+{
+ return log_config_get_int(c, key, default_val);
+}
+
+/**
* @brief Set a config value
* @details Sets the entry with the given key in the given config to the given value
* @param[in] c The config to work with