2014-08-08 14:54:54 +04:00
/*
ctdb logging code
Copyright ( C ) Andrew Tridgell 2008
This program is free software ; you can redistribute it and / or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation ; either version 3 of the License , or
( at your option ) any later version .
This program is distributed in the hope that it will be useful ,
but WITHOUT ANY WARRANTY ; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
GNU General Public License for more details .
You should have received a copy of the GNU General Public License
along with this program ; if not , see < http : //www.gnu.org/licenses/>.
*/
2015-10-26 08:50:46 +03:00
# include "replace.h"
2014-08-08 14:54:54 +04:00
# include "system/time.h"
# include "system/filesys.h"
2015-10-26 08:50:46 +03:00
# include "system/network.h"
# include <talloc.h>
# include "lib/util/debug.h"
2014-10-16 12:46:43 +04:00
# include "lib/util/time_basic.h"
2015-10-26 08:50:46 +03:00
# include "ctdb_private.h"
# include "ctdb_client.h"
2015-10-23 06:11:53 +03:00
# include "common/system.h"
2014-08-08 14:54:54 +04:00
2014-08-11 11:07:41 +04:00
# define CTDB_LOG_FILE_PREFIX "file"
2014-08-08 14:54:54 +04:00
struct file_state {
int fd ;
} ;
/*
log file logging function
*/
static void ctdb_log_to_file ( void * private_ptr , int dbglevel , const char * s )
{
struct file_state * state = talloc_get_type (
private_ptr , struct file_state ) ;
2014-10-16 12:46:43 +04:00
struct timeval tv ;
struct timeval_buf tvbuf ;
2014-08-08 14:54:54 +04:00
char * s2 = NULL ;
int ret ;
2014-10-16 12:46:43 +04:00
GetTimeOfDay ( & tv ) ;
timeval_str_buf ( & tv , false , true , & tvbuf ) ;
2014-08-08 14:54:54 +04:00
2014-10-16 12:46:43 +04:00
ret = asprintf ( & s2 , " %s [%s%5u]: %s \n " ,
tvbuf . buf ,
2014-08-08 14:54:54 +04:00
debug_extra , ( unsigned ) getpid ( ) , s ) ;
if ( ret = = - 1 ) {
const char * errstr = " asprintf failed \n " ;
sys_write ( state - > fd , errstr , strlen ( errstr ) ) ;
return ;
}
if ( s2 ) {
sys_write ( state - > fd , s2 , strlen ( s2 ) ) ;
free ( s2 ) ;
}
}
static int file_state_destructor ( struct file_state * state )
{
close ( state - > fd ) ;
state - > fd = - 1 ;
return 0 ;
}
2014-08-11 11:07:41 +04:00
static int ctdb_log_setup_file ( TALLOC_CTX * mem_ctx ,
const char * logging ,
const char * app_name )
2014-08-08 14:54:54 +04:00
{
struct file_state * state ;
2014-08-11 11:07:41 +04:00
const char * logfile ;
size_t l ;
l = strlen ( CTDB_LOG_FILE_PREFIX ) ;
if ( logging [ l ] ! = ' : ' ) {
return EINVAL ;
}
logfile = & logging [ 0 ] + l + 1 ;
2014-08-08 14:54:54 +04:00
state = talloc_zero ( mem_ctx , struct file_state ) ;
if ( state = = NULL ) {
return ENOMEM ;
}
if ( logfile = = NULL | | strcmp ( logfile , " - " ) = = 0 ) {
int ret ;
state - > fd = 1 ;
/* also catch stderr of subcommands to stdout */
ret = dup2 ( 1 , 2 ) ;
if ( ret = = - 1 ) {
return errno ;
}
} else {
state - > fd = open ( logfile , O_WRONLY | O_APPEND | O_CREAT , 0666 ) ;
if ( state - > fd = = - 1 ) {
return errno ;
}
}
talloc_set_destructor ( state , file_state_destructor ) ;
debug_set_callback ( state , ctdb_log_to_file ) ;
return 0 ;
}
2014-08-11 11:07:41 +04:00
void ctdb_log_init_file ( void )
{
ctdb_log_register_backend ( CTDB_LOG_FILE_PREFIX , ctdb_log_setup_file ) ;
}