Skip to content

Commit

Permalink
log: support multiple log handles
Browse files Browse the repository at this point in the history
  • Loading branch information
mrc0mmand committed Jul 11, 2022
1 parent 66c9f45 commit f10b206
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 15 deletions.
4 changes: 2 additions & 2 deletions src/dfuzzer.c
Original file line number Diff line number Diff line change
Expand Up @@ -712,10 +712,10 @@ static void df_parse_parameters(int argc, char **argv)
df_supflg = 1;
break;
case 'd':
df_set_log_level(DF_LOG_LEVEL_DEBUG);
df_set_log_level(DF_LOG_HANDLE_GENERAL, DF_LOG_LEVEL_DEBUG);
break;
case 'v':
df_set_log_level(DF_LOG_LEVEL_VERBOSE);
df_set_log_level(DF_LOG_HANDLE_GENERAL, DF_LOG_LEVEL_VERBOSE);
break;
case 'l':
df_list_names = 1;
Expand Down
4 changes: 2 additions & 2 deletions src/fuzz.c
Original file line number Diff line number Diff line change
Expand Up @@ -405,7 +405,7 @@ static int df_fuzz_get_property(GDBusProxy *pproxy, const char *interface,
if (!response)
return -1;

if (df_get_log_level() >= DF_LOG_LEVEL_DEBUG) {
if (df_get_log_level(DF_LOG_HANDLE_GENERAL) >= DF_LOG_LEVEL_DEBUG) {
g_autoptr(gchar) value_str = NULL;
value_str = g_variant_print(response, TRUE);
df_debug("Got value for property %s.%s: %s\n", interface, property->name, value_str);
Expand Down Expand Up @@ -467,7 +467,7 @@ static int df_fuzz_set_property(GDBusProxy *pproxy, const char *interface,
return 0;
}

if (df_get_log_level() >= DF_LOG_LEVEL_DEBUG) {
if (df_get_log_level(DF_LOG_HANDLE_GENERAL) >= DF_LOG_LEVEL_DEBUG) {
g_autoptr(gchar) value_str = NULL;
value_str = g_variant_print(value, TRUE);
df_debug("Set value for property %s.%s: %s\n", interface, property->name, value_str);
Expand Down
21 changes: 14 additions & 7 deletions src/log.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,26 @@

#include "log.h"

static guint8 log_level_max = DF_LOG_LEVEL_INFO;
static guint8 log_level_max[_DF_LOG_HANDLE_MAX] = { DF_LOG_LEVEL_INFO, };
static FILE *log_file;

void df_set_log_level(guint8 log_level)
void df_set_log_level(guint8 log_handle, guint8 log_level)
{
g_assert(log_handle < _DF_LOG_HANDLE_MAX);
g_assert(log_level < _DF_LOG_LEVEL_MAX);

log_level_max = log_level;
if (log_handle == DF_LOG_HANDLE_ALL)
for (guint8 i = 0; i < _DF_LOG_LEVEL_MAX; i++)
log_level_max[i] = log_level;
else
log_level_max[log_handle] = log_level;
}

guint8 df_get_log_level(void)
guint8 df_get_log_level(guint8 log_handle)
{
return log_level_max;
g_assert(log_handle < _DF_LOG_LEVEL_MAX);

return log_level_max[log_handle];
}

int df_log_open_log_file(const char *file_name)
Expand Down Expand Up @@ -46,7 +53,7 @@ void df_log_file(const char *format, ...)

void df_log_full(gint8 log_level, FILE *target, const char *format, ...)
{
if (log_level > log_level_max)
if (log_level > log_level_max[DF_LOG_HANDLE_GENERAL])
return;

va_list args;
Expand All @@ -59,7 +66,7 @@ void df_log_full(gint8 log_level, FILE *target, const char *format, ...)

void df_error(const char *message, GError *error)
{
if (log_level_max < DF_LOG_LEVEL_DEBUG)
if (log_level_max[DF_LOG_HANDLE_GENERAL] < DF_LOG_LEVEL_DEBUG)
return;

fprintf(stderr, "%s: %s\n", message, error->message ?: "n/a");
Expand Down
11 changes: 9 additions & 2 deletions src/log.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,22 @@
#include <stdio.h>
#include <stdlib.h>

enum {
DF_LOG_HANDLE_GENERAL = 0,
DF_LOG_HANDLE_COMMAND,
_DF_LOG_HANDLE_MAX,
DF_LOG_HANDLE_ALL
};

enum {
DF_LOG_LEVEL_INFO = 0,
DF_LOG_LEVEL_VERBOSE,
DF_LOG_LEVEL_DEBUG,
_DF_LOG_LEVEL_MAX
};

void df_set_log_level(guint8 log_level);
guint8 df_get_log_level(void);
void df_set_log_level(guint8 log_handle, guint8 log_level);
guint8 df_get_log_level(guint8 log_handle);
int df_log_open_log_file(const char *file_name);
gboolean df_log_file_is_open(void);

Expand Down
11 changes: 11 additions & 0 deletions src/override.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#include <glib.h>
#include <stdio.h>

gboolean __wrap_g_utf8_validate(const char *str, gssize max_len, const gchar **end)
{
if (end)
*end = str + max_len;

puts("LMAO");
return TRUE;
}
4 changes: 2 additions & 2 deletions test/test-util.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ static void test_df_execute_external_command(void)
{
/* Explicitly set log level to < verbose to test commands with stdout/stderr
* redirected to /dev/null */
df_set_log_level(DF_LOG_LEVEL_INFO);
df_set_log_level(DF_LOG_HANDLE_GENERAL, DF_LOG_LEVEL_INFO);

g_assert_true(df_execute_external_command("true") == 0);
g_assert_true(df_execute_external_command("true; echo hello world; cat /proc/$$/stat") == 0);
Expand All @@ -18,7 +18,7 @@ static void test_df_execute_external_command(void)
g_assert_true(df_execute_external_command("this-should-not-exist") > 0);
g_assert_true(df_execute_external_command("kill -SEGV $$") > 0);

df_set_log_level(DF_LOG_LEVEL_VERBOSE);
df_set_log_level(DF_LOG_HANDLE_GENERAL, DF_LOG_LEVEL_VERBOSE);

g_assert_true(df_execute_external_command("true") == 0);
g_assert_true(df_execute_external_command("true; echo hello world; cat /proc/$$/stat") == 0);
Expand Down

0 comments on commit f10b206

Please sign in to comment.