Skip to content

Commit

Permalink
config: add the logdir option dynamic reloading support
Browse files Browse the repository at this point in the history
NOTE: for now this couldn't support the glfs's logging setting, and
the glfs will use the old path.

Signed-off-by: Xiubo Li <xiubli@redhat.com>
  • Loading branch information
lxbsz committed May 4, 2018
1 parent 8847f90 commit 4c418b7
Show file tree
Hide file tree
Showing 6 changed files with 950 additions and 36 deletions.
29 changes: 25 additions & 4 deletions libtcmu_config.c
Original file line number Diff line number Diff line change
Expand Up @@ -138,15 +138,17 @@ do { \

static void tcmu_conf_set_options(struct tcmu_config *cfg, bool reloading)
{
int ret;

/* set log_level option */
TCMU_PARSE_CFG_INT(cfg, log_level, TCMU_CONF_LOG_INFO);
if (cfg->log_level) {
tcmu_set_log_level(cfg->log_level);
}

/* set log_dir path option */
TCMU_PARSE_CFG_STR(cfg, log_dir_path);
if (!reloading) {
/* set log_dir path option */
TCMU_PARSE_CFG_STR(cfg, log_dir_path);
/*
* The priority of the logdir setting is:
* 1, --tcmu_log_dir/-l LOG_DIR_PATH
Expand All @@ -155,11 +157,30 @@ static void tcmu_conf_set_options(struct tcmu_config *cfg, bool reloading)
* 4, default /var/log/
*/
if (!tcmu_get_logdir())
tcmu_logdir_create(cfg->log_dir_path);
tcmu_logdir_create(cfg->log_dir_path, false);
else
tcmu_warn("The logdir option from the tcmu.conf will be ignored\n");
} else {
tcmu_warn("The logdir option is not supported by dynamic reloading for now!\n");
/*
* Here we asume that users want to change the
* log_dir_path without considering the priority
* mentioned above.
*/
tcmu_logbuf_lock();

if (tcmu_is_logbuf_destoried())
goto unlock;

tcmu_logdir_create(cfg->log_dir_path, true);
ret = tcmu_create_file_output(tcmu_get_log_level(),
TCMU_LOG_FILENAME,
true);
if (ret < 0)
tcmu_err("Could not change log path to %s, ret:%d.\n",
cfg->log_dir_path, ret);

unlock:
tcmu_logbuf_unlock();
}

/* add your new config options */
Expand Down
125 changes: 96 additions & 29 deletions libtcmu_log.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ struct log_buf {
pthread_mutex_t lock;

bool thread_active;
int init_state;
bool destoried;
bool finish_initialize;

unsigned int head;
Expand All @@ -60,10 +60,13 @@ struct log_output {
void *data;
tcmu_log_destination dest;
bool bypass;
bool enabled;
};

static int tcmu_log_level = TCMU_LOG_INFO;
static struct log_buf *logbuf = NULL;
pthread_mutex_t logbuf_lock = PTHREAD_MUTEX_INITIALIZER;
bool logbuf_destoried = false;

/* covert log level from tcmu config to syslog */
static inline int to_syslog_level(int level)
Expand Down Expand Up @@ -101,7 +104,7 @@ bool tcmu_logdir_getenv(void)
if (!log_path)
return true;

if (!tcmu_logdir_create(log_path))
if (!tcmu_logdir_create(log_path, false))
return false;

return true;
Expand Down Expand Up @@ -156,20 +159,80 @@ static inline void rb_update_head(struct log_buf *logbuf)
logbuf->head = (logbuf->head + 1) % LOG_ENTRYS;
}

static void log_cleanup_output(struct log_output *output)
{
if (!output)
return;

if (output->close_fn != NULL)
output->close_fn(output->data);
if (output->name != NULL)
free(output->name);
}

void tcmu_logbuf_lock(void)
{
pthread_mutex_lock(&logbuf_lock);
}

void tcmu_logbuf_unlock(void)
{
pthread_mutex_unlock(&logbuf_lock);
}

bool tcmu_is_logbuf_destoried(void)
{
return logbuf_destoried;
}

static void log_cleanup(void *arg)
{
struct log_buf *logbuf = arg;
struct log_output *output;

tcmu_logbuf_lock();

logbuf_destoried = true;

pthread_cond_destroy(&logbuf->cond);
pthread_mutex_destroy(&logbuf->lock);

darray_foreach(output, logbuf->outputs)
log_cleanup_output(output);

darray_free(logbuf->outputs);

free(logbuf);

tcmu_logbuf_unlock();
}

static void log_output(int pri, const char *msg, bool bypass)
{
struct log_output *output;
char timestamp[TCMU_TIME_STRING_BUFLEN] = {0, };
int ret;
int i = 0;

ret = time_string_now(timestamp);
if (ret < 0)
return;

darray_foreach (output, logbuf->outputs) {
if (output->bypass == bypass && pri <= output->priority) {
output->output_fn(pri, timestamp, msg, output->data);
if (output->enabled) {
if (output->bypass == bypass && pri <= output->priority)
output->output_fn(pri, timestamp,
msg, output->data);
} else {
/*
* We just close and free the resource here to make
* sure no outputing operation is in process.
*/
log_cleanup_output(output);
darray_remove(logbuf->outputs, i);
continue;
}
i++;
}
}

Expand Down Expand Up @@ -297,12 +360,29 @@ static int append_output(log_output_fn_t output_fn, log_close_fn_t close_fn, voi
output.dest = dest;
output.name = ndup;
output.bypass = bypass;
output.enabled = true;

darray_append(logbuf->outputs, output);

return 0;
}

static void log_output_disable(const tcmu_log_destination dest)
{
struct log_output *output;
struct log_output *last = NULL;

/* This will just keep the last one enabled. */
darray_foreach(output, logbuf->outputs) {
if (output->dest == dest && output->enabled) {
if (last)
last->enabled = false;

last = output;
}
}
}

static int output_to_syslog(int pri, const char *timestamp,
const char *str, void *data)
{
Expand Down Expand Up @@ -405,7 +485,7 @@ static int create_stdout_output(int pri)
return 0;
}

static int create_file_output(int pri, const char *filename)
int tcmu_create_file_output(int pri, const char *filename, bool reloading)
{
char log_file_path[PATH_MAX];
int fd, ret;
Expand All @@ -430,6 +510,10 @@ static int create_file_output(int pri, const char *filename)
return ret;
}

/* Disable the old entries */
if (reloading)
log_output_disable(TCMU_LOG_TO_FILE);

return 0;
}

Expand Down Expand Up @@ -468,26 +552,6 @@ static bool log_buf_not_empty_output(struct log_buf *logbuf)
return true;
}

static void log_cleanup(void *arg)
{
struct log_buf *logbuf = arg;
struct log_output *output;

pthread_cond_destroy(&logbuf->cond);
pthread_mutex_destroy(&logbuf->lock);

darray_foreach(output, logbuf->outputs) {
if (output->close_fn != NULL)
output->close_fn(output->data);
if (output->name != NULL)
free(output->name);
}

darray_free(logbuf->outputs);

free(logbuf);
}

static void *log_thread_start(void *arg)
{
struct log_buf *logbuf = arg;
Expand Down Expand Up @@ -538,7 +602,7 @@ char *tcmu_get_logdir(void)
return tcmu_log_dir;
}

static char *tcmu_alloc_and_set_log_dir(const char *log_dir)
static char *tcmu_alloc_and_set_log_dir(const char *log_dir, bool reloading)
{
/*
* Do nothing here and will use the /var/log/
Expand All @@ -547,6 +611,9 @@ static char *tcmu_alloc_and_set_log_dir(const char *log_dir)
if (!log_dir)
return NULL;

if (reloading && tcmu_log_dir)
free(tcmu_log_dir);

tcmu_log_dir = strdup(log_dir);
if (!tcmu_log_dir)
tcmu_err("Failed to copy log dir: %s\n", log_dir);
Expand Down Expand Up @@ -606,15 +673,15 @@ static int tcmu_mkdirs(const char *pathname)
return tcmu_mkdir(path);
}

bool tcmu_logdir_create(const char *path)
bool tcmu_logdir_create(const char *path, bool reloading)
{
if (!tcmu_logdir_check(path))
return false;

if (!tcmu_mkdirs(path))
return false;

return !!tcmu_alloc_and_set_log_dir(path);
return !!tcmu_alloc_and_set_log_dir(path, reloading);
}

int tcmu_make_absolute_logfile(char *path, const char *filename)
Expand Down Expand Up @@ -651,7 +718,7 @@ int tcmu_setup_log(void)
if (ret < 0)
tcmu_err("create stdout output error \n");

ret = create_file_output(TCMU_LOG_DEBUG, TCMU_LOG_FILENAME);
ret = tcmu_create_file_output(TCMU_LOG_DEBUG, TCMU_LOG_FILENAME, false);
if (ret < 0)
tcmu_err("create file output error \n");

Expand Down
Loading

0 comments on commit 4c418b7

Please sign in to comment.