1 /* Log file output.
2 Copyright (C) 2003, 2005 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
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 /* Handle multi-threaded applications. */
30 #ifdef _LIBC
31 # include <bits/libc-lock.h>
32 #else
33 # include "lock.h"
34 #endif
35
36 /* Print an ASCII string with quotes and escape sequences where needed. */
37 static void
print_escaped(FILE * stream,const char * str)38 print_escaped (FILE *stream, const char *str)
39 {
40 putc ('"', stream);
41 for (; *str != '\0'; str++)
42 if (*str == '\n')
43 {
44 fputs ("\\n\"", stream);
45 if (str[1] == '\0')
46 return;
47 fputs ("\n\"", stream);
48 }
49 else
50 {
51 if (*str == '"' || *str == '\\')
52 putc ('\\', stream);
53 putc (*str, stream);
54 }
55 putc ('"', stream);
56 }
57
58 static char *last_logfilename = NULL;
59 static FILE *last_logfile = NULL;
__libc_lock_define_initialized(static,lock)60 __libc_lock_define_initialized (static, lock)
61
62 static inline void
63 _nl_log_untranslated_locked (const char *logfilename, const char *domainname,
64 const char *msgid1, const char *msgid2, int plural)
65 {
66 FILE *logfile;
67
68 /* Can we reuse the last opened logfile? */
69 if (last_logfilename == NULL || strcmp (logfilename, last_logfilename) != 0)
70 {
71 /* Close the last used logfile. */
72 if (last_logfilename != NULL)
73 {
74 if (last_logfile != NULL)
75 {
76 fclose (last_logfile);
77 last_logfile = NULL;
78 }
79 free (last_logfilename);
80 last_logfilename = NULL;
81 }
82 /* Open the logfile. */
83 last_logfilename = (char *) malloc (strlen (logfilename) + 1);
84 if (last_logfilename == NULL)
85 return;
86 strcpy (last_logfilename, logfilename);
87 last_logfile = fopen (logfilename, "a");
88 if (last_logfile == NULL)
89 return;
90 }
91 logfile = last_logfile;
92
93 fprintf (logfile, "domain ");
94 print_escaped (logfile, domainname);
95 fprintf (logfile, "\nmsgid ");
96 print_escaped (logfile, msgid1);
97 if (plural)
98 {
99 fprintf (logfile, "\nmsgid_plural ");
100 print_escaped (logfile, msgid2);
101 fprintf (logfile, "\nmsgstr[0] \"\"\n");
102 }
103 else
104 fprintf (logfile, "\nmsgstr \"\"\n");
105 putc ('\n', logfile);
106 }
107
108 /* Add to the log file an entry denoting a failed translation. */
109 void
_nl_log_untranslated(const char * logfilename,const char * domainname,const char * msgid1,const char * msgid2,int plural)110 _nl_log_untranslated (const char *logfilename, const char *domainname,
111 const char *msgid1, const char *msgid2, int plural)
112 {
113 __libc_lock_lock (lock);
114 _nl_log_untranslated_locked (logfilename, domainname, msgid1, msgid2, plural);
115 __libc_lock_unlock (lock);
116 }
117