libctdb: add logging infrastructure
[sahlberg/ctdb.git] / libctdb / logging.c
1 /*
2    logging wrapper for libctdb
3
4    Copyright (C) Ronnie Sahlberg 2010
5
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 3 of the License, or
9    (at your option) any later version.
10
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15
16    You should have received a copy of the GNU General Public License
17    along with this program; if not, see <http://www.gnu.org/licenses/>.
18 */
19 #include <stdio.h>
20 #include <errno.h>
21 #include <ctdb.h>
22 #include <string.h>
23 #include "libctdb_private.h"
24
25 int ctdb_log_level = LOG_WARNING;
26
27 void ctdb_do_debug(struct ctdb_connection *ctdb,
28                    int severity, const char *format, ...)
29 {
30         va_list ap;
31
32         va_start(ap, format);
33         ctdb->log(ctdb->log_priv, severity, format, ap);
34         va_end(ap);
35 }
36
37 /* Convenient log helper. */
38 void ctdb_log_file(FILE *outf, int priority, const char *format, va_list ap)
39 {
40         fprintf(outf, "%s:",
41                 priority == LOG_EMERG ? "EMERG" :
42                 priority == LOG_ALERT ? "ALERT" :
43                 priority == LOG_CRIT ? "CRIT" :
44                 priority == LOG_ERR ? "ERR" :
45                 priority == LOG_WARNING ? "WARNING" :
46                 priority == LOG_NOTICE ? "NOTICE" :
47                 priority == LOG_INFO ? "INFO" :
48                 priority == LOG_DEBUG ? "DEBUG" :
49                 "Unknown Error Level");
50
51         vfprintf(outf, format, ap);
52         if (priority == LOG_ERR) {
53                 fprintf(outf, " (%s)", strerror(errno));
54         }
55         fprintf(outf, "\n");
56 }