Skip to content

Commit

Permalink
Wakelock debug logging is disabled by default
Browse files Browse the repository at this point in the history
It can be enabled via "--trace=wakelocks" command line option.
  • Loading branch information
spiiroin committed Jan 11, 2013
1 parent 5ca9f2a commit 2977fd6
Show file tree
Hide file tree
Showing 3 changed files with 137 additions and 87 deletions.
28 changes: 10 additions & 18 deletions libwakelock.c
Expand Up @@ -87,23 +87,8 @@ static char *lwl_concat(char *buf, size_t len, const char *str, ...)
}

#if LWL_ENABLE_LOGGING
/** Debug predicate function
*
* Even if logging is enabled, we will not write to stderr
* unless we're running from console.
*
* [Systemd captures stderr output and we do not want to
* cause unnecessary scheduling close to entry to suspend.]
*
* @return Non-zero value if logging can be done
*/
static int lwl_debug_p(void)
{
static int res = -1;
if( res == -1 )
res = (isatty(2) > 0);
return res;
}
/** Flag for enabling wakelock debug logging */
static int lwl_debug_enabled = 0;

/** Logging functionality that can be configured out at compile time
*/
Expand Down Expand Up @@ -135,7 +120,7 @@ static void lwl_debug_(const char *m, ...)

# define lwl_debug(MSG, MORE...) \
do {\
if( lwl_debug_p() ) lwl_debug_(MSG, ##MORE); \
if( lwl_debug_enabled ) lwl_debug_(MSG, ##MORE); \
} while( 0 )

#else
Expand Down Expand Up @@ -268,3 +253,10 @@ void wakelock_block_suspend_until_exit(void)
lwl_shutting_down = 1;
wakelock_block_suspend();
}

/** Enable wakelock debug logging (if support compiled in)
*/
void lwl_enable_logging(void)
{
lwl_debug_enabled = 1;
}
2 changes: 2 additions & 0 deletions libwakelock.h
Expand Up @@ -20,6 +20,8 @@ void wakelock_allow_suspend(void);
void wakelock_block_suspend(void);
void wakelock_block_suspend_until_exit(void);

void lwl_enable_logging(void);

# ifdef __cplusplus
};
# endif
Expand Down
194 changes: 125 additions & 69 deletions mce.c
Expand Up @@ -165,36 +165,32 @@ static void no_error_check_write(int fd, const void *data, size_t size)
rc = rc;
}

static const char usage_fmt[] =
"Usage: %s [OPTION]...\n"
"Mode Control Entity\n"
"\n"
" -d, --daemonflag run MCE as a daemon\n"
" -s, --force-syslog log to syslog even when not daemonized\n"
" -T, --force-stderr log to stderr even when daemonized\n"
" -S, --session use the session bus instead of the system\n"
" bus for D-Bus\n"
" -M, --show-module-info show information about loaded modules\n"
" -D, --debug-mode run even if dsme fails\n"
" -q, --quiet decrease debug message verbosity\n"
" -v, --verbose increase debug message verbosity\n"
" -t, --trace=<what> enable domain specific debug logging;\n"
" supported values: \"wakelocks\"\n"
" -h, --help display this help and exit\n"
" -V, --version output version information and exit\n"
"\n"
"Report bugs to <david.weinehall@nokia.com>\n"
;
/**
* Display usage information
*/
static void usage(void)
{
fprintf(stdout,
_("Usage: %s [OPTION]...\n"
"Mode Control Entity\n"
"\n"
" -d, --daemonflag run MCE as a daemon\n"
" -s, --force-syslog log to syslog even when not "
"daemonized\n"
" -T, --force-stderr log to stderr even when "
"daemonized\n"
" -S, --session use the session bus instead\n"
" of the "
"system bus for D-Bus\n"
" -M, --show-module-info show information about "
"loaded modules\n"
" -D, --debug-mode run even if dsme fails\n"
" -q, --quiet decrease debug message "
"verbosity\n"
" -v, --verbose increase debug message "
"verbosity\n"
" -h, --help display this help and exit\n"
" -V, --version output version information "
"and exit\n"
"\n"
"Report bugs to <david.weinehall@nokia.com>\n"),
progname);
fprintf(stdout, usage_fmt, progname);
}

/**
Expand Down Expand Up @@ -659,6 +655,84 @@ static gboolean daemonize(void)
return 0;
}

/** Helper for determining how long common prefix two strings have
*
* @param str1 non null string
* @param str2 non null string
*
* @return length of common prefix strings share
*/
static size_t common_length(const char *str1, const char *str2)
{
size_t i;
for( i = 0; str1[i] && str1[i] == str2[i]; ++i ) {}
return i;
}

/** Handle --trace=flags options
*
* @param flags comma separated list of trace domains
*
* @return TRUE on success, FALSE if unknown domains used
*/
static gboolean mce_enable_trace(const char *flags)
{
static const struct {
const char *domain;
void (*callback)(void);
} lut[] = {
#ifdef ENABLE_WAKELOCKS
{ "wakelocks", lwl_enable_logging },
#endif
{ NULL, NULL }
};

gboolean res = TRUE;
gchar *tmp = g_strdup(flags);

gchar *now, *zen;
size_t bi, bn;

for( now = tmp; now; now = zen ) {
if( (zen = strchr(now, ',')) )
*zen++ = 0;

// initialize to: no match
bi = bn = 0;

for( size_t ti = 0; lut[ti].domain; ++ti ) {
size_t tn = common_length(lut[ti].domain, now);

// all of flag mathed?
if( now[tn] )
continue;

// better or equal as the previous best?
if( bn <= tn )
bi = ti, bn = tn;

// full match found?
if( !lut[ti].domain[tn] )
break;
}

// did we find a match?
if( !bn ) {
fprintf(stderr, "unknown trace domain: '%s'\n", now);
res = FALSE;
}
else {
// report if non-full match was used
if( lut[bi].domain[bn] )
fprintf(stderr, "trace: %s\n", lut[bi].domain);
lut[bi].callback();
}
}

g_free(tmp);
return res;
}

/**
* Main
*
Expand All @@ -672,29 +746,30 @@ int main(int argc, char **argv)
int opt_index;

int verbosity = LL_DEFAULT;
int logtype = -1;
int logtype = MCE_LOG_SYSLOG;

gint status = 0;
gint status = EXIT_FAILURE;
gboolean show_module_info = FALSE;
gboolean daemonflag = FALSE;
gboolean systembus = TRUE;
gboolean debugmode = FALSE;

const char optline[] = "dsTSMDqvhV";
const char optline[] = "dsTSMDqvhVt:";

struct option const options[] = {
{ "daemonflag", no_argument, 0, 'd' },
{ "force-syslog", no_argument, 0, 's' },
{ "force-stderr", no_argument, 0, 'T' },
{ "session", no_argument, 0, 'S' },
{ "show-module-info", no_argument, 0, 'M' },
{ "debug-mode", no_argument, 0, 'D' },
{ "quiet", no_argument, 0, 'q' },
{ "verbose", no_argument, 0, 'v' },
{ "help", no_argument, 0, 'h' },
{ "version", no_argument, 0, 'V' },
{ "daemonflag", no_argument, 0, 'd' },
{ "force-syslog", no_argument, 0, 's' },
{ "force-stderr", no_argument, 0, 'T' },
{ "session", no_argument, 0, 'S' },
{ "show-module-info", no_argument, 0, 'M' },
{ "debug-mode", no_argument, 0, 'D' },
{ "quiet", no_argument, 0, 'q' },
{ "verbose", no_argument, 0, 'v' },
{ "help", no_argument, 0, 'h' },
{ "version", no_argument, 0, 'V' },
{ "trace", required_argument, 0, 't' },
{ 0, 0, 0, 0 }
};
};

/* Initialise support for locales, and set the program-name */
if (init_locales(PRG_NAME) != 0)
Expand All @@ -709,22 +784,10 @@ int main(int argc, char **argv)
break;

case 's':
if (logtype != -1) {
usage();
status = EINVAL;
goto EXIT;
}

logtype = MCE_LOG_SYSLOG;
break;

case 'T':
if (logtype != -1) {
usage();
status = EINVAL;
goto EXIT;
}

logtype = MCE_LOG_STDERR;
break;

Expand Down Expand Up @@ -752,16 +815,18 @@ int main(int argc, char **argv)

case 'h':
usage();
goto EXIT;
exit(EXIT_SUCCESS);

case 'V':
version();
goto EXIT;

exit(EXIT_SUCCESS);
case 't':
if( !mce_enable_trace(optarg) )
exit(EXIT_FAILURE);
break;
default:
usage();
status = EINVAL;
goto EXIT;
exit(EXIT_FAILURE);
}
}

Expand All @@ -771,14 +836,9 @@ int main(int argc, char **argv)
_("%s: Too many arguments\n"
"Try: `%s --help' for more information.\n"),
progname, progname);
status = EINVAL;
goto EXIT;
exit(EXIT_FAILURE);
}

if (logtype == -1)
logtype = (daemonflag == TRUE) ? MCE_LOG_SYSLOG :
MCE_LOG_STDERR;

mce_log_open(PRG_NAME, LOG_DAEMON, logtype);
mce_log_set_verbosity(verbosity);

Expand Down Expand Up @@ -902,7 +962,6 @@ int main(int argc, char **argv)
* pre-requisite: mce_dbus_init()
*/
if (mce_mode_init() == FALSE) {
status = EXIT_FAILURE;
goto EXIT;
}

Expand All @@ -914,40 +973,34 @@ int main(int argc, char **argv)
if (mce_dsme_init(debugmode) == FALSE) {
if (debugmode == FALSE) {
mce_log(LL_CRIT, "Cannot connect to DSME");
status = EXIT_FAILURE;
goto EXIT;
}
}

/* Initialise powerkey driver */
if (mce_powerkey_init() == FALSE) {
status = EXIT_FAILURE;
goto EXIT;
}

/* Initialise /dev/input driver
* pre-requisite: g_type_init()
*/
if (mce_input_init() == FALSE) {
status = EXIT_FAILURE;
goto EXIT;
}

/* Initialise switch driver */
if (mce_switches_init() == FALSE) {
status = EXIT_FAILURE;
goto EXIT;
}

/* Initialise tklock driver */
if (mce_tklock_init() == FALSE) {
status = EXIT_FAILURE;
goto EXIT;
}

/* Load all modules */
if (mce_modules_init() == FALSE) {
status = EXIT_FAILURE;
goto EXIT;
}

Expand All @@ -956,6 +1009,9 @@ int main(int argc, char **argv)
goto EXIT;
}

/* MCE startup succeeded */
status = EXIT_SUCCESS;

/* Run the main loop */
g_main_loop_run(mainloop);

Expand Down

0 comments on commit 2977fd6

Please sign in to comment.