deprecation.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. /* Copyright (C) 2001, 2006, 2010, 2011, 2012 Free Software Foundation, Inc.
  2. *
  3. * This library is free software; you can redistribute it and/or
  4. * modify it under the terms of the GNU Lesser General Public License
  5. * as published by the Free Software Foundation; either version 3 of
  6. * the License, or (at your option) any later version.
  7. *
  8. * This library is distributed in the hope that it will be useful, but
  9. * WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. * Lesser General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU Lesser General Public
  14. * License along with this library; if not, write to the Free Software
  15. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  16. * 02110-1301 USA
  17. */
  18. #ifdef HAVE_CONFIG_H
  19. # include <config.h>
  20. #endif
  21. #include <stdio.h>
  22. #include <string.h>
  23. #include <stdarg.h>
  24. #include "libguile/_scm.h"
  25. #include "libguile/deprecation.h"
  26. #include "libguile/strings.h"
  27. #include "libguile/ports.h"
  28. #include "libguile/private-options.h"
  29. /* Windows defines. */
  30. #ifdef __MINGW32__
  31. #define vsnprintf _vsnprintf
  32. #endif
  33. struct issued_warning {
  34. struct issued_warning *prev;
  35. const char *message;
  36. };
  37. static scm_i_pthread_mutex_t warn_lock = SCM_I_PTHREAD_MUTEX_INITIALIZER;
  38. static struct issued_warning *issued_warnings;
  39. static int print_summary = 0;
  40. void
  41. scm_c_issue_deprecation_warning (const char *msg)
  42. {
  43. if (!SCM_WARN_DEPRECATED)
  44. print_summary = 1;
  45. else
  46. {
  47. struct issued_warning *iw;
  48. scm_i_pthread_mutex_lock (&warn_lock);
  49. for (iw = issued_warnings; iw; iw = iw->prev)
  50. if (!strcmp (iw->message, msg))
  51. {
  52. msg = NULL;
  53. break;
  54. }
  55. if (msg)
  56. {
  57. msg = strdup (msg);
  58. iw = malloc (sizeof (struct issued_warning));
  59. if (msg == NULL || iw == NULL)
  60. /* Nothing sensible to do if you can't allocate this small
  61. amount of memory. */
  62. abort ();
  63. iw->message = msg;
  64. iw->prev = issued_warnings;
  65. issued_warnings = iw;
  66. }
  67. scm_i_pthread_mutex_unlock (&warn_lock);
  68. /* All this dance is to avoid printing to a port inside a mutex,
  69. which could recurse and deadlock. */
  70. if (msg)
  71. {
  72. if (scm_gc_running_p)
  73. fprintf (stderr, "%s\n", msg);
  74. else
  75. {
  76. scm_puts_unlocked (msg, scm_current_warning_port ());
  77. scm_newline (scm_current_warning_port ());
  78. }
  79. }
  80. }
  81. }
  82. void
  83. scm_c_issue_deprecation_warning_fmt (const char *msg, ...)
  84. {
  85. va_list ap;
  86. char buf[512];
  87. va_start (ap, msg);
  88. vsnprintf (buf, 511, msg, ap);
  89. va_end (ap);
  90. buf[511] = '\0';
  91. scm_c_issue_deprecation_warning (buf);
  92. }
  93. SCM_DEFINE(scm_issue_deprecation_warning,
  94. "issue-deprecation-warning", 0, 0, 1,
  95. (SCM msgs),
  96. "Output @var{msgs} to @code{(current-error-port)} when this "
  97. "is the first call to @code{issue-deprecation-warning} with "
  98. "this specific @var{msgs}. Do nothing otherwise. "
  99. "The argument @var{msgs} should be a list of strings; "
  100. "they are printed in turn, each one followed by a newline.")
  101. #define FUNC_NAME s_scm_issue_deprecation_warning
  102. {
  103. if (!SCM_WARN_DEPRECATED)
  104. print_summary = 1;
  105. else
  106. {
  107. SCM nl = scm_from_locale_string ("\n");
  108. SCM msgs_nl = SCM_EOL;
  109. char *c_msgs;
  110. while (scm_is_pair (msgs))
  111. {
  112. if (!scm_is_null (msgs_nl))
  113. msgs_nl = scm_cons (nl, msgs_nl);
  114. msgs_nl = scm_cons (SCM_CAR (msgs), msgs_nl);
  115. msgs = SCM_CDR (msgs);
  116. }
  117. msgs_nl = scm_string_append (scm_reverse_x (msgs_nl, SCM_EOL));
  118. c_msgs = scm_to_locale_string (msgs_nl);
  119. scm_c_issue_deprecation_warning (c_msgs);
  120. free (c_msgs);
  121. }
  122. return SCM_UNSPECIFIED;
  123. }
  124. #undef FUNC_NAME
  125. static void
  126. print_deprecation_summary (void)
  127. {
  128. if (print_summary)
  129. {
  130. fputs ("\n"
  131. "Some deprecated features have been used. Set the environment\n"
  132. "variable GUILE_WARN_DEPRECATED to \"detailed\" and rerun the\n"
  133. "program to get more information. Set it to \"no\" to suppress\n"
  134. "this message.\n", stderr);
  135. }
  136. }
  137. SCM_DEFINE(scm_include_deprecated_features,
  138. "include-deprecated-features", 0, 0, 0,
  139. (),
  140. "Return @code{#t} iff deprecated features should be included "
  141. "in public interfaces.")
  142. #define FUNC_NAME s_scm_include_deprecated_features
  143. {
  144. return scm_from_bool (SCM_ENABLE_DEPRECATED == 1);
  145. }
  146. #undef FUNC_NAME
  147. void
  148. scm_init_deprecation ()
  149. {
  150. const char *level = getenv ("GUILE_WARN_DEPRECATED");
  151. if (level == NULL)
  152. level = SCM_WARN_DEPRECATED_DEFAULT;
  153. if (!strcmp (level, "detailed"))
  154. SCM_WARN_DEPRECATED = 1;
  155. else if (!strcmp (level, "no"))
  156. SCM_WARN_DEPRECATED = 0;
  157. else
  158. {
  159. SCM_WARN_DEPRECATED = 0;
  160. atexit (print_deprecation_summary);
  161. }
  162. scm_c_atfork_lock_static_mutex (&warn_lock);
  163. #include "libguile/deprecation.x"
  164. }
  165. /*
  166. Local Variables:
  167. c-file-style: "gnu"
  168. End:
  169. */