2001-08-21 16:56:08 +04:00
/*
2001-10-03 15:06:31 +04:00
* Copyright ( C ) 2001 Sistina Software ( UK ) Limited .
2001-08-21 16:56:08 +04:00
*
2001-10-31 15:47:01 +03:00
* This file is released under the LGPL .
2001-08-21 16:56:08 +04:00
*/
2002-11-18 17:01:16 +03:00
# include "lib.h"
2001-08-21 16:56:08 +04:00
# include <stdarg.h>
# include <syslog.h>
static FILE * _log = 0 ;
static int _verbose_level = 0 ;
static int _test = 0 ;
2002-01-29 20:23:33 +03:00
static int _partial = 0 ;
2003-04-30 19:22:52 +04:00
static int _pvmove = 0 ;
2001-08-21 16:56:08 +04:00
static int _debug_level = 0 ;
static int _syslog = 0 ;
2002-01-22 18:33:40 +03:00
static int _indent = 1 ;
static int _log_cmd_name = 0 ;
2002-03-16 01:54:04 +03:00
static int _log_suppress = 0 ;
2002-07-11 00:43:32 +04:00
static int _ignorelockingfailure = 0 ;
2002-01-22 18:33:40 +03:00
static char _cmd_name [ 30 ] = " " ;
static char _msg_prefix [ 30 ] = " " ;
2001-08-21 16:56:08 +04:00
2002-11-18 17:01:16 +03:00
void init_log ( FILE * fp )
2002-03-16 01:54:04 +03:00
{
2001-08-21 16:56:08 +04:00
_log = fp ;
}
2002-03-16 01:54:04 +03:00
void init_syslog ( int facility )
{
2001-08-21 16:56:08 +04:00
openlog ( " lvm " , LOG_PID , facility ) ;
_syslog = 1 ;
}
2002-03-16 01:54:04 +03:00
void log_suppress ( int suppress )
{
_log_suppress = suppress ;
}
void fin_log ( )
{
2001-08-21 16:56:08 +04:00
_log = 0 ;
}
2002-03-16 01:54:04 +03:00
void fin_syslog ( )
{
2002-01-19 00:26:37 +03:00
if ( _syslog )
closelog ( ) ;
2001-08-21 16:56:08 +04:00
_syslog = 0 ;
}
2002-03-16 01:54:04 +03:00
void init_verbose ( int level )
{
2001-08-21 16:56:08 +04:00
_verbose_level = level ;
}
2002-03-16 01:54:04 +03:00
void init_test ( int level )
{
2002-11-18 17:01:16 +03:00
if ( ! _test & & level )
log_print ( " Test mode: Metadata will NOT be updated. " ) ;
2001-08-21 16:56:08 +04:00
_test = level ;
}
2002-03-16 01:54:04 +03:00
void init_partial ( int level )
{
2002-01-29 20:23:33 +03:00
_partial = level ;
}
2003-04-30 19:22:52 +04:00
void init_pvmove ( int level )
{
_pvmove = level ;
}
2002-07-11 00:43:32 +04:00
void init_ignorelockingfailure ( int level )
{
_ignorelockingfailure = level ;
}
2002-03-16 01:54:04 +03:00
void init_cmd_name ( int status )
{
2002-01-22 18:33:40 +03:00
_log_cmd_name = status ;
}
2002-03-16 01:54:04 +03:00
void set_cmd_name ( const char * cmd )
{
2002-01-22 18:33:40 +03:00
if ( ! _log_cmd_name )
return ;
strncpy ( _cmd_name , cmd , sizeof ( _cmd_name ) ) ;
_cmd_name [ sizeof ( _cmd_name ) - 1 ] = ' \0 ' ;
}
2002-03-16 01:54:04 +03:00
void init_msg_prefix ( const char * prefix )
{
2002-01-22 18:33:40 +03:00
strncpy ( _msg_prefix , prefix , sizeof ( _msg_prefix ) ) ;
_msg_prefix [ sizeof ( _msg_prefix ) - 1 ] = ' \0 ' ;
}
2002-03-16 01:54:04 +03:00
void init_indent ( int indent )
{
2002-01-22 18:33:40 +03:00
_indent = indent ;
}
2002-03-16 01:54:04 +03:00
int test_mode ( )
{
2001-08-21 16:56:08 +04:00
return _test ;
}
2002-03-16 01:54:04 +03:00
int partial_mode ( )
{
2002-01-29 20:23:33 +03:00
return _partial ;
}
2003-04-30 19:22:52 +04:00
int pvmove_mode ( )
{
return _pvmove ;
}
2002-07-11 00:43:32 +04:00
int ignorelockingfailure ( )
{
return _ignorelockingfailure ;
}
2002-03-16 01:54:04 +03:00
void init_debug ( int level )
{
2001-08-21 16:56:08 +04:00
_debug_level = level ;
}
2002-03-16 01:54:04 +03:00
int debug_level ( )
{
2001-08-21 16:56:08 +04:00
return _debug_level ;
}
2002-03-16 01:54:04 +03:00
void print_log ( int level , const char * file , int line , const char * format , . . . )
{
2001-08-21 16:56:08 +04:00
va_list ap ;
2002-03-16 01:54:04 +03:00
if ( ! _log_suppress ) {
va_start ( ap , format ) ;
switch ( level ) {
case _LOG_DEBUG :
if ( ! strcmp ( " <backtrace> " , format ) )
break ;
if ( _verbose_level > 2 ) {
printf ( " %s%s " , _cmd_name , _msg_prefix ) ;
if ( _indent )
printf ( " " ) ;
vprintf ( format , ap ) ;
putchar ( ' \n ' ) ;
}
2002-02-21 00:26:40 +03:00
break ;
2001-08-21 16:56:08 +04:00
2002-03-16 01:54:04 +03:00
case _LOG_INFO :
if ( _verbose_level > 1 ) {
printf ( " %s%s " , _cmd_name , _msg_prefix ) ;
if ( _indent )
printf ( " " ) ;
vprintf ( format , ap ) ;
putchar ( ' \n ' ) ;
}
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 :
2002-01-22 18:33:40 +03:00
printf ( " %s%s " , _cmd_name , _msg_prefix ) ;
2001-08-21 16:56:08 +04:00
vprintf ( format , ap ) ;
putchar ( ' \n ' ) ;
2002-03-16 01:54:04 +03:00
break ;
case _LOG_ERR :
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 ;
;
2001-08-21 16:56:08 +04:00
}
2002-03-16 01:54:04 +03:00
va_end ( ap ) ;
2001-08-21 16:56:08 +04:00
}
if ( level > _debug_level )
return ;
if ( _log ) {
2002-03-16 01:54:04 +03:00
fprintf ( _log , " %s:%d %s%s " , file , line , _cmd_name , _msg_prefix ) ;
2001-08-21 16:56:08 +04:00
va_start ( ap , format ) ;
vfprintf ( _log , format , ap ) ;
va_end ( ap ) ;
fprintf ( _log , " \n " ) ;
fflush ( _log ) ;
}
if ( _syslog ) {
2002-03-16 01:54:04 +03:00
va_start ( ap , format ) ;
2001-08-21 16:56:08 +04:00
vsyslog ( level , format , ap ) ;
va_end ( ap ) ;
}
}