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