From 7a16f18ae1ef5d21812c916470022280617c4e91 Mon Sep 17 00:00:00 2001 From: Steve Guminski Date: Thu, 3 Aug 2017 09:33:39 -0400 Subject: [PATCH] LU-5170 utils: Add utility and command name to llapi messages Allow error and info messages printed by llapi functions to include the name of the utility and the command that caused the error. The command name is provided by the caller before invoking any llapi functions through the use of the new function llapi_set_command_name(). After the command has completed, the utility should reset the name using llapi_clear_command_name(). If no command name is provided, only the utility name is printed. The lfs and lctl utilities have been updated with the new functions. They will print both the utility name and the command name while in non-interactive mode. In interactive mode, only the utility name is printed. Test-Parameters: trivial Signed-off-by: Steve Guminski Change-Id: Ifaa1549f9bac6fdb25120f5721f69a8e1a7a52e1 Reviewed-on: https://review.whamcloud.com/28571 Tested-by: Jenkins Tested-by: Maloo Reviewed-by: John L. Hammond Reviewed-by: Jian Yu Reviewed-by: Oleg Drokin --- lustre/include/lustre/lustreapi.h | 2 ++ lustre/utils/lctl.c | 3 +++ lustre/utils/lfs.c | 9 ++++++--- lustre/utils/liblustreapi.c | 21 +++++++++++++++++++++ 4 files changed, 32 insertions(+), 3 deletions(-) diff --git a/lustre/include/lustre/lustreapi.h b/lustre/include/lustre/lustreapi.h index 59253dd..8821b7f 100644 --- a/lustre/include/lustre/lustreapi.h +++ b/lustre/include/lustre/lustreapi.h @@ -139,6 +139,8 @@ int llapi_get_poolmembers(const char *poolname, char **members, int list_size, char *buffer, int buffer_size); int llapi_file_get_stripe(const char *path, struct lov_user_md *lum); int llapi_file_lookup(int dirfd, const char *name); +void llapi_set_command_name(const char *cmd); +void llapi_clear_command_name(void); #define VERBOSE_COUNT 0x1 #define VERBOSE_SIZE 0x2 diff --git a/lustre/utils/lctl.c b/lustre/utils/lctl.c index 8df6e7b..32473b5 100644 --- a/lustre/utils/lctl.c +++ b/lustre/utils/lctl.c @@ -45,6 +45,7 @@ #include #include "obdctl.h" #include +#include static int lctl_list_commands(int argc, char **argv); @@ -546,7 +547,9 @@ int lctl_main(int argc, char **argv) Parser_init("lctl > ", cmdlist); if (argc > 1) { + llapi_set_command_name(argv[1]); rc = Parser_execarg(argc - 1, argv + 1, cmdlist); + llapi_clear_command_name(); } else { rc = Parser_commands(); } diff --git a/lustre/utils/lfs.c b/lustre/utils/lfs.c index 16dead2..b99ee0c 100644 --- a/lustre/utils/lfs.c +++ b/lustre/utils/lfs.c @@ -6829,11 +6829,14 @@ int main(int argc, char **argv) Parser_init("lfs > ", cmdlist); - progname = argv[0]; /* Used in error messages */ - if (argc > 1) + progname = program_invocation_short_name; /* Used in error messages */ + if (argc > 1) { + llapi_set_command_name(argv[1]); rc = Parser_execarg(argc - 1, argv + 1, cmdlist); - else + llapi_clear_command_name(); + } else { rc = Parser_commands(); + } return rc < 0 ? -rc : rc; } diff --git a/lustre/utils/liblustreapi.c b/lustre/utils/liblustreapi.c index fcc6014..4479092 100644 --- a/lustre/utils/liblustreapi.c +++ b/lustre/utils/liblustreapi.c @@ -79,6 +79,7 @@ #include "lustreapi_internal.h" static int llapi_msg_level = LLAPI_MSG_MAX; +const char *liblustreapi_cmd; char *mdt_hash_name[] = { "none", LMV_HASH_NAME_ALL_CHARS, @@ -100,9 +101,24 @@ int llapi_msg_get_level(void) return llapi_msg_level; } +void llapi_set_command_name(const char *cmd) +{ + liblustreapi_cmd = cmd; +} + +void llapi_clear_command_name(void) +{ + liblustreapi_cmd = NULL; +} + static void error_callback_default(enum llapi_message_level level, int err, const char *fmt, va_list ap) { + if (liblustreapi_cmd != NULL) + fprintf(stderr, "%s %s: ", program_invocation_short_name, + liblustreapi_cmd); + else + fprintf(stderr, "%s: ", program_invocation_short_name); vfprintf(stderr, fmt, ap); if (level & LLAPI_MSG_NO_ERRNO) fprintf(stderr, "\n"); @@ -113,6 +129,11 @@ static void error_callback_default(enum llapi_message_level level, int err, static void info_callback_default(enum llapi_message_level level, int err, const char *fmt, va_list ap) { + if (liblustreapi_cmd != NULL) + fprintf(stderr, "%s %s: ", program_invocation_short_name, + liblustreapi_cmd); + else + fprintf(stderr, "%s: ", program_invocation_short_name); vfprintf(stdout, fmt, ap); } -- 1.8.3.1