OSDN Git Service

maint: don't hard-code bug-reporting address
[android-x86/external-parted.git] / libparted / debug.c
1 /*
2     libparted - a library for manipulating disk partitions
3     Copyright (C) 2000, 2005, 2007, 2009-2010 Free Software Foundation,
4     Inc.
5
6     This program is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 3 of the License, or
9     (at your option) any later version.
10
11     This program is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with this program.  If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include <config.h>
21 #include <parted/parted.h>
22 #include <parted/debug.h>
23
24 #if ENABLE_NLS
25 #  include <libintl.h>
26 #  define _(String) dgettext (PACKAGE, String)
27 #else
28 #  define _(String) (String)
29 #endif /* ENABLE_NLS */
30
31 #ifdef DEBUG
32
33 #if HAVE_BACKTRACE
34 #include <execinfo.h>
35 #endif
36
37 static void default_handler ( const int level, const char* file, int line,
38                 const char* function, const char* msg );
39 static PedDebugHandler* debug_handler = &default_handler;
40
41
42 /**
43  * Default debug handler.
44  * Will print all information to stderr.
45  */
46 static void default_handler ( const int level, const char* file, int line,
47                 const char* function, const char* msg )
48 {
49         fprintf ( stderr, "[%d] %s:%d (%s): %s\n",
50                         level, file, line, function, msg );
51 }
52
53 /**
54  * Send a debug message.
55  * Do not call this directly -- use PED_DEBUG() instead.
56  *
57  * level        log level, 0 ~= "print definitely"
58  */
59 void ped_debug ( const int level, const char* file, int line,
60                  const char* function, const char* msg, ... )
61 {
62         va_list         arg_list;
63         char*           msg_concat = ped_malloc(8192);
64
65         va_start ( arg_list, msg );
66                 vsnprintf ( msg_concat, 8192, msg, arg_list );
67         va_end ( arg_list );
68
69         debug_handler ( level, file, line, function, msg_concat );
70
71         free ( msg_concat );
72 }
73
74 /*
75  * handler      debug handler; NULL for default handler
76  */
77 void ped_debug_set_handler ( PedDebugHandler* handler )
78 {
79         debug_handler = ( handler ? handler : default_handler );
80 }
81
82 /*
83  * Check an assertion.
84  * Do not call this directly -- use PED_ASSERT() instead.
85  */
86 void ped_assert (const char* cond_text,
87                  const char* file, int line, const char* function)
88 {
89 #if HAVE_BACKTRACE
90         /* Print backtrace stack */
91         void *stack[20];
92         char **strings, **string;
93         int size = backtrace(stack, 20);
94         strings = backtrace_symbols(stack, size);
95
96         if (strings) {
97                 printf(_("Backtrace has %d calls on stack:\n"), size);
98
99                 for (string = strings; size > 0; size--, string++)
100                         printf("  %d: %s\n", size, *string);
101
102                 free(strings);
103         }
104 #endif
105
106         /* Throw the exception */
107         ped_exception_throw (
108                 PED_EXCEPTION_BUG,
109                 PED_EXCEPTION_FATAL,
110                 _("Assertion (%s) at %s:%d in function %s() failed."),
111                 cond_text, file, line, function);
112         abort ();
113 }
114
115 #endif /* DEBUG */