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