#ifndef _LOGCONFIG_H_
#define _LOGCONFIG_H_
+#include <stdbool.h>
+
#define MAX_CONF_KEY_LEN 32
#define MAX_CONF_VAL_LEN 256
#define MAX_CONF_ENTRY_LEN (MAX_CONF_KEY_LEN + MAX_CONF_VAL_LEN + 2) // +2 for the delimiter and newline
void log_config_set(struct log_config *config, const char *key, const char *value);
const char *log_config_get(const struct log_config *config, const char *key);
int log_config_get_int(const struct log_config *config, const char *key, int default_val);
-int log_config_get_boolean(const struct log_config *config, const char *key, int default_val);
+bool log_config_get_boolean(const struct log_config *config, const char *key, bool default_val);
int log_config_read(struct log_config *config);
int log_config_read_file(struct log_config *config, const char *filename);
void log_config_free(struct log_config *config);
#ifndef PTRS_LIST_INCLUDED
#define PTRS_LIST_INCLUDED
+#include <stdbool.h>
+
struct list_elem;
/**
* @brief Condition callback for list element
* @param value list element value
* @param user_data pointer to user data to be forwarded to callback
- * @return nonzero if condition if fulfilled, zero if not
+ * @return true if condition if fulfilled, false if not
*/
-typedef int (*cond_cb)(elem_value value, void *user_data);
+typedef bool (*cond_cb)(elem_value value, void *user_data);
/**
* @brief Apply callback to list element
* @details Push front a new element
* @param[in,out] head pointer to the list head
* @param[in] value user data pointer
- * @return nonzero on success, zero on error (bad alloc)
+ * @return true on success, false on allocation failure
*/
-int list_add(list_head *head, elem_value value);
+bool list_add(list_head *head, elem_value value);
/**
* @brief Remove list elements equal to given parameter
list_clear(list_from);
}
-static int log_storage_reader_check_final(void *reader_, void *lse_)
+static bool log_storage_reader_check_final(void *reader_, void *lse_)
{
assert(reader_);
assert(lse_);
#include <ctype.h>
#include <fcntl.h>
#include <signal.h>
+#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <pwd.h>
DLOG_EXIT_ERR_RUNTIME
};
-enum { FALSE = 0, TRUE };
-
/* Size in bytes of the pipe given to libdlog clients.
* The default size (1 page, usually 4 kiB) is fairly small
* which leads programs that log a lot to easily clog the
* @details Use this function when deleting all readers in the list
* @param[in] ptr pointer to the reader
* @param[in] user_data pointer to the logger to remove readers waiting for an event
- * @return always nonzero (the reader is to be removed)
+ * @return always true (the reader is to be removed)
*/
-static int cond_reader_free(void *ptr, void *user_data)
+static bool cond_reader_free(void *ptr, void *user_data)
{
struct reader *reader = (struct reader *)ptr;
struct logger *logger = (struct logger *)user_data;
if (reader->fd_entity.fd >= 0)
remove_fd_entity(logger, &reader->fd_entity);
reader_free(reader);
- return TRUE;
+ return true;
}
/**
* @details Use this function when servicing all readers in the list
* @param[in] ptr pointer to the reader
* @param[in] user_data pointer to the logger to add readers waiting for an event
- * @return nonzero if the reader is to be removed
+ * @return true if the reader is to be removed
*/
-static int cond_service_reader(void *ptr, void *user_data)
+static bool cond_service_reader(void *ptr, void *user_data)
{
assert(ptr);
assert(user_data);
struct logger *logger = (struct logger *)user_data;
if (!logger->exiting && reader_should_buffer(reader, &logger->buf_params, logger->now))
- return FALSE;
+ return false;
assert(reader->service_reader);
int r = reader->service_reader(reader);
if (reader->fd_entity.fd >= 0)
remove_fd_entity(logger, &reader->fd_entity);
reader_free(reader);
- return TRUE;
+ return true;
}
/* `service_reader()` returns -1 if everything was flushed, or 0 if
* to such buffer would leak until a log finally arrived (which could
* be never). This is why waiting is also done on EPOLLHUP. */
modify_fd_entity(logger, &reader->fd_entity, (r == 0) ? EPOLLOUT : EPOLLHUP);
- return FALSE;
+ return false;
}
/**
return 0;
}
-static int cond_writer_free(void *ptr, void *user_data)
+static bool cond_writer_free(void *ptr, void *user_data)
{
writer_free((struct writer *)ptr, user_data);
- return TRUE;
+ return true;
}
/**
}
data->is_buffer_enabled[LOG_ID_KMSG] = 1;
data->is_buffer_enabled[LOG_ID_SYSLOG] =
- dev_log_sock_get() >= 0 || log_config_get_boolean(&conf, "syslog_force", FALSE);
+ dev_log_sock_get() >= 0 || log_config_get_boolean(&conf, "syslog_force", false);
for (log_id_t buf_id = 0; buf_id < LOG_ID_MAX; ++buf_id) {
if (data->is_buffer_enabled[buf_id]) {
return ret;
}
-static int cond_string_free(void *ptr, void *user_data)
+static bool cond_string_free(void *ptr, void *user_data)
{
free(ptr);
- return TRUE;
+ return true;
}
static void free_config_data(struct logger_config_data *data)
#include <fcntl.h>
#include <logcommon.h>
#include <limits.h>
+#include <stdbool.h>
/**
* @addtogroup SHARED_FUNCTIONS
/**
* @brief Get a config value converted to boolean
- * @details Returns TRUE if config string is non-zero digital number
+ * @details Returns true if config string is a non-zero 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
+ * @return If the entry exists and is a number, its value cast to boolean; else default value
* @see log_config_get_int
*/
-int log_config_get_boolean(const struct log_config *c, const char *key, int default_val)
+bool log_config_get_boolean(const struct log_config *c, const char *key, bool default_val)
{
- return log_config_get_int(c, key, default_val);
+ return !!log_config_get_int(c, key, default_val);
}
/**
#include <stdlib.h>
#include <ptrs_list.h>
-enum { FALSE = 0, TRUE };
-
struct list_elem {
struct list_elem *next;
elem_value value;
* with some primitive values like integers
* @param[in] value list value to compare
* @param[in] user_data pointer to value to compare with
- * @return nonzero if user data points on the same value
+ * @return true if user data points on the same value
*/
-static int cond_value_equal(elem_value value, void *user_data)
+static bool cond_value_equal(elem_value value, void *user_data)
{
return (value == *((elem_value *)user_data));
}
(*counter)++;
}
-int list_add(list_head *head, elem_value value)
+bool list_add(list_head *head, elem_value value)
{
assert(head); //valid pointer
struct list_elem *elem = (struct list_elem *)calloc(1, sizeof(struct list_elem));
if (elem == NULL)
- return FALSE;
+ return false;
*elem = (struct list_elem) {
.next = *head,
};
(*head) = elem;
- return TRUE;
+ return true;
}
void list_remove(list_head *head, elem_value value)
get = log_config_get(&config, "valid_extra_conf");
assert(get);
assert(!strncmp(get, "123", 4));
+ assert(log_config_get_int(&config, "valid_extra_conf", 567) == 123);
get = log_config_get(&config, "invalid_extra_conf");
assert(!get);
+ assert(log_config_get_int(&config, "invalid_extra_conf", 678) == 678);
/* test existing key modification */
log_config_set(&config, "foo", "quux");
assert(get);
assert(!strncmp(get, "ok", 3));
+ /* test boolean conversion */
+ log_config_set(&config, "b_true" , "1" );
+ log_config_set(&config, "b_false", "0" );
+ log_config_set(&config, "b_none" , "foo");
+
+ assert( log_config_get_boolean(&config, "b_true" , true ));
+ assert( log_config_get_boolean(&config, "b_true" , false));
+ assert(!log_config_get_boolean(&config, "b_false", true ));
+ assert(!log_config_get_boolean(&config, "b_false", false));
+ assert( log_config_get_boolean(&config, "b_none" , true ));
+ assert(!log_config_get_boolean(&config, "b_none" , false));
+
return 0;
}