Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 330 lines (283 sloc) 7.889 kb
d1b8326 Dave Beckett RDF logging
authored
1 /* -*- Mode: c; c-basic-offset: 2 -*-
2 *
3 * rdf_log.c - Kids love log
4 *
198943c Dave Beckett 2008
authored
5 * Copyright (C) 2004-2008, David Beckett http://www.dajobe.org/
6cad134 Dave Beckett autodocs
authored
6 * Copyright (C) 2004-2004, University of Bristol, UK http://www.bristol.ac.uk/
d1b8326 Dave Beckett RDF logging
authored
7 *
4827af4 Dave Beckett LGPL / Apache 2.0 license
authored
8 * This package is Free Software and part of Redland http://librdf.org/
9 *
10 * It is licensed under the following three licenses as alternatives:
11 * 1. GNU Lesser General Public License (LGPL) V2.1 or any newer version
12 * 2. GNU General Public License (GPL) V2 or any newer version
13 * 3. Apache License, V2.0 or any newer version
14 *
15 * You may not use this file except in compliance with at least one of
16 * the above three licenses.
d1b8326 Dave Beckett RDF logging
authored
17 *
18 * See LICENSE.html or LICENSE.txt at the top of this package for the
4827af4 Dave Beckett LGPL / Apache 2.0 license
authored
19 * complete terms and further detail along with the license texts for
20 * the licenses in COPYING.LIB, COPYING and LICENSE-2.0.txt respectively.
d1b8326 Dave Beckett RDF logging
authored
21 *
22 *
23 */
24
25
c8e95fa Dave Beckett Added #include <win32_rdf_config.h> when WIN32 defined
authored
26 #ifdef HAVE_CONFIG_H
d1b8326 Dave Beckett RDF logging
authored
27 #include <rdf_config.h>
c8e95fa Dave Beckett Added #include <win32_rdf_config.h> when WIN32 defined
authored
28 #endif
29
30 #ifdef WIN32
31 #include <win32_rdf_config.h>
32 #endif
d1b8326 Dave Beckett RDF logging
authored
33
34 #include <stdio.h>
607476c Dave Beckett (librdf_log_simple): Pass a nulled va_list to the old error and
authored
35 #include <string.h>
d1b8326 Dave Beckett RDF logging
authored
36 #ifdef WITH_THREADS
37 #include <pthread.h>
38 #endif
39 #ifdef HAVE_STDLIB_H
cb1cb16 Dave Beckett code style
authored
40 #include <stdlib.h>
d1b8326 Dave Beckett RDF logging
authored
41 #endif
42
43 /* for gettimeofday */
44 #if TIME_WITH_SYS_TIME
45 #include <sys/time.h>
46 #include <time.h>
47 #else
48 #if HAVE_SYS_TIME_H
49 #include <sys/time.h>
50 #else
51 #include <time.h>
52 #endif
53 #endif
54
24dbd08 Dave Beckett Add accessors for components of the librdf_log_message type. (Edd Dumbil...
authored
55 #include <redland.h>
d1b8326 Dave Beckett RDF logging
authored
56
57
7a93922 Lauri Aalto Remove writable static data. Make log_level_names array const.
laalto authored
58 static const char * const log_level_names[LIBRDF_LOG_LAST+1]={
d1b8326 Dave Beckett RDF logging
authored
59 "none", "debug", "info", "warning", "error", "fatal"
60 };
61
62
86a539f Dave Beckett Export several useful internal documented functions marked INTERNAL.
authored
63 /**
8a78c77 Dave Beckett automated docucomments updates for gtk-doc
authored
64 * librdf_log_simple:
d1b8326 Dave Beckett RDF logging
authored
65 * @world: redland world object or NULL
66 * @code: error code
794716d Dave Beckett Updates for gtk-doc. Pull out internal headers
authored
67 * @level: #librdf_log_level log level
68 * @facility: #librdf_log_facility log facility
d1b8326 Dave Beckett RDF logging
authored
69 * @locator: raptor_locator if available or NULL
70 * @message: message to record
71 *
2ce609a Lauri Aalto (librdf_log,librdf_log_simple): Pulled librdf_log(), librdf_log_simple()...
laalto authored
72 * Log a message.
8a78c77 Dave Beckett automated docucomments updates for gtk-doc
authored
73 *
d1b8326 Dave Beckett RDF logging
authored
74 * If world is NULL, the error ocurred in redland startup before
75 * the world was created.
76 **/
77 void
78 librdf_log_simple(librdf_world* world, int code,
79 librdf_log_level level, librdf_log_facility facility,
80 void *locator, const char *message)
81 {
82 if(level > LIBRDF_LOG_LAST)
83 level=LIBRDF_LOG_NONE;
84
85 if(facility > LIBRDF_FROM_LAST)
86 facility=LIBRDF_FROM_NONE;
87
88 if(world) {
89 if(world->log_handler) {
90 world->log.code=code;
91 world->log.level=level;
92 world->log.facility=facility;
93 world->log.message=message;
aa8a352 Dave Beckett Casts for C++
authored
94 world->log.locator=(raptor_locator*)locator;
d1b8326 Dave Beckett RDF logging
authored
95
8310a82 Dave Beckett (librdf_log_simple): Use correct user_data. Patch from Ren� Puls.
authored
96 if(world->log_handler(world->log_user_data, &world->log))
d1b8326 Dave Beckett RDF logging
authored
97 return;
98
99 } else {
607476c Dave Beckett (librdf_log_simple): Pass a nulled va_list to the old error and
authored
100 va_list null_valist;
101 memset(&null_valist, '\0', sizeof(va_list));
102
d1b8326 Dave Beckett RDF logging
authored
103 switch(level) {
104 case LIBRDF_LOG_ERROR:
f9c09db Dave Beckett (librdf_log_simple): Return if error, warning messages are handled
authored
105 if(world->error_handler) {
607476c Dave Beckett (librdf_log_simple): Pass a nulled va_list to the old error and
authored
106 if(world->error_handler(world->error_user_data, message, null_valist))
f9c09db Dave Beckett (librdf_log_simple): Return if error, warning messages are handled
authored
107 return;
108 }
d1b8326 Dave Beckett RDF logging
authored
109 break;
110
111 case LIBRDF_LOG_WARN:
f9c09db Dave Beckett (librdf_log_simple): Return if error, warning messages are handled
authored
112 if(world->warning_handler) {
607476c Dave Beckett (librdf_log_simple): Pass a nulled va_list to the old error and
authored
113 if(world->warning_handler(world->warning_user_data, message, null_valist))
f9c09db Dave Beckett (librdf_log_simple): Return if error, warning messages are handled
authored
114 return;
115 }
d1b8326 Dave Beckett RDF logging
authored
116 break;
117
36a5018 Dave Beckett Add missing switch cases to make -Wswitch-enum happy.
authored
118 case LIBRDF_LOG_NONE:
119 case LIBRDF_LOG_DEBUG:
120 case LIBRDF_LOG_INFO:
121 case LIBRDF_LOG_FATAL:
122
d1b8326 Dave Beckett RDF logging
authored
123 default:
124 break;
125 }
126 }
127 }
128
129 fputs("librdf ", stderr);
130 fputs(log_level_names[level], stderr);
131 if(locator) {
7776326 Lauri Aalto Refactored librdf to use Raptor v2 APIs if available. Flagged with RAPTO...
laalto authored
132 int locator_len;
bb20328 Lauri Aalto Work-in-progress to get librdf to build on raptor2 git HEAD.
laalto authored
133 locator_len = raptor_locator_format(NULL, 0, (raptor_locator*)locator);
8e9bd01 Dave Beckett Good casts to size_t from int for log lengths [gcc warnings]
authored
134 if(locator_len > 0) {
135 size_t slocator_len = LIBRDF_GOOD_CAST(size_t, locator_len);
136 char *buffer = LIBRDF_MALLOC(char*, slocator_len + 2);
0bf9271 Lauri Aalto (librdf_log_simple): Do not format and print locator string if raptor_fo...
laalto authored
137 *buffer=' ';
8e9bd01 Dave Beckett Good casts to size_t from int for log lengths [gcc warnings]
authored
138 raptor_locator_format(buffer + 1, slocator_len, (raptor_locator*)locator);
0bf9271 Lauri Aalto (librdf_log_simple): Do not format and print locator string if raptor_fo...
laalto authored
139 fputs(buffer, stderr);
3d1d169 Dave Beckett Code style change and cleanup for alloc/free macros
authored
140 LIBRDF_FREE(char*, buffer);
0bf9271 Lauri Aalto (librdf_log_simple): Do not format and print locator string if raptor_fo...
laalto authored
141 }
d1b8326 Dave Beckett RDF logging
authored
142 }
143
144 fputs(" - ", stderr);
caea861 Dave Beckett (librdf_log_simple): Handle attempting to fputs a NULL message.
authored
145 fputs((message ? message : "(no message)"), stderr);
d1b8326 Dave Beckett RDF logging
authored
146 fputc('\n', stderr);
147 }
148
149
86a539f Dave Beckett Export several useful internal documented functions marked INTERNAL.
authored
150 /**
8a78c77 Dave Beckett automated docucomments updates for gtk-doc
authored
151 * librdf_log:
d1b8326 Dave Beckett RDF logging
authored
152 * @world: redland world object or NULL
153 * @code: error code
794716d Dave Beckett Updates for gtk-doc. Pull out internal headers
authored
154 * @level: #librdf_log_level log level
155 * @facility: #librdf_log_facility log facility
d1b8326 Dave Beckett RDF logging
authored
156 * @locator: raptor_locator if available or NULL
157 * @message: message to record
e8296db Dave Beckett autodocs and add section general to the manual
authored
158 * @...: message parameters
d1b8326 Dave Beckett RDF logging
authored
159 *
2ce609a Lauri Aalto (librdf_log,librdf_log_simple): Pulled librdf_log(), librdf_log_simple()...
laalto authored
160 * Log a message.
8a78c77 Dave Beckett automated docucomments updates for gtk-doc
authored
161 *
d1b8326 Dave Beckett RDF logging
authored
162 * If world is NULL, the error ocurred in redland startup before
163 * the world was created.
164 **/
165 void
166 librdf_log(librdf_world* world, int code,
167 librdf_log_level level, librdf_log_facility facility,
168 void *locator, const char *message, ...)
169 {
170 va_list arguments;
171 char *buffer;
172
173 va_start(arguments, message);
174
98fae87 Dave Beckett (librdf_log): Use raptor_vasprintf for raptor >= 2.0.5
authored
175 #if RAPTOR_VERSION >= 20005
176 buffer = NULL;
177 if(raptor_vasprintf(&buffer, message, arguments) < 0)
178 buffer = NULL;
179 #else
180 buffer = raptor_vsnprintf(message, arguments);
181 #endif
d1b8326 Dave Beckett RDF logging
authored
182 librdf_log_simple(world, code, level, facility, locator, buffer);
69f3c15 Lauri Aalto (librdf_log): Resiliency fix: avoid raptor_free_memory(0)
laalto authored
183 if(buffer)
184 raptor_free_memory(buffer);
d1b8326 Dave Beckett RDF logging
authored
185
186 va_end(arguments);
187 }
188
86a539f Dave Beckett Export several useful internal documented functions marked INTERNAL.
authored
189
190 /**
8a78c77 Dave Beckett automated docucomments updates for gtk-doc
authored
191 * librdf_fatal:
d1b8326 Dave Beckett RDF logging
authored
192 * @world: redland world object or NULL
193 * @message: message arguments
194 *
8a78c77 Dave Beckett automated docucomments updates for gtk-doc
authored
195 * INTERNAL - Fatal error.
196 *
d1b8326 Dave Beckett RDF logging
authored
197 * If world is NULL, the error ocurred in redland startup before
198 * the world was created.
199 **/
381d19c Dave Beckett Mark librdf_fatal as REDLAND_NORETURN [gcc warnings]
authored
200 REDLAND_NORETURN
d1b8326 Dave Beckett RDF logging
authored
201 void
202 librdf_fatal(librdf_world* world, int facility,
203 const char *file, int line, const char *function,
204 const char *message)
205 {
206 char empty_buffer[1];
207 char *buffer;
8e9bd01 Dave Beckett Good casts to size_t from int for log lengths [gcc warnings]
authored
208 int length;
209 size_t slength;
d1b8326 Dave Beckett RDF logging
authored
210
211 /* Not passing NULL to snprintf since that seems to not be portable */
8e9bd01 Dave Beckett Good casts to size_t from int for log lengths [gcc warnings]
authored
212 length = snprintf(empty_buffer, 1, "%s:%d:%s: fatal error: %s",
213 file, line, function, message);
214 slength = LIBRDF_GOOD_CAST(size_t, length);
061ee55 Dave Beckett (librdf_fatal): Off by 1 in snprintf on OSX
authored
215
8e9bd01 Dave Beckett Good casts to size_t from int for log lengths [gcc warnings]
authored
216 slength++; /* add the length 1 passed in */
217 buffer = LIBRDF_MALLOC(char*, slength + 1); /* for \0 */
6acdb3a Dave Beckett (librdf_fatal): Code tidy to call abort() just once and always print the...
authored
218 if(buffer)
8e9bd01 Dave Beckett Good casts to size_t from int for log lengths [gcc warnings]
authored
219 snprintf(buffer, slength, "%s:%d:%s: fatal error: %s",
6acdb3a Dave Beckett (librdf_fatal): Code tidy to call abort() just once and always print the...
authored
220 file, line, function, message);
221
222 librdf_log_simple(world, /* code */ 0, LIBRDF_LOG_FATAL,
223 (librdf_log_facility)facility, NULL,
224 (buffer ? (const char*)buffer : message));
225
226 if(buffer)
3d1d169 Dave Beckett Code style change and cleanup for alloc/free macros
authored
227 LIBRDF_FREE(char*, buffer);
6acdb3a Dave Beckett (librdf_fatal): Code tidy to call abort() just once and always print the...
authored
228
d1b8326 Dave Beckett RDF logging
authored
229 abort();
230 }
231
232
24dbd08 Dave Beckett Add accessors for components of the librdf_log_message type. (Edd Dumbil...
authored
233 /**
8a78c77 Dave Beckett automated docucomments updates for gtk-doc
authored
234 * librdf_log_message_code:
24dbd08 Dave Beckett Add accessors for components of the librdf_log_message type. (Edd Dumbil...
authored
235 * @message: log message
236 *
8a78c77 Dave Beckett automated docucomments updates for gtk-doc
authored
237 * Retrieve error code from log message.
238 *
24dbd08 Dave Beckett Add accessors for components of the librdf_log_message type. (Edd Dumbil...
authored
239 * Return value: int error code
240 **/
241 int
242 librdf_log_message_code(librdf_log_message *message)
243 {
244 return message->code;
245 }
246
247
248 /**
8a78c77 Dave Beckett automated docucomments updates for gtk-doc
authored
249 * librdf_log_message_level:
24dbd08 Dave Beckett Add accessors for components of the librdf_log_message type. (Edd Dumbil...
authored
250 * @message: log message
251 *
8a78c77 Dave Beckett automated docucomments updates for gtk-doc
authored
252 * Retrieve severity of log message.
253 *
24dbd08 Dave Beckett Add accessors for components of the librdf_log_message type. (Edd Dumbil...
authored
254 * The log message severity level is defined in rdf_log.h as values
6cad134 Dave Beckett autodocs
authored
255 * of enum #librdf_log_level
24dbd08 Dave Beckett Add accessors for components of the librdf_log_message type. (Edd Dumbil...
authored
256 *
257 * Return value: severity level
258 **/
259 librdf_log_level
260 librdf_log_message_level(librdf_log_message *message)
261 {
262 return message->level;
263 }
264
265
266 /**
8a78c77 Dave Beckett automated docucomments updates for gtk-doc
authored
267 * librdf_log_message_facility:
24dbd08 Dave Beckett Add accessors for components of the librdf_log_message type. (Edd Dumbil...
authored
268 * @message: log message
269 *
8a78c77 Dave Beckett automated docucomments updates for gtk-doc
authored
270 * Retrieve facility that generated the message.
271 *
24dbd08 Dave Beckett Add accessors for components of the librdf_log_message type. (Edd Dumbil...
authored
272 * The log message facility is defined in rdf_log.h as values
6cad134 Dave Beckett autodocs
authored
273 * of enum #librdf_log_facility
24dbd08 Dave Beckett Add accessors for components of the librdf_log_message type. (Edd Dumbil...
authored
274 *
275 * Return value: ID of Redland facility that generated the log message.
276 **/
277 librdf_log_facility
278 librdf_log_message_facility(librdf_log_message *message)
279 {
280 return message->facility;
281 }
282
283
284 /**
8a78c77 Dave Beckett automated docucomments updates for gtk-doc
authored
285 * librdf_log_message_message:
24dbd08 Dave Beckett Add accessors for components of the librdf_log_message type. (Edd Dumbil...
authored
286 * @message: log message
287 *
8a78c77 Dave Beckett automated docucomments updates for gtk-doc
authored
288 * Retrieve text message from log entry.
289 *
24dbd08 Dave Beckett Add accessors for components of the librdf_log_message type. (Edd Dumbil...
authored
290 * The string returned is shared and must be copied by the caller
291 * if required to be retained.
292 *
293 * Return value: shared pointer to the log message string
294 **/
295 const char *
296 librdf_log_message_message(librdf_log_message *message)
297 {
298 return message->message;
299 }
300
301
302 /**
8a78c77 Dave Beckett automated docucomments updates for gtk-doc
authored
303 * librdf_log_message_locator:
24dbd08 Dave Beckett Add accessors for components of the librdf_log_message type. (Edd Dumbil...
authored
304 * @message: log message
305 *
8a78c77 Dave Beckett automated docucomments updates for gtk-doc
authored
306 * Retrieve locator of log entry.
307 *
24dbd08 Dave Beckett Add accessors for components of the librdf_log_message type. (Edd Dumbil...
authored
308 * Return value: pointer to an appropriate raptor_locator* or NULL if not available
309 **/
310 raptor_locator*
311 librdf_log_message_locator(librdf_log_message *message)
312 {
313 return message->locator;
314 }
315
316
ceb69b4 Dave Beckett (librdf_test_error,librdf_test_warning): Added for testing errors.
authored
317 /* prototypes for testing errors only - NOT PART OF API */
318 void
319 librdf_test_error(librdf_world* world, const char *message)
320 {
321 librdf_log_simple(world, 0, LIBRDF_LOG_ERROR, LIBRDF_FROM_NONE, NULL, message);
322 }
323
324 void
325 librdf_test_warning(librdf_world* world, const char *message)
326 {
327 librdf_log_simple(world, 0, LIBRDF_LOG_WARN, LIBRDF_FROM_NONE, NULL, message);
328 }
329
Something went wrong with that request. Please try again.