您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 
 
 
 

344 行
12 KiB

  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, write to the
  16. * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  17. * Boston, MA 02111-1307, USA.
  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. #if defined(G_DISABLE_SINGLE_INCLUDES) && !defined (__GLIB_H_INSIDE__) && !defined (GLIB_COMPILATION)
  26. #error "Only <glib.h> can be included directly."
  27. #endif
  28. #ifndef __G_MESSAGES_H__
  29. #define __G_MESSAGES_H__
  30. #include <stdarg.h>
  31. #include <glib/gtypes.h>
  32. #include <glib/gmacros.h>
  33. /* Suppress warnings when GCC is in -pedantic mode and not -std=c99
  34. */
  35. #if (__GNUC__ >= 3 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 96))
  36. #pragma GCC system_header
  37. #endif
  38. G_BEGIN_DECLS
  39. /* calculate a string size, guaranteed to fit format + args.
  40. */
  41. gsize g_printf_string_upper_bound (const gchar* format,
  42. va_list args);
  43. /* Log level shift offset for user defined
  44. * log levels (0-7 are used by GLib).
  45. */
  46. #define G_LOG_LEVEL_USER_SHIFT (8)
  47. /* Glib log levels and flags.
  48. */
  49. typedef enum
  50. {
  51. /* log flags */
  52. G_LOG_FLAG_RECURSION = 1 << 0,
  53. G_LOG_FLAG_FATAL = 1 << 1,
  54. /* GLib log levels */
  55. G_LOG_LEVEL_ERROR = 1 << 2, /* always fatal */
  56. G_LOG_LEVEL_CRITICAL = 1 << 3,
  57. G_LOG_LEVEL_WARNING = 1 << 4,
  58. G_LOG_LEVEL_MESSAGE = 1 << 5,
  59. G_LOG_LEVEL_INFO = 1 << 6,
  60. G_LOG_LEVEL_DEBUG = 1 << 7,
  61. G_LOG_LEVEL_MASK = ~(G_LOG_FLAG_RECURSION | G_LOG_FLAG_FATAL)
  62. } GLogLevelFlags;
  63. /* GLib log levels that are considered fatal by default */
  64. #define G_LOG_FATAL_MASK (G_LOG_FLAG_RECURSION | G_LOG_LEVEL_ERROR)
  65. typedef void (*GLogFunc) (const gchar *log_domain,
  66. GLogLevelFlags log_level,
  67. const gchar *message,
  68. gpointer user_data);
  69. /* Logging mechanism
  70. */
  71. guint g_log_set_handler (const gchar *log_domain,
  72. GLogLevelFlags log_levels,
  73. GLogFunc log_func,
  74. gpointer user_data);
  75. void g_log_remove_handler (const gchar *log_domain,
  76. guint handler_id);
  77. void g_log_default_handler (const gchar *log_domain,
  78. GLogLevelFlags log_level,
  79. const gchar *message,
  80. gpointer unused_data);
  81. GLogFunc g_log_set_default_handler (GLogFunc log_func,
  82. gpointer user_data);
  83. void g_log (const gchar *log_domain,
  84. GLogLevelFlags log_level,
  85. const gchar *format,
  86. ...) G_GNUC_PRINTF (3, 4);
  87. void g_logv (const gchar *log_domain,
  88. GLogLevelFlags log_level,
  89. const gchar *format,
  90. va_list args);
  91. GLogLevelFlags g_log_set_fatal_mask (const gchar *log_domain,
  92. GLogLevelFlags fatal_mask);
  93. GLogLevelFlags g_log_set_always_fatal (GLogLevelFlags fatal_mask);
  94. /* internal */
  95. G_GNUC_INTERNAL void _g_log_fallback_handler (const gchar *log_domain,
  96. GLogLevelFlags log_level,
  97. const gchar *message,
  98. gpointer unused_data);
  99. /* Internal functions, used to implement the following macros */
  100. void g_return_if_fail_warning (const char *log_domain,
  101. const char *pretty_function,
  102. const char *expression);
  103. void g_warn_message (const char *domain,
  104. const char *file,
  105. int line,
  106. const char *func,
  107. const char *warnexpr);
  108. #ifndef G_DISABLE_DEPRECATED
  109. void g_assert_warning (const char *log_domain,
  110. const char *file,
  111. const int line,
  112. const char *pretty_function,
  113. const char *expression) G_GNUC_NORETURN;
  114. #endif /* !G_DISABLE_DEPRECATED */
  115. #ifndef G_LOG_DOMAIN
  116. #define G_LOG_DOMAIN ((gchar*) 0)
  117. #endif /* G_LOG_DOMAIN */
  118. #ifdef G_HAVE_ISO_VARARGS
  119. /* for(;;) ; so that GCC knows that control doesn't go past g_error().
  120. * Put space before ending semicolon to avoid C++ build warnings.
  121. */
  122. #define g_error(...) G_STMT_START { \
  123. g_log (G_LOG_DOMAIN, \
  124. G_LOG_LEVEL_ERROR, \
  125. __VA_ARGS__); \
  126. for (;;) ; \
  127. } G_STMT_END
  128. #define g_message(...) g_log (G_LOG_DOMAIN, \
  129. G_LOG_LEVEL_MESSAGE, \
  130. __VA_ARGS__)
  131. #define g_critical(...) g_log (G_LOG_DOMAIN, \
  132. G_LOG_LEVEL_CRITICAL, \
  133. __VA_ARGS__)
  134. #define g_warning(...) g_log (G_LOG_DOMAIN, \
  135. G_LOG_LEVEL_WARNING, \
  136. __VA_ARGS__)
  137. #define g_debug(...) g_log (G_LOG_DOMAIN, \
  138. G_LOG_LEVEL_DEBUG, \
  139. __VA_ARGS__)
  140. #elif defined(G_HAVE_GNUC_VARARGS)
  141. #define g_error(format...) G_STMT_START { \
  142. g_log (G_LOG_DOMAIN, \
  143. G_LOG_LEVEL_ERROR, \
  144. format); \
  145. for (;;) ; \
  146. } G_STMT_END
  147. #define g_message(format...) g_log (G_LOG_DOMAIN, \
  148. G_LOG_LEVEL_MESSAGE, \
  149. format)
  150. #define g_critical(format...) g_log (G_LOG_DOMAIN, \
  151. G_LOG_LEVEL_CRITICAL, \
  152. format)
  153. #define g_warning(format...) g_log (G_LOG_DOMAIN, \
  154. G_LOG_LEVEL_WARNING, \
  155. format)
  156. #define g_debug(format...) g_log (G_LOG_DOMAIN, \
  157. G_LOG_LEVEL_DEBUG, \
  158. format)
  159. #else /* no varargs macros */
  160. static void
  161. g_error (const gchar *format,
  162. ...)
  163. {
  164. va_list args;
  165. va_start (args, format);
  166. g_logv (G_LOG_DOMAIN, G_LOG_LEVEL_ERROR, format, args);
  167. va_end (args);
  168. for(;;) ;
  169. }
  170. static void
  171. g_message (const gchar *format,
  172. ...)
  173. {
  174. va_list args;
  175. va_start (args, format);
  176. g_logv (G_LOG_DOMAIN, G_LOG_LEVEL_MESSAGE, format, args);
  177. va_end (args);
  178. }
  179. static void
  180. g_critical (const gchar *format,
  181. ...)
  182. {
  183. va_list args;
  184. va_start (args, format);
  185. g_logv (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL, format, args);
  186. va_end (args);
  187. }
  188. static void
  189. g_warning (const gchar *format,
  190. ...)
  191. {
  192. va_list args;
  193. va_start (args, format);
  194. g_logv (G_LOG_DOMAIN, G_LOG_LEVEL_WARNING, format, args);
  195. va_end (args);
  196. }
  197. static void
  198. g_debug (const gchar *format,
  199. ...)
  200. {
  201. va_list args;
  202. va_start (args, format);
  203. g_logv (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, format, args);
  204. va_end (args);
  205. }
  206. #endif /* !__GNUC__ */
  207. typedef void (*GPrintFunc) (const gchar *string);
  208. void g_print (const gchar *format,
  209. ...) G_GNUC_PRINTF (1, 2);
  210. GPrintFunc g_set_print_handler (GPrintFunc func);
  211. void g_printerr (const gchar *format,
  212. ...) G_GNUC_PRINTF (1, 2);
  213. GPrintFunc g_set_printerr_handler (GPrintFunc func);
  214. /* Provide macros for graceful error handling.
  215. * The "return" macros will return from the current function.
  216. * Two different definitions are given for the macros in
  217. * order to support gcc's __PRETTY_FUNCTION__ capability.
  218. */
  219. #define g_warn_if_reached() do { g_warn_message (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, NULL); } while (0)
  220. #define g_warn_if_fail(expr) do { if G_LIKELY (expr) ; else \
  221. g_warn_message (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, #expr); } while (0)
  222. #ifdef G_DISABLE_CHECKS
  223. #define g_return_if_fail(expr) G_STMT_START{ (void)0; }G_STMT_END
  224. #define g_return_val_if_fail(expr,val) G_STMT_START{ (void)0; }G_STMT_END
  225. #define g_return_if_reached() G_STMT_START{ return; }G_STMT_END
  226. #define g_return_val_if_reached(val) G_STMT_START{ return (val); }G_STMT_END
  227. #else /* !G_DISABLE_CHECKS */
  228. #ifdef __GNUC__
  229. #define g_return_if_fail(expr) G_STMT_START{ \
  230. if G_LIKELY(expr) { } else \
  231. { \
  232. g_return_if_fail_warning (G_LOG_DOMAIN, \
  233. __PRETTY_FUNCTION__, \
  234. #expr); \
  235. return; \
  236. }; }G_STMT_END
  237. #define g_return_val_if_fail(expr,val) G_STMT_START{ \
  238. if G_LIKELY(expr) { } else \
  239. { \
  240. g_return_if_fail_warning (G_LOG_DOMAIN, \
  241. __PRETTY_FUNCTION__, \
  242. #expr); \
  243. return (val); \
  244. }; }G_STMT_END
  245. #define g_return_if_reached() G_STMT_START{ \
  246. g_log (G_LOG_DOMAIN, \
  247. G_LOG_LEVEL_CRITICAL, \
  248. "file %s: line %d (%s): should not be reached", \
  249. __FILE__, \
  250. __LINE__, \
  251. __PRETTY_FUNCTION__); \
  252. return; }G_STMT_END
  253. #define g_return_val_if_reached(val) G_STMT_START{ \
  254. g_log (G_LOG_DOMAIN, \
  255. G_LOG_LEVEL_CRITICAL, \
  256. "file %s: line %d (%s): should not be reached", \
  257. __FILE__, \
  258. __LINE__, \
  259. __PRETTY_FUNCTION__); \
  260. return (val); }G_STMT_END
  261. #else /* !__GNUC__ */
  262. #define g_return_if_fail(expr) G_STMT_START{ \
  263. if (expr) { } else \
  264. { \
  265. g_log (G_LOG_DOMAIN, \
  266. G_LOG_LEVEL_CRITICAL, \
  267. "file %s: line %d: assertion `%s' failed", \
  268. __FILE__, \
  269. __LINE__, \
  270. #expr); \
  271. return; \
  272. }; }G_STMT_END
  273. #define g_return_val_if_fail(expr, val) G_STMT_START{ \
  274. if (expr) { } else \
  275. { \
  276. g_log (G_LOG_DOMAIN, \
  277. G_LOG_LEVEL_CRITICAL, \
  278. "file %s: line %d: assertion `%s' failed", \
  279. __FILE__, \
  280. __LINE__, \
  281. #expr); \
  282. return (val); \
  283. }; }G_STMT_END
  284. #define g_return_if_reached() G_STMT_START{ \
  285. g_log (G_LOG_DOMAIN, \
  286. G_LOG_LEVEL_CRITICAL, \
  287. "file %s: line %d: should not be reached", \
  288. __FILE__, \
  289. __LINE__); \
  290. return; }G_STMT_END
  291. #define g_return_val_if_reached(val) G_STMT_START{ \
  292. g_log (G_LOG_DOMAIN, \
  293. G_LOG_LEVEL_CRITICAL, \
  294. "file %s: line %d: should not be reached", \
  295. __FILE__, \
  296. __LINE__); \
  297. return (val); }G_STMT_END
  298. #endif /* !__GNUC__ */
  299. #endif /* !G_DISABLE_CHECKS */
  300. G_END_DECLS
  301. #endif /* __G_MESSAGES_H__ */