log.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /* Log file output.
  2. Copyright (C) 2003 Free Software Foundation, Inc.
  3. This program is free software; you can redistribute it and/or modify it
  4. under the terms of the GNU Library General Public License as published
  5. by the Free Software Foundation; either version 2, or (at your option)
  6. any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  10. Library General Public License for more details.
  11. You should have received a copy of the GNU Library General Public
  12. License along with this program; if not, write to the Free Software
  13. Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301,
  14. USA. */
  15. /* Written by Bruno Haible <bruno@clisp.org>. */
  16. #ifdef HAVE_CONFIG_H
  17. # include <config.h>
  18. #endif
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21. #include <string.h>
  22. /* Print an ASCII string with quotes and escape sequences where needed. */
  23. static void
  24. print_escaped (stream, str)
  25. FILE *stream;
  26. const char *str;
  27. {
  28. putc ('"', stream);
  29. for (; *str != '\0'; str++)
  30. if (*str == '\n')
  31. {
  32. fputs ("\\n\"", stream);
  33. if (str[1] == '\0')
  34. return;
  35. fputs ("\n\"", stream);
  36. }
  37. else
  38. {
  39. if (*str == '"' || *str == '\\')
  40. putc ('\\', stream);
  41. putc (*str, stream);
  42. }
  43. putc ('"', stream);
  44. }
  45. /* Add to the log file an entry denoting a failed translation. */
  46. void
  47. _nl_log_untranslated (logfilename, domainname, msgid1, msgid2, plural)
  48. const char *logfilename;
  49. const char *domainname;
  50. const char *msgid1;
  51. const char *msgid2;
  52. int plural;
  53. {
  54. static char *last_logfilename = NULL;
  55. static FILE *last_logfile = NULL;
  56. FILE *logfile;
  57. /* Can we reuse the last opened logfile? */
  58. if (last_logfilename == NULL || strcmp (logfilename, last_logfilename) != 0)
  59. {
  60. /* Close the last used logfile. */
  61. if (last_logfilename != NULL)
  62. {
  63. if (last_logfile != NULL)
  64. {
  65. fclose (last_logfile);
  66. last_logfile = NULL;
  67. }
  68. free (last_logfilename);
  69. last_logfilename = NULL;
  70. }
  71. /* Open the logfile. */
  72. last_logfilename = (char *) malloc (strlen (logfilename) + 1);
  73. if (last_logfilename == NULL)
  74. return;
  75. strcpy (last_logfilename, logfilename);
  76. last_logfile = fopen (logfilename, "a");
  77. if (last_logfile == NULL)
  78. return;
  79. }
  80. logfile = last_logfile;
  81. fprintf (logfile, "domain ");
  82. print_escaped (logfile, domainname);
  83. fprintf (logfile, "\nmsgid ");
  84. print_escaped (logfile, msgid1);
  85. if (plural)
  86. {
  87. fprintf (logfile, "\nmsgid_plural ");
  88. print_escaped (logfile, msgid2);
  89. fprintf (logfile, "\nmsgstr[0] \"\"\n");
  90. }
  91. else
  92. fprintf (logfile, "\nmsgstr \"\"\n");
  93. putc ('\n', logfile);
  94. }