summaryrefslogtreecommitdiff
path: root/src/pk-syslog.c
blob: 3102b8ea514e582ea3e04a32d5f0d0b3242d503f (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
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
 *
 * Copyright (C) 2008 Richard Hughes <richard@hughsie.com>
 *
 * Licensed under the GNU General Public License Version 2
 *
 * 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.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#include <syslog.h>
#include <glib.h>

#include "pk-syslog.h"
#include "pk-conf.h"

#define PK_SYSLOG_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), PK_TYPE_SYSLOG, PkSyslogPrivate))

struct PkSyslogPrivate
{
	gboolean		 enabled;
	/* any logging instance data here */
};

G_DEFINE_TYPE (PkSyslog, pk_syslog, G_TYPE_OBJECT)
static gpointer pk_syslog_object = NULL;

/**
 * pk_syslog_add:
 **/
void
pk_syslog_add (PkSyslog *self, PkSyslogType type, const gchar *format, ...)
{
	va_list args;
	gchar va_args_buffer[1025];

	g_return_if_fail (PK_IS_SYSLOG (self));

	if (!self->priv->enabled)
		return;

	va_start (args, format);
	g_vsnprintf (va_args_buffer, 1024, format, args);
	va_end (args);

	/* auth messages are special */
	if (type == PK_SYSLOG_TYPE_AUTH)
		syslog (LOG_AUTHPRIV, "%s", va_args_buffer);

	g_debug ("logging to syslog '%s'", va_args_buffer);
	syslog (LOG_DAEMON, "%s", va_args_buffer);
}

/**
 * pk_syslog_finalize:
 **/
static void
pk_syslog_finalize (GObject *object)
{
	g_return_if_fail (PK_IS_SYSLOG (object));

	/* shut down syslog */
	closelog ();

	G_OBJECT_CLASS (pk_syslog_parent_class)->finalize (object);
}

/**
 * pk_syslog_class_init:
 **/
static void
pk_syslog_class_init (PkSyslogClass *klass)
{
	GObjectClass *object_class = G_OBJECT_CLASS (klass);
	object_class->finalize = pk_syslog_finalize;
	g_type_class_add_private (klass, sizeof (PkSyslogPrivate));
}

/**
 * pk_syslog_init:
 **/
static void
pk_syslog_init (PkSyslog *self)
{
	PkConf *conf;
	self->priv = PK_SYSLOG_GET_PRIVATE (self);

	conf = pk_conf_new ();
	self->priv->enabled = pk_conf_get_bool (conf, "UseSyslog");
	g_object_unref (conf);

	if (!self->priv->enabled) {
		g_debug ("syslog fucntionality disabled");
		return;
	}

	/* open syslog */
	openlog ("PackageKit", LOG_NDELAY, LOG_USER);
}

/**
 * pk_syslog_new:
 * Return value: A new syslog class instance.
 **/
PkSyslog *
pk_syslog_new (void)
{
	if (pk_syslog_object != NULL) {
		g_object_ref (pk_syslog_object);
	} else {
		pk_syslog_object = g_object_new (PK_TYPE_SYSLOG, NULL);
		g_object_add_weak_pointer (pk_syslog_object, &pk_syslog_object);
	}
	return PK_SYSLOG (pk_syslog_object);
}