aboutsummaryrefslogtreecommitdiffstats
path: root/libfsoframework/fsoframework/logger.c
blob: 74871ec89cad16967e4aff6d75f58cd7cd235347 (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
/**
 * Copyright (C) 2009 Michael 'Mickey' Lauer <mlauer@vanille-media.de>
 *
 * 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 <fsoframework/logger.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>




enum  {
	FSO_FRAMEWORK_ABSTRACT_LOGGER_DUMMY_PROPERTY
};
static void fso_framework_abstract_logger_real_write (FsoFrameworkAbstractLogger* self, const char* message);
static gpointer fso_framework_abstract_logger_parent_class = NULL;
static void fso_framework_abstract_logger_finalize (GObject* obj);
struct _FsoFrameworkFileLoggerPrivate {
	gint file;
};

#define FSO_FRAMEWORK_FILE_LOGGER_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), FSO_FRAMEWORK_TYPE_FILE_LOGGER, FsoFrameworkFileLoggerPrivate))
enum  {
	FSO_FRAMEWORK_FILE_LOGGER_DUMMY_PROPERTY
};
static gpointer fso_framework_file_logger_parent_class = NULL;
static void fso_framework_file_logger_finalize (GObject* obj);



static void fso_framework_abstract_logger_real_write (FsoFrameworkAbstractLogger* self, const char* message) {
	g_return_if_fail (self != NULL);
	g_return_if_fail (message != NULL);
}


void fso_framework_abstract_logger_write (FsoFrameworkAbstractLogger* self, const char* message) {
	FSO_FRAMEWORK_ABSTRACT_LOGGER_GET_CLASS (self)->write (self, message);
}


FsoFrameworkAbstractLogger* fso_framework_abstract_logger_construct (GType object_type, const char* domain) {
	FsoFrameworkAbstractLogger * self;
	char* _tmp1;
	const char* _tmp0;
	g_return_val_if_fail (domain != NULL, NULL);
	self = g_object_newv (object_type, 0, NULL);
	_tmp1 = NULL;
	_tmp0 = NULL;
	self->domain = (_tmp1 = (_tmp0 = domain, (_tmp0 == NULL) ? NULL : g_strdup (_tmp0)), self->domain = (g_free (self->domain), NULL), _tmp1);
	return self;
}


FsoFrameworkAbstractLogger* fso_framework_abstract_logger_new (const char* domain) {
	return fso_framework_abstract_logger_construct (FSO_FRAMEWORK_TYPE_ABSTRACT_LOGGER, domain);
}


void fso_framework_abstract_logger_setLevel (FsoFrameworkAbstractLogger* self, GLogLevelFlags level) {
	g_return_if_fail (self != NULL);
	self->level = (guint) level;
}


void fso_framework_abstract_logger_setDestination (FsoFrameworkAbstractLogger* self, const char* destination) {
	char* _tmp1;
	const char* _tmp0;
	g_return_if_fail (self != NULL);
	g_return_if_fail (destination != NULL);
	_tmp1 = NULL;
	_tmp0 = NULL;
	self->destination = (_tmp1 = (_tmp0 = destination, (_tmp0 == NULL) ? NULL : g_strdup (_tmp0)), self->destination = (g_free (self->destination), NULL), _tmp1);
}


void fso_framework_abstract_logger_debug (FsoFrameworkAbstractLogger* self, const char* message) {
	g_return_if_fail (self != NULL);
	g_return_if_fail (message != NULL);
	if (self->level >= ((guint) G_LOG_LEVEL_DEBUG)) {
		fso_framework_abstract_logger_write (self, message);
	}
}


void fso_framework_abstract_logger_info (FsoFrameworkAbstractLogger* self, const char* message) {
	g_return_if_fail (self != NULL);
	g_return_if_fail (message != NULL);
	if (self->level >= ((guint) G_LOG_LEVEL_INFO)) {
		fso_framework_abstract_logger_write (self, message);
	}
}


void fso_framework_abstract_logger_warning (FsoFrameworkAbstractLogger* self, const char* message) {
	g_return_if_fail (self != NULL);
	g_return_if_fail (message != NULL);
	if (self->level >= ((guint) G_LOG_LEVEL_WARNING)) {
		fso_framework_abstract_logger_write (self, message);
	}
}


void fso_framework_abstract_logger_error (FsoFrameworkAbstractLogger* self, const char* message) {
	g_return_if_fail (self != NULL);
	g_return_if_fail (message != NULL);
	if (self->level >= ((guint) G_LOG_LEVEL_ERROR)) {
		fso_framework_abstract_logger_write (self, message);
	}
}


static void fso_framework_abstract_logger_class_init (FsoFrameworkAbstractLoggerClass * klass) {
	fso_framework_abstract_logger_parent_class = g_type_class_peek_parent (klass);
	G_OBJECT_CLASS (klass)->finalize = fso_framework_abstract_logger_finalize;
	FSO_FRAMEWORK_ABSTRACT_LOGGER_CLASS (klass)->write = fso_framework_abstract_logger_real_write;
}


static void fso_framework_abstract_logger_instance_init (FsoFrameworkAbstractLogger * self) {
	self->level = (guint) G_LOG_LEVEL_INFO;
}


static void fso_framework_abstract_logger_finalize (GObject* obj) {
	FsoFrameworkAbstractLogger * self;
	self = FSO_FRAMEWORK_ABSTRACT_LOGGER (obj);
	self->domain = (g_free (self->domain), NULL);
	self->destination = (g_free (self->destination), NULL);
	G_OBJECT_CLASS (fso_framework_abstract_logger_parent_class)->finalize (obj);
}


GType fso_framework_abstract_logger_get_type (void) {
	static GType fso_framework_abstract_logger_type_id = 0;
	if (fso_framework_abstract_logger_type_id == 0) {
		static const GTypeInfo g_define_type_info = { sizeof (FsoFrameworkAbstractLoggerClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) fso_framework_abstract_logger_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (FsoFrameworkAbstractLogger), 0, (GInstanceInitFunc) fso_framework_abstract_logger_instance_init, NULL };
		fso_framework_abstract_logger_type_id = g_type_register_static (G_TYPE_OBJECT, "FsoFrameworkAbstractLogger", &g_define_type_info, G_TYPE_FLAG_ABSTRACT);
	}
	return fso_framework_abstract_logger_type_id;
}


FsoFrameworkFileLogger* fso_framework_file_logger_construct (GType object_type, const char* domain) {
	FsoFrameworkFileLogger * self;
	g_return_val_if_fail (domain != NULL, NULL);
	self = (FsoFrameworkFileLogger*) fso_framework_abstract_logger_construct (object_type, domain);
	return self;
}


FsoFrameworkFileLogger* fso_framework_file_logger_new (const char* domain) {
	return fso_framework_file_logger_construct (FSO_FRAMEWORK_TYPE_FILE_LOGGER, domain);
}


void fso_framework_file_logger_setFile (FsoFrameworkFileLogger* self, const char* filename, gboolean append) {
	gint flags;
	char* _tmp2;
	const char* _tmp1;
	g_return_if_fail (self != NULL);
	g_return_if_fail (filename != NULL);
	if (self->priv->file != (-1)) {
		char* _tmp0;
		_tmp0 = NULL;
		((FsoFrameworkAbstractLogger*) self)->destination = (_tmp0 = NULL, ((FsoFrameworkAbstractLogger*) self)->destination = (g_free (((FsoFrameworkAbstractLogger*) self)->destination), NULL), _tmp0);
		close (self->priv->file);
	}
	flags = (O_EXCL | O_CREAT) | O_WRONLY;
	if (append) {
		flags = flags | O_APPEND;
	}
	self->priv->file = open (filename, flags, S_IRWXU);
	_tmp2 = NULL;
	_tmp1 = NULL;
	((FsoFrameworkAbstractLogger*) self)->destination = (_tmp2 = (_tmp1 = filename, (_tmp1 == NULL) ? NULL : g_strdup (_tmp1)), ((FsoFrameworkAbstractLogger*) self)->destination = (g_free (((FsoFrameworkAbstractLogger*) self)->destination), NULL), _tmp2);
}


static void fso_framework_file_logger_class_init (FsoFrameworkFileLoggerClass * klass) {
	fso_framework_file_logger_parent_class = g_type_class_peek_parent (klass);
	g_type_class_add_private (klass, sizeof (FsoFrameworkFileLoggerPrivate));
	G_OBJECT_CLASS (klass)->finalize = fso_framework_file_logger_finalize;
}


static void fso_framework_file_logger_instance_init (FsoFrameworkFileLogger * self) {
	self->priv = FSO_FRAMEWORK_FILE_LOGGER_GET_PRIVATE (self);
	self->priv->file = -1;
}


static void fso_framework_file_logger_finalize (GObject* obj) {
	FsoFrameworkFileLogger * self;
	self = FSO_FRAMEWORK_FILE_LOGGER (obj);
	G_OBJECT_CLASS (fso_framework_file_logger_parent_class)->finalize (obj);
}


GType fso_framework_file_logger_get_type (void) {
	static GType fso_framework_file_logger_type_id = 0;
	if (fso_framework_file_logger_type_id == 0) {
		static const GTypeInfo g_define_type_info = { sizeof (FsoFrameworkFileLoggerClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) fso_framework_file_logger_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (FsoFrameworkFileLogger), 0, (GInstanceInitFunc) fso_framework_file_logger_instance_init, NULL };
		fso_framework_file_logger_type_id = g_type_register_static (FSO_FRAMEWORK_TYPE_ABSTRACT_LOGGER, "FsoFrameworkFileLogger", &g_define_type_info, 0);
	}
	return fso_framework_file_logger_type_id;
}