]>
Commit | Line | Data |
---|---|---|
2bd0ea18 | 1 | /* |
da23017d NS |
2 | * Copyright (c) 2000-2001,2005 Silicon Graphics, Inc. |
3 | * All Rights Reserved. | |
dfc130f3 | 4 | * |
da23017d NS |
5 | * This program is free software; you can redistribute it and/or |
6 | * modify it under the terms of the GNU General Public License as | |
2bd0ea18 | 7 | * published by the Free Software Foundation. |
dfc130f3 | 8 | * |
da23017d NS |
9 | * This program is distributed in the hope that it would be useful, |
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | * GNU General Public License for more details. | |
dfc130f3 | 13 | * |
da23017d NS |
14 | * You should have received a copy of the GNU General Public License |
15 | * along with this program; if not, write the Free Software Foundation, | |
16 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | |
2bd0ea18 NS |
17 | */ |
18 | ||
6b803e5a | 19 | #include "libxfs.h" |
2bd0ea18 NS |
20 | #include <stdarg.h> |
21 | #include "command.h" | |
22 | #include "output.h" | |
23 | #include "sig.h" | |
24 | #include "malloc.h" | |
25 | #include "init.h" | |
26 | ||
27 | static int log_f(int argc, char **argv); | |
28 | ||
29 | static const cmdinfo_t log_cmd = | |
9ee7055c AM |
30 | { "log", NULL, log_f, 0, 2, 0, N_("[stop|start <filename>]"), |
31 | N_("start or stop logging to a file"), NULL }; | |
2bd0ea18 NS |
32 | |
33 | int dbprefix; | |
34 | static FILE *log_file; | |
35 | static char *log_file_name; | |
36 | ||
37 | int | |
38 | dbprintf(const char *fmt, ...) | |
39 | { | |
40 | va_list ap; | |
41 | int i; | |
42 | ||
43 | if (seenint()) | |
44 | return 0; | |
45 | va_start(ap, fmt); | |
46 | blockint(); | |
47 | i = 0; | |
48 | if (dbprefix) | |
49 | i += printf("%s: ", fsdevice); | |
50 | i += vprintf(fmt, ap); | |
51 | unblockint(); | |
52 | va_end(ap); | |
53 | if (log_file) { | |
54 | va_start(ap, fmt); | |
55 | vfprintf(log_file, fmt, ap); | |
56 | va_end(ap); | |
57 | } | |
58 | return i; | |
59 | } | |
60 | ||
61 | static int | |
62 | log_f( | |
63 | int argc, | |
64 | char **argv) | |
65 | { | |
66 | if (argc == 1) { | |
67 | if (log_file) | |
9ee7055c | 68 | dbprintf(_("logging to %s\n"), log_file_name); |
2bd0ea18 | 69 | else |
9ee7055c | 70 | dbprintf(_("no log file\n")); |
2bd0ea18 NS |
71 | } else if (argc == 2 && strcmp(argv[1], "stop") == 0) { |
72 | if (log_file) { | |
73 | xfree(log_file_name); | |
74 | fclose(log_file); | |
75 | log_file = NULL; | |
76 | } else | |
9ee7055c | 77 | dbprintf(_("no log file\n")); |
2bd0ea18 NS |
78 | } else if (argc == 3 && strcmp(argv[1], "start") == 0) { |
79 | if (log_file) | |
9ee7055c | 80 | dbprintf(_("already logging to %s\n"), log_file_name); |
2bd0ea18 NS |
81 | else { |
82 | log_file = fopen(argv[2], "a"); | |
83 | if (log_file == NULL) | |
9ee7055c | 84 | dbprintf(_("can't open %s for writing\n"), |
2bd0ea18 NS |
85 | argv[2]); |
86 | else | |
87 | log_file_name = xstrdup(argv[1]); | |
88 | } | |
89 | } else | |
9ee7055c | 90 | dbprintf(_("bad log command, ignored\n")); |
2bd0ea18 NS |
91 | return 0; |
92 | } | |
93 | ||
94 | void | |
95 | logprintf(const char *fmt, ...) | |
96 | { | |
97 | va_list ap; | |
98 | ||
99 | if (log_file) { | |
100 | va_start(ap, fmt); | |
101 | (void)vfprintf(log_file, fmt, ap); | |
102 | va_end(ap); | |
103 | } | |
104 | } | |
105 | ||
106 | void | |
107 | output_init(void) | |
108 | { | |
109 | add_command(&log_cmd); | |
110 | } |