Add logging configuration option

This commit is contained in:
Håvard Pettersson 2017-02-08 23:15:12 -08:00
parent 8ab5cce563
commit 4c8c4f598d
7 changed files with 144 additions and 19 deletions

View File

@ -89,10 +89,9 @@ twc_chat_new(struct t_twc_profile *profile, const char *name)
return NULL;
}
/* disable logging for buffer */
weechat_hook_signal_send("logger_stop",
WEECHAT_HOOK_SIGNAL_POINTER,
chat->buffer);
/* set correct logging state for buffer */
bool log = TWC_PROFILE_OPTION_BOOLEAN(profile, TWC_PROFILE_OPTION_LOGGING);
twc_set_buffer_logging(chat->buffer, log);
twc_chat_queue_refresh(chat);
twc_list_item_new_data_add(profile->chats, chat);
@ -276,6 +275,7 @@ twc_chat_search_buffer(struct t_gui_buffer *buffer)
return NULL;
}
/**
* Print a chat message to a chat's buffer.
*/

View File

@ -52,6 +52,9 @@ twc_chat_search_group(struct t_twc_profile *profile,
struct t_twc_chat *
twc_chat_search_buffer(struct t_gui_buffer *target_buffer);
enum t_twc_rc
twc_chat_set_logging(struct t_twc_chat const *const chat, bool logging);
void
twc_chat_print_message(struct t_twc_chat *chat,
const char *tags,

View File

@ -50,6 +50,7 @@ char *twc_profile_option_names[TWC_PROFILE_NUM_OPTIONS] =
"udp",
"ipv6",
"passphrase",
"logging",
};
/**
@ -154,7 +155,7 @@ twc_config_profile_check_value_callback(const void *pointer, void *data,
struct t_config_option *option,
const char *value)
{
enum t_twc_profile_option option_index = (intptr_t)pointer;
enum t_twc_profile_option option_index = *(int *)data;
switch (option_index)
{
@ -172,13 +173,44 @@ void
twc_config_profile_change_callback(const void *pointer, void *data,
struct t_config_option *option)
{
struct t_twc_profile *profile = (void *)pointer;
enum t_twc_profile_option option_index = *(int *)data;
switch (option_index)
{
case TWC_PROFILE_OPTION_LOGGING:
if (profile)
{
/* toggle logging for a specific profile */
bool logging_enabled = weechat_config_boolean(option);
twc_profile_set_logging(profile, logging_enabled);
}
else
{
/* option changed for default profile, update all profiles */
size_t index;
struct t_twc_list_item *item;
twc_list_foreach(twc_profiles, index, item)
{
bool logging_enabled
= TWC_PROFILE_OPTION_BOOLEAN(item->profile,
TWC_PROFILE_OPTION_LOGGING);
twc_profile_set_logging(item->profile, logging_enabled);
}
}
default:
break;
}
}
/**
* Create a new option for a profile.
* Create a new option for a profile. Returns NULL if an error occurs.
*/
struct t_config_option *
twc_config_init_option(struct t_config_section *section,
twc_config_init_option(struct t_twc_profile *profile,
struct t_config_section *section,
int option_index, const char *option_name,
bool is_default_profile)
{
@ -190,7 +222,6 @@ twc_config_init_option(struct t_config_section *section,
char *default_value = NULL;
bool null_allowed = false;
switch (option_index)
{
case TWC_PROFILE_OPTION_AUTOLOAD:
@ -211,6 +242,11 @@ twc_config_init_option(struct t_config_section *section,
"network";
default_value = "on";
break;
case TWC_PROFILE_OPTION_LOGGING:
type = "boolean";
description = "log chat buffers to disk";
default_value = "off";
break;
case TWC_PROFILE_OPTION_MAX_FRIEND_REQUESTS:
type = "integer";
description = "maximum amount of friend requests to retain before "
@ -259,14 +295,24 @@ twc_config_init_option(struct t_config_section *section,
null_allowed = null_allowed || !is_default_profile;
value = is_default_profile ? default_value : NULL;
/* store option index as data for WeeChat callbacks */
int *index_check_pointer = malloc(sizeof(int));
int *index_change_pointer = malloc(sizeof(int));
if (!index_check_pointer || !index_change_pointer)
{
free(index_check_pointer);
free(index_change_pointer);
return NULL;
}
*index_check_pointer = option_index;
*index_change_pointer = option_index;
return weechat_config_new_option(
twc_config_file, section,
option_name, type, description, string_values, min, max,
default_value, value, null_allowed,
twc_config_profile_check_value_callback,
(void *)(intptr_t)option_index, NULL,
twc_config_profile_change_callback,
(void *)(intptr_t)option_index, NULL,
twc_config_profile_check_value_callback, profile, index_check_pointer,
twc_config_profile_change_callback, profile, index_change_pointer,
NULL, NULL, NULL);
}
@ -300,7 +346,7 @@ twc_config_init()
for (int i = 0; i < TWC_PROFILE_NUM_OPTIONS; ++i)
{
twc_config_profile_default[i] =
twc_config_init_option(twc_config_section_profile_default,
twc_config_init_option(NULL, twc_config_section_profile_default,
i, twc_profile_option_names[i], true);
}
@ -350,8 +396,8 @@ twc_config_init_profile(struct t_twc_profile *profile)
profile->name,
twc_profile_option_names[i]);
profile->options[i] = twc_config_init_option(twc_config_section_profile,
i, option_name, false);
profile->options[i] = twc_config_init_option(profile,
twc_config_section_profile, i, option_name, false);
free(option_name);
}
}

View File

@ -286,10 +286,10 @@ twc_profile_load(struct t_twc_profile *profile)
if (!(profile->buffer))
return TWC_RC_ERROR;
/* disable logging for buffer */
weechat_hook_signal_send("logger_stop",
WEECHAT_HOOK_SIGNAL_POINTER,
profile->buffer);
/* disable logging for buffer if option is off */
bool logging = TWC_PROFILE_OPTION_BOOLEAN(profile,
TWC_PROFILE_OPTION_LOGGING);
twc_set_buffer_logging(profile->buffer, logging);
profile->nicklist_group = weechat_nicklist_add_group(profile->buffer, NULL,
NULL, NULL, true);
@ -570,6 +570,49 @@ twc_profile_search_buffer(struct t_gui_buffer *buffer)
return NULL;
}
/**
* Enable or disable the WeeChat logger for all buffers for a profile.
*
* Return TWC_RC_OK if logging was enabled or disabled for all buffers.
* Return TWC_RC_ERROR if one or more errors occurred.
*/
enum t_twc_rc
twc_profile_set_logging(struct t_twc_profile *profile, bool logging)
{
/* track if an error occurs */
bool error;
/* if profile buffer is NULL, profile isn't loaded */
if (!profile->buffer)
return TWC_RC_OK;
/* signal profile's main buffer */
if (WEECHAT_RC_ERROR == twc_set_buffer_logging(profile->buffer, logging))
{
error = true;
weechat_printf(profile->buffer,
"%swarning: failed to %s logging in this buffer!",
weechat_prefix("error"), logging ? "enable" : "disable");
}
/* signal all chat buffers for profile */
size_t index;
struct t_twc_list_item *item;
twc_list_foreach(profile->chats, index, item)
{
if (WEECHAT_RC_ERROR == twc_set_buffer_logging(item->chat->buffer,
logging))
{
error = true;
weechat_printf(item->chat->buffer,
"%swarning: failed to %s logging in this buffer!",
weechat_prefix("error"), logging ? "enable" : "disable");
}
}
return error ? TWC_RC_ERROR : TWC_RC_OK;
}
/**
* Delete a profile. Unloads, frees and deletes everything. If delete_data is
* true, Tox data on disk is also deleted.

View File

@ -37,6 +37,7 @@ enum t_twc_profile_option
TWC_PROFILE_OPTION_UDP,
TWC_PROFILE_OPTION_IPV6,
TWC_PROFILE_OPTION_PASSPHRASE,
TWC_PROFILE_OPTION_LOGGING,
TWC_PROFILE_NUM_OPTIONS,
};
@ -119,6 +120,9 @@ twc_profile_search_name(const char *name);
struct t_twc_profile *
twc_profile_search_buffer(struct t_gui_buffer *buffer);
enum t_twc_rc
twc_profile_set_logging(struct t_twc_profile *profile, bool logging);
void
twc_profile_delete(struct t_twc_profile *profile, bool delete_data);

View File

@ -200,3 +200,28 @@ twc_fit_utf8(const char *str, int max)
{
return weechat_utf8_real_pos(str, weechat_utf8_strnlen(str, max));
}
/**
* Enable or disable logging for a WeeChat buffer.
*/
int
twc_set_buffer_logging(struct t_gui_buffer *buffer, bool logging)
{
if (!buffer)
return WEECHAT_RC_ERROR;
char const *signal;
if (logging)
{
weechat_buffer_set(buffer, "localvar_del_no_log", "");
signal = "logger_start";
}
else
{
weechat_buffer_set(buffer, "localvar_set_no_log", "1");
signal = "logger_stop";
}
return weechat_hook_signal_send(signal,
WEECHAT_HOOK_SIGNAL_POINTER, buffer);
}

View File

@ -22,6 +22,7 @@
#include <stdlib.h>
#include <weechat/weechat-plugin.h>
#include <tox/tox.h>
void
@ -54,5 +55,8 @@ twc_uint32_reverse_bytes(uint32_t num);
int
twc_fit_utf8(const char *str, int max);
int
twc_set_buffer_logging(struct t_gui_buffer *buffer, bool logging);
#endif // TOX_WEECHAT_UTILS_H