]> git.ipfire.org Git - thirdparty/xfsprogs-dev.git/blob - db/output.c
xfs_db: Don't ASSERT on unrecognized metadata
[thirdparty/xfsprogs-dev.git] / db / output.c
1 /*
2 * Copyright (c) 2000-2001,2005 Silicon Graphics, Inc.
3 * All Rights Reserved.
4 *
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
7 * published by the Free Software Foundation.
8 *
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.
13 *
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
17 */
18
19 #include "libxfs.h"
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 =
30 { "log", NULL, log_f, 0, 2, 0, N_("[stop|start <filename>]"),
31 N_("start or stop logging to a file"), NULL };
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)
68 dbprintf(_("logging to %s\n"), log_file_name);
69 else
70 dbprintf(_("no log file\n"));
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
77 dbprintf(_("no log file\n"));
78 } else if (argc == 3 && strcmp(argv[1], "start") == 0) {
79 if (log_file)
80 dbprintf(_("already logging to %s\n"), log_file_name);
81 else {
82 log_file = fopen(argv[2], "a");
83 if (log_file == NULL)
84 dbprintf(_("can't open %s for writing\n"),
85 argv[2]);
86 else
87 log_file_name = xstrdup(argv[1]);
88 }
89 } else
90 dbprintf(_("bad log command, ignored\n"));
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 }