File: x_log.c

package info (click to toggle)
geda-gaf 1%3A1.6.2-4.3
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 38,616 kB
  • sloc: ansic: 80,253; sh: 13,262; lisp: 7,177; makefile: 2,597; perl: 2,401; python: 940; lex: 887; awk: 362; yacc: 289; sed: 27; xml: 23
file content (333 lines) | stat: -rw-r--r-- 10,279 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
/* gEDA - GPL Electronic Design Automation
 * gschem - gEDA Schematic Capture
 * Copyright (C) 1998-2010 Ales Hvezda
 * Copyright (C) 1998-2010 gEDA Contributors (see ChangeLog for details)
 *
 * 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */
#include <config.h>

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#ifdef HAVE_STDLIB_H
#include <stdlib.h>
#endif
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#ifdef HAVE_FCNTL_H
#include <fcntl.h>
#endif
#ifdef HAVE_STRING_H
#include <string.h>
#endif

#include "gschem.h"

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

static void x_log_callback_response (GtkDialog *dialog,
                                     gint arg1,
                                     gpointer user_data);
static void log_message (Log *log, 
                         const gchar *message, 
                         const gchar *style);

static void log_class_init (LogClass *class);
static void log_init       (Log *log);

static GtkWidget *log_dialog = NULL;

/*! \todo Finish function documentation!!!
 *  \brief
 *  \par Function Description
 * 
 */
void x_log_open ()
{
  if (log_dialog == NULL) {
    gchar *contents;
    
    log_dialog = GTK_WIDGET (g_object_new (TYPE_LOG,
                                           /* GschemDialog */
                                           "settings-name", "log",
                                           /* "toplevel", TOPEVEL * */
                                           NULL));

    g_signal_connect (log_dialog,
                      "response",
                      G_CALLBACK (x_log_callback_response),
                      NULL);

    /* make it read the content of the current log file */
    /* and add its contents to the dialog */
    contents = s_log_read ();

    /* s_log_read can return NULL if the log file cannot be written to */
    if (contents == NULL)
    {
      return;
    }

    log_message (LOG (log_dialog), contents, "old");
    g_free (contents);

    x_log_update_func = x_log_message;
   
    if( auto_place_mode )
	gtk_widget_set_uposition( log_dialog, 10, 10); 
    gtk_widget_show (log_dialog);
  } else {
    g_assert (IS_LOG (log_dialog));
    gtk_window_present ((GtkWindow*)log_dialog);
  }

}

/*! \todo Finish function documentation!!!
 *  \brief
 *  \par Function Description
 * 
 */
void x_log_close ()
{
  if (log_dialog) {
    g_assert (IS_LOG (log_dialog));
    gtk_widget_destroy (log_dialog);
    x_log_update_func = NULL;
    log_dialog = NULL;
  }

}

/*! \todo Finish function documentation!!!
 *  \brief
 *  \par Function Description
 * 
 */
void x_log_message (const gchar *log_domain, GLogLevelFlags log_level,
                    const gchar *message)
{
  gchar *style;
  g_return_if_fail (log_dialog != NULL);

  if (log_level & (G_LOG_LEVEL_CRITICAL | G_LOG_LEVEL_ERROR)) {
    style = "critical";
  } else if (log_level & G_LOG_LEVEL_WARNING) {
    style = "warning";
  } else {
    style = "message";
  }

  log_message (LOG(log_dialog), message, style);
}

/*! \todo Finish function documentation!!!
 *  \brief
 *  \par Function Description
 * 
 */
static void x_log_callback_response (GtkDialog *dialog,
				     gint arg1,
				     gpointer user_data)
{
  switch (arg1) {
    case GTK_RESPONSE_DELETE_EVENT:
    case LOG_RESPONSE_CLOSE:
    g_assert (GTK_WIDGET (dialog) == log_dialog);
    x_log_close ();
    break;
    default:
    g_assert_not_reached ();
  }
  
}

/*! \todo Finish function documentation!!!
 *  \brief
 *  \par Function Description
 * 
 */
static void log_message (Log *log, const gchar *message, 
                         const gchar *style)
{
  GtkTextBuffer *buffer;
  GtkTextIter iter;
  GtkTextMark *mark;
  
  g_return_if_fail (IS_LOG (log));

  buffer = gtk_text_view_get_buffer (log->textview);
  gtk_text_buffer_get_end_iter (buffer, &iter);
  /* Apply the "plain" tag before the level-specific tag in order to
   * reset the formatting */

  if (g_utf8_validate (message, -1, NULL)) {
    gtk_text_buffer_insert_with_tags_by_name (buffer, &iter, message, -1,
                                              "plain", style, NULL);
  } else {
    /* If UTF-8 wasn't valid (due to a system locale encoded filename or
     * other string being included by mistake), log a warning, and print
     * the original message to stderr, where it may be partly intelligible */
    gtk_text_buffer_insert_with_tags_by_name (buffer, &iter,
      _("** Invalid UTF-8 in log message. See stderr or gschem.log.\n"),
                                              -1, "plain", style, NULL);
    fprintf (stderr, "%s", message);
  }

  mark = gtk_text_buffer_create_mark(buffer, NULL, &iter, FALSE);
  gtk_text_view_scroll_to_mark (log->textview, mark, 0, TRUE, 0, 1);
  gtk_text_buffer_delete_mark (buffer, mark);
}

/*! \todo Finish function documentation!!!
 *  \brief
 *  \par Function Description
 * 
 */
GType log_get_type ()
{
  static GType log_type = 0;
  
  if (!log_type) {
    static const GTypeInfo log_info = {
      sizeof(LogClass),
      NULL, /* base_init */
      NULL, /* base_finalize */
      (GClassInitFunc) log_class_init,
      NULL, /* class_finalize */
      NULL, /* class_data */
      sizeof(Log),
      0,    /* n_preallocs */
      (GInstanceInitFunc) log_init,
    };
		
    log_type = g_type_register_static (GSCHEM_TYPE_DIALOG,
                                       "Log",
                                       &log_info, 0);
  }
  
  return log_type;
}

/*! \todo Finish function documentation!!!
 *  \brief
 *  \par Function Description
 * 
 */
static void log_class_init (LogClass *klass)
{
/*   GObjectClass *gobject_class = G_OBJECT_CLASS (klass); */
	
}

/*! \todo Finish function documentation!!!
 *  \brief
 *  \par Function Description
 * 
 */
static void log_init (Log *log)
{
  GtkWidget *scrolled_win, *text_view;
  GtkTextBuffer *text_buffer;
  GtkTextMark *mark;

  /* dialog initialization */
  g_object_set (G_OBJECT (log),
                /* GtkContainer */
                "border-width",    0,
                /* GtkWindow */
                "type",            GTK_WINDOW_TOPLEVEL,
                "title",           _("Status"),
                "default-width",   600,
                "default-height",  200,
                "modal",           FALSE,
                "window-position", GTK_WIN_POS_NONE,
                "type-hint",       GDK_WINDOW_TYPE_HINT_NORMAL,
                /* GtkDialog */
                "has-separator",   TRUE,
                NULL);

  /* create a scrolled window for the textview */
  scrolled_win = GTK_WIDGET (
    g_object_new (GTK_TYPE_SCROLLED_WINDOW,
                  /* GtkContainer */
                  "border-width",      5,
                  /* GtkScrolledWindow */
                  "hscrollbar-policy", GTK_POLICY_AUTOMATIC,
                  "vscrollbar-policy", GTK_POLICY_AUTOMATIC,
                  "shadow-type",       GTK_SHADOW_ETCHED_IN,
                  NULL));
  /* create the text buffer */
  text_buffer = GTK_TEXT_BUFFER (g_object_new (GTK_TYPE_TEXT_BUFFER,
                                               NULL));

  /* Add some tags for highlighting log messages to the buffer */
  gtk_text_buffer_create_tag (text_buffer, "plain",
                              "foreground", "black",
                              "foreground-set", TRUE,
                              "weight", PANGO_WEIGHT_NORMAL,
                              "weight-set", TRUE,
                              NULL);
  /* The default "message" style is plain */
  gtk_text_buffer_create_tag (text_buffer, "message", NULL);
  /* "old" messages are in dark grey */
  gtk_text_buffer_create_tag (text_buffer, "old",
                              "foreground", "#404040",
                              "foreground-set", TRUE,
			      NULL);
  /* "warning" messages are printed in red */
  gtk_text_buffer_create_tag (text_buffer, "warning",
                              "foreground", "red",
                              "foreground-set", TRUE,
                              NULL);
  /* "critical" messages are bold red */
  gtk_text_buffer_create_tag (text_buffer, "critical",
                              "foreground", "red",
                              "foreground-set", TRUE,
                              "weight", PANGO_WEIGHT_BOLD,
                              "weight-set", TRUE,
                              NULL);

  /* create the text view and attach the buffer to it */
  text_view = GTK_WIDGET (g_object_new (GTK_TYPE_TEXT_VIEW,
                                        /* GtkTextView */
/* unknown property in GTK 2.2, use gtk_text_view_set_buffer() instead */
/*                                         "buffer",   text_buffer, */
                                        "editable", FALSE,
                                        NULL));
  gtk_text_view_set_buffer (GTK_TEXT_VIEW (text_view), text_buffer);

  /* add the text view to the scrolled window */
  gtk_container_add (GTK_CONTAINER (scrolled_win), text_view);
  /* set textview of log */
  log->textview = GTK_TEXT_VIEW (text_view);

  /* add the scrolled window to the dialog vbox */
  gtk_box_pack_start (GTK_BOX (GTK_DIALOG (log)->vbox), scrolled_win,
                      TRUE, TRUE, 0);
  gtk_widget_show_all (scrolled_win);

  /* now add the close button to the action area */
  gtk_dialog_add_button (GTK_DIALOG (log),
                         GTK_STOCK_CLOSE, LOG_RESPONSE_CLOSE);

  /* scroll to the end of the buffer */
  mark = gtk_text_buffer_get_insert (text_buffer);
  gtk_text_view_scroll_to_mark (GTK_TEXT_VIEW (text_view), mark, 0.0, TRUE, 0.0, 1.0);
}