tufte layout files:
[lyx.git] / intl / log.c
blobe3ab5d0e66b27b1c0cea178fff1ccf9803722786
1 /* Log file output.
2 Copyright (C) 2003, 2005 Free Software Foundation, Inc.
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.
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.
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. */
19 /* Written by Bruno Haible <bruno@clisp.org>. */
21 #ifdef HAVE_CONFIG_H
22 # include <config.h>
23 #endif
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
29 /* Handle multi-threaded applications. */
30 #ifdef _LIBC
31 # include <bits/libc-lock.h>
32 #else
33 # include "lock.h"
34 #endif
36 /* Print an ASCII string with quotes and escape sequences where needed. */
37 static void
38 print_escaped (FILE *stream, const char *str)
40 putc ('"', stream);
41 for (; *str != '\0'; str++)
42 if (*str == '\n')
44 fputs ("\\n\"", stream);
45 if (str[1] == '\0')
46 return;
47 fputs ("\n\"", stream);
49 else
51 if (*str == '"' || *str == '\\')
52 putc ('\\', stream);
53 putc (*str, stream);
55 putc ('"', stream);
58 static char *last_logfilename = NULL;
59 static FILE *last_logfile = NULL;
60 __libc_lock_define_initialized (static, lock)
62 static inline void
63 _nl_log_untranslated_locked (const char *logfilename, const char *domainname,
64 const char *msgid1, const char *msgid2, int plural)
66 FILE *logfile;
68 /* Can we reuse the last opened logfile? */
69 if (last_logfilename == NULL || strcmp (logfilename, last_logfilename) != 0)
71 /* Close the last used logfile. */
72 if (last_logfilename != NULL)
74 if (last_logfile != NULL)
76 fclose (last_logfile);
77 last_logfile = NULL;
79 free (last_logfilename);
80 last_logfilename = NULL;
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;
91 logfile = last_logfile;
93 fprintf (logfile, "domain ");
94 print_escaped (logfile, domainname);
95 fprintf (logfile, "\nmsgid ");
96 print_escaped (logfile, msgid1);
97 if (plural)
99 fprintf (logfile, "\nmsgid_plural ");
100 print_escaped (logfile, msgid2);
101 fprintf (logfile, "\nmsgstr[0] \"\"\n");
103 else
104 fprintf (logfile, "\nmsgstr \"\"\n");
105 putc ('\n', logfile);
108 /* Add to the log file an entry denoting a failed translation. */
109 void
110 _nl_log_untranslated (const char *logfilename, const char *domainname,
111 const char *msgid1, const char *msgid2, int plural)
113 __libc_lock_lock (lock);
114 _nl_log_untranslated_locked (logfilename, domainname, msgid1, msgid2, plural);
115 __libc_lock_unlock (lock);