File: debug.c

package info (click to toggle)
parted 3.2-7
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 15,312 kB
  • ctags: 12,128
  • sloc: ansic: 68,106; sh: 18,156; makefile: 632; perl: 179; python: 45; asm: 36; sed: 16
file content (114 lines) | stat: -rw-r--r-- 3,343 bytes parent folder | download | duplicates (3)
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/*
    libparted - a library for manipulating disk partitions
    Copyright (C) 2000, 2005, 2007, 2009-2014 Free Software Foundation, Inc.

    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 <config.h>
#include <parted/parted.h>
#include <parted/debug.h>

#if ENABLE_NLS
#  include <libintl.h>
#  define _(String) dgettext (PACKAGE, String)
#else
#  define _(String) (String)
#endif /* ENABLE_NLS */

#ifdef DEBUG

#if HAVE_BACKTRACE
#include <execinfo.h>
#endif

static void default_handler ( const int level, const char* file, int line,
                const char* function, const char* msg );
static PedDebugHandler* debug_handler = &default_handler;


/**
 * Default debug handler.
 * Will print all information to stderr.
 */
static void default_handler ( const int level, const char* file, int line,
                const char* function, const char* msg )
{
        fprintf ( stderr, "[%d] %s:%d (%s): %s\n",
                        level, file, line, function, msg );
}

/**
 * Send a debug message.
 * Do not call this directly -- use PED_DEBUG() instead.
 *
 * level        log level, 0 ~= "print definitely"
 */
void ped_debug ( const int level, const char* file, int line,
                 const char* function, const char* msg, ... )
{
        va_list         arg_list;
        char*           msg_concat = ped_malloc(8192);

        va_start ( arg_list, msg );
                vsnprintf ( msg_concat, 8192, msg, arg_list );
        va_end ( arg_list );

        debug_handler ( level, file, line, function, msg_concat );

        free ( msg_concat );
}

/*
 * handler      debug handler; NULL for default handler
 */
void ped_debug_set_handler ( PedDebugHandler* handler )
{
        debug_handler = ( handler ? handler : default_handler );
}

/*
 * Check an assertion.
 * Do not call this directly -- use PED_ASSERT() instead.
 */
void ped_assert (const char* cond_text,
                 const char* file, int line, const char* function)
{
#if HAVE_BACKTRACE
        /* Print backtrace stack */
        void *stack[20];
        char **strings, **string;
        int size = backtrace(stack, 20);
        strings = backtrace_symbols(stack, size);

        if (strings) {
                printf(_("Backtrace has %d calls on stack:\n"), size);

                for (string = strings; size > 0; size--, string++)
                        printf("  %d: %s\n", size, *string);

                free(strings);
        }
#endif

        /* Throw the exception */
        ped_exception_throw (
                PED_EXCEPTION_BUG,
                PED_EXCEPTION_FATAL,
                _("Assertion (%s) at %s:%d in function %s() failed."),
                cond_text, file, line, function);
        abort ();
}

#endif /* DEBUG */