summaryrefslogtreecommitdiff
path: root/src/libtracker-common/tracker-log.c
blob: a848f9ae405fefb85c8b68331548bd724be89aca (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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/*
 * Copyright (C) 2008, Nokia (urho.konttori@nokia.com)
 *
 * 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 Street, Fifth Floor,
 * Boston, MA  02110-1301, USA.
 */

#include "config.h"

#include <sys/time.h>
#include <sys/resource.h>
#include <sys/types.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>

#include <glib/gstdio.h>

#include "tracker-log.h"
#include "tracker-file-utils.h"

static gboolean  initialized;
static GMutex   *mutex;
static FILE     *fd;
static gint      verbosity;
static guint     log_handler_id;

static inline void
log_output (const gchar    *domain,
            GLogLevelFlags  log_level,
            const gchar    *message)
{
	time_t        now;
	gchar         time_str[64];
	gchar        *output;
	struct tm    *local_time;
	GTimeVal      current_time;
	const gchar  *log_level_str;
	static gsize  size = 0;

	g_return_if_fail (initialized == TRUE);
	g_return_if_fail (message != NULL && message[0] != '\0');

	/* Ensure file logging is thread safe */
	g_mutex_lock (mutex);

	/* Check log size, 10MiB limit */
	if (size > (10 << 20) && fd) {
		rewind (fd);

		if (ftruncate (fileno (fd), 0) != 0) {
			/* FIXME: What should we do if this fails? */
		}

		size = 0;
	}

	g_get_current_time (&current_time);

	now = time ((time_t *) NULL);
	local_time = localtime (&now);
	strftime (time_str, 64, "%d %b %Y, %H:%M:%S:", local_time);

	switch (log_level) {
	case G_LOG_LEVEL_WARNING:
		log_level_str = "-Warning **";
		break;

	case G_LOG_LEVEL_CRITICAL:
		log_level_str = "-Critical **";
		break;

	case G_LOG_LEVEL_ERROR:
		log_level_str = "-Error **";
		break;
	case G_LOG_FLAG_RECURSION:
	case G_LOG_FLAG_FATAL:
	case G_LOG_LEVEL_MESSAGE:
	case G_LOG_LEVEL_INFO:
	case G_LOG_LEVEL_DEBUG:
	case G_LOG_LEVEL_MASK:
	default:
		log_level_str = NULL;
		break;
	}

	output = g_strdup_printf ("%s%s %s%s: %s",
	                          log_level_str ? "\n" : "",
	                          time_str,
	                          domain,
	                          log_level_str ? log_level_str : "",
	                          message);

	if (G_UNLIKELY (fd == NULL)) {
		g_fprintf (stderr, "%s\n", output);
		fflush (stderr);
	} else {
		size += g_fprintf (fd, "%s\n", output);
		fflush (fd);
	}

	g_free (output);

	g_mutex_unlock (mutex);
}

static void
tracker_log_handler (const gchar    *domain,
                     GLogLevelFlags  log_level,
                     const gchar    *message,
                     gpointer        user_data)
{
	if (!tracker_log_should_handle (log_level, verbosity)) {
		return;
	}

	log_output (domain, log_level, message);

	/* Now show the message through stdout/stderr as usual */
	g_log_default_handler (domain, log_level, message, user_data);
}

gboolean
tracker_log_init (gint    this_verbosity,
                  gchar **used_filename)
{
	gchar *filename;
	gchar *basename;

	if (initialized) {
		return TRUE;
	}

	basename = g_strdup_printf ("%s.log", g_get_application_name ());
	filename = g_build_filename (g_get_user_data_dir (),
	                             "tracker",
	                             basename,
	                             NULL);
	g_free (basename);

	/* Remove previous log */
	g_unlink (filename);

	/* Open file */
	fd = g_fopen (filename, "a");
	if (!fd) {
		const gchar *error_string;

		error_string = g_strerror (errno);
		g_fprintf (stderr,
		           "Could not open log:'%s', %s\n",
		           filename,
		           error_string);
		g_fprintf (stderr,
		           "All logging will go to stderr\n");
	}

	verbosity = CLAMP (this_verbosity, 0, 3);
	mutex = g_mutex_new ();

	/* Add log handler function */
	log_handler_id = g_log_set_handler (NULL,
	                                    G_LOG_LEVEL_MASK | G_LOG_FLAG_FATAL,
	                                    tracker_log_handler,
	                                    NULL);

	g_log_set_default_handler (tracker_log_handler, NULL);

	if (used_filename) {
		*used_filename = filename;
	} else {
		g_free (filename);
	}

	initialized = TRUE;

	return TRUE;
}

void
tracker_log_shutdown (void)
{
	if (!initialized) {
		return;
	}

	if (fd) {
		fclose (fd);
	}

	g_log_remove_handler (NULL, log_handler_id);
	log_handler_id = 0;

	g_mutex_free (mutex);

	initialized = FALSE;
}

gboolean
tracker_log_should_handle (GLogLevelFlags log_level,
                           gint           this_verbosity)
{
	switch (this_verbosity) {
		/* Log level 3: EVERYTHING */
	case 3:
		break;

		/* Log level 2: CRITICAL/ERROR/WARNING/INFO/MESSAGE only */
	case 2:
		if (!(log_level & G_LOG_LEVEL_MESSAGE) &&
		    !(log_level & G_LOG_LEVEL_INFO) &&
		    !(log_level & G_LOG_LEVEL_WARNING) &&
		    !(log_level & G_LOG_LEVEL_ERROR) &&
		    !(log_level & G_LOG_LEVEL_CRITICAL)) {
			return FALSE;
		}

		break;

		/* Log level 1: CRITICAL/ERROR/WARNING/INFO only */
	case 1:
		if (!(log_level & G_LOG_LEVEL_INFO) &&
		    !(log_level & G_LOG_LEVEL_WARNING) &&
		    !(log_level & G_LOG_LEVEL_ERROR) &&
		    !(log_level & G_LOG_LEVEL_CRITICAL)) {
			return FALSE;
		}

		break;

		/* Log level 0: CRITICAL/ERROR/WARNING only (default) */
	default:
	case 0:
		if (!(log_level & G_LOG_LEVEL_WARNING) &&
		    !(log_level & G_LOG_LEVEL_ERROR) &&
		    !(log_level & G_LOG_LEVEL_CRITICAL)) {
			return FALSE;
		}

		break;
	}

	return TRUE;
}