]> git.ipfire.org Git - thirdparty/xfsprogs-dev.git/blame - db/output.c
Update copyright/license notices to match SGI legal prefered boilerplate.
[thirdparty/xfsprogs-dev.git] / db / output.c
CommitLineData
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
1d7e80ee 19#include <xfs/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
27static int log_f(int argc, char **argv);
28
29static const cmdinfo_t log_cmd =
30 { "log", NULL, log_f, 0, 2, 0, "[stop|start <filename>]",
31 "start or stop logging to a file", NULL };
32
33int dbprefix;
34static FILE *log_file;
35static char *log_file_name;
36
37int
38dbprintf(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
61static int
62log_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
94void
95logprintf(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
106void
107output_init(void)
108{
109 add_command(&log_cmd);
110}