Initial commit
[platform/upstream/glib2.0.git] / docs / reference / glib / tmpl / warnings.sgml
1 <!-- ##### SECTION Title ##### -->
2 Message Output and Debugging Functions
3
4 <!-- ##### SECTION Short_Description ##### -->
5 functions to output messages and help debug applications
6
7 <!-- ##### SECTION Long_Description ##### -->
8 <para>
9 These functions provide support for outputting messages.
10 </para>
11 <para>
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. 
21 </para>
22
23 <!-- ##### SECTION See_Also ##### -->
24 <para>
25
26 </para>
27
28 <!-- ##### SECTION Stability_Level ##### -->
29
30
31 <!-- ##### FUNCTION g_print ##### -->
32 <para>
33 Outputs a formatted message via the print handler.
34 The default print handler simply outputs the message to stdout.
35 </para>
36 <para>
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().
42 </para>
43
44 @format: the message format. See the printf() documentation.
45 @Varargs: the parameters to insert into the format string.
46
47
48 <!-- ##### FUNCTION g_set_print_handler ##### -->
49 <para>
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.
55 </para>
56
57 @func: the new print handler.
58 @Returns: the old print handler.
59
60
61 <!-- ##### USER_FUNCTION GPrintFunc ##### -->
62 <para>
63 Specifies the type of the print handler functions.
64 These are called with the complete formatted string to output.
65 </para>
66
67 @string: the message to be output.
68
69
70 <!-- ##### FUNCTION g_printerr ##### -->
71 <para>
72 Outputs a formatted message via the error message handler.
73 The default handler simply outputs the message to stderr.
74 </para>
75 <para>
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().
78 </para>
79
80 @format: the message format. See the printf() documentation.
81 @Varargs: the parameters to insert into the format string.
82
83
84 <!-- ##### FUNCTION g_set_printerr_handler ##### -->
85 <para>
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.
91 </para>
92
93 @func: the new error message handler.
94 @Returns: the old error message handler.
95
96
97 <!-- ##### MACRO g_return_if_fail ##### -->
98 <para>
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
102 a value.
103 </para>
104
105 @expr: the expression to check.
106
107
108 <!-- ##### MACRO g_return_val_if_fail ##### -->
109 <para>
110 Returns from the current function, returning the value @val, if the expression
111 is not true.
112 If the expression evaluates to %FALSE, a critical message is logged and
113 @val is returned.
114 </para>
115
116 @expr: the expression to check.
117 @val: the value to return from the current function if the expression is not
118 true.
119
120
121 <!-- ##### MACRO g_return_if_reached ##### -->
122 <para>
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.
125 </para>
126
127
128
129 <!-- ##### MACRO g_return_val_if_reached ##### -->
130 <para>
131 Logs a critical message and returns @val. 
132 </para>
133
134 @val: the value to return from the current function.
135
136
137 <!-- ##### MACRO g_warn_if_fail ##### -->
138 <para>
139 Logs a warning if the expression is not true.
140 </para>
141
142 @expr: the expression to check
143 @Since: 2.16
144
145
146 <!-- ##### MACRO g_warn_if_reached ##### -->
147 <para>
148 Logs a critical warning.
149 </para>
150
151 @Since: 2.16
152
153
154 <!-- ##### FUNCTION g_on_error_query ##### -->
155 <para>
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.
159 </para>
160 <informalexample><programlisting>
161 &num;include &lt;glib.h&gt;
162
163 static void 
164 log_handler (const gchar   *log_domain,
165              GLogLevelFlags log_level,
166              const gchar   *message,
167              gpointer       user_data)
168 {
169   g_log_default_handler (log_domain, log_level, message, user_data);
170
171   g_on_error_query (MY_PROGRAM_NAME);
172 }
173
174 int main (int argc, char *argv[])
175 {
176   g_log_set_handler (MY_LOG_DOMAIN,
177                      G_LOG_LEVEL_WARNING | 
178                      G_LOG_LEVEL_ERROR | 
179                      G_LOG_LEVEL_CRITICAL,
180                      log_handler,
181                      NULL);
182
183  /* ... */  
184 </programlisting></informalexample>
185 <para>
186 If [E]xit is selected, the application terminates with a call to
187 <function>_exit(0)</function>.
188 </para>
189 <para>
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).
193 </para>
194 <para>
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.
198 </para>
199 <para>
200 If [P]roceed is selected, the function returns.
201 </para>
202 <para>
203 This function may cause different actions on non-UNIX platforms.
204 </para>
205
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).
209
210
211 <!-- ##### FUNCTION g_on_error_stack_trace ##### -->
212 <para>
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.
215 </para>
216 <para>
217 This function may cause different actions on non-UNIX platforms.
218 </para>
219
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).
223
224
225 <!-- ##### MACRO G_BREAKPOINT ##### -->
226 <para>
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
229 a %SIGTRAP signal.
230 </para>
231
232
233