adding a callback definition for logging
[platform/upstream/libsolv.git] / src / sat_debug.c
1 /*
2  * Copyright (c) 2007, Novell Inc.
3  *
4  * This program is licensed under the BSD license, read LICENSE.BSD
5  * for further information
6  *
7  * debug.c
8  * general logging function
9  *
10  */
11
12
13 #include <sat_debug.h>
14 #include <stdlib.h>
15 #include <stdarg.h>
16 #include <stdio.h>
17
18 #define MAX_OUTPUT_LEN 200
19
20 // debug level which can be set
21 static DebugLevel debug_level = ERROR;
22 // log file,function,line too
23 static int sat_log_lineNr = 0;
24 // Callback for logging
25 SatDebugFn debugCallback = NULL;
26
27 void
28 sat_set_debugCallback (SatDebugFn callback)
29 {
30     debugCallback = callback;
31 }
32
33 void
34 sat_set_debug (DebugLevel level, int log_line_nr)
35 {
36     debug_level = level;
37     sat_log_lineNr = log_line_nr;
38 }
39
40 DebugLevel sat_debug_level ()
41 {
42     return debug_level;
43 }
44
45 void
46 sat_debug (DebugLevel level, const char *format, ...)
47 {
48     va_list args;
49     char str[MAX_OUTPUT_LEN];
50
51     va_start (args, format);
52     vsnprintf (str, MAX_OUTPUT_LEN, format, args);
53     va_end (args);
54
55     if (sat_debug_level() >= level) {
56         if (sat_log_lineNr) {
57             char pre[MAX_OUTPUT_LEN];
58             snprintf (pre, MAX_OUTPUT_LEN, "(%s, %s:%d) ", __FUNCTION__, __FILE__, __LINE__);       
59             if (debugCallback == NULL) 
60                 printf("%s", pre);
61             else 
62                 debugCallback (pre);
63         }
64         if (debugCallback == NULL)     
65             printf ("%s", str);
66         else 
67             debugCallback (str);        
68     }
69 }