]> git.deb.at Git - pkg/abook.git/blob - intl/log.c
a3e4b27263e9573791c0ebf698555d8e8e619959
[pkg/abook.git] / intl / log.c
1 /* Log file output.
2    Copyright (C) 2003, 2005, 2009 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 /* Separator between msgctxt and msgid in .mo files.  */
37 #define MSGCTXT_SEPARATOR '\004'  /* EOT */
38
39 /* Print an ASCII string with quotes and escape sequences where needed.  */
40 static void
41 print_escaped (FILE *stream, const char *str, const char *str_end)
42 {
43   putc ('"', stream);
44   for (; str != str_end; str++)
45     if (*str == '\n')
46       {
47         fputs ("\\n\"", stream);
48         if (str + 1 == str_end)
49           return;
50         fputs ("\n\"", stream);
51       }
52     else
53       {
54         if (*str == '"' || *str == '\\')
55           putc ('\\', stream);
56         putc (*str, stream);
57       }
58   putc ('"', stream);
59 }
60
61 static char *last_logfilename = NULL;
62 static FILE *last_logfile = NULL;
63 __libc_lock_define_initialized (static, lock)
64
65 static inline void
66 _nl_log_untranslated_locked (const char *logfilename, const char *domainname,
67                              const char *msgid1, const char *msgid2, int plural)
68 {
69   FILE *logfile;
70   const char *separator;
71
72   /* Can we reuse the last opened logfile?  */
73   if (last_logfilename == NULL || strcmp (logfilename, last_logfilename) != 0)
74     {
75       /* Close the last used logfile.  */
76       if (last_logfilename != NULL)
77         {
78           if (last_logfile != NULL)
79             {
80               fclose (last_logfile);
81               last_logfile = NULL;
82             }
83           free (last_logfilename);
84           last_logfilename = NULL;
85         }
86       /* Open the logfile.  */
87       last_logfilename = (char *) malloc (strlen (logfilename) + 1);
88       if (last_logfilename == NULL)
89         return;
90       strcpy (last_logfilename, logfilename);
91       last_logfile = fopen (logfilename, "a");
92       if (last_logfile == NULL)
93         return;
94     }
95   logfile = last_logfile;
96
97   fprintf (logfile, "domain ");
98   print_escaped (logfile, domainname, domainname + strlen (domainname));
99   separator = strchr (msgid1, MSGCTXT_SEPARATOR);
100   if (separator != NULL)
101     {
102       /* The part before the MSGCTXT_SEPARATOR is the msgctxt.  */
103       fprintf (logfile, "\nmsgctxt ");
104       print_escaped (logfile, msgid1, separator);
105       msgid1 = separator + 1;
106     }
107   fprintf (logfile, "\nmsgid ");
108   print_escaped (logfile, msgid1, msgid1 + strlen (msgid1));
109   if (plural)
110     {
111       fprintf (logfile, "\nmsgid_plural ");
112       print_escaped (logfile, msgid2, msgid2 + strlen (msgid2));
113       fprintf (logfile, "\nmsgstr[0] \"\"\n");
114     }
115   else
116     fprintf (logfile, "\nmsgstr \"\"\n");
117   putc ('\n', logfile);
118 }
119
120 /* Add to the log file an entry denoting a failed translation.  */
121 void
122 _nl_log_untranslated (const char *logfilename, const char *domainname,
123                       const char *msgid1, const char *msgid2, int plural)
124 {
125   __libc_lock_lock (lock);
126   _nl_log_untranslated_locked (logfilename, domainname, msgid1, msgid2, plural);
127   __libc_lock_unlock (lock);
128 }