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