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
27 #include "write-buffer.h"
32 static int rawlog_lines;
33 static int signal_rawlog;
34 static int log_file_create_mode;
36 RAWLOG_REC *rawlog_create(void)
40 rec = g_new0(RAWLOG_REC, 1);
44 void rawlog_destroy(RAWLOG_REC *rawlog)
46 g_return_if_fail(rawlog != NULL);
48 g_slist_foreach(rawlog->lines, (GFunc) g_free, NULL);
49 g_slist_free(rawlog->lines);
51 if (rawlog->logging) {
53 close(rawlog->handle);
58 /* NOTE! str must be dynamically allocated and must not be freed after! */
59 static void rawlog_add(RAWLOG_REC *rawlog, char *str)
61 if (rawlog->nlines < rawlog_lines || rawlog_lines <= 2)
64 g_free(rawlog->lines->data);
65 rawlog->lines = g_slist_remove(rawlog->lines,
69 if (rawlog->logging) {
70 write_buffer(rawlog->handle, str, strlen(str));
71 write_buffer(rawlog->handle, "\n", 1);
74 rawlog->lines = g_slist_append(rawlog->lines, str);
75 signal_emit_id(signal_rawlog, 2, rawlog, str);
78 void rawlog_input(RAWLOG_REC *rawlog, const char *str)
80 g_return_if_fail(rawlog != NULL);
81 g_return_if_fail(str != NULL);
83 rawlog_add(rawlog, g_strdup_printf(">> %s", str));
86 void rawlog_output(RAWLOG_REC *rawlog, const char *str)
88 g_return_if_fail(rawlog != NULL);
89 g_return_if_fail(str != NULL);
91 rawlog_add(rawlog, g_strdup_printf("<< %s", str));
94 void rawlog_redirect(RAWLOG_REC *rawlog, const char *str)
96 g_return_if_fail(rawlog != NULL);
97 g_return_if_fail(str != NULL);
99 rawlog_add(rawlog, g_strdup_printf("--> %s", str));
102 static void rawlog_dump(RAWLOG_REC *rawlog, int f)
106 for (tmp = rawlog->lines; tmp != NULL; tmp = tmp->next) {
107 write(f, tmp->data, strlen((char *) tmp->data));
112 void rawlog_open(RAWLOG_REC *rawlog, const char *fname)
116 g_return_if_fail(rawlog != NULL);
117 g_return_if_fail(fname != NULL);
122 path = convert_home(fname);
123 rawlog->handle = open(path, O_WRONLY | O_APPEND | O_CREAT,
124 log_file_create_mode);
127 rawlog_dump(rawlog, rawlog->handle);
128 rawlog->logging = rawlog->handle != -1;
131 void rawlog_close(RAWLOG_REC *rawlog)
133 if (rawlog->logging) {
134 write_buffer_flush();
135 close(rawlog->handle);
140 void rawlog_save(RAWLOG_REC *rawlog, const char *fname)
145 path = convert_home(fname);
146 f = open(path, O_WRONLY | O_APPEND | O_CREAT, log_file_create_mode);
149 rawlog_dump(rawlog, f);
153 void rawlog_set_size(int lines)
155 rawlog_lines = lines;
158 static void read_settings(void)
160 rawlog_set_size(settings_get_int("rawlog_lines"));
161 log_file_create_mode = octal2dec(settings_get_int("log_create_mode"));
164 static void cmd_rawlog(const char *data, SERVER_REC *server, void *item)
166 command_runsub("rawlog", data, server, item);
169 /* SYNTAX: RAWLOG SAVE <file> */
170 static void cmd_rawlog_save(const char *data, SERVER_REC *server)
172 g_return_if_fail(data != NULL);
173 if (server == NULL || server->rawlog == NULL)
174 cmd_return_error(CMDERR_NOT_CONNECTED);
176 if (*data == '\0') cmd_return_error(CMDERR_NOT_ENOUGH_PARAMS);
177 rawlog_save(server->rawlog, data);
180 /* SYNTAX: RAWLOG OPEN <file> */
181 static void cmd_rawlog_open(const char *data, SERVER_REC *server)
183 g_return_if_fail(data != NULL);
184 if (server == NULL || server->rawlog == NULL)
185 cmd_return_error(CMDERR_NOT_CONNECTED);
187 if (*data == '\0') cmd_return_error(CMDERR_NOT_ENOUGH_PARAMS);
188 rawlog_open(server->rawlog, data);
191 /* SYNTAX: RAWLOG CLOSE */
192 static void cmd_rawlog_close(const char *data, SERVER_REC *server)
194 g_return_if_fail(data != NULL);
195 if (server == NULL || server->rawlog == NULL)
196 cmd_return_error(CMDERR_NOT_CONNECTED);
198 rawlog_close(server->rawlog);
201 void rawlog_init(void)
203 signal_rawlog = signal_get_uniq_id("rawlog");
205 settings_add_int("history", "rawlog_lines", 200);
208 signal_add("setup changed", (SIGNAL_FUNC) read_settings);
210 command_bind("rawlog", NULL, (SIGNAL_FUNC) cmd_rawlog);
211 command_bind("rawlog save", NULL, (SIGNAL_FUNC) cmd_rawlog_save);
212 command_bind("rawlog open", NULL, (SIGNAL_FUNC) cmd_rawlog_open);
213 command_bind("rawlog close", NULL, (SIGNAL_FUNC) cmd_rawlog_close);
216 void rawlog_deinit(void)
218 signal_remove("setup changed", (SIGNAL_FUNC) read_settings);
220 command_unbind("rawlog", (SIGNAL_FUNC) cmd_rawlog);
221 command_unbind("rawlog save", (SIGNAL_FUNC) cmd_rawlog_save);
222 command_unbind("rawlog open", (SIGNAL_FUNC) cmd_rawlog_open);
223 command_unbind("rawlog close", (SIGNAL_FUNC) cmd_rawlog_close);