degesch: add "on_change" for behaviour.logging

This commit is contained in:
Přemysl Eric Janouch 2015-07-05 17:02:11 +02:00
parent 8c2c0472cb
commit 550f8baa1a

View File

@ -1297,6 +1297,7 @@ struct app_context
char *attrs[ATTR_COUNT]; ///< Terminal attributes
bool isolate_buffers; ///< Isolate global/server buffers
bool beep_on_highlight; ///< Beep on highlight
bool logging; ///< Logging to file enabled
struct str_map servers; ///< Our servers
@ -1418,6 +1419,7 @@ static bool irc_is_this_us (struct server *s, const char *prefix);
// --- Configuration -----------------------------------------------------------
static void on_config_attribute_change (struct config_item_ *item);
static void on_config_logging_change (struct config_item_ *item);
#define TRIVIAL_BOOLEAN_ON_CHANGE(name) \
static void \
@ -1578,7 +1580,8 @@ static struct config_schema g_config_behaviour[] =
{ .name = "logging",
.comment = "Log buffer contents to file",
.type = CONFIG_ITEM_BOOLEAN,
.default_ = "off" },
.default_ = "off",
.on_change = on_config_logging_change },
{}
};
@ -2707,7 +2710,7 @@ make_log_filename (const char *filename, struct str *output)
static void
buffer_open_log_file (struct app_context *ctx, struct buffer *buffer)
{
if (!get_config_boolean (ctx->config.root, "behaviour.logging"))
if (!ctx->logging || buffer->log_file)
return;
struct str path;
@ -2728,6 +2731,29 @@ buffer_open_log_file (struct app_context *ctx, struct buffer *buffer)
str_free (&path);
}
static void
buffer_close_log_file (struct buffer *buffer)
{
if (buffer->log_file)
(void) fclose (buffer->log_file);
buffer->log_file = NULL;
}
static void
on_config_logging_change (struct config_item_ *item)
{
struct app_context *ctx = item->user_data;
ctx->logging = item->value.boolean;
for (struct buffer *buffer = ctx->buffers; buffer; buffer = buffer->next)
if (ctx->logging)
buffer_open_log_file (ctx, buffer);
else
buffer_close_log_file (buffer);
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
static struct buffer *
buffer_by_name (struct app_context *ctx, const char *name)
{
@ -2875,11 +2901,7 @@ buffer_rename (struct app_context *ctx,
str_map_set (&ctx->buffers_by_name, buffer->name, NULL);
str_map_set (&ctx->buffers_by_name, new_name, buffer);
if (buffer->log_file)
{
(void) fclose (buffer->log_file);
buffer->log_file = NULL;
}
buffer_close_log_file (buffer);
buffer_open_log_file (ctx, buffer);
free (buffer->name);