mirror of
git://sourceware.org/git/lvm2.git
synced 2025-01-03 05:18:29 +03:00
Customisable message output prefix / indentation.
This commit is contained in:
parent
5556a49278
commit
7a403967d5
@ -14,6 +14,10 @@ static int _verbose_level = 0;
|
||||
static int _test = 0;
|
||||
static int _debug_level = 0;
|
||||
static int _syslog = 0;
|
||||
static int _indent = 1;
|
||||
static int _log_cmd_name = 0;
|
||||
static char _cmd_name[30] = "";
|
||||
static char _msg_prefix[30] = " ";
|
||||
|
||||
void init_log(FILE *fp) {
|
||||
_log = fp;
|
||||
@ -44,6 +48,26 @@ void init_test(int level) {
|
||||
log_print("Test mode. Metadata will NOT be updated.");
|
||||
}
|
||||
|
||||
void init_cmd_name(int status) {
|
||||
_log_cmd_name = status;
|
||||
}
|
||||
|
||||
void set_cmd_name(const char *cmd) {
|
||||
if (!_log_cmd_name)
|
||||
return;
|
||||
strncpy(_cmd_name, cmd, sizeof(_cmd_name));
|
||||
_cmd_name[sizeof(_cmd_name) - 1] = '\0';
|
||||
}
|
||||
|
||||
void init_msg_prefix(const char *prefix) {
|
||||
strncpy(_msg_prefix, prefix, sizeof(_msg_prefix));
|
||||
_msg_prefix[sizeof(_msg_prefix) - 1] = '\0';
|
||||
}
|
||||
|
||||
void init_indent(int indent) {
|
||||
_indent = indent;
|
||||
}
|
||||
|
||||
int test_mode() {
|
||||
return _test;
|
||||
}
|
||||
@ -63,6 +87,8 @@ void print_log(int level, const char *file, int line, const char *format, ...) {
|
||||
switch(level) {
|
||||
case _LOG_DEBUG:
|
||||
if (_verbose_level > 2 && format[1]) {
|
||||
printf("%s%s", _cmd_name, _msg_prefix);
|
||||
if (_indent)
|
||||
printf(" ");
|
||||
vprintf(format, ap);
|
||||
putchar('\n');
|
||||
@ -71,6 +97,8 @@ void print_log(int level, const char *file, int line, const char *format, ...) {
|
||||
|
||||
case _LOG_INFO:
|
||||
if (_verbose_level > 1) {
|
||||
printf("%s%s", _cmd_name, _msg_prefix);
|
||||
if (_indent)
|
||||
printf(" ");
|
||||
vprintf(format, ap);
|
||||
putchar('\n');
|
||||
@ -78,23 +106,26 @@ void print_log(int level, const char *file, int line, const char *format, ...) {
|
||||
break;
|
||||
case _LOG_NOTICE:
|
||||
if (_verbose_level) {
|
||||
printf("%s%s", _cmd_name, _msg_prefix);
|
||||
if (_indent)
|
||||
printf(" ");
|
||||
vprintf(format, ap);
|
||||
putchar('\n');
|
||||
}
|
||||
break;
|
||||
case _LOG_WARN:
|
||||
printf(" ");
|
||||
printf("%s%s", _cmd_name, _msg_prefix);
|
||||
vprintf(format, ap);
|
||||
putchar('\n');
|
||||
break;
|
||||
case _LOG_ERR:
|
||||
fprintf(stderr, " ");
|
||||
fprintf(stderr, "%s%s", _cmd_name, _msg_prefix);
|
||||
vfprintf(stderr, format, ap);
|
||||
fputc('\n',stderr);
|
||||
break;
|
||||
case _LOG_FATAL:
|
||||
default:
|
||||
fprintf(stderr, "%s%s", _cmd_name, _msg_prefix);
|
||||
vfprintf(stderr, format, ap);
|
||||
fputc('\n',stderr);
|
||||
break;
|
||||
@ -106,7 +137,8 @@ void print_log(int level, const char *file, int line, const char *format, ...) {
|
||||
return;
|
||||
|
||||
if (_log) {
|
||||
fprintf(_log, "%s:%d ", file, line);
|
||||
fprintf(_log, "%s:%d %s%s", file, line, _cmd_name,
|
||||
_msg_prefix);
|
||||
|
||||
va_start(ap, format);
|
||||
vfprintf(_log, format, ap);
|
||||
@ -123,9 +155,3 @@ void print_log(int level, const char *file, int line, const char *format, ...) {
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Local variables:
|
||||
* c-file-style: "linux"
|
||||
* End:
|
||||
*/
|
||||
|
||||
|
@ -49,6 +49,11 @@ void fin_syslog(void);
|
||||
void init_verbose(int level);
|
||||
void init_test(int level);
|
||||
void init_debug(int level);
|
||||
void init_cmd_name(int status);
|
||||
void init_msg_prefix(const char *prefix);
|
||||
void init_indent(int indent);
|
||||
|
||||
void set_cmd_name(const char *cmd_name);
|
||||
|
||||
int test_mode(void);
|
||||
int debug_level(void);
|
||||
|
11
tools/lvm.c
11
tools/lvm.c
@ -671,10 +671,13 @@ static int run_command(int argc, char **argv)
|
||||
return EINVALID_CMD_LINE;
|
||||
}
|
||||
|
||||
set_cmd_name(the_command->name);
|
||||
|
||||
if ((ret = process_common_commands(the_command)))
|
||||
return ret;
|
||||
|
||||
_use_settings(&_current_settings);
|
||||
|
||||
ret = the_command->fn(argc, argv);
|
||||
|
||||
/*
|
||||
@ -734,7 +737,7 @@ static void __init_log(struct config_file *cf)
|
||||
{
|
||||
char *open_mode = "a";
|
||||
|
||||
const char *log_file;
|
||||
const char *log_file, *prefix;
|
||||
|
||||
|
||||
_default_settings.syslog =
|
||||
@ -753,6 +756,12 @@ static void __init_log(struct config_file *cf)
|
||||
find_config_int(cf->root, "log/verbose", '/', 0);
|
||||
init_verbose(_default_settings.verbose);
|
||||
|
||||
init_indent(find_config_int(cf->root, "log/indent", '/', 1));
|
||||
if ((prefix = find_config_str(cf->root, "log/prefix", '/', 0)))
|
||||
init_msg_prefix(prefix);
|
||||
|
||||
init_cmd_name(find_config_int(cf->root, "log/command_names", '/', 0));
|
||||
|
||||
_default_settings.test = find_config_int(cf->root, "global/test",
|
||||
'/', 0);
|
||||
if (find_config_int(cf->root, "log/overwrite", '/', 0))
|
||||
|
Loading…
Reference in New Issue
Block a user