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