]>
Commit | Line | Data |
---|---|---|
1 | // SPDX-License-Identifier: GPL-2.0 | |
2 | /* | |
3 | * Copyright (c) 2000-2001,2005 Silicon Graphics, Inc. | |
4 | * All Rights Reserved. | |
5 | */ | |
6 | ||
7 | #include "libxfs.h" | |
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 | ||
15 | static int log_f(int argc, char **argv); | |
16 | ||
17 | static const cmdinfo_t log_cmd = | |
18 | { "log", NULL, log_f, 0, 2, 0, N_("[stop|start <filename>]"), | |
19 | N_("start or stop logging to a file"), NULL }; | |
20 | ||
21 | int dbprefix; | |
22 | static FILE *log_file; | |
23 | static char *log_file_name; | |
24 | ||
25 | int | |
26 | dbprintf(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 | ||
49 | static int | |
50 | log_f( | |
51 | int argc, | |
52 | char **argv) | |
53 | { | |
54 | if (argc == 1) { | |
55 | if (log_file) | |
56 | dbprintf(_("logging to %s\n"), log_file_name); | |
57 | else | |
58 | dbprintf(_("no log file\n")); | |
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 | |
65 | dbprintf(_("no log file\n")); | |
66 | } else if (argc == 3 && strcmp(argv[1], "start") == 0) { | |
67 | if (log_file) | |
68 | dbprintf(_("already logging to %s\n"), log_file_name); | |
69 | else { | |
70 | log_file = fopen(argv[2], "a"); | |
71 | if (log_file == NULL) | |
72 | dbprintf(_("can't open %s for writing\n"), | |
73 | argv[2]); | |
74 | else | |
75 | log_file_name = xstrdup(argv[1]); | |
76 | } | |
77 | } else | |
78 | dbprintf(_("bad log command, ignored\n")); | |
79 | return 0; | |
80 | } | |
81 | ||
82 | void | |
83 | logprintf(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 | ||
94 | void | |
95 | output_init(void) | |
96 | { | |
97 | add_command(&log_cmd); | |
98 | } |