Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add possibility to specify a log directory #2

Merged
merged 3 commits into from
Jan 27, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
14 changes: 12 additions & 2 deletions src/log_proxy.c
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand All @@ -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]);
thefab marked this conversation as resolved.
Show resolved Hide resolved
// 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
Expand Down Expand Up @@ -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;
}
14 changes: 13 additions & 1 deletion src/options.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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());
thebaptiste marked this conversation as resolved.
Show resolved Hide resolved
}
}
}

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 },
Expand Down
23 changes: 23 additions & 0 deletions src/util.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
#include <limits.h>
thebaptiste marked this conversation as resolved.
Show resolved Hide resolved
#include "util.h"

static GRand *__rand = NULL;
Expand Down Expand Up @@ -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
*
Expand Down
1 change: 1 addition & 0 deletions src/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -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_ */