summaryrefslogtreecommitdiff
path: root/src/telepathy-logger.c
blob: 9f63bdf9032055bb0a03a94fa80a5c4b0cb1493f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/*
 * Copyright (C) 2009 Collabora Ltd.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * Authors: Cosimo Alfarano <cosimo.alfarano@collabora.co.uk>
 */

#include <config.h>

#include <glib.h>

#include <telepathy-glib/telepathy-glib.h>

#include <telepathy-logger/observer-internal.h>
#include <telepathy-logger/dbus-service-internal.h>

#define DEBUG_FLAG TPL_DEBUG_MAIN
#include <telepathy-logger/debug-internal.h>

static GMainLoop *loop = NULL;

#ifdef ENABLE_DEBUG
static TpDebugSender *debug_sender = NULL;
static gboolean stamp_logs = FALSE;


static void
log_to_debug_sender (const gchar *log_domain,
    GLogLevelFlags log_level,
    const gchar *string)
{
  GTimeVal now;

  g_return_if_fail (TP_IS_DEBUG_SENDER (debug_sender));

  g_get_current_time (&now);

  tp_debug_sender_add_message (debug_sender, &now, log_domain, log_level,
      string);
}


static void
log_handler (const gchar *log_domain,
    GLogLevelFlags log_level,
    const gchar *message,
    gpointer user_data)
{
  if (stamp_logs)
    {
      GTimeVal now;
      gchar now_str[32];
      gchar *tmp;
      struct tm tm;

      g_get_current_time (&now);
      localtime_r (&(now.tv_sec), &tm);
      strftime (now_str, 32, "%Y-%m-%d %H:%M:%S", &tm);
      tmp = g_strdup_printf ("%s.%06ld: %s",
        now_str, now.tv_usec, message);

      g_log_default_handler (log_domain, log_level, tmp, NULL);

      g_free (tmp);
    }
  else
    {
      g_log_default_handler (log_domain, log_level, message, NULL);
    }

  log_to_debug_sender (log_domain, log_level, message);
}
#endif /* ENABLE_DEBUG */


static TplDBusService *
telepathy_logger_dbus_init (void)
{
  TplDBusService *dbus_srv = NULL;
  TpDBusDaemon *tp_bus = NULL;
  GError *error = NULL;


  DEBUG ("Initializing TPL DBus service");
  tp_bus = tp_dbus_daemon_dup (&error);
  if (tp_bus == NULL)
    {
      g_critical ("Failed to acquire bus daemon: %s", error->message);
      goto out;
    }

  if (!tp_dbus_daemon_request_name (tp_bus, TPL_DBUS_SRV_WELL_KNOWN_BUS_NAME,
        FALSE, &error))
    {
      g_critical ("Failed to acquire bus name %s: %s",
          TPL_DBUS_SRV_WELL_KNOWN_BUS_NAME, error->message);
      goto out;
    }

  dbus_srv = _tpl_dbus_service_new ();
  tp_dbus_daemon_register_object (tp_bus, TPL_DBUS_SRV_OBJECT_PATH,
      G_OBJECT (dbus_srv));

  DEBUG ("TPL DBus service registered to: %s",
      TPL_DBUS_SRV_WELL_KNOWN_BUS_NAME);

out:
  if (error != NULL)
      g_clear_error (&error);
  if (tp_bus != NULL)
    g_object_unref (tp_bus);

  return dbus_srv;
}


int
main (int argc,
    char *argv[])
{
  TplDBusService *dbus_srv = NULL;
  TplObserver *observer = NULL;
  GError *error = NULL;

  g_type_init ();

  g_set_prgname (PACKAGE_NAME);

  tp_debug_divert_messages (g_getenv ("TPL_LOGFILE"));

#ifdef ENABLE_DEBUG
  _tpl_debug_set_flags_from_env ();

  stamp_logs = (g_getenv ("TPL_TIMING") != NULL);
  debug_sender = tp_debug_sender_dup ();

  g_log_set_default_handler (log_handler, NULL);
#endif /* ENABLE_DEBUG */

  observer = _tpl_observer_dup (&error);

  if (observer == NULL) {
    g_critical ("Failed to create observer: %s", error->message);
    g_error_free (error);
    goto out;
  }

  if (!tp_base_client_register (TP_BASE_CLIENT (observer), &error))
    {
      g_critical ("Error during D-Bus registration: %s", error->message);
      goto out;
    }
  DEBUG ("TPL Observer registered to: %s", TPL_OBSERVER_WELL_KNOWN_BUS_NAME);

  dbus_srv = telepathy_logger_dbus_init ();

  loop = g_main_loop_new (NULL, FALSE);
  g_main_loop_run (loop);

out:
  if (observer != NULL)
    g_object_unref (observer);
  if (dbus_srv != NULL)
    g_object_unref (dbus_srv);

#ifdef ENABLE_DEBUG
  g_log_set_default_handler (g_log_default_handler, NULL);
  g_object_unref (debug_sender);
#endif /* ENABLE_DEBUG */

  return 0;
}