gmessages.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  1. /* GLIB - Library of useful routines for C programming
  2. * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
  3. *
  4. * This library is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Lesser General Public
  6. * License as published by the Free Software Foundation; either
  7. * version 2 of the License, or (at your option) any later version.
  8. *
  9. * This library is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Lesser General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Lesser General Public
  15. * License along with this library; if not, see <http://www.gnu.org/licenses/>.
  16. */
  17. /*
  18. * Modified by the GLib Team and others 1997-2000. See the AUTHORS
  19. * file for a list of people on the GLib Team. See the ChangeLog
  20. * files for a list of changes. These files are distributed with
  21. * GLib at ftp://ftp.gtk.org/pub/gtk/.
  22. */
  23. #ifndef __G_MESSAGES_H__
  24. #define __G_MESSAGES_H__
  25. #if !defined (__GLIB_H_INSIDE__) && !defined (GLIB_COMPILATION)
  26. #error "Only <glib.h> can be included directly."
  27. #endif
  28. #include <stdarg.h>
  29. #include <glib/gtypes.h>
  30. #include <glib/gmacros.h>
  31. /* Suppress warnings when GCC is in -pedantic mode and not -std=c99
  32. */
  33. #if (__GNUC__ >= 3 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 96))
  34. #pragma GCC system_header
  35. #endif
  36. G_BEGIN_DECLS
  37. /* calculate a string size, guaranteed to fit format + args.
  38. */
  39. GLIB_AVAILABLE_IN_ALL
  40. gsize g_printf_string_upper_bound (const gchar* format,
  41. va_list args) G_GNUC_PRINTF(1, 0);
  42. /* Log level shift offset for user defined
  43. * log levels (0-7 are used by GLib).
  44. */
  45. #define G_LOG_LEVEL_USER_SHIFT (8)
  46. /* Glib log levels and flags.
  47. */
  48. typedef enum
  49. {
  50. /* log flags */
  51. G_LOG_FLAG_RECURSION = 1 << 0,
  52. G_LOG_FLAG_FATAL = 1 << 1,
  53. /* GLib log levels */
  54. G_LOG_LEVEL_ERROR = 1 << 2, /* always fatal */
  55. G_LOG_LEVEL_CRITICAL = 1 << 3,
  56. G_LOG_LEVEL_WARNING = 1 << 4,
  57. G_LOG_LEVEL_MESSAGE = 1 << 5,
  58. G_LOG_LEVEL_INFO = 1 << 6,
  59. G_LOG_LEVEL_DEBUG = 1 << 7,
  60. G_LOG_LEVEL_MASK = ~(G_LOG_FLAG_RECURSION | G_LOG_FLAG_FATAL)
  61. } GLogLevelFlags;
  62. /* GLib log levels that are considered fatal by default */
  63. #define G_LOG_FATAL_MASK (G_LOG_FLAG_RECURSION | G_LOG_LEVEL_ERROR)
  64. typedef void (*GLogFunc) (const gchar *log_domain,
  65. GLogLevelFlags log_level,
  66. const gchar *message,
  67. gpointer user_data);
  68. /* Logging mechanism
  69. */
  70. GLIB_AVAILABLE_IN_ALL
  71. guint g_log_set_handler (const gchar *log_domain,
  72. GLogLevelFlags log_levels,
  73. GLogFunc log_func,
  74. gpointer user_data);
  75. GLIB_AVAILABLE_IN_ALL
  76. void g_log_remove_handler (const gchar *log_domain,
  77. guint handler_id);
  78. GLIB_AVAILABLE_IN_ALL
  79. void g_log_default_handler (const gchar *log_domain,
  80. GLogLevelFlags log_level,
  81. const gchar *message,
  82. gpointer unused_data);
  83. GLIB_AVAILABLE_IN_ALL
  84. GLogFunc g_log_set_default_handler (GLogFunc log_func,
  85. gpointer user_data);
  86. GLIB_AVAILABLE_IN_ALL
  87. void g_log (const gchar *log_domain,
  88. GLogLevelFlags log_level,
  89. const gchar *format,
  90. ...) G_GNUC_PRINTF (3, 4);
  91. GLIB_AVAILABLE_IN_ALL
  92. void g_logv (const gchar *log_domain,
  93. GLogLevelFlags log_level,
  94. const gchar *format,
  95. va_list args) G_GNUC_PRINTF(3, 0);
  96. GLIB_AVAILABLE_IN_ALL
  97. GLogLevelFlags g_log_set_fatal_mask (const gchar *log_domain,
  98. GLogLevelFlags fatal_mask);
  99. GLIB_AVAILABLE_IN_ALL
  100. GLogLevelFlags g_log_set_always_fatal (GLogLevelFlags fatal_mask);
  101. /* internal */
  102. void _g_log_fallback_handler (const gchar *log_domain,
  103. GLogLevelFlags log_level,
  104. const gchar *message,
  105. gpointer unused_data);
  106. /* Internal functions, used to implement the following macros */
  107. GLIB_AVAILABLE_IN_ALL
  108. void g_return_if_fail_warning (const char *log_domain,
  109. const char *pretty_function,
  110. const char *expression) G_ANALYZER_NORETURN;
  111. GLIB_AVAILABLE_IN_ALL
  112. void g_warn_message (const char *domain,
  113. const char *file,
  114. int line,
  115. const char *func,
  116. const char *warnexpr) G_ANALYZER_NORETURN;
  117. GLIB_DEPRECATED
  118. void g_assert_warning (const char *log_domain,
  119. const char *file,
  120. const int line,
  121. const char *pretty_function,
  122. const char *expression) G_GNUC_NORETURN;
  123. #ifndef G_LOG_DOMAIN
  124. #define G_LOG_DOMAIN ((gchar*) 0)
  125. #endif /* G_LOG_DOMAIN */
  126. #if defined(G_HAVE_ISO_VARARGS) && !G_ANALYZER_ANALYZING
  127. /* for(;;) ; so that GCC knows that control doesn't go past g_error().
  128. * Put space before ending semicolon to avoid C++ build warnings.
  129. */
  130. #define g_error(...) G_STMT_START { \
  131. g_log (G_LOG_DOMAIN, \
  132. G_LOG_LEVEL_ERROR, \
  133. __VA_ARGS__); \
  134. for (;;) ; \
  135. } G_STMT_END
  136. #define g_message(...) g_log (G_LOG_DOMAIN, \
  137. G_LOG_LEVEL_MESSAGE, \
  138. __VA_ARGS__)
  139. #define g_critical(...) g_log (G_LOG_DOMAIN, \
  140. G_LOG_LEVEL_CRITICAL, \
  141. __VA_ARGS__)
  142. #define g_warning(...) g_log (G_LOG_DOMAIN, \
  143. G_LOG_LEVEL_WARNING, \
  144. __VA_ARGS__)
  145. #define g_info(...) g_log (G_LOG_DOMAIN, \
  146. G_LOG_LEVEL_INFO, \
  147. __VA_ARGS__)
  148. #define g_debug(...) g_log (G_LOG_DOMAIN, \
  149. G_LOG_LEVEL_DEBUG, \
  150. __VA_ARGS__)
  151. #elif defined(G_HAVE_GNUC_VARARGS) && !G_ANALYZER_ANALYZING
  152. #define g_error(format...) G_STMT_START { \
  153. g_log (G_LOG_DOMAIN, \
  154. G_LOG_LEVEL_ERROR, \
  155. format); \
  156. for (;;) ; \
  157. } G_STMT_END
  158. #define g_message(format...) g_log (G_LOG_DOMAIN, \
  159. G_LOG_LEVEL_MESSAGE, \
  160. format)
  161. #define g_critical(format...) g_log (G_LOG_DOMAIN, \
  162. G_LOG_LEVEL_CRITICAL, \
  163. format)
  164. #define g_warning(format...) g_log (G_LOG_DOMAIN, \
  165. G_LOG_LEVEL_WARNING, \
  166. format)
  167. #define g_info(format...) g_log (G_LOG_DOMAIN, \
  168. G_LOG_LEVEL_INFO, \
  169. format)
  170. #define g_debug(format...) g_log (G_LOG_DOMAIN, \
  171. G_LOG_LEVEL_DEBUG, \
  172. format)
  173. #else /* no varargs macros */
  174. static void g_error (const gchar *format, ...) G_ANALYZER_NORETURN;
  175. static void g_critical (const gchar *format, ...) G_ANALYZER_NORETURN;
  176. static void
  177. g_error (const gchar *format,
  178. ...)
  179. {
  180. va_list args;
  181. va_start (args, format);
  182. g_logv (G_LOG_DOMAIN, G_LOG_LEVEL_ERROR, format, args);
  183. va_end (args);
  184. for(;;) ;
  185. }
  186. static void
  187. g_message (const gchar *format,
  188. ...)
  189. {
  190. va_list args;
  191. va_start (args, format);
  192. g_logv (G_LOG_DOMAIN, G_LOG_LEVEL_MESSAGE, format, args);
  193. va_end (args);
  194. }
  195. static void
  196. g_critical (const gchar *format,
  197. ...)
  198. {
  199. va_list args;
  200. va_start (args, format);
  201. g_logv (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL, format, args);
  202. va_end (args);
  203. }
  204. static void
  205. g_warning (const gchar *format,
  206. ...)
  207. {
  208. va_list args;
  209. va_start (args, format);
  210. g_logv (G_LOG_DOMAIN, G_LOG_LEVEL_WARNING, format, args);
  211. va_end (args);
  212. }
  213. static void
  214. g_info (const gchar *format,
  215. ...)
  216. {
  217. va_list args;
  218. va_start (args, format);
  219. g_logv (G_LOG_DOMAIN, G_LOG_LEVEL_INFO, format, args);
  220. va_end (args);
  221. }
  222. static void
  223. g_debug (const gchar *format,
  224. ...)
  225. {
  226. va_list args;
  227. va_start (args, format);
  228. g_logv (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, format, args);
  229. va_end (args);
  230. }
  231. #endif /* !__GNUC__ */
  232. /**
  233. * GPrintFunc:
  234. * @string: the message to output
  235. *
  236. * Specifies the type of the print handler functions.
  237. * These are called with the complete formatted string to output.
  238. */
  239. typedef void (*GPrintFunc) (const gchar *string);
  240. GLIB_AVAILABLE_IN_ALL
  241. void g_print (const gchar *format,
  242. ...) G_GNUC_PRINTF (1, 2);
  243. GLIB_AVAILABLE_IN_ALL
  244. GPrintFunc g_set_print_handler (GPrintFunc func);
  245. GLIB_AVAILABLE_IN_ALL
  246. void g_printerr (const gchar *format,
  247. ...) G_GNUC_PRINTF (1, 2);
  248. GLIB_AVAILABLE_IN_ALL
  249. GPrintFunc g_set_printerr_handler (GPrintFunc func);
  250. /**
  251. * g_warn_if_reached:
  252. *
  253. * Logs a critical warning.
  254. *
  255. * Since: 2.16
  256. */
  257. #define g_warn_if_reached() \
  258. do { \
  259. g_warn_message (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, NULL); \
  260. } while (0)
  261. /**
  262. * g_warn_if_fail:
  263. * @expr: the expression to check
  264. *
  265. * Logs a warning if the expression is not true.
  266. *
  267. * Since: 2.16
  268. */
  269. #define g_warn_if_fail(expr) \
  270. do { \
  271. if G_LIKELY (expr) ; \
  272. else g_warn_message (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, #expr); \
  273. } while (0)
  274. #ifdef G_DISABLE_CHECKS
  275. /**
  276. * g_return_if_fail:
  277. * @expr: the expression to check
  278. *
  279. * Verifies that the expression @expr, usually representing a precondition,
  280. * evaluates to %TRUE. If the function returns a value, use
  281. * g_return_val_if_fail() instead.
  282. *
  283. * If @expr evaluates to %FALSE, the current function should be considered to
  284. * have undefined behaviour (a programmer error). The only correct solution
  285. * to such an error is to change the module that is calling the current
  286. * function, so that it avoids this incorrect call.
  287. *
  288. * To make this undefined behaviour visible, if @expr evaluates to %FALSE,
  289. * the result is usually that a critical message is logged and the current
  290. * function returns.
  291. *
  292. * If G_DISABLE_CHECKS is defined then the check is not performed. You
  293. * should therefore not depend on any side effects of @expr.
  294. */
  295. #define g_return_if_fail(expr) G_STMT_START{ (void)0; }G_STMT_END
  296. /**
  297. * g_return_val_if_fail:
  298. * @expr: the expression to check
  299. * @val: the value to return from the current function
  300. * if the expression is not true
  301. *
  302. * Verifies that the expression @expr, usually representing a precondition,
  303. * evaluates to %TRUE. If the function does not return a value, use
  304. * g_return_if_fail() instead.
  305. *
  306. * If @expr evaluates to %FALSE, the current function should be considered to
  307. * have undefined behaviour (a programmer error). The only correct solution
  308. * to such an error is to change the module that is calling the current
  309. * function, so that it avoids this incorrect call.
  310. *
  311. * To make this undefined behaviour visible, if @expr evaluates to %FALSE,
  312. * the result is usually that a critical message is logged and @val is
  313. * returned from the current function.
  314. *
  315. * If G_DISABLE_CHECKS is defined then the check is not performed. You
  316. * should therefore not depend on any side effects of @expr.
  317. */
  318. #define g_return_val_if_fail(expr,val) G_STMT_START{ (void)0; }G_STMT_END
  319. /**
  320. * g_return_if_reached:
  321. *
  322. * Logs a critical message and returns from the current function.
  323. * This can only be used in functions which do not return a value.
  324. */
  325. #define g_return_if_reached() G_STMT_START{ return; }G_STMT_END
  326. /**
  327. * g_return_val_if_reached:
  328. * @val: the value to return from the current function
  329. *
  330. * Logs a critical message and returns @val.
  331. */
  332. #define g_return_val_if_reached(val) G_STMT_START{ return (val); }G_STMT_END
  333. #else /* !G_DISABLE_CHECKS */
  334. #define g_return_if_fail(expr) G_STMT_START{ \
  335. if G_LIKELY(expr) { } else \
  336. { \
  337. g_return_if_fail_warning (G_LOG_DOMAIN, \
  338. G_STRFUNC, \
  339. #expr); \
  340. return; \
  341. }; }G_STMT_END
  342. #define g_return_val_if_fail(expr,val) G_STMT_START{ \
  343. if G_LIKELY(expr) { } else \
  344. { \
  345. g_return_if_fail_warning (G_LOG_DOMAIN, \
  346. G_STRFUNC, \
  347. #expr); \
  348. return (val); \
  349. }; }G_STMT_END
  350. #define g_return_if_reached() G_STMT_START{ \
  351. g_log (G_LOG_DOMAIN, \
  352. G_LOG_LEVEL_CRITICAL, \
  353. "file %s: line %d (%s): should not be reached", \
  354. __FILE__, \
  355. __LINE__, \
  356. G_STRFUNC); \
  357. return; }G_STMT_END
  358. #define g_return_val_if_reached(val) G_STMT_START{ \
  359. g_log (G_LOG_DOMAIN, \
  360. G_LOG_LEVEL_CRITICAL, \
  361. "file %s: line %d (%s): should not be reached", \
  362. __FILE__, \
  363. __LINE__, \
  364. G_STRFUNC); \
  365. return (val); }G_STMT_END
  366. #endif /* !G_DISABLE_CHECKS */
  367. G_END_DECLS
  368. #endif /* __G_MESSAGES_H__ */