geany  1.38
About: Geany is a text editor (using GTK2) with basic features of an integrated development environment (syntax highlighting, code folding, symbol name auto-completion, ...). F: office T: editor programming GTK+ IDE
  Fossies Dox: geany-1.38.tar.bz2  ("unofficial" and yet experimental doxygen-generated source code documentation)  

log.c
Go to the documentation of this file.
1/*
2 * log.c - this file is part of Geany, a fast and lightweight IDE
3 *
4 * Copyright 2008 The Geany contributors
5 *
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.
10 *
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.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 */
20
21/*
22 * Logging functions and the debug messages window.
23 */
24
25#ifdef HAVE_CONFIG_H
26# include "config.h"
27#endif
28
29#include "log.h"
30
31#include "app.h"
32#include "support.h"
33#include "utils.h"
34#include "ui_utils.h"
35
36#include <gtk/gtk.h>
37
38#ifdef HAVE_LOCALE_H
39# include <locale.h>
40#endif
41
42static GString *log_buffer = NULL;
43static GtkTextBuffer *dialog_textbuffer = NULL;
44
45enum
46{
48};
49
50
51static void update_dialog(void)
52{
54 {
55 GtkTextMark *mark;
56 GtkTextView *textview = g_object_get_data(G_OBJECT(dialog_textbuffer), "textview");
57
58 gtk_text_buffer_set_text(dialog_textbuffer, log_buffer->str, log_buffer->len);
59 /* scroll to the end of the messages as this might be most interesting */
60 mark = gtk_text_buffer_get_insert(dialog_textbuffer);
61 gtk_text_view_scroll_to_mark(textview, mark, 0.0, FALSE, 0.0, 0.0);
62 }
63}
64
65
66/* Geany's main debug/log function, declared in geany.h */
67void geany_debug(gchar const *format, ...)
68{
69 va_list args;
70 va_start(args, format);
71 g_logv(G_LOG_DOMAIN, G_LOG_LEVEL_INFO, format, args);
72 va_end(args);
73}
74
75
76static void handler_print(const gchar *msg)
77{
78 printf("%s", msg);
79 if (G_LIKELY(log_buffer != NULL))
80 {
81 g_string_append_printf(log_buffer, "%s", msg);
83 }
84}
85
86
87static void handler_printerr(const gchar *msg)
88{
89 fprintf(stderr, "%s", msg);
90 if (G_LIKELY(log_buffer != NULL))
91 {
92 g_string_append_printf(log_buffer, "%s", msg);
94 }
95}
96
97
98static const gchar *get_log_prefix(GLogLevelFlags log_level)
99{
100 switch (log_level & G_LOG_LEVEL_MASK)
101 {
102 case G_LOG_LEVEL_ERROR:
103 return "ERROR\t\t";
104 case G_LOG_LEVEL_CRITICAL:
105 return "CRITICAL\t";
106 case G_LOG_LEVEL_WARNING:
107 return "WARNING\t";
108 case G_LOG_LEVEL_MESSAGE:
109 return "MESSAGE\t";
110 case G_LOG_LEVEL_INFO:
111 return "INFO\t\t";
112 case G_LOG_LEVEL_DEBUG:
113 return "DEBUG\t";
114 }
115 return "LOG";
116}
117
118
119static void handler_log(const gchar *domain, GLogLevelFlags level, const gchar *msg, gpointer data)
120{
121 gchar *time_str;
122
123 if (G_LIKELY(app != NULL && app->debug_mode) ||
124 ! ((G_LOG_LEVEL_DEBUG | G_LOG_LEVEL_INFO | G_LOG_LEVEL_MESSAGE) & level))
125 {
126#ifdef G_OS_WIN32
127 /* On Windows g_log_default_handler() is not enough, we need to print it
128 * explicitly on stderr for the console window */
129 /** TODO this can be removed if/when we remove the console window on Windows */
130 if (domain != NULL)
131 fprintf(stderr, "%s: %s\n", domain, msg);
132 else
133 fprintf(stderr, "%s\n", msg);
134#else
135 /* print the message as usual on stdout/stderr */
136 g_log_default_handler(domain, level, msg, data);
137#endif
138 }
139
140 time_str = utils_get_current_time_string(TRUE);
141
142 g_string_append_printf(log_buffer, "%s: %s %s: %s\n", time_str, domain,
143 get_log_prefix(level), msg);
144
145 g_free(time_str);
146
148}
149
150
152{
153 log_buffer = g_string_sized_new(2048);
154
155 g_set_print_handler(handler_print);
156 g_set_printerr_handler(handler_printerr);
157 g_log_set_default_handler(handler_log, NULL);
158}
159
160
161static void on_dialog_response(GtkDialog *dialog, gint response, gpointer user_data)
162{
163 if (response == DIALOG_RESPONSE_CLEAR)
164 {
165 GtkTextIter start_iter, end_iter;
166
167 gtk_text_buffer_get_start_iter(dialog_textbuffer, &start_iter);
168 gtk_text_buffer_get_end_iter(dialog_textbuffer, &end_iter);
169 gtk_text_buffer_delete(dialog_textbuffer, &start_iter, &end_iter);
170
171 g_string_erase(log_buffer, 0, -1);
172 }
173 else
174 {
175 gtk_widget_destroy(GTK_WIDGET(dialog));
177 }
178}
179
180
182{
183 GtkWidget *dialog, *textview, *vbox, *swin;
184
185 dialog = gtk_dialog_new_with_buttons(_("Debug Messages"), GTK_WINDOW(main_widgets.window),
186 GTK_DIALOG_DESTROY_WITH_PARENT,
187 _("Cl_ear"), DIALOG_RESPONSE_CLEAR,
188 GTK_STOCK_CLOSE, GTK_RESPONSE_CLOSE, NULL);
189 vbox = ui_dialog_vbox_new(GTK_DIALOG(dialog));
190 gtk_box_set_spacing(GTK_BOX(vbox), 6);
191 gtk_widget_set_name(dialog, "GeanyDialog");
192
193 gtk_window_set_default_size(GTK_WINDOW(dialog), 550, 300);
194 gtk_dialog_set_default_response(GTK_DIALOG(dialog), GTK_RESPONSE_CLOSE);
195
196 textview = gtk_text_view_new();
197 dialog_textbuffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(textview));
198 g_object_set_data(G_OBJECT(dialog_textbuffer), "textview", textview);
199 gtk_text_view_set_editable(GTK_TEXT_VIEW(textview), FALSE);
200 gtk_text_view_set_cursor_visible(GTK_TEXT_VIEW(textview), FALSE);
201 gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(textview), GTK_WRAP_WORD_CHAR);
202
203 swin = gtk_scrolled_window_new(NULL, NULL);
204 gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(swin), GTK_SHADOW_IN);
205 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(swin),
206 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
207 gtk_container_add(GTK_CONTAINER(swin), textview);
208
209 gtk_box_pack_start(GTK_BOX(vbox), swin, TRUE, TRUE, 0);
210
211 g_signal_connect(dialog, "response", G_CALLBACK(on_dialog_response), textview);
213
214 update_dialog(); /* set text after showing the window, to not scroll an unrealized textview */
215}
216
217
218void log_finalize(void)
219{
220 g_log_set_default_handler(g_log_default_handler, NULL);
221
222 g_string_free(log_buffer, TRUE);
224}
Contains the GeanyApp.
CobolFormat format
Definition: geany_cobol.c:137
GeanyApp * app
Definition: libmain.c:86
void log_handlers_init(void)
Definition: log.c:151
static GtkTextBuffer * dialog_textbuffer
Definition: log.c:43
static void handler_printerr(const gchar *msg)
Definition: log.c:87
void log_finalize(void)
Definition: log.c:218
static void update_dialog(void)
Definition: log.c:51
static void handler_print(const gchar *msg)
Definition: log.c:76
@ DIALOG_RESPONSE_CLEAR
Definition: log.c:47
static void on_dialog_response(GtkDialog *dialog, gint response, gpointer user_data)
Definition: log.c:161
void geany_debug(gchar const *format,...)
Definition: log.c:67
static const gchar * get_log_prefix(GLogLevelFlags log_level)
Definition: log.c:98
static void handler_log(const gchar *domain, GLogLevelFlags level, const gchar *msg, gpointer data)
Definition: log.c:119
void log_show_debug_messages_dialog(void)
Definition: log.c:181
static GString * log_buffer
Definition: log.c:42
#define NULL
Definition: rbtree.h:150
GtkWidget * dialog
gtk_container_add(GTK_CONTAINER(dialog->vbox), check_button)
gtk_widget_show_all(dialog)
gboolean debug_mode
TRUE if debug messages should be printed.
Definition: app.h:39
GtkWidget * window
Main window.
Definition: ui_utils.h:80
Defines internationalization macros.
#define _(String)
Definition: support.h:42
GtkWidget * ui_dialog_vbox_new(GtkDialog *dialog)
Makes a fixed border for dialogs without increasing the button box border.
Definition: ui_utils.c:1499
GeanyMainWidgets main_widgets
Definition: ui_utils.c:72
User Interface general utility functions.
gchar * utils_get_current_time_string(gboolean include_microseconds)
Definition: utils.c:1019
General utility functions, non-GTK related.