context: new source files to keep the daemon context

This commit is contained in:
Aleksander Morgado
2012-02-28 23:09:10 +01:00
parent 1701230f51
commit 952b9164b9
4 changed files with 131 additions and 30 deletions

View File

@@ -132,6 +132,8 @@ nodist_ModemManager_SOURCES = \
ModemManager_SOURCES = \
main.c \
mm-context.h \
mm-context.c \
mm-log.c \
mm-log.h \
mm-private-boxed-types.h \

View File

@@ -27,6 +27,7 @@
#include "mm-manager.h"
#include "mm-log.h"
#include "mm-context.h"
#if !defined(MM_DIST_VERSION)
# define MM_DIST_VERSION VERSION
@@ -103,41 +104,18 @@ main (int argc, char *argv[])
{
GDBusConnection *bus;
GError *err = NULL;
GOptionContext *opt_ctx;
guint name_id;
const char *log_level = NULL, *log_file = NULL;
gboolean debug = FALSE, show_ts = FALSE, rel_ts = FALSE;
GOptionEntry entries[] = {
{ "debug", 0, 0, G_OPTION_ARG_NONE, &debug, "Output to console rather than syslog", NULL },
{ "log-level", 0, 0, G_OPTION_ARG_STRING, &log_level, "Log level: one of [ERR, WARN, INFO, DEBUG]", "INFO" },
{ "log-file", 0, 0, G_OPTION_ARG_STRING, &log_file, "Path to log file", NULL },
{ "timestamps", 0, 0, G_OPTION_ARG_NONE, &show_ts, "Show timestamps in log output", NULL },
{ "relative-timestamps", 0, 0, G_OPTION_ARG_NONE, &rel_ts, "Use relative timestamps (from MM start)", NULL },
{ NULL }
};
g_type_init ();
opt_ctx = g_option_context_new (NULL);
g_option_context_set_summary (opt_ctx, "DBus system service to communicate with modems.");
g_option_context_add_main_entries (opt_ctx, entries, NULL);
/* Setup application context */
mm_context_init (argc, argv);
if (!g_option_context_parse (opt_ctx, &argc, &argv, &err)) {
g_warning ("%s\n", err->message);
g_error_free (err);
exit (1);
}
g_option_context_free (opt_ctx);
if (debug) {
log_level = "DEBUG";
if (!show_ts && !rel_ts)
show_ts = TRUE;
}
if (!mm_log_setup (log_level, log_file, show_ts, rel_ts, &err)) {
if (!mm_log_setup (mm_context_get_log_level (),
mm_context_get_log_file (),
mm_context_get_timestamps (),
mm_context_get_relative_timestamps (),
&err)) {
g_warning ("Failed to set up logging: %s", err->message);
g_error_free (err);
exit (1);

91
src/mm-context.c Normal file
View File

@@ -0,0 +1,91 @@
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details:
*
* Copyright (C) 2012 Aleksander Morgado <aleksander@gnu.org>
*/
#include <stdlib.h>
#include "mm-context.h"
/* Application context */
static gboolean debug;
static const gchar *log_level;
static const gchar *log_file;
static gboolean show_ts;
static gboolean rel_ts;
static const GOptionEntry entries[] = {
{ "debug", 0, 0, G_OPTION_ARG_NONE, &debug, "Run with extended debugging capabilities", NULL },
{ "log-level", 0, 0, G_OPTION_ARG_STRING, &log_level, "Log level: one of [ERR, WARN, INFO, DEBUG]", "INFO" },
{ "log-file", 0, 0, G_OPTION_ARG_STRING, &log_file, "Path to log file", NULL },
{ "timestamps", 0, 0, G_OPTION_ARG_NONE, &show_ts, "Show timestamps in log output", NULL },
{ "relative-timestamps", 0, 0, G_OPTION_ARG_NONE, &rel_ts, "Use relative timestamps (from MM start)", NULL },
{ NULL }
};
gboolean
mm_context_get_debug (void)
{
return debug;
}
const gchar *
mm_context_get_log_level (void)
{
return log_level;
}
const gchar *
mm_context_get_log_file (void)
{
return log_file;
}
gboolean
mm_context_get_timestamps (void)
{
return show_ts;
}
gboolean
mm_context_get_relative_timestamps (void)
{
return rel_ts;
}
void
mm_context_init (gint argc,
gchar **argv)
{
GError *error = NULL;
GOptionContext *ctx;
ctx = g_option_context_new (NULL);
g_option_context_set_summary (ctx, "DBus system service to communicate with modems.");
g_option_context_add_main_entries (ctx, entries, NULL);
if (!g_option_context_parse (ctx, &argc, &argv, &error)) {
g_warning ("%s\n", error->message);
g_error_free (error);
exit (1);
}
g_option_context_free (ctx);
/* Additional setup to be done on debug mode */
if (debug) {
log_level = "DEBUG";
if (!show_ts && !rel_ts)
show_ts = TRUE;
}
}

30
src/mm-context.h Normal file
View File

@@ -0,0 +1,30 @@
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details:
*
* Copyright (C) 2012 Aleksander Morgado <aleksander@gnu.org>
*/
#ifndef MM_CONTEXT_H
#define MM_CONTEXT_H
#include <glib.h>
void mm_context_init (gint argc,
gchar **argv);
gboolean mm_context_get_debug (void);
const gchar *mm_context_get_log_level (void);
const gchar *mm_context_get_log_file (void);
gboolean mm_context_get_timestamps (void);
gboolean mm_context_get_relative_timestamps (void);
#endif /* MM_CONTEXT_H */