Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[dbus] Allow adjusting mce verbosity while it is running. Fixes JB#32492
Now and then a situation arises where mce verbosity needs to be adjusted
for debugging purposes without restarting the process. Currently it can
be done via sending USR1/USR2 signals to mce process, but that is a bit
clumsy and does not allow choosing the required logging level.

Implement dbus method calls for adjusting mce verbosity and add mcetool
option that utilizes this to select required verbosity level:
      --set-verbosity=<emerg|alert|crit|err|warning|notice|info|debug>

The change is temporary and mce returns to default verbosity when the
service is restarted / device is rebooted.
  • Loading branch information
spiiroin committed Oct 2, 2015
1 parent b38544e commit 03e2ef5
Show file tree
Hide file tree
Showing 4 changed files with 227 additions and 2 deletions.
94 changes: 94 additions & 0 deletions mce-dbus.c
Expand Up @@ -726,6 +726,84 @@ static gboolean suspend_stats_get_dbus_cb(DBusMessage *const req)
return TRUE;
}

/** D-Bus callback for: get mce verbosity method call
*
* @param req The D-Bus message to reply to
*
* @return TRUE
*/
static gboolean verbosity_get_dbus_cb(DBusMessage *const req)
{
DBusMessage *rsp = 0;

mce_log(LL_DEVEL, "verbosity get from %s",
mce_dbus_get_message_sender_ident(req));

dbus_int32_t verbosity = mce_log_get_verbosity();

rsp = dbus_new_method_reply(req);

if( !dbus_message_append_args(rsp,
DBUS_TYPE_INT32, &verbosity,
DBUS_TYPE_INVALID) ) {
mce_log(LL_ERR, "Failed to append arguments");
goto EXIT;
}

dbus_send_message(rsp), rsp = 0;

EXIT:
if( rsp )
dbus_message_unref(rsp);

return TRUE;
}

/** D-Bus callback for: set mce verbosity method call
*
* @param req The D-Bus message to reply to
*
* @return TRUE
*/
static gboolean verbosity_set_dbus_cb(DBusMessage *const req)
{
dbus_bool_t ack = false;
DBusError err = DBUS_ERROR_INIT;

mce_log(LL_DEVEL, "verbosity set from %s",
mce_dbus_get_message_sender_ident(req));

dbus_int32_t verbosity = LL_WARN;

if( !dbus_message_get_args(req, &err,
DBUS_TYPE_INT32, &verbosity,
DBUS_TYPE_INVALID) ) {
mce_log(LL_ERR, "%s: %s", err.name, err.message);
goto EXIT;
}

mce_log_set_verbosity(verbosity);
ack = true;

EXIT:
if( !dbus_message_get_no_reply(req) ) {
DBusMessage *rsp = dbus_new_method_reply(req);
if( !dbus_message_append_args(rsp,
DBUS_TYPE_BOOLEAN, &ack,
DBUS_TYPE_INVALID) ) {
mce_log(LL_ERR, "Failed to append arguments");
dbus_message_unref(rsp), rsp = 0;
}
else {
dbus_send_message(rsp), rsp = 0;
}
}

dbus_error_free(&err);

return TRUE;
}

/** Helper for appending gconf string list to dbus message
*
* @param conf GConfValue of string list type
Expand Down Expand Up @@ -3660,6 +3738,22 @@ static mce_dbus_handler_t mce_dbus_handlers[] =
" <arg direction=\"out\" name=\"uptime_ms\" type=\"x\"/>\n"
" <arg direction=\"out\" name=\"suspend_ms\" type=\"x\"/>\n"
},
{
.interface = MCE_REQUEST_IF,
.name = "get_verbosity",
.type = DBUS_MESSAGE_TYPE_METHOD_CALL,
.callback = verbosity_get_dbus_cb,
.args =
" <arg direction=\"out\" name=\"level\" type=\"i\"/>\n"
},
{
.interface = MCE_REQUEST_IF,
.name = "set_verbosity",
.type = DBUS_MESSAGE_TYPE_METHOD_CALL,
.callback = verbosity_set_dbus_cb,
.args =
" <arg direction=\"in\" name=\"level\" type=\"i\"/>\n"
},
{
.interface = DBUS_INTERFACE_INTROSPECTABLE,
.name = "Introspect",
Expand Down
16 changes: 15 additions & 1 deletion mce-log.c
Expand Up @@ -212,11 +212,25 @@ void mce_log_file(loglevel_t loglevel, const char *const file,
*
* @param verbosity minimum level for log level
*/
void mce_log_set_verbosity(const int verbosity)
void mce_log_set_verbosity(int verbosity)
{
if( verbosity < LL_MINIMUM )
verbosity = LL_MINIMUM;
else if( verbosity > LL_MAXIMUM )
verbosity = LL_MAXIMUM;

logverbosity = verbosity;
}

/** Set log verbosity
*
* @return current verbosity level
*/
int mce_log_get_verbosity(void)
{
return logverbosity;
}

/**
* Open log
*
Expand Down
6 changes: 5 additions & 1 deletion mce-log.h
Expand Up @@ -45,11 +45,15 @@ typedef enum {
LL_DEVEL = LL_NOTICE, /**< Otherwise verbose mode needed */
# endif

LL_MAXIMUM = LOG_DEBUG, /**< Minimum for bounds checking */
LL_MINIMUM = LOG_EMERG, /**< Maximum for bounds checking */

} loglevel_t;

# ifdef OSSOLOG_COMPILE
void mce_log_add_pattern(const char *pat);
void mce_log_set_verbosity(const int verbosity);
void mce_log_set_verbosity(int verbosity);
int mce_log_get_verbosity(void);

int mce_log_p_(const loglevel_t loglevel,
const char *const file, const char *const function);
Expand Down
113 changes: 113 additions & 0 deletions tools/mcetool.c
Expand Up @@ -37,6 +37,7 @@
#include <string.h>
#include <errno.h>
#include <math.h>
#include <syslog.h>

#include <dbus/dbus.h>

Expand Down Expand Up @@ -354,6 +355,50 @@ static gboolean xmce_ipc_uint_reply(const gchar *const name,
return ack;
}

/** Wrapper for making synchronous MCE D-Bus method calls that return INT32
*
* @param name [IN] D-Bus method call name
* @param reply [OUT] Where to store int from method return
* @param arg_type [IN] DBUS_TYPE_STRING etc, as with dbus_message_append_args()
* @param ... [IN] D-Bus arguments, terminated with DBUS_TYPE_INVALID
*
* @return TRUE if call was successfully sent and non-error reply received and parsed,
* or FALSE in case of errors
*/
static gboolean xmce_ipc_int_reply(const gchar *const name,
gint *result,
int arg_type, ...)
{
gboolean ack = FALSE;
DBusMessage *rsp = 0;
DBusError err = DBUS_ERROR_INIT;
dbus_int32_t dta = 0;

va_list va;
va_start(va, arg_type);

if( !xmce_ipc_va(name, &rsp, arg_type, va) )
goto EXIT;

if( !dbus_message_get_args(rsp, &err,
DBUS_TYPE_INT32, &dta,
DBUS_TYPE_INVALID) )
goto EXIT;

*result = dta;

ack = TRUE;
EXIT:
if( dbus_error_is_set(&err) ) {
errorf("%s: %s: %s\n", name, err.name, err.message);
dbus_error_free(&err);
}
if( rsp ) dbus_message_unref(rsp);

va_end(va);
return ack;
}

/** Wrapper for making synchronous MCE D-Bus method calls that return BOOLEAN
*
* @param name [IN] D-Bus method call name
Expand Down Expand Up @@ -1920,6 +1965,56 @@ static bool mcetool_do_deactivate_pattern(const char *args)
DBUS_TYPE_STRING, &args,
DBUS_TYPE_INVALID);
}
/* ------------------------------------------------------------------------- *
* mce verbosity
* ------------------------------------------------------------------------- */

/** Lookup table for verbosity levels */
static const symbol_t verbosity_levels[] = {
// official
{ "emerg", LOG_EMERG },
{ "alert", LOG_ALERT },
{ "crit", LOG_CRIT },
{ "err", LOG_ERR },
{ "warning", LOG_WARNING },
{ "notice", LOG_NOTICE },
{ "info", LOG_INFO },
{ "debug", LOG_DEBUG },
// aliases
{ "emergency", LOG_EMERG },
{ "critical", LOG_CRIT },
{ "error", LOG_ERR },
{ "warn", LOG_WARNING },
{ NULL, -1 }
};

/** Set MCE verbosity level
*/
static bool xmce_set_verbosity(const char *arg)
{
dbus_int32_t val = lookup(verbosity_levels, arg);
if( val < 0 ) {
errorf("%s: invalid verbosity level\n", arg);
return false;
}

return xmce_ipc_no_reply("set_verbosity",
DBUS_TYPE_INT32, &val,
DBUS_TYPE_INVALID);
}

/** Show current MCE verbosity level
*/
static void xmce_get_verbosity(void)
{
gint val = 0;
const char *txt = 0;

if( xmce_ipc_int_reply("get_verbosity", &val, DBUS_TYPE_INVALID) )
txt = rlookup(verbosity_levels, val);

printf("%-"PAD1"s %s \n", "Verbosity level:", txt ?: "unknown");
}

/* ------------------------------------------------------------------------- *
* color profile
Expand Down Expand Up @@ -5084,6 +5179,7 @@ static bool xmce_get_status(const char *args)
"-----------\n");

xmce_get_version();
xmce_get_verbosity();
xmce_get_radio_states();
xmce_get_call_state();
xmce_get_display_state();
Expand Down Expand Up @@ -6286,6 +6382,23 @@ static const mce_opt_t options[] =
"output version information and exit\n"

},
{
.name = "set-verbosity",
.with_arg = xmce_set_verbosity,
.values = "emerg|alert|crit|err|warning|notice|info|debug",
.usage =
"set the mce verbosity level\n"
"\n"
"Valid levels conform to syslog standard and are:\n"
" emerg - Silent (not used by mce)\n"
" alert - Silent (not used by mce)\n"
" crit - Critical problems that can cause mce to exit\n"
" err - Unexpected operational failures\n"
" warning - Tolerable operational failures\n"
" notice - Important status changes, external triggers\n"
" info - Status changes relevant in debugging\n"
" debug - Low importance changes/often occurring events\n"
},
{
.name = "set-memuse-warning-used",
.with_arg = xmce_set_memnotify_warning_used,
Expand Down

0 comments on commit 03e2ef5

Please sign in to comment.