From 4b4873fda426d9904533283c31434405b895ea3c Mon Sep 17 00:00:00 2001 From: thebaptiste Date: Thu, 23 Jan 2020 14:10:21 +0100 Subject: [PATCH] feat: add possibility to specify a log directory Close: #1 --- src/log_proxy.c | 13 +++++++++++-- src/options.h | 16 +++++++++++++++- src/util.c | 23 +++++++++++++++++++++++ src/util.h | 1 + 4 files changed, 50 insertions(+), 3 deletions(-) diff --git a/src/log_proxy.c b/src/log_proxy.c index 76de78d..44b4970 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 diff --git a/src/options.h b/src/options.h index 4cabe37..88a56a0 100644 --- a/src/options.h +++ b/src/options.h @@ -4,11 +4,14 @@ #include #include #include +#include +#include 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 +60,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_ */