1/* 2 ldb database library 3 4 Copyright (C) Andrew Tridgell 2004 5 6 ** NOTE! The following LGPL license applies to the ldb 7 ** library. This does NOT imply that all of Samba is released 8 ** under the LGPL 9 10 This library is free software; you can redistribute it and/or 11 modify it under the terms of the GNU Lesser General Public 12 License as published by the Free Software Foundation; either 13 version 3 of the License, or (at your option) any later version. 14 15 This library is distributed in the hope that it will be useful, 16 but WITHOUT ANY WARRANTY; without even the implied warranty of 17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 18 Lesser General Public License for more details. 19 20 You should have received a copy of the GNU Lesser General Public 21 License along with this library; if not, see <http://www.gnu.org/licenses/>. 22*/ 23 24/* 25 * Name: ldb 26 * 27 * Component: ldb debug 28 * 29 * Description: functions for printing debug messages 30 * 31 * Author: Andrew Tridgell 32 */ 33 34#include "ldb_private.h" 35 36/* 37 this allows the user to choose their own debug function 38*/ 39int ldb_set_debug(struct ldb_context *ldb, 40 void (*debug)(void *context, enum ldb_debug_level level, 41 const char *fmt, va_list ap), 42 void *context) 43{ 44 ldb->debug_ops.debug = debug; 45 ldb->debug_ops.context = context; 46 return 0; 47} 48 49/* 50 debug function for ldb_set_debug_stderr 51*/ 52static void ldb_debug_stderr(void *context, enum ldb_debug_level level, 53 const char *fmt, va_list ap) PRINTF_ATTRIBUTE(3,0); 54static void ldb_debug_stderr(void *context, enum ldb_debug_level level, 55 const char *fmt, va_list ap) 56{ 57 if (level <= LDB_DEBUG_WARNING) { 58 vfprintf(stderr, fmt, ap); 59 fprintf(stderr, "\n"); 60 } 61} 62 63static void ldb_debug_stderr_all(void *context, enum ldb_debug_level level, 64 const char *fmt, va_list ap) PRINTF_ATTRIBUTE(3,0); 65static void ldb_debug_stderr_all(void *context, enum ldb_debug_level level, 66 const char *fmt, va_list ap) 67{ 68 vfprintf(stderr, fmt, ap); 69 fprintf(stderr, "\n"); 70} 71 72/* 73 convenience function to setup debug messages on stderr 74 messages of level LDB_DEBUG_WARNING and higher are printed 75*/ 76int ldb_set_debug_stderr(struct ldb_context *ldb) 77{ 78 return ldb_set_debug(ldb, ldb_debug_stderr, ldb); 79} 80 81/* 82 log a message 83*/ 84void ldb_debug(struct ldb_context *ldb, enum ldb_debug_level level, const char *fmt, ...) 85{ 86 va_list ap; 87 if (ldb->debug_ops.debug == NULL) { 88 if (ldb->flags & LDB_FLG_ENABLE_TRACING) { 89 ldb_set_debug(ldb, ldb_debug_stderr_all, ldb); 90 } else { 91 ldb_set_debug_stderr(ldb); 92 } 93 } 94 va_start(ap, fmt); 95 ldb->debug_ops.debug(ldb->debug_ops.context, level, fmt, ap); 96 va_end(ap); 97} 98 99/* 100 add to an accumulated log message 101 */ 102void ldb_debug_add(struct ldb_context *ldb, const char *fmt, ...) 103{ 104 va_list ap; 105 va_start(ap, fmt); 106 if (ldb->partial_debug == NULL) { 107 ldb->partial_debug = talloc_vasprintf(ldb, fmt, ap); 108 } else { 109 ldb->partial_debug = talloc_vasprintf_append(ldb->partial_debug, 110 fmt, ap); 111 } 112 va_end(ap); 113} 114 115/* 116 send the accumulated log message, and free it 117 */ 118void ldb_debug_end(struct ldb_context *ldb, enum ldb_debug_level level) 119{ 120 ldb_debug(ldb, level, "%s", ldb->partial_debug); 121 talloc_free(ldb->partial_debug); 122 ldb->partial_debug = NULL; 123} 124 125/* 126 log a message, and set the ldb error string to the same message 127*/ 128void ldb_debug_set(struct ldb_context *ldb, enum ldb_debug_level level, 129 const char *fmt, ...) 130{ 131 va_list ap; 132 char *msg; 133 va_start(ap, fmt); 134 msg = talloc_vasprintf(ldb, fmt, ap); 135 va_end(ap); 136 if (msg != NULL) { 137 ldb_set_errstring(ldb, msg); 138 ldb_debug(ldb, level, "%s", msg); 139 } 140 talloc_free(msg); 141} 142 143