add a machinereadable version of ctdb stats/statistics
[metze/ctdb/wip.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 <tdb.h>
24 #include "libctdb_private.h"
25
26 int ctdb_log_level = LOG_WARNING;
27
28 void ctdb_do_debug(struct ctdb_connection *ctdb,
29                    int severity, const char *format, ...)
30 {
31         va_list ap;
32
33         va_start(ap, format);
34         ctdb->log(ctdb->log_priv, severity, format, ap);
35         va_end(ap);
36 }
37
38 /* Attach tdb logging to our ctdb logging. */
39 void ctdb_tdb_log_bridge(struct tdb_context *tdb,
40                          enum tdb_debug_level level,
41                          const char *format, ...)
42 {
43         va_list ap;
44         int sev;
45         struct ctdb_connection *ctdb = tdb_get_logging_private(tdb);
46         char *newformat;
47
48         switch (level) {
49         case TDB_DEBUG_FATAL:
50                 sev = LOG_CRIT;
51                 break;
52         case TDB_DEBUG_ERROR:
53                 sev = LOG_ERR;
54                 break;
55         case TDB_DEBUG_WARNING:
56                 sev = LOG_WARNING;
57                 break;
58         case TDB_DEBUG_TRACE:
59                 sev = LOG_DEBUG;
60                 break;
61         default:
62                 sev = LOG_CRIT;
63         }
64
65         if (sev > ctdb_log_level) {
66                 return;
67         }
68
69         newformat = malloc(sizeof("TDB error: ") + strlen(format));
70         if (!newformat) {
71                 DEBUG(ctdb, LOG_ERR,
72                       "memory allocation failure reporting tdb error %s",
73                       format);
74                 return;
75         }
76
77         /* Prepend TDB error: and remove \n */
78         strcpy(newformat, "TDB error: ");
79         strcat(newformat, format);
80         if (newformat[strlen(newformat)-1] == '\n')
81                 newformat[strlen(newformat)-1] = '\0';
82
83         va_start(ap, format);
84         ctdb->log(ctdb->log_priv, sev, newformat, ap);
85         va_end(ap);
86         free(newformat);
87 }
88
89 /* Convenient log helper. */
90 void ctdb_log_file(FILE *outf, int priority, const char *format, va_list ap)
91 {
92         fprintf(outf, "%s:",
93                 priority == LOG_EMERG ? "EMERG" :
94                 priority == LOG_ALERT ? "ALERT" :
95                 priority == LOG_CRIT ? "CRIT" :
96                 priority == LOG_ERR ? "ERR" :
97                 priority == LOG_WARNING ? "WARNING" :
98                 priority == LOG_NOTICE ? "NOTICE" :
99                 priority == LOG_INFO ? "INFO" :
100                 priority == LOG_DEBUG ? "DEBUG" :
101                 "Unknown Error Level");
102
103         vfprintf(outf, format, ap);
104         if (priority == LOG_ERR) {
105                 fprintf(outf, " (%s)", strerror(errno));
106         }
107         fprintf(outf, "\n");
108 }