File: debug.c

package info (click to toggle)
libnbd 1.24.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 10,956 kB
  • sloc: ansic: 55,158; ml: 12,325; sh: 8,811; python: 4,757; makefile: 3,038; perl: 165; cpp: 24
file content (99 lines) | stat: -rw-r--r-- 2,476 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/* NBD client library in userspace
 * Copyright Red Hat
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

#include <config.h>

#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <stdbool.h>
#include <errno.h>

#include "internal.h"

int
nbd_unlocked_set_debug (struct nbd_handle *h, bool fl)
{
  h->debug = fl;
  return 0;
}

/* NB: may_set_error = false. */
int
nbd_unlocked_get_debug (struct nbd_handle *h)
{
  return h->debug;
}

int
nbd_unlocked_clear_debug_callback (struct nbd_handle *h)
{
  FREE_CALLBACK (h->debug_callback);
  return 0;
}

int
nbd_unlocked_set_debug_callback (struct nbd_handle *h,
                                 nbd_debug_callback *debug_callback)
{
  /* This can't fail at the moment - see implementation above. */
  nbd_unlocked_clear_debug_callback (h);

  h->debug_callback = *debug_callback;
  SET_CALLBACK_TO_NULL (*debug_callback);
  return 0;
}

/* Note this preserves the value of errno, making it safe to use in
 * all situations.
 */
void
nbd_internal_debug (struct nbd_handle *h, const char *context,
                    const char *fs, ...)
{
  int err, r;
  va_list args;
  char *msg = NULL;

  /* The debug() wrapper checks this, but check it again in case
   * something calls nbd_internal_debug directly.
   */
  if (!h->debug) return;

  err = errno;

  if (context == NULL)
    context = nbd_internal_get_error_context ();

  va_start (args, fs);
  r = vasprintf (&msg, fs, args);
  va_end (args);
  if (r == -1)
    goto out;

  if (CALLBACK_IS_NOT_NULL (h->debug_callback))
    /* ignore return value */
    CALL_CALLBACK (h->debug_callback, context, msg);
  else
    fprintf (stderr, "libnbd: debug: %s: %s: %s\n",
             h->hname, context ? : "unknown", msg);
 out:
  free (msg);
  errno = err;
  return;
}