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
56 #include "../include/prototype.h"
58 #ifdef HAVE_LIBDMALLOC
63 static void s_log_handler (const gchar
*log_domain
,
64 GLogLevelFlags log_level
,
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
) {
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
);
94 /* install the log handler */
95 log_handler_id
= g_log_set_handler (NULL
,
102 /*! \brief Terminates the logging of messages.
103 * \par Function Description
104 * This function deregisters the handler for redirection to the log file
107 void s_log_close (void)
109 do_logging
= FALSE
; /* subsequent messages are lost after the close */
111 if (logfile_fd
== -1)
116 /* remove the handler */
117 g_log_remove_handler (NULL
, log_handler_id
);
120 if (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)
142 if (logfile_fd
== -1) {
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
);
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>
171 * \param [in] log_domain (unused).
172 * \param [in] log_level (unused).
173 * \param [in] message Character string containing message to
175 * \param [in] user_data (unused).
178 static void s_log_handler (const gchar
*log_domain
,
179 GLogLevelFlags log_level
,
180 const gchar
*message
,
185 if (do_logging
== FALSE
) {
188 g_assert (logfile_fd
!= -1);
190 status
= write (logfile_fd
, message
, strlen (message
));
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
);