diff options
author | Peter Rajnoha <prajnoha@redhat.com> | 2016-07-08 16:47:51 +0200 |
---|---|---|
committer | Peter Rajnoha <prajnoha@redhat.com> | 2016-07-26 12:30:32 +0200 |
commit | 0e57d5f4b510ed9f83391e90f4e3365c289f05ac (patch) | |
tree | c4885475e625a88f7b01a7cf37decd55c38e3696 /lib/commands/toolcontext.c | |
parent | f9697ea0067ef574aff09f1c8f25e1c7fab74c17 (diff) | |
download | lvm2-dev-prajnoha-custom-output-fds.tar.gz |
log: separate output and make it possible to use given FDsdev-prajnoha-custom-output-fds
Currently, the output is separated in 3 parts and each part can go into
a separate and user-defined file descriptor:
- common output (stdout by default, customizable by LVM_OUT_FD environment variable)
- error output (stderr by default, customizable by LVM_ERR_FD environment variable)
- report output (stdout by default, customizable by LVM_REPORT_FD environment variable)
For example, each type of output goes to different output file:
[0] fedora/~ # export LVM_REPORT_FD=3
[0] fedora/~ # lvs fedora vg/abc 1>out 2>err 3>report
[0] fedora/~ # cat out
[0] fedora/~ # cat err
Volume group "vg" not found
Cannot process volume group vg
[0] fedora/~ # cat report
LV VG Attr LSize Layout Role CTime
root fedora -wi-ao---- 19.00g linear public Wed May 27 2015 08:09:21
swap fedora -wi-ao---- 500.00m linear public Wed May 27 2015 08:09:21
Another example in LVM shell where the report goes to "report" file:
[0] fedora/~ # export LVM_REPORT_FD=3
[0] fedora/~ # lvm 3>report
(in lvm shell)
lvm> vgs
(content of "report" file)
[1] fedora/~ # cat report
VG #PV #LV #SN Attr VSize VFree
fedora 1 2 0 wz--n- 19.49g 0
(in lvm shell)
lvm> lvs
(content of "report" file)
[1] fedora/~ # cat report
VG #PV #LV #SN Attr VSize VFree
fedora 1 2 0 wz--n- 19.49g 0
LV VG Attr LSize Layout Role CTime
root fedora -wi-ao---- 19.00g linear public Wed May 27 2015 08:09:21
swap fedora -wi-ao---- 500.00m linear public Wed May 27 2015 08:09:21
Diffstat (limited to 'lib/commands/toolcontext.c')
-rw-r--r-- | lib/commands/toolcontext.c | 50 |
1 files changed, 6 insertions, 44 deletions
diff --git a/lib/commands/toolcontext.c b/lib/commands/toolcontext.c index d3bd5bcdf..82e990ed9 100644 --- a/lib/commands/toolcontext.c +++ b/lib/commands/toolcontext.c @@ -1650,37 +1650,6 @@ static void _init_globals(struct cmd_context *cmd) } /* - * Close and reopen stream on file descriptor fd. - */ -static int _reopen_stream(FILE *stream, int fd, const char *mode, const char *name, FILE **new_stream) -{ - int fd_copy, new_fd; - - if ((fd_copy = dup(fd)) < 0) { - log_sys_error("dup", name); - return 0; - } - - if (fclose(stream)) - log_sys_error("fclose", name); - - if ((new_fd = dup2(fd_copy, fd)) < 0) - log_sys_error("dup2", name); - else if (new_fd != fd) - log_error("dup2(%d, %d) returned %d", fd_copy, fd, new_fd); - - if (close(fd_copy) < 0) - log_sys_error("close", name); - - if (!(*new_stream = fdopen(fd, mode))) { - log_sys_error("fdopen", name); - return 0; - } - - return 1; -} - -/* * init_connections(); * _init_lvmetad(); * lvmetad_disconnect(); (close previous connection) @@ -1835,7 +1804,6 @@ struct cmd_context *create_toolcontext(unsigned is_long_lived, unsigned set_filters) { struct cmd_context *cmd; - FILE *new_stream; int flags; #ifdef M_MMAP_MAX @@ -1885,9 +1853,8 @@ struct cmd_context *create_toolcontext(unsigned is_long_lived, if (is_valid_fd(STDIN_FILENO) && ((flags = fcntl(STDIN_FILENO, F_GETFL)) > 0) && (flags & O_ACCMODE) != O_WRONLY) { - if (!_reopen_stream(stdin, STDIN_FILENO, "r", "stdin", &new_stream)) + if (!reopen_standard_stream(&stdin, "r")) goto_out; - stdin = new_stream; if (setvbuf(stdin, cmd->linebuffer, _IOLBF, linebuffer_size)) { log_sys_error("setvbuf", ""); goto out; @@ -1897,9 +1864,8 @@ struct cmd_context *create_toolcontext(unsigned is_long_lived, if (is_valid_fd(STDOUT_FILENO) && ((flags = fcntl(STDOUT_FILENO, F_GETFL)) > 0) && (flags & O_ACCMODE) != O_RDONLY) { - if (!_reopen_stream(stdout, STDOUT_FILENO, "w", "stdout", &new_stream)) + if (!reopen_standard_stream(&stdout, "w")) goto_out; - stdout = new_stream; if (setvbuf(stdout, cmd->linebuffer + linebuffer_size, _IOLBF, linebuffer_size)) { log_sys_error("setvbuf", ""); @@ -1911,7 +1877,6 @@ struct cmd_context *create_toolcontext(unsigned is_long_lived, /* Without buffering, must not use stdin/stdout */ init_silent(1); #endif - /* * Environment variable LVM_SYSTEM_DIR overrides this below. */ @@ -2227,7 +2192,6 @@ int refresh_toolcontext(struct cmd_context *cmd) void destroy_toolcontext(struct cmd_context *cmd) { struct dm_config_tree *cft_cmdline; - FILE *new_stream; int flags; if (cmd->dump_filter && cmd->filter && cmd->filter->dump && @@ -2266,20 +2230,18 @@ void destroy_toolcontext(struct cmd_context *cmd) if (is_valid_fd(STDIN_FILENO) && ((flags = fcntl(STDIN_FILENO, F_GETFL)) > 0) && (flags & O_ACCMODE) != O_WRONLY) { - if (_reopen_stream(stdin, STDIN_FILENO, "r", "stdin", &new_stream)) { - stdin = new_stream; + if (reopen_standard_stream(&stdin, "r")) setlinebuf(stdin); - } else + else cmd->linebuffer = NULL; /* Leave buffer in place (deliberate leak) */ } if (is_valid_fd(STDOUT_FILENO) && ((flags = fcntl(STDOUT_FILENO, F_GETFL)) > 0) && (flags & O_ACCMODE) != O_RDONLY) { - if (_reopen_stream(stdout, STDOUT_FILENO, "w", "stdout", &new_stream)) { - stdout = new_stream; + if (reopen_standard_stream(&stdout, "w")) setlinebuf(stdout); - } else + else cmd->linebuffer = NULL; /* Leave buffer in place (deliberate leak) */ } |