File: s_log.texi

package info (click to toggle)
libgeda 20050313-1
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 5,636 kB
  • ctags: 1,505
  • sloc: ansic: 19,449; sh: 8,627; makefile: 197; perl: 59
file content (388 lines) | stat: -rw-r--r-- 8,184 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
@c -*- mode: Noweb; noweb-doc-mode: texinfo-mode; noweb-code-mode: c-mode -*-

@node File s_log.c,,,Top
@chapter File @file{s_log.c}

@section File header

@format
@anchor{s_log.c - -root-}
@i{<<s_log.c : *>>=}@t{
@i{<<s_log.c : copyright and license -- @ref{s_log.c - copyright and license}>>}

/* DO NOT read or edit this file ! Use ../noweb/s_log.nw instead */

@i{<<s_log.c : include directives -- @ref{s_log.c - include directives}>>}

@i{<<s_log.c : global variables -- @ref{s_log.c - global variables}>>}


@i{<<s_log.c : s_log_init() -- @ref{s_log.c - s_log_init()}>>}
@i{<<s_log.c : s_log_close() -- @ref{s_log.c - s_log_close()}>>}

@i{<<s_log.c : s_log_read() -- @ref{s_log.c - s_log_read()}>>}

@i{<<s_log.c : s_log_handler() -- @ref{s_log.c - s_log_handler()}>>}

}
@end format




@format
@anchor{s_log.c - copyright and license}
@i{<<s_log.c : copyright and license>>=}@t{
/* gEDA - GPL Electronic Design Automation
 * libgeda - gEDA's library
 * Copyright (C) 1998-2000 Ales V. Hvezda
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111 USA
 */

}
@end format




@format
@anchor{s_log.c - include directives}
@i{<<s_log.c : include directives>>=}@t{
#include <config.h>

#include <stdio.h>
#include <sys/stat.h>
#ifdef HAVE_STDLIB_H
#include <stdlib.h>
#endif
#ifdef HAVE_ASSERT_H
#include <assert.h>
#endif
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#ifdef HAVE_STDARG_H
#include <stdarg.h>
#endif
#ifdef HAVE_FCNTL_H
#include <fcntl.h>
#endif
#ifdef HAVE_ERRNO_H
#include <errno.h>
#endif
#ifdef HAVE_STRING_H
#include <string.h>
#endif

#include <gtk/gtk.h>
#include <libguile.h>

#include "defines.h"
#include "struct.h"
#include "defines.h"
#include "globals.h"
#include "funcs.h"
#include "o_types.h"

#include "../include/prototype.h"

#ifdef HAVE_LIBDMALLOC
#include <dmalloc.h>
#endif


static void s_log_handler (const gchar *log_domain,
                           GLogLevelFlags log_level,
                           const gchar *message,
                           gpointer user_data);

@display
@r{Uses :
@t{s_log_handler},  -- @ref{s_log.c - s_log_handler()}.
}
@end display
}
@end format




@format
@anchor{s_log.c - global variables}
@i{<<s_log.c : global variables>>=}@t{
static int logfile_fd = -1;

static guint log_handler_id;

@display
@r{Defines :
@t{log_handler_id},
 -- @ref{s_log.c - s_log_close()},
 -- @ref{s_log.c - s_log_init()}.
@t{logfile_fd},
 -- @ref{s_log.c - s_log_close()},
 -- @ref{s_log.c - s_log_init()},
 -- @ref{s_log.c - s_log_read()},
 -- @ref{s_log.c - s_log_handler()}.
}
@end display
}
@end format



@section Function @code{s_log_init()}

@defun s_log_init filename
Initializes the logging feature of libgeda.

It opens the file @code{filename} to log to and registers the handler to redirect log message to this file.
@end defun

@format
@anchor{s_log.c - s_log_init()}
@i{<<s_log.c : s_log_init()>>=}@t{
void
s_log_init (const gchar *filename)
@{
  if (do_logging == FALSE) @{
    logfile_fd = -1;
    return;
  @}

  /* create log file */
  logfile_fd = open (filename, O_RDWR|O_CREAT|O_TRUNC, 0600);
  if (logfile_fd == -1) @{
    fprintf(stderr, "Could not open log file: %s\n", filename);
    fprintf(stderr, "Errno was: %d\n", errno);
    return;
  @}

  /* install the log handler */
  log_handler_id = g_log_set_handler (NULL,
                                      G_LOG_LEVEL_MESSAGE,
                                      s_log_handler,
                                      NULL);

@}

@display
@r{Defines :
.
}
@end display
@display
@r{Uses :
@t{log_handler_id},  -- @ref{s_log.c - global variables}.
@t{logfile_fd},  -- @ref{s_log.c - global variables}.
@t{s_log_handler},  -- @ref{s_log.c - s_log_handler()}.
}
@end display
}
@end format



@section Function @code{s_log_close()}

@defun s_log_close
Terminates the logging of messages.

It deregisters the handler for redirection to the log file and closes it.
@end defun

@format
@anchor{s_log.c - s_log_close()}
@i{<<s_log.c : s_log_close()>>=}@t{
void
s_log_close (void)
@{
  do_logging = FALSE; /* subsequent messages are lost after the close */

  /* remove the handler */
  g_log_remove_handler (NULL, log_handler_id);
  
  /* close the file */
  if (logfile_fd != -1) @{
    close (logfile_fd);
    logfile_fd = -1;
  @}

@}

@display
@r{Defines :
.
}
@end display
@display
@r{Uses :
@t{log_handler_id},  -- @ref{s_log.c - global variables}.
@t{logfile_fd},  -- @ref{s_log.c - global variables}.
}
@end display
}
@end format



@section Function @code{s_log_read()}

@defun s_log_read
Reads the current log file and returns its contents.
@end defun

@format
@anchor{s_log.c - s_log_read()}
@i{<<s_log.c : s_log_read()>>=}@t{
gchar*
s_log_read (void)
@{
  gboolean tmp;
#define BUFSIZE 200  
  gchar buf[BUFSIZE];
  GString *contents;
  gint len;
  
  if (logfile_fd == -1) @{
    return NULL;
  @}

  tmp = do_logging;
  do_logging = FALSE;

  /* rewind the file */
  lseek(logfile_fd, 0, SEEK_SET);

  /* read its contents and build a string */
  contents = g_string_new ("");
  while ((len = read (logfile_fd, &buf, BUFSIZE)) != 0) @{
    contents = g_string_append_len (contents, buf, len);
  @}

  do_logging = tmp;

  return g_string_free (contents, FALSE);
@}

@display
@r{Defines :
.
}
@end display
@display
@r{Uses :
@t{logfile_fd},  -- @ref{s_log.c - global variables}.
}
@end display
}
@end format



@section Function @code{s_log_handler()}

@defun s_log_handler log_domain log_level message user_data
Writes @code{message} to the current log file whose file descriptor is @code{logfile_fd}.

It also sends @code{message} to the optional function @code{x_log_update} for further use.
@end defun

@format
@anchor{s_log.c - s_log_handler()}
@i{<<s_log.c : s_log_handler()>>=}@t{
static void
s_log_handler (const gchar *log_domain,
               GLogLevelFlags log_level,
               const gchar *message,
               gpointer user_data)
@{
  int status;

  if (do_logging == FALSE) @{
    return;
  @}
  g_assert (logfile_fd != -1);
  
  status = write (logfile_fd, message, strlen (message));
  if (status == -1) @{
    fprintf(stderr, "Could not write message to log file\n");
    fprintf(stderr, "Message was: %s\n", message);
    fprintf(stderr, "Errno was: %d\n", errno);
  @}

  if (x_log_update_func) @{
    (*x_log_update_func) (message);
  @}

@}

@display
@r{Defines :
@t{s_log_handler},
 -- @ref{s_log.c - include directives},
 -- @ref{s_log.c - s_log_init()}.
}
@end display
@display
@r{Uses :
@t{logfile_fd},  -- @ref{s_log.c - global variables}.
}
@end display
}
@end format



@section Macro @code{s_log_message()}

@defmac s_log_message format ...
Logs a normal message.
@end defmac

@format
@anchor{defines.h - macro s_log_message()}
@i{<<defines.h : macro s_log_message()>>=}@t{
/* inspired of GLib's g_message() (gmessages.h) - LGPL */
#ifdef G_HAVE_ISO_VARARGS
#define s_log_message(...)  g_log (G_LOG_DOMAIN,         \
                                   G_LOG_LEVEL_MESSAGE,  \
                                   __VA_ARGS__)
#elif defined(G_HAVE_GNUC_VARARGS)
#define s_log_message(format...)    g_log (G_LOG_DOMAIN,         \
                                           G_LOG_LEVEL_MESSAGE,  \
                                           format)
#else   /* no varargs macros */
static void
s_log_message (const gchar *format,
               ...)
@{
  va_list args;
  va_start (args, format);
  g_logv (G_LOG_DOMAIN, G_LOG_LEVEL_MESSAGE, format, args);
  va_end (args);
@}
#endif  /* !__GNUC__ */

@display
@r{Defines :
.
}
@end display
}
@end format