4 Copyright (C) 1999-2000 Timo Sirainen
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-1307 USA
26 #include "write-buffer.h"
29 static int rawlog_lines;
30 static int signal_rawlog;
31 static int log_file_create_mode;
33 RAWLOG_REC *rawlog_create(void)
37 rec = g_new0(RAWLOG_REC, 1);
41 void rawlog_destroy(RAWLOG_REC *rawlog)
43 g_return_if_fail(rawlog != NULL);
45 g_slist_foreach(rawlog->lines, (GFunc) g_free, NULL);
46 g_slist_free(rawlog->lines);
48 if (rawlog->logging) {
50 close(rawlog->handle);
55 /* NOTE! str must be dynamically allocated and must not be freed after! */
56 static void rawlog_add(RAWLOG_REC *rawlog, char *str)
58 if (rawlog->nlines < rawlog_lines || rawlog_lines <= 2)
61 g_free(rawlog->lines->data);
62 rawlog->lines = g_slist_remove(rawlog->lines,
66 if (rawlog->logging) {
67 write_buffer(rawlog->handle, str, strlen(str));
68 write_buffer(rawlog->handle, "\n", 1);
71 rawlog->lines = g_slist_append(rawlog->lines, str);
72 signal_emit_id(signal_rawlog, 2, rawlog, str);
75 void rawlog_input(RAWLOG_REC *rawlog, const char *str)
77 g_return_if_fail(rawlog != NULL);
78 g_return_if_fail(str != NULL);
80 rawlog_add(rawlog, g_strdup_printf(">> %s", str));
83 void rawlog_output(RAWLOG_REC *rawlog, const char *str)
85 g_return_if_fail(rawlog != NULL);
86 g_return_if_fail(str != NULL);
88 rawlog_add(rawlog, g_strdup_printf("<< %s", str));
91 void rawlog_redirect(RAWLOG_REC *rawlog, const char *str)
93 g_return_if_fail(rawlog != NULL);
94 g_return_if_fail(str != NULL);
96 rawlog_add(rawlog, g_strdup_printf("--> %s", str));
99 static void rawlog_dump(RAWLOG_REC *rawlog, int f)
103 for (tmp = rawlog->lines; tmp != NULL; tmp = tmp->next) {
104 write(f, tmp->data, strlen((char *) tmp->data));
109 void rawlog_open(RAWLOG_REC *rawlog, const char *fname)
113 g_return_if_fail(rawlog != NULL);
114 g_return_if_fail(fname != NULL);
119 path = convert_home(fname);
120 rawlog->handle = open(path, O_WRONLY | O_APPEND | O_CREAT,
121 log_file_create_mode);
124 rawlog_dump(rawlog, rawlog->handle);
125 rawlog->logging = rawlog->handle != -1;
128 void rawlog_close(RAWLOG_REC *rawlog)
130 if (rawlog->logging) {
131 write_buffer_flush();
132 close(rawlog->handle);
137 void rawlog_save(RAWLOG_REC *rawlog, const char *fname)
142 path = convert_home(fname);
143 f = open(path, O_WRONLY | O_APPEND | O_CREAT, log_file_create_mode);
146 rawlog_dump(rawlog, f);
150 void rawlog_set_size(int lines)
152 rawlog_lines = lines;
155 static void read_settings(void)
157 rawlog_set_size(settings_get_int("rawlog_lines"));
158 log_file_create_mode = octal2dec(settings_get_int("log_create_mode"));
161 void rawlog_init(void)
163 signal_rawlog = signal_get_uniq_id("rawlog");
165 settings_add_int("history", "rawlog_lines", 200);
168 signal_add("setup changed", (SIGNAL_FUNC) read_settings);
171 void rawlog_deinit(void)
173 signal_remove("setup changed", (SIGNAL_FUNC) read_settings);