[pkg-fso-commits] [SCM] Automatic Display Manager branch, master, updated. debian/0.8-1-8-ga22b3b9
Enrico Zini
enrico at enricozini.org
Fri Jul 8 22:55:27 UTC 2011
The following commit has been merged in the master branch:
commit a22b3b97c6ac8d4c98660cc0d87891ee62be4b89
Author: Enrico Zini <enrico at enricozini.org>
Date: Sat Jul 9 00:55:21 2011 +0200
Cleaned up log interface and exposed more logging options on command line
diff --git a/log.c b/log.c
index 8b84453..9a89d78 100644
--- a/log.c
+++ b/log.c
@@ -30,8 +30,7 @@ void log_config_init(struct log_config* conf)
conf->program_name = 0;
conf->log_to_syslog = true;
conf->log_to_stderr = true;
- conf->info_to_stderr = false;
- conf->verbose = false;
+ conf->log_level = LOG_INFO;
}
void log_start(const struct log_config* conf)
@@ -48,88 +47,63 @@ void log_end()
closelog();
}
-void log_verbose(const char* fmt, ...)
+static void log_common(int prio, const char* fmt, va_list ap)
{
- if (!config->verbose) return;
-
- va_list ap;
-
- if (config->info_to_stderr)
+ if (config->log_to_stderr)
{
+ va_list loc;
+ va_copy(loc, ap);
fprintf(stderr, "%s:", config->program_name);
- va_start(ap, fmt);
- vfprintf(stderr, fmt, ap);
- va_end(ap);
+ vfprintf(stderr, fmt, loc);
+ va_end(loc);
fputc('\n', stderr);
}
if (config->log_to_syslog)
{
- va_start(ap, fmt);
- vsyslog(LOG_WARNING, fmt, ap);
- va_end(ap);
+ va_list loc;
+ va_copy(loc, ap);
+ vsyslog(prio, fmt, loc);
+ va_end(loc);
}
}
-void log_info(const char* fmt, ...)
+void log_verbose(const char* fmt, ...)
{
+ if (!config->log_level < NODM_LL_VERB) return;
+
va_list ap;
+ va_start(ap, fmt);
+ log_common(LOG_INFO, fmt, ap);
+ va_end(ap);
+}
- if (config->info_to_stderr)
- {
- fprintf(stderr, "%s:", config->program_name);
- va_start(ap, fmt);
- vfprintf(stderr, fmt, ap);
- va_end(ap);
- fputc('\n', stderr);
- }
+void log_info(const char* fmt, ...)
+{
+ if (!config->log_level < NODM_LL_INFO) return;
- if (config->log_to_syslog)
- {
- va_start(ap, fmt);
- vsyslog(LOG_WARNING, fmt, ap);
- va_end(ap);
- }
+ va_list ap;
+ va_start(ap, fmt);
+ log_common(LOG_NOTICE, fmt, ap);
+ va_end(ap);
}
void log_warn(const char* fmt, ...)
{
- va_list ap;
+ if (!config->log_level < NODM_LL_WARN) return;
- if (config->log_to_stderr)
- {
- fprintf(stderr, "%s:", config->program_name);
- va_start(ap, fmt);
- vfprintf(stderr, fmt, ap);
- va_end(ap);
- fputc('\n', stderr);
- }
-
- if (config->log_to_syslog)
- {
- va_start(ap, fmt);
- vsyslog(LOG_WARNING, fmt, ap);
- va_end(ap);
- }
+ va_list ap;
+ va_start(ap, fmt);
+ log_common(LOG_WARNING, fmt, ap);
+ va_end(ap);
}
void log_err(const char* fmt, ...)
{
- va_list ap;
+ if (!config->log_level < NODM_LL_ERR) return;
- if (config->log_to_stderr)
- {
- fprintf(stderr, "%s:", config->program_name);
- va_start(ap, fmt);
- vfprintf(stderr, fmt, ap);
- va_end(ap);
- fputc('\n', stderr);
- }
-
- if (config->log_to_syslog)
- {
- va_start(ap, fmt);
- vsyslog(LOG_ERR, fmt, ap);
- va_end(ap);
- }
+ va_list ap;
+ va_start(ap, fmt);
+ log_common(LOG_ERR, fmt, ap);
+ va_end(ap);
}
diff --git a/log.h b/log.h
index 5638228..f84ff7c 100644
--- a/log.h
+++ b/log.h
@@ -29,17 +29,25 @@ struct log_config
/// The program name to use in error messages
const char* program_name;
- /// Log all messages to syslog
+ /// Log to syslog
bool log_to_syslog;
- /// Log warnings and errors to stderr
+ /// Log to stderr
bool log_to_stderr;
- /// Also log info messages to stderr
- bool info_to_stderr;
-
- /// Log verbose messages
- bool verbose;
+ /**
+ * Log level:
+ * 1. errors
+ * 2. warnings
+ * 3. info
+ * 4. verbose
+ */
+ enum {
+ NODM_LL_ERR = 1,
+ NODM_LL_WARN = 2,
+ NODM_LL_INFO = 3,
+ NODM_LL_VERB = 4
+ } log_level;
};
/**
diff --git a/nodm.c b/nodm.c
index 9118e4d..b588569 100644
--- a/nodm.c
+++ b/nodm.c
@@ -44,12 +44,15 @@ static void do_help(int argc, char** argv, FILE* out)
{
fprintf(out, "Usage: %s [options]\n\n", argv[0]);
fprintf(out, "Options:\n");
- fprintf(out, " --help print this help message\n");
- fprintf(out, " --version print %s's version number\n", NAME);
- fprintf(out, " --verbose verbose outpout or logging\n");
- fprintf(out, " --nested run a nested X server, does not require root.");
- fprintf(out, " The server defaults to \"/usr/bin/Xnest :1\",");
- fprintf(out, " override with NODM_X_OPTIONS\n");
+ fprintf(out, " --help print this help message\n");
+ fprintf(out, " --version print %s's version number\n", NAME);
+ fprintf(out, " --verbose verbose outpout or logging\n");
+ fprintf(out, " --quiet only log warnings and errors\n");
+ fprintf(out, " --nested run a nested X server, does not require root.");
+ fprintf(out, " The server defaults to \"/usr/bin/Xnest :1\",");
+ fprintf(out, " override with NODM_X_OPTIONS\n");
+ fprintf(out, " --[no-]syslog enable/disable logging to syslog\n");
+ fprintf(out, " --[no-]stderr enable/disable logging to stderr\n");
}
@@ -66,17 +69,24 @@ int main (int argc, char **argv)
static int opt_help = 0;
static int opt_version = 0;
static int opt_verbose = 0;
+ static int opt_quiet = 0;
static int opt_nested = 0;
+ static int opt_log_syslog = -1; // -1 for 'default'
+ static int opt_log_stderr = -1; // -1 for 'default'
static struct option options[] =
{
/* These options set a flag. */
{"help", no_argument, &opt_help, 1},
{"version", no_argument, &opt_version, 1},
{"verbose", no_argument, &opt_verbose, 1},
+ {"quiet", no_argument, &opt_quiet, 1},
{"nested", no_argument, &opt_nested, 1},
+ {"syslog", no_argument, &opt_log_syslog, 1},
+ {"stderr", no_argument, &opt_log_stderr, 1},
+ {"no-syslog", no_argument, &opt_log_syslog, 0},
+ {"no-stderr", no_argument, &opt_log_stderr, 0},
{0, 0, 0, 0}
};
- // TODO: more output control, such as something like --quiet, --no-syslog, --no-stderr
// Parse command line options
while (1)
@@ -114,18 +124,23 @@ int main (int argc, char **argv)
// Setup logging
struct log_config cfg = {
.program_name = basename(argv[0]),
- .verbose = opt_verbose
};
+ if (opt_quiet)
+ cfg.log_level = NODM_LL_WARN;
+ else if (opt_verbose)
+ cfg.log_level = NODM_LL_VERB;
+ else
+ cfg.log_level = NODM_LL_INFO;
if (opt_nested)
{
- cfg.log_to_syslog = false;
- cfg.log_to_stderr = true;
- cfg.info_to_stderr = opt_verbose;
+ if (opt_log_syslog == -1) opt_log_syslog = 0;
+ if (opt_log_stderr == -1) opt_log_stderr = 1;
} else {
- cfg.log_to_syslog = true;
- cfg.log_to_stderr = false;
- cfg.info_to_stderr = false;
+ if (opt_log_syslog == -1) opt_log_syslog = 1;
+ if (opt_log_stderr == -1) opt_log_stderr = 0;
}
+ cfg.log_to_syslog = opt_log_syslog ? true : false;
+ cfg.log_to_stderr = opt_log_stderr ? true : false;
log_start(&cfg);
log_info("starting nodm");
diff --git a/test.c b/test.c
index fa6d891..80dd19c 100644
--- a/test.c
+++ b/test.c
@@ -58,8 +58,8 @@ void test_start(const char* testname, bool verbose)
cfg.program_name = testname,
cfg.log_to_syslog = false,
cfg.log_to_stderr = true,
- cfg.info_to_stderr = verbose,
- cfg.verbose = verbose,
+ cfg.log_level = NODM_LL_INFO;
+ cfg.log_level = NODM_LL_VERB;
log_start(&cfg);
}
--
Automatic Display Manager
More information about the pkg-fso-commits
mailing list