gmessages.h 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701
  1. /* GLIB - Library of useful routines for C programming
  2. * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
  3. *
  4. * SPDX-License-Identifier: LGPL-2.1-or-later
  5. *
  6. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * This library is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with this library; if not, see <http://www.gnu.org/licenses/>.
  18. */
  19. /*
  20. * Modified by the GLib Team and others 1997-2000. See the AUTHORS
  21. * file for a list of people on the GLib Team. See the ChangeLog
  22. * files for a list of changes. These files are distributed with
  23. * GLib at ftp://ftp.gtk.org/pub/gtk/.
  24. */
  25. #ifndef __G_MESSAGES_H__
  26. #define __G_MESSAGES_H__
  27. #if !defined (__GLIB_H_INSIDE__) && !defined (GLIB_COMPILATION)
  28. #error "Only <glib.h> can be included directly."
  29. #endif
  30. #include <stdarg.h>
  31. #include <glib/gatomic.h>
  32. #include <glib/gtypes.h>
  33. #include <glib/gmacros.h>
  34. #include <glib/gvariant.h>
  35. G_BEGIN_DECLS
  36. /* calculate a string size, guaranteed to fit format + args.
  37. */
  38. GLIB_AVAILABLE_IN_ALL
  39. gsize g_printf_string_upper_bound (const gchar* format,
  40. va_list args) G_GNUC_PRINTF(1, 0);
  41. /* Log level shift offset for user defined
  42. * log levels (0-7 are used by GLib).
  43. */
  44. #define G_LOG_LEVEL_USER_SHIFT (8)
  45. /* Glib log levels and flags.
  46. */
  47. typedef enum
  48. {
  49. /* log flags */
  50. G_LOG_FLAG_RECURSION = 1 << 0,
  51. G_LOG_FLAG_FATAL = 1 << 1,
  52. /* GLib log levels */
  53. G_LOG_LEVEL_ERROR = 1 << 2, /* always fatal */
  54. G_LOG_LEVEL_CRITICAL = 1 << 3,
  55. G_LOG_LEVEL_WARNING = 1 << 4,
  56. G_LOG_LEVEL_MESSAGE = 1 << 5,
  57. G_LOG_LEVEL_INFO = 1 << 6,
  58. G_LOG_LEVEL_DEBUG = 1 << 7,
  59. G_LOG_LEVEL_MASK = ~(G_LOG_FLAG_RECURSION | G_LOG_FLAG_FATAL)
  60. } GLogLevelFlags;
  61. /* GLib log levels that are considered fatal by default */
  62. #define G_LOG_FATAL_MASK (G_LOG_FLAG_RECURSION | G_LOG_LEVEL_ERROR)
  63. typedef void (*GLogFunc) (const gchar *log_domain,
  64. GLogLevelFlags log_level,
  65. const gchar *message,
  66. gpointer user_data);
  67. /* Logging mechanism
  68. */
  69. GLIB_AVAILABLE_IN_ALL
  70. guint g_log_set_handler (const gchar *log_domain,
  71. GLogLevelFlags log_levels,
  72. GLogFunc log_func,
  73. gpointer user_data);
  74. GLIB_AVAILABLE_IN_2_46
  75. guint g_log_set_handler_full (const gchar *log_domain,
  76. GLogLevelFlags log_levels,
  77. GLogFunc log_func,
  78. gpointer user_data,
  79. GDestroyNotify destroy);
  80. GLIB_AVAILABLE_IN_ALL
  81. void g_log_remove_handler (const gchar *log_domain,
  82. guint handler_id);
  83. GLIB_AVAILABLE_IN_ALL
  84. void g_log_default_handler (const gchar *log_domain,
  85. GLogLevelFlags log_level,
  86. const gchar *message,
  87. gpointer unused_data);
  88. GLIB_AVAILABLE_IN_ALL
  89. GLogFunc g_log_set_default_handler (GLogFunc log_func,
  90. gpointer user_data);
  91. GLIB_AVAILABLE_IN_ALL
  92. void g_log (const gchar *log_domain,
  93. GLogLevelFlags log_level,
  94. const gchar *format,
  95. ...) G_GNUC_PRINTF (3, 4);
  96. GLIB_AVAILABLE_IN_ALL
  97. void g_logv (const gchar *log_domain,
  98. GLogLevelFlags log_level,
  99. const gchar *format,
  100. va_list args) G_GNUC_PRINTF(3, 0);
  101. GLIB_AVAILABLE_IN_ALL
  102. GLogLevelFlags g_log_set_fatal_mask (const gchar *log_domain,
  103. GLogLevelFlags fatal_mask);
  104. GLIB_AVAILABLE_IN_ALL
  105. GLogLevelFlags g_log_set_always_fatal (GLogLevelFlags fatal_mask);
  106. /* Structured logging mechanism. */
  107. /**
  108. * GLogWriterOutput:
  109. * @G_LOG_WRITER_HANDLED: Log writer has handled the log entry.
  110. * @G_LOG_WRITER_UNHANDLED: Log writer could not handle the log entry.
  111. *
  112. * Return values from #GLogWriterFuncs to indicate whether the given log entry
  113. * was successfully handled by the writer, or whether there was an error in
  114. * handling it (and hence a fallback writer should be used).
  115. *
  116. * If a #GLogWriterFunc ignores a log entry, it should return
  117. * %G_LOG_WRITER_HANDLED.
  118. *
  119. * Since: 2.50
  120. */
  121. typedef enum
  122. {
  123. G_LOG_WRITER_HANDLED = 1,
  124. G_LOG_WRITER_UNHANDLED = 0,
  125. } GLogWriterOutput;
  126. /**
  127. * GLogField:
  128. * @key: field name (UTF-8 string)
  129. * @value: field value (arbitrary bytes)
  130. * @length: length of @value, in bytes, or -1 if it is nul-terminated
  131. *
  132. * Structure representing a single field in a structured log entry. See
  133. * g_log_structured() for details.
  134. *
  135. * Log fields may contain arbitrary values, including binary with embedded nul
  136. * bytes. If the field contains a string, the string must be UTF-8 encoded and
  137. * have a trailing nul byte. Otherwise, @length must be set to a non-negative
  138. * value.
  139. *
  140. * Since: 2.50
  141. */
  142. typedef struct _GLogField GLogField;
  143. struct _GLogField
  144. {
  145. const gchar *key;
  146. gconstpointer value;
  147. gssize length;
  148. };
  149. /**
  150. * GLogWriterFunc:
  151. * @log_level: log level of the message
  152. * @fields: (array length=n_fields): fields forming the message
  153. * @n_fields: number of @fields
  154. * @user_data: user data passed to g_log_set_writer_func()
  155. *
  156. * Writer function for log entries. A log entry is a collection of one or more
  157. * #GLogFields, using the standard [field names from journal
  158. * specification](https://www.freedesktop.org/software/systemd/man/systemd.journal-fields.html).
  159. * See g_log_structured() for more information.
  160. *
  161. * Writer functions must ignore fields which they do not recognise, unless they
  162. * can write arbitrary binary output, as field values may be arbitrary binary.
  163. *
  164. * @log_level is guaranteed to be included in @fields as the `PRIORITY` field,
  165. * but is provided separately for convenience of deciding whether or where to
  166. * output the log entry.
  167. *
  168. * Writer functions should return %G_LOG_WRITER_HANDLED if they handled the log
  169. * message successfully or if they deliberately ignored it. If there was an
  170. * error handling the message (for example, if the writer function is meant to
  171. * send messages to a remote logging server and there is a network error), it
  172. * should return %G_LOG_WRITER_UNHANDLED. This allows writer functions to be
  173. * chained and fall back to simpler handlers in case of failure.
  174. *
  175. * Returns: %G_LOG_WRITER_HANDLED if the log entry was handled successfully;
  176. * %G_LOG_WRITER_UNHANDLED otherwise
  177. *
  178. * Since: 2.50
  179. */
  180. typedef GLogWriterOutput (*GLogWriterFunc) (GLogLevelFlags log_level,
  181. const GLogField *fields,
  182. gsize n_fields,
  183. gpointer user_data);
  184. GLIB_AVAILABLE_IN_2_50
  185. void g_log_structured (const gchar *log_domain,
  186. GLogLevelFlags log_level,
  187. ...);
  188. GLIB_AVAILABLE_IN_2_50
  189. void g_log_structured_array (GLogLevelFlags log_level,
  190. const GLogField *fields,
  191. gsize n_fields);
  192. GLIB_AVAILABLE_IN_2_50
  193. void g_log_variant (const gchar *log_domain,
  194. GLogLevelFlags log_level,
  195. GVariant *fields);
  196. GLIB_AVAILABLE_IN_2_50
  197. void g_log_set_writer_func (GLogWriterFunc func,
  198. gpointer user_data,
  199. GDestroyNotify user_data_free);
  200. GLIB_AVAILABLE_IN_2_50
  201. gboolean g_log_writer_supports_color (gint output_fd);
  202. GLIB_AVAILABLE_IN_2_50
  203. gboolean g_log_writer_is_journald (gint output_fd);
  204. GLIB_AVAILABLE_IN_2_50
  205. gchar *g_log_writer_format_fields (GLogLevelFlags log_level,
  206. const GLogField *fields,
  207. gsize n_fields,
  208. gboolean use_color);
  209. GLIB_AVAILABLE_IN_2_80
  210. GLogWriterOutput g_log_writer_syslog (GLogLevelFlags log_level,
  211. const GLogField *fields,
  212. gsize n_fields,
  213. gpointer user_data);
  214. GLIB_AVAILABLE_IN_2_50
  215. GLogWriterOutput g_log_writer_journald (GLogLevelFlags log_level,
  216. const GLogField *fields,
  217. gsize n_fields,
  218. gpointer user_data);
  219. GLIB_AVAILABLE_IN_2_50
  220. GLogWriterOutput g_log_writer_standard_streams (GLogLevelFlags log_level,
  221. const GLogField *fields,
  222. gsize n_fields,
  223. gpointer user_data);
  224. GLIB_AVAILABLE_IN_2_50
  225. GLogWriterOutput g_log_writer_default (GLogLevelFlags log_level,
  226. const GLogField *fields,
  227. gsize n_fields,
  228. gpointer user_data);
  229. GLIB_AVAILABLE_IN_2_68
  230. void g_log_writer_default_set_use_stderr (gboolean use_stderr);
  231. GLIB_AVAILABLE_IN_2_68
  232. gboolean g_log_writer_default_would_drop (GLogLevelFlags log_level,
  233. const char *log_domain);
  234. GLIB_AVAILABLE_IN_2_80
  235. void g_log_writer_default_set_debug_domains (const gchar * const *domains);
  236. /* G_MESSAGES_DEBUG enablement */
  237. GLIB_AVAILABLE_IN_2_72
  238. gboolean g_log_get_debug_enabled (void);
  239. GLIB_AVAILABLE_IN_2_72
  240. void g_log_set_debug_enabled (gboolean enabled);
  241. /**
  242. * G_DEBUG_HERE:
  243. *
  244. * A convenience form of g_log_structured(), recommended to be added to
  245. * functions when debugging. It prints the current monotonic time and the code
  246. * location using %G_STRLOC.
  247. *
  248. * Since: 2.50
  249. */
  250. #define G_DEBUG_HERE() \
  251. g_log_structured (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, \
  252. "CODE_FILE", __FILE__, \
  253. "CODE_LINE", G_STRINGIFY (__LINE__), \
  254. "CODE_FUNC", G_STRFUNC, \
  255. "MESSAGE", "%" G_GINT64_FORMAT ": %s", \
  256. g_get_monotonic_time (), G_STRLOC)
  257. /* internal */
  258. void _g_log_fallback_handler (const gchar *log_domain,
  259. GLogLevelFlags log_level,
  260. const gchar *message,
  261. gpointer unused_data);
  262. /* Internal functions, used to implement the following macros */
  263. GLIB_AVAILABLE_IN_ALL
  264. void g_return_if_fail_warning (const char *log_domain,
  265. const char *pretty_function,
  266. const char *expression) G_ANALYZER_NORETURN;
  267. GLIB_AVAILABLE_IN_ALL
  268. void g_warn_message (const char *domain,
  269. const char *file,
  270. int line,
  271. const char *func,
  272. const char *warnexpr) G_ANALYZER_NORETURN;
  273. G_NORETURN
  274. GLIB_DEPRECATED
  275. void g_assert_warning (const char *log_domain,
  276. const char *file,
  277. const int line,
  278. const char *pretty_function,
  279. const char *expression);
  280. GLIB_AVAILABLE_IN_2_56
  281. void g_log_structured_standard (const gchar *log_domain,
  282. GLogLevelFlags log_level,
  283. const gchar *file,
  284. const gchar *line,
  285. const gchar *func,
  286. const gchar *message_format,
  287. ...) G_GNUC_PRINTF (6, 7);
  288. #ifndef G_LOG_DOMAIN
  289. #define G_LOG_DOMAIN ((gchar*) 0)
  290. #endif /* G_LOG_DOMAIN */
  291. #if defined(G_HAVE_ISO_VARARGS) && !G_ANALYZER_ANALYZING
  292. #if defined(G_LOG_USE_STRUCTURED) && GLIB_VERSION_MAX_ALLOWED >= GLIB_VERSION_2_56
  293. #define g_error(...) G_STMT_START { \
  294. g_log_structured_standard (G_LOG_DOMAIN, G_LOG_LEVEL_ERROR, \
  295. __FILE__, G_STRINGIFY (__LINE__), \
  296. G_STRFUNC, __VA_ARGS__); \
  297. for (;;) ; \
  298. } G_STMT_END
  299. #define g_message(...) g_log_structured_standard (G_LOG_DOMAIN, G_LOG_LEVEL_MESSAGE, \
  300. __FILE__, G_STRINGIFY (__LINE__), \
  301. G_STRFUNC, __VA_ARGS__)
  302. #define g_critical(...) g_log_structured_standard (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL, \
  303. __FILE__, G_STRINGIFY (__LINE__), \
  304. G_STRFUNC, __VA_ARGS__)
  305. #define g_warning(...) g_log_structured_standard (G_LOG_DOMAIN, G_LOG_LEVEL_WARNING, \
  306. __FILE__, G_STRINGIFY (__LINE__), \
  307. G_STRFUNC, __VA_ARGS__)
  308. #define g_info(...) g_log_structured_standard (G_LOG_DOMAIN, G_LOG_LEVEL_INFO, \
  309. __FILE__, G_STRINGIFY (__LINE__), \
  310. G_STRFUNC, __VA_ARGS__)
  311. #define g_debug(...) g_log_structured_standard (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, \
  312. __FILE__, G_STRINGIFY (__LINE__), \
  313. G_STRFUNC, __VA_ARGS__)
  314. #else
  315. /* for(;;) ; so that GCC knows that control doesn't go past g_error().
  316. * Put space before ending semicolon to avoid C++ build warnings.
  317. */
  318. #define g_error(...) G_STMT_START { \
  319. g_log (G_LOG_DOMAIN, \
  320. G_LOG_LEVEL_ERROR, \
  321. __VA_ARGS__); \
  322. for (;;) ; \
  323. } G_STMT_END
  324. #define g_message(...) g_log (G_LOG_DOMAIN, \
  325. G_LOG_LEVEL_MESSAGE, \
  326. __VA_ARGS__)
  327. #define g_critical(...) g_log (G_LOG_DOMAIN, \
  328. G_LOG_LEVEL_CRITICAL, \
  329. __VA_ARGS__)
  330. #define g_warning(...) g_log (G_LOG_DOMAIN, \
  331. G_LOG_LEVEL_WARNING, \
  332. __VA_ARGS__)
  333. #define g_info(...) g_log (G_LOG_DOMAIN, \
  334. G_LOG_LEVEL_INFO, \
  335. __VA_ARGS__)
  336. #define g_debug(...) g_log (G_LOG_DOMAIN, \
  337. G_LOG_LEVEL_DEBUG, \
  338. __VA_ARGS__)
  339. #endif
  340. #elif defined(G_HAVE_GNUC_VARARGS) && !G_ANALYZER_ANALYZING
  341. #if defined(G_LOG_USE_STRUCTURED) && GLIB_VERSION_MAX_ALLOWED >= GLIB_VERSION_2_56
  342. #define g_error(format...) G_STMT_START { \
  343. g_log_structured_standard (G_LOG_DOMAIN, G_LOG_LEVEL_ERROR, \
  344. __FILE__, G_STRINGIFY (__LINE__), \
  345. G_STRFUNC, format); \
  346. for (;;) ; \
  347. } G_STMT_END
  348. #define g_message(format...) g_log_structured_standard (G_LOG_DOMAIN, G_LOG_LEVEL_MESSAGE, \
  349. __FILE__, G_STRINGIFY (__LINE__), \
  350. G_STRFUNC, format)
  351. #define g_critical(format...) g_log_structured_standard (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL, \
  352. __FILE__, G_STRINGIFY (__LINE__), \
  353. G_STRFUNC, format)
  354. #define g_warning(format...) g_log_structured_standard (G_LOG_DOMAIN, G_LOG_LEVEL_WARNING, \
  355. __FILE__, G_STRINGIFY (__LINE__), \
  356. G_STRFUNC, format)
  357. #define g_info(format...) g_log_structured_standard (G_LOG_DOMAIN, G_LOG_LEVEL_INFO, \
  358. __FILE__, G_STRINGIFY (__LINE__), \
  359. G_STRFUNC, format)
  360. #define g_debug(format...) g_log_structured_standard (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, \
  361. __FILE__, G_STRINGIFY (__LINE__), \
  362. G_STRFUNC, format)
  363. #else
  364. #define g_error(format...) G_STMT_START { \
  365. g_log (G_LOG_DOMAIN, \
  366. G_LOG_LEVEL_ERROR, \
  367. format); \
  368. for (;;) ; \
  369. } G_STMT_END
  370. #define g_message(format...) g_log (G_LOG_DOMAIN, \
  371. G_LOG_LEVEL_MESSAGE, \
  372. format)
  373. #define g_critical(format...) g_log (G_LOG_DOMAIN, \
  374. G_LOG_LEVEL_CRITICAL, \
  375. format)
  376. #define g_warning(format...) g_log (G_LOG_DOMAIN, \
  377. G_LOG_LEVEL_WARNING, \
  378. format)
  379. #define g_info(format...) g_log (G_LOG_DOMAIN, \
  380. G_LOG_LEVEL_INFO, \
  381. format)
  382. #define g_debug(format...) g_log (G_LOG_DOMAIN, \
  383. G_LOG_LEVEL_DEBUG, \
  384. format)
  385. #endif
  386. #else /* no varargs macros */
  387. G_NORETURN static void g_error (const gchar *format, ...) G_ANALYZER_NORETURN;
  388. static void g_critical (const gchar *format, ...) G_ANALYZER_NORETURN;
  389. static inline void
  390. g_error (const gchar *format,
  391. ...)
  392. {
  393. va_list args;
  394. va_start (args, format);
  395. g_logv (G_LOG_DOMAIN, G_LOG_LEVEL_ERROR, format, args);
  396. va_end (args);
  397. for(;;) ;
  398. }
  399. static inline void
  400. g_message (const gchar *format,
  401. ...)
  402. {
  403. va_list args;
  404. va_start (args, format);
  405. g_logv (G_LOG_DOMAIN, G_LOG_LEVEL_MESSAGE, format, args);
  406. va_end (args);
  407. }
  408. static inline void
  409. g_critical (const gchar *format,
  410. ...)
  411. {
  412. va_list args;
  413. va_start (args, format);
  414. g_logv (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL, format, args);
  415. va_end (args);
  416. }
  417. static inline void
  418. g_warning (const gchar *format,
  419. ...)
  420. {
  421. va_list args;
  422. va_start (args, format);
  423. g_logv (G_LOG_DOMAIN, G_LOG_LEVEL_WARNING, format, args);
  424. va_end (args);
  425. }
  426. static inline void
  427. g_info (const gchar *format,
  428. ...)
  429. {
  430. va_list args;
  431. va_start (args, format);
  432. g_logv (G_LOG_DOMAIN, G_LOG_LEVEL_INFO, format, args);
  433. va_end (args);
  434. }
  435. static inline void
  436. g_debug (const gchar *format,
  437. ...)
  438. {
  439. va_list args;
  440. va_start (args, format);
  441. g_logv (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, format, args);
  442. va_end (args);
  443. }
  444. #endif /* !__GNUC__ */
  445. /**
  446. * g_warning_once:
  447. * @...: format string, followed by parameters to insert
  448. * into the format string (as with printf())
  449. *
  450. * Logs a warning only once.
  451. *
  452. * g_warning_once() calls g_warning() with the passed message the first time
  453. * the statement is executed; subsequent times it is a no-op.
  454. *
  455. * Note! On platforms where the compiler doesn't support variadic macros, the
  456. * warning is printed each time instead of only once.
  457. *
  458. * Since: 2.64
  459. */
  460. #if defined(G_HAVE_ISO_VARARGS) && !G_ANALYZER_ANALYZING
  461. #define g_warning_once(...) \
  462. G_STMT_START { \
  463. static int G_PASTE (_GWarningOnceBoolean, __LINE__) = 0; /* (atomic) */ \
  464. if (g_atomic_int_compare_and_exchange (&G_PASTE (_GWarningOnceBoolean, __LINE__), \
  465. 0, 1)) \
  466. g_warning (__VA_ARGS__); \
  467. } G_STMT_END \
  468. GLIB_AVAILABLE_MACRO_IN_2_64
  469. #elif defined(G_HAVE_GNUC_VARARGS) && !G_ANALYZER_ANALYZING
  470. #define g_warning_once(format...) \
  471. G_STMT_START { \
  472. static int G_PASTE (_GWarningOnceBoolean, __LINE__) = 0; /* (atomic) */ \
  473. if (g_atomic_int_compare_and_exchange (&G_PASTE (_GWarningOnceBoolean, __LINE__), \
  474. 0, 1)) \
  475. g_warning (format); \
  476. } G_STMT_END \
  477. GLIB_AVAILABLE_MACRO_IN_2_64
  478. #else
  479. #define g_warning_once g_warning
  480. #endif
  481. /**
  482. * GPrintFunc:
  483. * @string: the message to output
  484. *
  485. * Specifies the type of the print handler functions.
  486. * These are called with the complete formatted string to output.
  487. */
  488. typedef void (*GPrintFunc) (const gchar *string);
  489. GLIB_AVAILABLE_IN_ALL
  490. void g_print (const gchar *format,
  491. ...) G_GNUC_PRINTF (1, 2);
  492. GLIB_AVAILABLE_IN_ALL
  493. GPrintFunc g_set_print_handler (GPrintFunc func);
  494. GLIB_AVAILABLE_IN_ALL
  495. void g_printerr (const gchar *format,
  496. ...) G_GNUC_PRINTF (1, 2);
  497. GLIB_AVAILABLE_IN_ALL
  498. GPrintFunc g_set_printerr_handler (GPrintFunc func);
  499. /**
  500. * g_warn_if_reached:
  501. *
  502. * Logs a warning.
  503. *
  504. * Since: 2.16
  505. */
  506. #define g_warn_if_reached() \
  507. do { \
  508. g_warn_message (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, NULL); \
  509. } while (0)
  510. /**
  511. * g_warn_if_fail:
  512. * @expr: the expression to check
  513. *
  514. * Logs a warning if the expression is not true.
  515. *
  516. * Unlike g_return_if_fail(), the expression is always evaluated, even if
  517. * checks and assertions are disabled.
  518. *
  519. * Since: 2.16
  520. */
  521. #define g_warn_if_fail(expr) \
  522. do { \
  523. if G_LIKELY (expr) ; \
  524. else g_warn_message (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, #expr); \
  525. } while (0)
  526. #ifdef G_DISABLE_CHECKS
  527. /**
  528. * g_return_if_fail:
  529. * @expr: the expression to check
  530. *
  531. * Verifies that the expression @expr, usually representing a precondition,
  532. * evaluates to %TRUE. If the function returns a value, use
  533. * g_return_val_if_fail() instead.
  534. *
  535. * If @expr evaluates to %FALSE, the current function should be considered to
  536. * have undefined behaviour (a programmer error). The only correct solution
  537. * to such an error is to change the module that is calling the current
  538. * function, so that it avoids this incorrect call.
  539. *
  540. * To make this undefined behaviour visible, if @expr evaluates to %FALSE,
  541. * the result is usually that a critical message is logged and the current
  542. * function returns.
  543. *
  544. * If `G_DISABLE_CHECKS` is defined then the check is not performed. You
  545. * should therefore not depend on any side effects of @expr.
  546. *
  547. * To debug failure of a g_return_if_fail() check, run the code under a debugger
  548. * with `G_DEBUG=fatal-criticals` or `G_DEBUG=fatal-warnings` defined in the
  549. * environment (see [Running GLib Applications](glib-running.html)):
  550. *
  551. * |[
  552. * G_DEBUG=fatal-warnings gdb ./my-program
  553. * ]|
  554. *
  555. * Any unrelated failures can be skipped over in
  556. * [gdb](https://www.gnu.org/software/gdb/) using the `continue` command.
  557. */
  558. #define g_return_if_fail(expr) G_STMT_START{ (void)0; }G_STMT_END
  559. /**
  560. * g_return_val_if_fail:
  561. * @expr: the expression to check
  562. * @val: the value to return from the current function
  563. * if the expression is not true
  564. *
  565. * Verifies that the expression @expr, usually representing a precondition,
  566. * evaluates to %TRUE. If the function does not return a value, use
  567. * g_return_if_fail() instead.
  568. *
  569. * If @expr evaluates to %FALSE, the current function should be considered to
  570. * have undefined behaviour (a programmer error). The only correct solution
  571. * to such an error is to change the module that is calling the current
  572. * function, so that it avoids this incorrect call.
  573. *
  574. * To make this undefined behaviour visible, if @expr evaluates to %FALSE,
  575. * the result is usually that a critical message is logged and @val is
  576. * returned from the current function.
  577. *
  578. * If `G_DISABLE_CHECKS` is defined then the check is not performed. You
  579. * should therefore not depend on any side effects of @expr.
  580. *
  581. * See g_return_if_fail() for guidance on how to debug failure of this check.
  582. */
  583. #define g_return_val_if_fail(expr,val) G_STMT_START{ (void)0; }G_STMT_END
  584. /**
  585. * g_return_if_reached:
  586. *
  587. * Logs a critical message and returns from the current function.
  588. * This can only be used in functions which do not return a value.
  589. *
  590. * See g_return_if_fail() for guidance on how to debug failure of this check.
  591. */
  592. #define g_return_if_reached() G_STMT_START{ return; }G_STMT_END
  593. /**
  594. * g_return_val_if_reached:
  595. * @val: the value to return from the current function
  596. *
  597. * Logs a critical message and returns @val.
  598. *
  599. * See g_return_if_fail() for guidance on how to debug failure of this check.
  600. */
  601. #define g_return_val_if_reached(val) G_STMT_START{ return (val); }G_STMT_END
  602. #else /* !G_DISABLE_CHECKS */
  603. #define g_return_if_fail(expr) \
  604. G_STMT_START { \
  605. if (G_LIKELY (expr)) \
  606. { } \
  607. else \
  608. { \
  609. g_return_if_fail_warning (G_LOG_DOMAIN, \
  610. G_STRFUNC, \
  611. #expr); \
  612. return; \
  613. } \
  614. } G_STMT_END
  615. #define g_return_val_if_fail(expr, val) \
  616. G_STMT_START { \
  617. if (G_LIKELY (expr)) \
  618. { } \
  619. else \
  620. { \
  621. g_return_if_fail_warning (G_LOG_DOMAIN, \
  622. G_STRFUNC, \
  623. #expr); \
  624. return (val); \
  625. } \
  626. } G_STMT_END
  627. #define g_return_if_reached() \
  628. G_STMT_START { \
  629. g_log (G_LOG_DOMAIN, \
  630. G_LOG_LEVEL_CRITICAL, \
  631. "file %s: line %d (%s): should not be reached", \
  632. __FILE__, \
  633. __LINE__, \
  634. G_STRFUNC); \
  635. return; \
  636. } G_STMT_END
  637. #define g_return_val_if_reached(val) \
  638. G_STMT_START { \
  639. g_log (G_LOG_DOMAIN, \
  640. G_LOG_LEVEL_CRITICAL, \
  641. "file %s: line %d (%s): should not be reached", \
  642. __FILE__, \
  643. __LINE__, \
  644. G_STRFUNC); \
  645. return (val); \
  646. } G_STMT_END
  647. #endif /* !G_DISABLE_CHECKS */
  648. G_END_DECLS
  649. #endif /* __G_MESSAGES_H__ */