1 <!-- ##### SECTION Title ##### -->
2 Message Output and Debugging Functions
4 <!-- ##### SECTION Short_Description ##### -->
5 functions to output messages and help debug applications
7 <!-- ##### SECTION Long_Description ##### -->
9 These functions provide support for outputting messages.
12 The <function>g_return</function> family of macros (g_return_if_fail(),
13 g_return_val_if_fail(), g_return_if_reached(), g_return_val_if_reached())
14 should only be used for programming errors, a typical use case is
15 checking for invalid parameters at the beginning of a public function.
16 They should not be used if you just mean "if (error) return", they
17 should only be used if you mean "if (bug in program) return".
18 The program behavior is generally considered undefined after one of these
19 checks fails. They are not intended for normal control flow, only to
20 give a perhaps-helpful warning before giving up.
23 <!-- ##### SECTION See_Also ##### -->
28 <!-- ##### SECTION Stability_Level ##### -->
31 <!-- ##### FUNCTION g_print ##### -->
33 Outputs a formatted message via the print handler.
34 The default print handler simply outputs the message to stdout.
37 g_print() should not be used from within libraries for debugging messages,
38 since it may be redirected by applications to special purpose message
39 windows or even files.
40 Instead, libraries should use g_log(), or the convenience functions
41 g_message(), g_warning() and g_error().
44 @format: the message format. See the printf() documentation.
45 @Varargs: the parameters to insert into the format string.
48 <!-- ##### FUNCTION g_set_print_handler ##### -->
50 Sets the print handler.
51 Any messages passed to g_print() will be output via the new handler.
52 The default handler simply outputs the message to stdout.
53 By providing your own handler you can redirect the output, to a GTK+
54 widget or a log file for example.
57 @func: the new print handler.
58 @Returns: the old print handler.
61 <!-- ##### USER_FUNCTION GPrintFunc ##### -->
63 Specifies the type of the print handler functions.
64 These are called with the complete formatted string to output.
67 @string: the message to be output.
70 <!-- ##### FUNCTION g_printerr ##### -->
72 Outputs a formatted message via the error message handler.
73 The default handler simply outputs the message to stderr.
76 g_printerr() should not be used from within libraries. Instead g_log() should
77 be used, or the convenience functions g_message(), g_warning() and g_error().
80 @format: the message format. See the printf() documentation.
81 @Varargs: the parameters to insert into the format string.
84 <!-- ##### FUNCTION g_set_printerr_handler ##### -->
86 Sets the handler for printing error messages.
87 Any messages passed to g_printerr() will be output via the new handler.
88 The default handler simply outputs the message to stderr.
89 By providing your own handler you can redirect the output, to a GTK+
90 widget or a log file for example.
93 @func: the new error message handler.
94 @Returns: the old error message handler.
97 <!-- ##### MACRO g_return_if_fail ##### -->
99 Returns from the current function if the expression is not true.
100 If the expression evaluates to %FALSE, a critical message is logged and
101 the function returns. This can only be used in functions which do not return
105 @expr: the expression to check.
108 <!-- ##### MACRO g_return_val_if_fail ##### -->
110 Returns from the current function, returning the value @val, if the expression
112 If the expression evaluates to %FALSE, a critical message is logged and
116 @expr: the expression to check.
117 @val: the value to return from the current function if the expression is not
121 <!-- ##### MACRO g_return_if_reached ##### -->
123 Logs a critical message and returns from the current function.
124 This can only be used in functions which do not return a value.
129 <!-- ##### MACRO g_return_val_if_reached ##### -->
131 Logs a critical message and returns @val.
134 @val: the value to return from the current function.
137 <!-- ##### MACRO g_warn_if_fail ##### -->
139 Logs a warning if the expression is not true.
142 @expr: the expression to check
146 <!-- ##### MACRO g_warn_if_reached ##### -->
148 Logs a critical warning.
154 <!-- ##### FUNCTION g_on_error_query ##### -->
156 Prompts the user with <computeroutput>[E]xit, [H]alt, show [S]tack trace or [P]roceed</computeroutput>.
157 This function is intended to be used for debugging use only. The following
158 example shows how it can be used together with the g_log() functions.
160 <informalexample><programlisting>
161 #include <glib.h>
164 log_handler (const gchar *log_domain,
165 GLogLevelFlags log_level,
166 const gchar *message,
169 g_log_default_handler (log_domain, log_level, message, user_data);
171 g_on_error_query (MY_PROGRAM_NAME);
174 int main (int argc, char *argv[])
176 g_log_set_handler (MY_LOG_DOMAIN,
177 G_LOG_LEVEL_WARNING |
179 G_LOG_LEVEL_CRITICAL,
184 </programlisting></informalexample>
186 If [E]xit is selected, the application terminates with a call to
187 <function>_exit(0)</function>.
190 If [H]alt is selected, the application enters an infinite loop.
191 The infinite loop can only be stopped by killing the application,
192 or by setting #glib_on_error_halt to %FALSE (possibly via a debugger).
195 If [S]tack trace is selected, g_on_error_stack_trace() is called. This
196 invokes <command>gdb</command>, which attaches to the current process and shows a stack trace.
197 The prompt is then shown again.
200 If [P]roceed is selected, the function returns.
203 This function may cause different actions on non-UNIX platforms.
206 @prg_name: the program name, needed by <command>gdb</command> for the [S]tack trace option.
207 If @prg_name is %NULL, g_get_prgname() is called to get the program name
208 (which will work correctly if gdk_init() or gtk_init() has been called).
211 <!-- ##### FUNCTION g_on_error_stack_trace ##### -->
213 Invokes <command>gdb</command>, which attaches to the current process and shows a stack trace.
214 Called by g_on_error_query() when the [S]tack trace option is selected.
217 This function may cause different actions on non-UNIX platforms.
220 @prg_name: the program name, needed by <command>gdb</command> for the [S]tack trace option.
221 If @prg_name is %NULL, g_get_prgname() is called to get the program name
222 (which will work correctly if gdk_init() or gtk_init() has been called).
225 <!-- ##### MACRO G_BREAKPOINT ##### -->
227 Inserts a breakpoint instruction into the code. On x86 and alpha systems
228 this is implemented as a soft interrupt and on other architectures it raises