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