LCOV - code coverage report
Current view: top level - lib/ldb/common - ldb_debug.c (source / functions) Hit Total Coverage
Test: coverage report for master 98b443d9 Lines: 34 52 65.4 %
Date: 2024-05-31 13:13:24 Functions: 6 9 66.7 %

          Line data    Source code
       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             : */
      39      761102 : int 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      761102 :         ldb->debug_ops.debug = debug;
      45      761102 :         ldb->debug_ops.context = context;
      46      761102 :         return 0;
      47             : }
      48             : 
      49             : /*
      50             :   debug function for ldb_set_debug_stderr
      51             : */
      52             : static void ldb_debug_stderr(void *context, enum ldb_debug_level level, 
      53             :                              const char *fmt, va_list ap) PRINTF_ATTRIBUTE(3,0);
      54   263881290 : static void ldb_debug_stderr(void *context, enum ldb_debug_level level, 
      55             :                              const char *fmt, va_list ap)
      56             : {
      57   263881290 :         if (level <= LDB_DEBUG_WARNING) {
      58        3175 :                 vfprintf(stderr, fmt, ap);
      59        3175 :                 fprintf(stderr, "\n");
      60             :         }
      61   263881290 : }
      62             : 
      63             : static void ldb_debug_stderr_all(void *context, enum ldb_debug_level level, 
      64             :                              const char *fmt, va_list ap) PRINTF_ATTRIBUTE(3,0);
      65           0 : static void ldb_debug_stderr_all(void *context, enum ldb_debug_level level, 
      66             :                              const char *fmt, va_list ap)
      67             : {
      68           0 :         vfprintf(stderr, fmt, ap);
      69           0 :         fprintf(stderr, "\n");
      70           0 : }
      71             : 
      72             : /*
      73             :   convenience function to setup debug messages on stderr
      74             :   messages of level LDB_DEBUG_WARNING and higher are printed
      75             : */
      76       57575 : int ldb_set_debug_stderr(struct ldb_context *ldb)
      77             : {
      78       57575 :         return ldb_set_debug(ldb, ldb_debug_stderr, ldb);
      79             : }
      80             : 
      81             : /*
      82             :   log a message (va_list helper for ldb_tevent_debug)
      83             : */
      84  1054667638 : void ldb_vdebug(struct ldb_context *ldb, enum ldb_debug_level level, const char *fmt, va_list ap)
      85             : {
      86  1054667638 :         if (ldb->debug_ops.debug == NULL) {
      87       42367 :                 if (ldb->flags & LDB_FLG_ENABLE_TRACING) {
      88           0 :                         ldb_set_debug(ldb, ldb_debug_stderr_all, ldb);
      89             :                 } else {
      90       42367 :                         ldb_set_debug_stderr(ldb);
      91             :                 }
      92             :         }
      93  1054667638 :         ldb->debug_ops.debug(ldb->debug_ops.context, level, fmt, ap);
      94  1054667638 : }
      95             : 
      96             : /*
      97             :   log a message
      98             : */
      99    15617731 : void ldb_debug(struct ldb_context *ldb, enum ldb_debug_level level, const char *fmt, ...)
     100             : {
     101     1230690 :         va_list ap;
     102    15617731 :         va_start(ap, fmt);
     103    15617731 :         ldb_vdebug(ldb, level, fmt, ap);
     104    15617731 :         va_end(ap);
     105    15617731 : }
     106             : 
     107             : /*
     108             :   add to an accumulated log message
     109             :  */
     110           0 : void ldb_debug_add(struct ldb_context *ldb, const char *fmt, ...)
     111             : {
     112           0 :         va_list ap;
     113           0 :         va_start(ap, fmt);
     114           0 :         if (ldb->partial_debug == NULL) {
     115           0 :                 ldb->partial_debug = talloc_vasprintf(ldb, fmt, ap);
     116             :         } else {
     117           0 :                 ldb->partial_debug = talloc_vasprintf_append(ldb->partial_debug, 
     118             :                                                              fmt, ap);
     119             :         }
     120           0 :         va_end(ap);
     121           0 : }
     122             : 
     123             : /*
     124             :   send the accumulated log message, and free it
     125             :  */
     126           0 : void ldb_debug_end(struct ldb_context *ldb, enum ldb_debug_level level)
     127             : {
     128           0 :         ldb_debug(ldb, level, "%s", ldb->partial_debug);
     129           0 :         talloc_free(ldb->partial_debug);
     130           0 :         ldb->partial_debug = NULL;
     131           0 : }
     132             : 
     133             : /*
     134             :   log a message, and set the ldb error string to the same message
     135             : */
     136         909 : void ldb_debug_set(struct ldb_context *ldb, enum ldb_debug_level level, 
     137             :                    const char *fmt, ...)
     138             : {
     139           3 :         va_list ap;
     140           3 :         char *msg;
     141         909 :         va_start(ap, fmt);
     142         909 :         msg = talloc_vasprintf(ldb, fmt, ap);
     143         909 :         va_end(ap);
     144         909 :         if (msg != NULL) {
     145         909 :                 ldb_set_errstring(ldb, msg);
     146         909 :                 ldb_debug(ldb, level, "%s", msg);
     147             :         }
     148         909 :         talloc_free(msg);
     149         909 : }
     150             : 

Generated by: LCOV version 1.14