From 3694242ea76e81a83a36ee20b29b53e11d0c4467 Mon Sep 17 00:00:00 2001 From: thebaptiste Date: Thu, 23 Jan 2020 14:10:21 +0100 Subject: [PATCH 1/3] feat: add possibility to specify a log directory Close: #1 --- src/log_proxy.c | 14 ++++++++++++-- src/options.h | 14 +++++++++++++- src/util.c | 23 +++++++++++++++++++++++ src/util.h | 1 + 4 files changed, 49 insertions(+), 3 deletions(-) diff --git a/src/log_proxy.c b/src/log_proxy.c index 76de78d..9a57c62 100644 --- a/src/log_proxy.c +++ b/src/log_proxy.c @@ -188,7 +188,7 @@ int main(int argc, char *argv[]) setlocale(LC_ALL, ""); context = g_option_context_new("LOGFILE - log proxy"); g_option_context_add_main_entries(context, entries, NULL); - gchar *description = "Optional environment variables to override defaults: \n LOGPROXY_ROTATION_SIZE\n LOGPROXY_ROTATION_TIME\n LOGPROXY_ROTATION_SUFFIX\n LOGPROXY_ROTATED_FILES\n\nExample for rotation-size option:\n- If log_proxy is run with the option --rotation-size on the command line, rotation-size will take the provided value\n- If the option --rotation-size is not provided on command line :\n - If the environment variable LOGPROXY_ROTATION_SIZE is set, rotation-size will take this value\n - If the environment variable LOGPROXY_ROTATION_SIZE is not set, rotation-size will take the default value 104857600\n"; + gchar *description = "Optional environment variables to override defaults: \n LOGPROXY_ROTATION_SIZE\n LOGPROXY_ROTATION_TIME\n LOGPROXY_ROTATION_SUFFIX\n LOGPROXY_LOG_DIRECTORY\n LOGPROXY_ROTATED_FILES\n\nExample for rotation-size option:\n- If log_proxy is run with the option --rotation-size on the command line, rotation-size will take the provided value\n- If the option --rotation-size is not provided on command line :\n - If the environment variable LOGPROXY_ROTATION_SIZE is set, rotation-size will take this value\n - If the environment variable LOGPROXY_ROTATION_SIZE is not set, rotation-size will take the default value 104857600\n"; g_option_context_set_description(context, description); if (!g_option_context_parse(context, &argc, &argv, NULL)) { g_print(g_option_context_get_help(context, TRUE, NULL)); @@ -201,8 +201,17 @@ int main(int argc, char *argv[]) atexit(exit_handler); signal(SIGTERM, signal_handler); signal(SIGINT, signal_handler); - log_file = g_strdup(argv[1]); set_default_values_from_env(); + log_file = compute_file_path(log_directory, argv[1]); + // Create log directory if not existing + gchar *log_dir = g_path_get_dirname(log_file); + if ( ! g_file_test(log_dir, G_FILE_TEST_IS_DIR) ) { + if ( g_mkdir_with_parents(log_dir, 0755) == -1 ) { + g_critical("Can't create directory %s => exit", log_dir); + return 1; + } + } + g_free(log_dir); GIOChannel *in = NULL; if (fifo == NULL) { // We read from stdin @@ -240,5 +249,6 @@ int main(int argc, char *argv[]) g_io_channel_unref(in); g_string_free(in_buffer, TRUE); g_option_context_free(context); + g_free(log_file); return 0; } diff --git a/src/options.h b/src/options.h index 4cabe37..ce086d6 100644 --- a/src/options.h +++ b/src/options.h @@ -9,6 +9,7 @@ static gchar *log_file = NULL; static glong rotation_size = -1; static glong rotation_time = -1; static gchar *rotation_suffix = NULL; +static gchar *log_directory = NULL; static gint rotated_files = -1; static gboolean rm_fifo_at_exit = FALSE; static gchar *fifo = NULL; @@ -57,12 +58,23 @@ void set_default_values_from_env() rotated_files = 5; } } + + if ( log_directory == NULL ) { + env_val = g_getenv("LOGPROXY_LOG_DIRECTORY"); + if ( env_val != NULL ) { + log_directory = (gchar *)env_val; + } + else { + log_directory = g_strdup(g_get_current_dir()); + } + } } static GOptionEntry entries[] = { { "rotation-size", 's', 0, G_OPTION_ARG_INT64, &rotation_size, "maximum size (in bytes) for a log file before rotation (0 => no maximum, default: content of environment variable LOGPROXY_ROTATION_SIZE or 104857600 (100MB))", NULL }, { "rotation-time", 't', 0, G_OPTION_ARG_INT64, &rotation_time, "maximum lifetime (in seconds) for a log file before rotation (0 => no maximum, default: content of environment variable LOGPROXY_ROTATION_TIME or 86400 (24H))", NULL }, - { "rotation-suffix", 'S', 0, G_OPTION_ARG_STRING, &rotation_suffix, "strftime based suffix to append to rotated log files (default: content of environment variable LOGPROXY_ROTATION_SUFFIX or .%%Y%%m%%d%%H%%M%%S", NULL }, + { "rotation-suffix", 'S', 0, G_OPTION_ARG_STRING, &rotation_suffix, "strftime based suffix to append to rotated log files (default: content of environment variable LOGPROXY_ROTATION_SUFFIX or .%%Y%%m%%d%%H%%M%%S)", NULL }, + { "log-directory", 'd', 0, G_OPTION_ARG_STRING, &log_directory, "directory to store log files (default: content of environment variable LOGPROXY_LOG_DIRECTORY or current directory), directory is created if missing", NULL }, { "rotated-files", 'n', 0, G_OPTION_ARG_INT, &rotated_files, "maximum number of rotated files to keep including main one (0 => no cleaning, default: content of environment variable LOGPROXY_ROTATED_FILES or 5)", NULL }, { "use-locks", 'm', 0, G_OPTION_ARG_NONE, &use_locks, "use locks to append to main log file (useful if several process writes to the same file)", NULL }, { "fifo", 'f', 0, G_OPTION_ARG_STRING, &fifo, "if set, read lines on this fifo instead of stdin", NULL }, diff --git a/src/util.c b/src/util.c index c67f7b5..496133c 100644 --- a/src/util.c +++ b/src/util.c @@ -3,6 +3,7 @@ #include #include #include +#include #include "util.h" static GRand *__rand = NULL; @@ -112,6 +113,28 @@ gchar *compute_strftime_suffix(const gchar *str, const gchar *strftime_suffix) { return g_strdup_printf("%s%s", str, outstr); } +/** + * Compute absolute file path from directory path and file name + * + * @param directory absolute or relative directory path + * @param file_name file name (absolute or relative, if absolute directory is ignored) + * @return newly allocated string (free it with g_free) with absolute path to the file + */ +gchar *compute_file_path(const gchar *directory, const gchar *file_name) { + if ( file_name[0] == '/' ) { + return g_strdup_printf("%s", file_name); + } + else { + if ( directory[0] == '/' ) { + return g_strdup_printf("%s/%s", directory, file_name); + } + else { + return g_strdup_printf("%s/%s/%s", g_get_current_dir(), directory, + file_name); + } + } +} + /** * Create an empty file if it does not exist * diff --git a/src/util.h b/src/util.h index b1571c1..ded1c68 100644 --- a/src/util.h +++ b/src/util.h @@ -10,5 +10,6 @@ gchar *get_unique_hexa_identifier(); glong get_file_inode(const gchar *file_path); glong get_fd_inode(int fd); gboolean create_empty(const gchar *file_path); +gchar *compute_file_path(const gchar *directory, const gchar *file_name); #endif /* UTIL_H_ */ From 1f6eec26e47bf0421c162065dda6d51f6a370cbf Mon Sep 17 00:00:00 2001 From: Jean-Baptiste VESLIN Date: Mon, 27 Jan 2020 14:25:27 +0100 Subject: [PATCH 2/3] Update src/options.h Co-Authored-By: Fabien MARTY --- src/options.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/options.h b/src/options.h index ce086d6..648d1b2 100644 --- a/src/options.h +++ b/src/options.h @@ -65,7 +65,7 @@ void set_default_values_from_env() log_directory = (gchar *)env_val; } else { - log_directory = g_strdup(g_get_current_dir()); + log_directory = g_get_current_dir(); } } } From 75620bcad3a0d639e9d3b23f1488f9af61b9e9c2 Mon Sep 17 00:00:00 2001 From: Jean-Baptiste VESLIN Date: Mon, 27 Jan 2020 14:27:57 +0100 Subject: [PATCH 3/3] Update util.c --- src/util.c | 1 - 1 file changed, 1 deletion(-) diff --git a/src/util.c b/src/util.c index 496133c..bd8c0a5 100644 --- a/src/util.c +++ b/src/util.c @@ -3,7 +3,6 @@ #include #include #include -#include #include "util.h" static GRand *__rand = NULL;