summaryrefslogtreecommitdiff
path: root/lib/talloc
diff options
context:
space:
mode:
authorStefan Metzmacher <metze@samba.org>2009-07-29 21:54:28 +0200
committerAndrew Tridgell <tridge@samba.org>2009-08-24 16:29:58 +1000
commite40f3144f2a3f8e7aebf009a4cddb9f463292c1c (patch)
tree589fe268bf2c5c1e569fab66bed3e5491ae3cec2 /lib/talloc
parentac8aeec824b49c69a97e107f170337fcaa75120f (diff)
downloadsamba-e40f3144f2a3f8e7aebf009a4cddb9f463292c1c.tar.gz
samba-e40f3144f2a3f8e7aebf009a4cddb9f463292c1c.tar.bz2
samba-e40f3144f2a3f8e7aebf009a4cddb9f463292c1c.zip
talloc: add talloc_set_log_fn() and talloc_set_log_stderr()
So that the application can setup a log function to get ERROR and WARNING messages. metze
Diffstat (limited to 'lib/talloc')
-rw-r--r--lib/talloc/talloc.c61
-rw-r--r--lib/talloc/talloc.h2
2 files changed, 51 insertions, 12 deletions
diff --git a/lib/talloc/talloc.c b/lib/talloc/talloc.c
index 64037b52cf..58776a9160 100644
--- a/lib/talloc/talloc.c
+++ b/lib/talloc/talloc.c
@@ -141,6 +141,41 @@ struct talloc_chunk {
#define TC_HDR_SIZE ((sizeof(struct talloc_chunk)+15)&~15)
#define TC_PTR_FROM_CHUNK(tc) ((void *)(TC_HDR_SIZE + (char*)tc))
+static void (*talloc_log_fn)(const char *message);
+
+void talloc_set_log_fn(void (*log_fn)(const char *message))
+{
+ talloc_log_fn = log_fn;
+}
+
+static void talloc_log(const char *fmt, ...) PRINTF_ATTRIBUTE(1,2);
+static void talloc_log(const char *fmt, ...)
+{
+ va_list ap;
+ char *message;
+
+ if (!talloc_log_fn) {
+ return;
+ }
+
+ va_start(ap, fmt);
+ message = talloc_vasprintf(NULL, fmt, ap);
+ va_end(ap);
+
+ talloc_log_fn(message);
+ talloc_free(message);
+}
+
+static void talloc_log_stderr(const char *message)
+{
+ fprintf(stderr, "%s", message);
+}
+
+void talloc_set_log_stderr(void)
+{
+ talloc_set_log_fn(talloc_log_stderr);
+}
+
static void (*talloc_abort_fn)(const char *reason);
void talloc_set_abort_fn(void (*abort_fn)(const char *reason))
@@ -150,6 +185,8 @@ void talloc_set_abort_fn(void (*abort_fn)(const char *reason))
static void talloc_abort(const char *reason)
{
+ talloc_log("%s\n", reason);
+
if (!talloc_abort_fn) {
TALLOC_ABORT(reason);
}
@@ -687,13 +724,13 @@ void *_talloc_steal_loc(const void *new_ctx, const void *ptr, const char *locati
if (unlikely(tc->refs != NULL) && talloc_parent(ptr) != new_ctx) {
struct talloc_reference_handle *h;
-#if DEVELOPER
- fprintf(stderr, "WARNING: talloc_steal with references at %s\n", location);
-#endif
+
+ talloc_log("WARNING: talloc_steal with references at %s\n",
+ location);
+
for (h=tc->refs; h; h=h->next) {
-#if DEVELOPER
- fprintf(stderr, "\treference at %s\n", h->location);
-#endif
+ talloc_log("\treference at %s\n",
+ h->location);
}
}
@@ -1061,13 +1098,13 @@ int _talloc_free(void *ptr, const char *location)
if (unlikely(tc->refs != NULL)) {
struct talloc_reference_handle *h;
-#if DEVELOPER
- fprintf(stderr, "ERROR: talloc_free with references at %s\n", location);
-#endif
+
+ talloc_log("ERROR: talloc_free with references at %s\n",
+ location);
+
for (h=tc->refs; h; h=h->next) {
-#if DEVELOPER
- fprintf(stderr, "\treference at %s\n", h->location);
-#endif
+ talloc_log("\treference at %s\n",
+ h->location);
}
return -1;
}
diff --git a/lib/talloc/talloc.h b/lib/talloc/talloc.h
index 855f778f8c..234c0b442c 100644
--- a/lib/talloc/talloc.h
+++ b/lib/talloc/talloc.h
@@ -189,6 +189,8 @@ char *talloc_asprintf_append(char *s, const char *fmt, ...) PRINTF_ATTRIBUTE(2,3
char *talloc_asprintf_append_buffer(char *s, const char *fmt, ...) PRINTF_ATTRIBUTE(2,3);
void talloc_set_abort_fn(void (*abort_fn)(const char *reason));
+void talloc_set_log_fn(void (*log_fn)(const char *message));
+void talloc_set_log_stderr(void);
/* for ABI compatibility, never use in new code */
void *_talloc_reference(const void *context, const void *ptr);