Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion BACKLOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

## bs_arg

* Support "extra" arguments of no type, eg. for `--help` or `--version`.
* Support "extra" arguments of no type, eg. for `--help` or `--version` ("switch").

## bs_file

Expand Down
12 changes: 0 additions & 12 deletions include/libbase/arg.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
#ifndef __LIBBASE_ARG_H__
#define __LIBBASE_ARG_H__

#include "log.h"
#include "test.h"

#include <inttypes.h>
Expand Down Expand Up @@ -206,17 +205,6 @@ void bs_arg_cleanup(const bs_arg_t *arg_ptr);
*/
int bs_arg_print_usage(FILE *stream_ptr, const bs_arg_t *arg_ptr);

/** Severity level names, for logging. Used for bs_arg_log_level. */
extern const bs_arg_enum_table_t bs_arg_log_level_names[];

/** Common commandline arg for `--log_level`. Sets @ref bs_log_severity. */
static const bs_arg_t bs_arg_log_level = BS_ARG_ENUM(
"log_level",
"Log level to apply. One of DEBUG, INFO, WARNING, ERROR.",
"INFO",
&bs_arg_log_level_names[0],
(int*)&bs_log_severity);

/** Unit test set. */
extern const bs_test_set_t bs_arg_test_set;

Expand Down
51 changes: 41 additions & 10 deletions include/libbase/log.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,13 @@
#ifndef __LIBBASE_LOG_H__
#define __LIBBASE_LOG_H__

#include "def.h"
#include "test.h"

#include <stdarg.h>
#include <stdbool.h>

#include "arg.h"
#include "def.h"
#include "test.h"

#ifdef __cplusplus
extern "C" {
#endif // __cplusplus
Expand All @@ -47,8 +48,16 @@ typedef enum {
BS_ERRNO = 0x80
} bs_log_severity_t;

/** Current severity for logging. Only equal-or-higher severity is logged. */
extern bs_log_severity_t bs_log_severity;
/** Defines the settings used for the logger. */
struct bs_log_settings {
/** Configured log severity. */
bs_log_severity_t severity;
/** Override: Log to stderr, even after bs_log_init_file(). */
bool log_to_stderr;
};

/** The logger's settings. */
extern struct bs_log_settings bs_log_settings;

/** Actually write a log message. Helper to bs_log. */
void bs_log_write(bs_log_severity_t severity,
Expand All @@ -69,19 +78,17 @@ void bs_log_vwrite(bs_log_severity_t severity,
*
* @param dirname_ptr Directory to log into. Uses "./" if NULL.
* @param filebase_ptr
* @param severity
*
* @return true on success. This function will fail if called more than once.
*/
bool bs_log_init_file(
bool bs_log_init(
const char *dirname_ptr,
const char *filebase_ptr,
bs_log_severity_t severity);
const char *filebase_ptr);

/** Returns whether log outut will happen for `severity`. */
static inline bool bs_will_log(bs_log_severity_t severity)
{
return ((severity & 0x7f) >= bs_log_severity ||
return ((severity & 0x7f) >= bs_log_settings.severity ||
(severity & 0x7f) == BS_FATAL);
}

Expand All @@ -94,6 +101,30 @@ static inline bool bs_will_log(bs_log_severity_t severity)
} \
}

/** Severity level names, for logging. Used for bs_arg_log_level. */
extern const bs_arg_enum_table_t bs_log_level_names[];

/**
* Common definition for commandline argument `--log_level`. Sets
* @ref bs_log_settings.severity.
*/
static const bs_arg_t bs_arg_log_level = BS_ARG_ENUM(
"log_level",
"Log level to apply. One of DEBUG, INFO, WARNING, ERROR.",
"INFO",
&bs_log_level_names[0],
(int*)&bs_log_settings.severity);

/**
* Common definition for commandline argument `--log_to_stderr`. Sets
* @ref bs_log_settings.log_to_stderr.
*/
static const bs_arg_t bs_arg_log_to_stderr = BS_ARG_BOOL(
"log_to_stderr",
"Overrules log output to be written to STDERR.",
false,
&bs_log_settings.log_to_stderr);

/** Unit testset. */
extern const bs_test_set_t bs_log_test_set;

Expand Down
6 changes: 3 additions & 3 deletions include/libbase/log_wrappers.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ static inline void *_logged_calloc(
const char *filename_ptr, int line_no, size_t nmemb, size_t size)
{
void *ptr = calloc(nmemb, size);
if ((NULL == ptr) && (BS_ERROR >= bs_log_severity)) {
if ((NULL == ptr) && bs_will_log(BS_ERROR)) {
bs_log_write((bs_log_severity_t)(BS_ERROR | BS_ERRNO), filename_ptr,
line_no, "Failed calloc(%zu, %zu)", nmemb, size);
}
Expand All @@ -50,7 +50,7 @@ static inline void *_logged_malloc(
const char *filename_ptr, int line_no, size_t size)
{
void *ptr = malloc(size);
if ((NULL == ptr) && (BS_ERROR >= bs_log_severity)) {
if ((NULL == ptr) && bs_will_log(BS_ERROR)) {
bs_log_write((bs_log_severity_t)(BS_ERROR | BS_ERRNO), filename_ptr,
line_no, "Failed malloc(%zu)", size);
}
Expand All @@ -69,7 +69,7 @@ static inline char *_logged_strdup(
char *new_str = malloc(len);
if (NULL != new_str) {
memcpy(new_str, str, len);
} else if (BS_ERROR >= bs_log_severity) {
} else if (bs_will_log(BS_ERROR)) {
bs_log_write((bs_log_severity_t)(BS_ERROR | BS_ERRNO), filename_ptr,
line_no, "Failed strdup(%s)", str);
}
Expand Down
30 changes: 0 additions & 30 deletions src/arg.c
Original file line number Diff line number Diff line change
Expand Up @@ -90,14 +90,6 @@ static bool lookup_enum(const bs_arg_enum_table_t *lookup_table,
static const char *bs_arg_bool_value_true = "true";
static const char *bs_arg_bool_value_false = "false";

const bs_arg_enum_table_t bs_arg_log_level_names[] = {
{ .name_ptr = "DEBUG", BS_DEBUG },
{ .name_ptr = "INFO", BS_INFO },
{ .name_ptr = "WARNING", BS_WARNING },
{ .name_ptr = "ERROR", BS_ERROR },
{ .name_ptr = NULL },
};

/* == Exported methods ===================================================== */

/* ------------------------------------------------------------------------- */
Expand Down Expand Up @@ -681,7 +673,6 @@ static void bs_arg_test_parse_arg_for_enum(bs_test_t *test_ptr);
static void bs_arg_test_set_defaults(bs_test_t *test_ptr);
static void bs_arg_test_parse(bs_test_t *test_ptr);
static void bs_arg_test_check_arg(bs_test_t *test_ptr);
static void bs_arg_test_log_level(bs_test_t *test_ptr);

/** Unit test cases. */
static const bs_test_case_t bs_arg_test_cases[] = {
Expand All @@ -696,7 +687,6 @@ static const bs_test_case_t bs_arg_test_cases[] = {
{ true, "set_defaults", bs_arg_test_set_defaults },
{ true, "parse", bs_arg_test_parse },
{ true, "check_arg", bs_arg_test_check_arg },
{ true, "log_level", bs_arg_test_log_level },
{ false, NULL, NULL }
};

Expand Down Expand Up @@ -1040,24 +1030,4 @@ static void bs_arg_test_check_arg(bs_test_t *test_ptr)
BS_TEST_VERIFY_TRUE(test_ptr, check_arg(valid_args));
}

/* ------------------------------------------------------------------------- */
void bs_arg_test_log_level(bs_test_t *test_ptr)
{
static const bs_arg_t args[] = {
bs_arg_log_level,
BS_ARG_SENTINEL()
};

const char *argv[] = { "program", "--log_level", "DEBUG" };
int argc = sizeof(argv) / sizeof(const char*);

bs_log_severity_t log_level = bs_log_severity;
bs_log_severity = BS_INFO;
BS_TEST_VERIFY_TRUE(
test_ptr,
bs_arg_parse(args, BS_ARG_MODE_NO_EXTRA, &argc, argv));
BS_TEST_VERIFY_EQ(test_ptr, BS_DEBUG, bs_log_severity);
bs_log_severity = log_level;
}

/* == End of arg.c ========================================================= */
Loading
Loading