]> git.ipfire.org Git - thirdparty/xfsprogs-dev.git/blame - db/output.c
xfsprogs: convert to SPDX license tags
[thirdparty/xfsprogs-dev.git] / db / output.c
CommitLineData
959ef981 1// SPDX-License-Identifier: GPL-2.0
2bd0ea18 2/*
da23017d
NS
3 * Copyright (c) 2000-2001,2005 Silicon Graphics, Inc.
4 * All Rights Reserved.
2bd0ea18
NS
5 */
6
6b803e5a 7#include "libxfs.h"
2bd0ea18
NS
8#include <stdarg.h>
9#include "command.h"
10#include "output.h"
11#include "sig.h"
12#include "malloc.h"
13#include "init.h"
14
15static int log_f(int argc, char **argv);
16
17static const cmdinfo_t log_cmd =
9ee7055c
AM
18 { "log", NULL, log_f, 0, 2, 0, N_("[stop|start <filename>]"),
19 N_("start or stop logging to a file"), NULL };
2bd0ea18
NS
20
21int dbprefix;
22static FILE *log_file;
23static char *log_file_name;
24
25int
26dbprintf(const char *fmt, ...)
27{
28 va_list ap;
29 int i;
30
31 if (seenint())
32 return 0;
33 va_start(ap, fmt);
34 blockint();
35 i = 0;
36 if (dbprefix)
37 i += printf("%s: ", fsdevice);
38 i += vprintf(fmt, ap);
39 unblockint();
40 va_end(ap);
41 if (log_file) {
42 va_start(ap, fmt);
43 vfprintf(log_file, fmt, ap);
44 va_end(ap);
45 }
46 return i;
47}
48
49static int
50log_f(
51 int argc,
52 char **argv)
53{
54 if (argc == 1) {
55 if (log_file)
9ee7055c 56 dbprintf(_("logging to %s\n"), log_file_name);
2bd0ea18 57 else
9ee7055c 58 dbprintf(_("no log file\n"));
2bd0ea18
NS
59 } else if (argc == 2 && strcmp(argv[1], "stop") == 0) {
60 if (log_file) {
61 xfree(log_file_name);
62 fclose(log_file);
63 log_file = NULL;
64 } else
9ee7055c 65 dbprintf(_("no log file\n"));
2bd0ea18
NS
66 } else if (argc == 3 && strcmp(argv[1], "start") == 0) {
67 if (log_file)
9ee7055c 68 dbprintf(_("already logging to %s\n"), log_file_name);
2bd0ea18
NS
69 else {
70 log_file = fopen(argv[2], "a");
71 if (log_file == NULL)
9ee7055c 72 dbprintf(_("can't open %s for writing\n"),
2bd0ea18
NS
73 argv[2]);
74 else
75 log_file_name = xstrdup(argv[1]);
76 }
77 } else
9ee7055c 78 dbprintf(_("bad log command, ignored\n"));
2bd0ea18
NS
79 return 0;
80}
81
82void
83logprintf(const char *fmt, ...)
84{
85 va_list ap;
86
87 if (log_file) {
88 va_start(ap, fmt);
89 (void)vfprintf(log_file, fmt, ap);
90 va_end(ap);
91 }
92}
93
94void
95output_init(void)
96{
97 add_command(&log_cmd);
98}