]>
Commit | Line | Data |
---|---|---|
2bd0ea18 | 1 | /* |
0d3e0b37 | 2 | * Copyright (c) 2000-2001 Silicon Graphics, Inc. All Rights Reserved. |
dfc130f3 | 3 | * |
2bd0ea18 NS |
4 | * This program is free software; you can redistribute it and/or modify it |
5 | * under the terms of version 2 of the GNU General Public License as | |
6 | * published by the Free Software Foundation. | |
dfc130f3 | 7 | * |
2bd0ea18 NS |
8 | * This program is distributed in the hope that it would be useful, but |
9 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |
10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | |
dfc130f3 | 11 | * |
2bd0ea18 NS |
12 | * Further, this software is distributed without any warranty that it is |
13 | * free of the rightful claim of any third person regarding infringement | |
14 | * or the like. Any license provided herein, whether implied or | |
15 | * otherwise, applies only to this software file. Patent licenses, if | |
16 | * any, provided herein do not apply to combinations of this program with | |
17 | * other software, or any other product whatsoever. | |
dfc130f3 | 18 | * |
2bd0ea18 NS |
19 | * You should have received a copy of the GNU General Public License along |
20 | * with this program; if not, write the Free Software Foundation, Inc., 59 | |
21 | * Temple Place - Suite 330, Boston MA 02111-1307, USA. | |
dfc130f3 | 22 | * |
2bd0ea18 NS |
23 | * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy, |
24 | * Mountain View, CA 94043, or: | |
dfc130f3 RC |
25 | * |
26 | * http://www.sgi.com | |
27 | * | |
28 | * For further information regarding this notice, see: | |
29 | * | |
2bd0ea18 NS |
30 | * http://oss.sgi.com/projects/GenInfo/SGIGPLNoticeExplan/ |
31 | */ | |
32 | ||
33 | #include <libxfs.h> | |
34 | #include <stdarg.h> | |
35 | #include "command.h" | |
36 | #include "output.h" | |
37 | #include "sig.h" | |
38 | #include "malloc.h" | |
39 | #include "init.h" | |
40 | ||
41 | static int log_f(int argc, char **argv); | |
42 | ||
43 | static const cmdinfo_t log_cmd = | |
44 | { "log", NULL, log_f, 0, 2, 0, "[stop|start <filename>]", | |
45 | "start or stop logging to a file", NULL }; | |
46 | ||
47 | int dbprefix; | |
48 | static FILE *log_file; | |
49 | static char *log_file_name; | |
50 | ||
51 | int | |
52 | dbprintf(const char *fmt, ...) | |
53 | { | |
54 | va_list ap; | |
55 | int i; | |
56 | ||
57 | if (seenint()) | |
58 | return 0; | |
59 | va_start(ap, fmt); | |
60 | blockint(); | |
61 | i = 0; | |
62 | if (dbprefix) | |
63 | i += printf("%s: ", fsdevice); | |
64 | i += vprintf(fmt, ap); | |
65 | unblockint(); | |
66 | va_end(ap); | |
67 | if (log_file) { | |
68 | va_start(ap, fmt); | |
69 | vfprintf(log_file, fmt, ap); | |
70 | va_end(ap); | |
71 | } | |
72 | return i; | |
73 | } | |
74 | ||
75 | static int | |
76 | log_f( | |
77 | int argc, | |
78 | char **argv) | |
79 | { | |
80 | if (argc == 1) { | |
81 | if (log_file) | |
82 | dbprintf("logging to %s\n", log_file_name); | |
83 | else | |
84 | dbprintf("no log file\n"); | |
85 | } else if (argc == 2 && strcmp(argv[1], "stop") == 0) { | |
86 | if (log_file) { | |
87 | xfree(log_file_name); | |
88 | fclose(log_file); | |
89 | log_file = NULL; | |
90 | } else | |
91 | dbprintf("no log file\n"); | |
92 | } else if (argc == 3 && strcmp(argv[1], "start") == 0) { | |
93 | if (log_file) | |
94 | dbprintf("already logging to %s\n", log_file_name); | |
95 | else { | |
96 | log_file = fopen(argv[2], "a"); | |
97 | if (log_file == NULL) | |
98 | dbprintf("can't open %s for writing\n", | |
99 | argv[2]); | |
100 | else | |
101 | log_file_name = xstrdup(argv[1]); | |
102 | } | |
103 | } else | |
104 | dbprintf("bad log command, ignored\n"); | |
105 | return 0; | |
106 | } | |
107 | ||
108 | void | |
109 | logprintf(const char *fmt, ...) | |
110 | { | |
111 | va_list ap; | |
112 | ||
113 | if (log_file) { | |
114 | va_start(ap, fmt); | |
115 | (void)vfprintf(log_file, fmt, ap); | |
116 | va_end(ap); | |
117 | } | |
118 | } | |
119 | ||
120 | void | |
121 | output_init(void) | |
122 | { | |
123 | add_command(&log_cmd); | |
124 | } |