1
0
mirror of https://github.com/samba-team/samba.git synced 2025-03-22 02:50:28 +03:00

libctdb: add logging infrastructure

This is based on Ronnie's work, merged with mine.  That means
errors are all my fault.

Differences from Ronnie's:
1) use syslog's LOG_ levels directly.
2) typesafe arg to log function, and use it (eg stderr) in helper function.
3) store fn in ctdb context, and expose ctdb_log_level directly thru API.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>

(This used to be ctdb commit 86259aa395555aaf7b2fae7326caa2ea62961092)
This commit is contained in:
Rusty Russell 2010-06-04 20:27:03 +09:30
parent cc8435852c
commit 379fd4e606
6 changed files with 105 additions and 4 deletions

View File

@ -47,7 +47,8 @@ CTDB_COMMON_OBJ = common/ctdb_io.o common/ctdb_util.o \
common/ctdb_logging.c
CTDB_LIB_OBJ = libctdb/ctdb.o libctdb/io_elem.o libctdb/local_tdb.o \
libctdb/messages.o libctdb/sync.o libctdb/control.o
libctdb/messages.o libctdb/sync.o libctdb/control.o \
libctdb/logging.o
CTDB_TCP_OBJ = tcp/tcp_connect.o tcp/tcp_io.o tcp/tcp_init.o

View File

@ -23,8 +23,15 @@
#include <sys/types.h>
#include <stdint.h>
#include <stdbool.h>
#include <stdarg.h>
#include <stdio.h>
#include <tdb.h>
/* The type of the first arg should match the arg given to ctdb_connect() */
typedef void (*ctdb_log_fn_t)(void *log_priv,
int severity, const char *format, va_list ap);
/* All *_send() functions are guaranteed to be non-blocking and fully
* asynchronous. The non-_send variants are synchronous.
*/
@ -34,7 +41,8 @@
* Returns a ctdb context if successful or NULL.
*
*/
struct ctdb_connection *ctdb_connect(const char *addr);
struct ctdb_connection *ctdb_connect(const char *addr,
ctdb_log_fn_t log_fn, void *log_priv);
int ctdb_get_fd(struct ctdb_connection *ctdb);
@ -212,12 +220,26 @@ int ctdb_getrecmaster(struct ctdb_connection *ctdb,
int ctdb_cancel(struct ctdb_connection *ctdb, struct ctdb_request *req);
/*
* functions for logging errors
*/
extern int ctdb_log_level; /* LOG_WARNING and above by default. */
void ctdb_log_file(FILE *, int severity, const char *format, va_list ap);
/* These ugly macro wrappers make the callbacks typesafe. */
#include <ctdb_typesafe_cb.h>
#define ctdb_sendcb(cb, cbdata) \
typesafe_cb_preargs(void, (cb), (cbdata), \
struct ctdb_connection *, struct ctdb_request *)
#define ctdb_connect(addr, log, logpriv) \
ctdb_connect((addr), \
typesafe_cb_postargs(void, (log), (logpriv), \
int, const char *, va_list), \
(logpriv))
#define ctdb_attachdb_send(ctdb, name, persistent, tdb_flags, cb, cbdata) \
ctdb_attachdb_send((ctdb), (name), (persistent), (tdb_flags), \
ctdb_sendcb((cb), (cbdata)), (cbdata))

View File

@ -34,6 +34,7 @@
/* Remove type-safety macros. */
#undef ctdb_attachdb_send
#undef ctdb_readrecordlock_async
#undef ctdb_connect
/* FIXME: Could be in shared util code with rest of ctdb */
static void close_noerr(int fd)
@ -78,7 +79,8 @@ static void set_pnn(struct ctdb_connection *ctdb,
ctdb_request_free(ctdb, req);
}
struct ctdb_connection *ctdb_connect(const char *addr)
struct ctdb_connection *ctdb_connect(const char *addr,
ctdb_log_fn_t log_fn, void *log_priv)
{
struct ctdb_connection *ctdb;
struct sockaddr_un sun;
@ -92,6 +94,8 @@ struct ctdb_connection *ctdb_connect(const char *addr)
ctdb->message_handlers = NULL;
ctdb->next_id = 0;
ctdb->broken = false;
ctdb->log = log_fn;
ctdb->log_priv = log_priv;
memset(&sun, 0, sizeof(sun));
sun.sun_family = AF_UNIX;

View File

@ -6,11 +6,25 @@
#include <stdlib.h>
#include <ctdb.h>
#include <ctdb_protocol.h>
#include <syslog.h>
#ifndef offsetof
#define offsetof(t,f) ((unsigned int)&((t *)0)->f)
#endif
#ifndef COLD_ATTRIBUTE
#if (__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 3)
#define COLD_ATTRIBUTE __attribute__((cold))
#else
#define COLD_ATTRIBUTE
#endif
#endif /* COLD_ATTRIBUTE */
#define DEBUG(ctdb, lvl, format, args...) do { if (lvl <= ctdb_log_level) { ctdb_do_debug(ctdb, lvl, format , ## args ); }} while(0)
void ctdb_do_debug(struct ctdb_connection *, int, const char *format, ...)
PRINTF_ATTRIBUTE(3, 4) COLD_ATTRIBUTE;
struct message_handler_info;
struct ctdb_reply_call;
@ -55,6 +69,9 @@ struct ctdb_connection {
struct message_handler_info *message_handlers;
/* PNN of this ctdb: valid by the time we do our first db connection. */
uint32_t pnn;
/* Extra logging. */
ctdb_log_fn_t log;
void *log_priv;
};
/* ctdb.c */

56
ctdb/libctdb/logging.c Normal file
View File

@ -0,0 +1,56 @@
/*
logging wrapper for libctdb
Copyright (C) Ronnie Sahlberg 2010
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, see <http://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <errno.h>
#include <ctdb.h>
#include <string.h>
#include "libctdb_private.h"
int ctdb_log_level = LOG_WARNING;
void ctdb_do_debug(struct ctdb_connection *ctdb,
int severity, const char *format, ...)
{
va_list ap;
va_start(ap, format);
ctdb->log(ctdb->log_priv, severity, format, ap);
va_end(ap);
}
/* Convenient log helper. */
void ctdb_log_file(FILE *outf, int priority, const char *format, va_list ap)
{
fprintf(outf, "%s:",
priority == LOG_EMERG ? "EMERG" :
priority == LOG_ALERT ? "ALERT" :
priority == LOG_CRIT ? "CRIT" :
priority == LOG_ERR ? "ERR" :
priority == LOG_WARNING ? "WARNING" :
priority == LOG_NOTICE ? "NOTICE" :
priority == LOG_INFO ? "INFO" :
priority == LOG_DEBUG ? "DEBUG" :
"Unknown Error Level");
vfprintf(outf, format, ap);
if (priority == LOG_ERR) {
fprintf(outf, " (%s)", strerror(errno));
}
fprintf(outf, "\n");
}

View File

@ -137,7 +137,8 @@ int main(int argc, char *argv[])
bool rrl_cb_called = false;
ctdb_connection = ctdb_connect("/tmp/ctdb.socket");
ctdb_connection = ctdb_connect("/tmp/ctdb.socket",
ctdb_log_file, stderr);
if (!ctdb_connection)
err(1, "Connecting to /tmp/ctdb.socket");