OSDN Git Service

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