Updated po files using make update-po
[geda-gaf/peter-b.git] / libgeda / src / s_log.c
bloba4746ad279f9bbc0d6870d1932c0f68d8a4bfc8d
1 /* gEDA - GPL Electronic Design Automation
2 * libgeda - gEDA's library
3 * Copyright (C) 1998-2007 Ales Hvezda
4 * Copyright (C) 1998-2007 gEDA Contributors (see ChangeLog for details)
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
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
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111 USA
20 #include <config.h>
22 #include <stdio.h>
23 #include <sys/stat.h>
24 #ifdef HAVE_STDLIB_H
25 #include <stdlib.h>
26 #endif
27 #ifdef HAVE_ASSERT_H
28 #include <assert.h>
29 #endif
30 #ifdef HAVE_UNISTD_H
31 #include <unistd.h>
32 #endif
33 #ifdef HAVE_STDARG_H
34 #include <stdarg.h>
35 #endif
36 #ifdef HAVE_FCNTL_H
37 #include <fcntl.h>
38 #endif
39 #ifdef HAVE_ERRNO_H
40 #include <errno.h>
41 #endif
42 #ifdef HAVE_STRING_H
43 #include <string.h>
44 #endif
46 #include <gtk/gtk.h>
47 #include <libguile.h>
49 #include "defines.h"
50 #include "struct.h"
51 #include "defines.h"
52 #include "globals.h"
53 #include "funcs.h"
54 #include "o_types.h"
56 #include "../include/prototype.h"
58 #ifdef HAVE_LIBDMALLOC
59 #include <dmalloc.h>
60 #endif
63 static void s_log_handler (const gchar *log_domain,
64 GLogLevelFlags log_level,
65 const gchar *message,
66 gpointer user_data);
68 static int logfile_fd = -1;
70 static guint log_handler_id;
72 /*! \brief Initialize libgeda logging feature.
73 * \par Function Description
74 * This function opens the file <B>filename</B> to log to and registers the
75 * handler to redirect log message to this file.
77 * \param [in] filename Character string with file name to log to.
79 void s_log_init (const gchar *filename)
81 if (do_logging == FALSE) {
82 logfile_fd = -1;
83 return;
86 /* create log file */
87 logfile_fd = open (filename, O_RDWR|O_CREAT|O_TRUNC, 0600);
88 if (logfile_fd == -1) {
89 fprintf(stderr, "Could not open log file: %s\n", filename);
90 fprintf(stderr, "Errno was: %d\n", errno);
91 return;
94 /* install the log handler */
95 log_handler_id = g_log_set_handler (NULL,
96 G_LOG_LEVEL_MESSAGE,
97 s_log_handler,
98 NULL);
102 /*! \brief Terminates the logging of messages.
103 * \par Function Description
104 * This function deregisters the handler for redirection to the log file
105 * and closes it.
107 void s_log_close (void)
109 do_logging = FALSE; /* subsequent messages are lost after the close */
111 if (logfile_fd == -1)
113 return;
116 /* remove the handler */
117 g_log_remove_handler (NULL, log_handler_id);
119 /* close the file */
120 if (logfile_fd != -1) {
121 close (logfile_fd);
122 logfile_fd = -1;
127 /*! \brief Reads the current log file and returns its contents.
128 * \par Function Description
129 * This function reads the current log file and returns its contents.
131 * \return Character string with current log's contents.
134 gchar *s_log_read (void)
136 gboolean tmp;
137 #define BUFSIZE 200
138 gchar buf[BUFSIZE];
139 GString *contents;
140 gint len;
142 if (logfile_fd == -1) {
143 return NULL;
146 tmp = do_logging;
147 do_logging = FALSE;
149 /* rewind the file */
150 lseek(logfile_fd, 0, SEEK_SET);
152 /* read its contents and build a string */
153 contents = g_string_new ("");
154 while ((len = read (logfile_fd, &buf, BUFSIZE)) != 0) {
155 contents = g_string_append_len (contents, buf, len);
158 do_logging = tmp;
160 return g_string_free (contents, FALSE);
163 /*! \brief Write a message to the current log file.
164 * \par Function Description
165 * Writes <B>message</B> to the current log file whose file descriptor
166 * is <B>logfile_fd</B>.
168 * It also sends <B>message</B> to the optional function <B>x_log_update</B>
169 * for further use.
171 * \param [in] log_domain (unused).
172 * \param [in] log_level (unused).
173 * \param [in] message Character string containing message to
174 * write to log.
175 * \param [in] user_data (unused).
178 static void s_log_handler (const gchar *log_domain,
179 GLogLevelFlags log_level,
180 const gchar *message,
181 gpointer user_data)
183 int status;
185 if (do_logging == FALSE) {
186 return;
188 g_assert (logfile_fd != -1);
190 status = write (logfile_fd, message, strlen (message));
191 if (status == -1) {
192 fprintf(stderr, "Could not write message to log file\n");
193 fprintf(stderr, "Message was: %s\n", message);
194 fprintf(stderr, "Errno was: %d\n", errno);
197 if (x_log_update_func) {
198 (*x_log_update_func) (message);