#include <fcntl.h>
#include <unistd.h>
#include <sys/stat.h>
#include <stdhdrs.h>
#include <strbuf.h>
#include <error.h>
#include <errorlog.h>
#include "log.h"
#include "err.h"
void Log::SetName( const char *logname )
{
/*
* Set the name of the log file.
*/
name.Set( logname );
}
void Log::SetLfd( const int fd )
{
/*
* Set the file descriptor of the log file.
*/
lfd = fd;
}
char *Log::GetName()
{
/*
* Get the name of the log file, if defined.
*/
return name.Length() ? name.Text() : 0;
}
int Log::GetLfd()
{
/*
* Get the file descriptor of the log file.
*/
return lfd;
}
bool Log::Init( const char *tag, const char *logname, Error *e )
{
/*
* Set the tag reported to the error log.
*/
SetTag( tag );
if( logname )
{
/*
* Name of the log file is defined.
*
* Save the name of the log file.
*/
SetName( logname );
/*
* Initialize the error log. The error log is opened and closed.
*/
SetLog( logname );
}
/*
* No errors encountered.
*/
return true;
}
bool Log::Open( Error *e )
{
int fd; // log file descriptor
/*
* Open the log file.
*/
if( ( fd = open( GetName(), O_WRONLY | O_APPEND | O_CREAT | O_LARGEFILE,
S_IRUSR | S_IWUSR ) ) == -1 )
return syserr( "open", GetName(), e );
/*
* Save the log file descriptor.
*/
SetLfd( fd );
/*
* No errors encountered.
*/
return true;
}
bool Log::Dup2( const int newfd, Error *e )
{
/*
* Duplicate the log file descriptor.
*/
if( dup2( GetLfd(), newfd ) == -1 )
return syserr( "dup2", GetName(), e );
/*
* No errors encountered.
*/
return true;
}