Loading...
Searching...
No Matches
FileLogger.cpp
Go to the documentation of this file.
// -*- c++ -*-
//------------------------------------------------------------------------------
// FileLogger.cpp
//------------------------------------------------------------------------------
// $Id: FileLogger.cpp,v 1.10 2006/07/20 02:30:53 vlg Exp $
//------------------------------------------------------------------------------
// Copyright (C) 1997-2002,2005 Vladislav Grinchenko
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Library General Public
// License as published by the Free Software Foundation; either
// version 2 of the License, or (at your option) any later version.
//------------------------------------------------------------------------------
#include <stdio.h>
#include <stdarg.h> // vsprintf(3)
#include <sys/types.h> // stat(2)
#include <sys/stat.h> // stat(2)
#include <unistd.h> // stat(2)
#include <string>
#include <iomanip>
#include <assa/TimeVal.h>
#include <assa/FileLogger.h>
#include <assa/Assure.h>
using namespace ASSA;
int
FileLogger::
log_open (const char* logfname_, u_long groups_, u_long maxsize_)
{
// std::cout << "Enter: FileLogger::log_open(fname=\""
// << logfname_ << "\""
// << " groups=0x" << std::setw(8) << std::hex << groups_
// << " maxsize=" << std::dec << maxsize_ << ")\n";
if (logfname_ == NULL || maxsize_ <= 0) {
// std::cout << "FileLogger::log_open() failed 1\n";
errno = EINVAL;
return -1;
}
if (m_state == opened) {
// std::cout << "FileLogger::log_open() already open\n";
errno = EEXIST;
return -1;
}
m_logfname = logfname_;
m_groups = groups_;
m_maxsize = maxsize_;
m_sink.open (m_logfname.c_str (), std::ios::out | std::ios::app);
if (!m_sink) {
// std::cout << "FileLogger::log_open() failed to open()!\n";
return -1;
}
// std::cout << "Success on FileLogger::log_open()\n";
m_state = opened;
return 0;
}
int
FileLogger::
log_close (void)
{
if (m_state != closed) {
m_sink << std::flush;
m_sink.close ();
m_state = closed;
if (m_groups == 0) {
::unlink (m_logfname.c_str ());
}
m_logfname.empty ();
m_maxsize = 0;
m_bytecount = 0;
}
return 0;
}
int
FileLogger::
log_msg (Group g_,
size_t indent_level_,
const string& func_name_,
size_t expected_sz_,
const char* fmt_,
va_list msg_list_)
{
// std::cout << "FileLogger::log_msg() enter\n"
// << "group__=0x" << std::setw(8) << std::hex << (u_long)g_ << "\n";
if (m_state == closed) {
// std::cout << "FileLogger::log_msg() sink closed!\n";
errno = EPERM;
return -1;
}
if (! group_enabled (g_)) {
// std::cout << "FileLogger::log_msg() group is not enabled!\n"
// << "m_groups=0x"
// << std::setw(8) << std::hex << m_groups << "\n";
return 0;
}
m_bytecount += add_timestamp (m_sink);
m_bytecount += indent_func_name (m_sink, func_name_,indent_level_,FUNC_MSG);
bool release = false;
char* msgbuf_ptr = format_msg (expected_sz_, fmt_, msg_list_, release);
if (msgbuf_ptr == NULL) {
// std::cout << "FileLogger::log_msg() call to format_msg() failed!"
// << " fmt_= \"" << fmt_ << "\"\n" << std::flush;
return -1; // failed to format
}
m_sink << msgbuf_ptr << std::flush;
m_bytecount += strlen (msgbuf_ptr);
if (release) {
delete [] msgbuf_ptr;
}
return handle_rollover ();
}
int
FileLogger::
log_func (Group g_, size_t indent_level_, const string& func_name_,
marker_t type_)
{
if (m_state == closed) {
errno = EPERM;
return -1;
}
if (! group_enabled (g_)) {
return 0;
}
m_bytecount += add_timestamp (m_sink);
m_bytecount += indent_func_name (m_sink, func_name_, indent_level_, type_);
m_sink << ((type_ == FUNC_ENTRY) ? "---v---\n" : "---^---\n") << std::flush;
m_bytecount += ::strlen ("---v---\n");
return handle_rollover ();
}
int
FileLogger::
log_raw_msg (const string& msg_)
{
if (m_state == closed) {
errno = EPERM;
return -1;
}
m_sink << msg_ << std::flush;
m_bytecount += msg_.length ();
return handle_rollover ();
}
int
FileLogger::
handle_rollover ()
{
if (m_bytecount >= m_maxsize) {
struct stat fst;
if (::stat (m_logfname.c_str(), &fst) == 0) {
if (S_ISREG (fst.st_mode)) {
m_sink << "\nReached maximum allowable size\n"
<< "m_bytecount = " << m_bytecount
<< ", m_maxsize = " << m_maxsize << std::endl;
m_sink.close ();
m_state = closed;
m_bytecount = 0;
string newname = m_logfname + ".0";
unlink (newname.c_str ());
rename (m_logfname.c_str (), newname.c_str ());
m_sink.open (m_logfname.c_str (),
std::ios::app | std::ios::out);
if (!m_sink) {
return -1;
}
m_state = opened;
}
else if (S_ISCHR (fst.st_mode)) { // It is /dev/null
m_bytecount = 0;
}
else {
Assure_exit (1);
}
}
}
return 0;
}
void
FileLogger::
dump (void)
{
#ifdef BUG_HUNTING
if (m_state == opened) {
m_sink << "m_logfname = \"" << m_logfname << "\"\n"
<< "m_groups = 0x";
char oldfill = m_sink.fill ('0');
m_sink << std::setw(8) << std::hex << m_groups << '\n' << std::dec;
m_sink.fill (oldfill);
m_sink << "m_indent_step = " << m_indent_step << '\n'
<< "m_tmflg = " << m_tmflg << '\n'
<< "m_maxsize = " << m_maxsize << '\n'
<< "m_state = opened\n"
<< "m_bytecount = " << m_bytecount << std::endl;
}
#endif
}
Assure.h
A collection of assert function wrappers.
Assure_exit
#define Assure_exit(exp_)
Macro that makes program exit if assert fails.
Definition Assure.h:39
FileLogger.h
Implemention of a Logger as a disk-based file.
u_long
unsigned long u_long
Definition Logger_Impl.h:41
TimeVal.h
Class TimeVal is a wrapper around UNIX timeval structure.
ASSA::FileLogger::handle_rollover
int handle_rollover()
Definition FileLogger.cpp:199
ASSA::FileLogger::dump
void dump(void)
Definition FileLogger.cpp:235
ASSA::FileLogger::log_func
virtual int log_func(Group g_, size_t indent_level_, const string &func_name_, marker_t type_)
Definition FileLogger.cpp:162
ASSA::FileLogger::log_open
virtual int log_open(const char *logfname_, u_long groups_, u_long maxsize_=10485760)
Open File Logger.
Definition FileLogger.cpp:33
ASSA::FileLogger::log_close
virtual int log_close(void)
Definition FileLogger.cpp:70
ASSA::FileLogger::log_msg
virtual int log_msg(Group g_, size_t indent_level_, const string &func_name_, size_t expected_sz_, const char *fmt_, va_list)
If output string is longer then LOGGER_MAXLINE-1, it is truncated to that size.
Definition FileLogger.cpp:117
ASSA::FileLogger::closed
@ closed
Definition FileLogger.h:60
ASSA::FileLogger::opened
@ opened
Definition FileLogger.h:60
ASSA::FileLogger::m_maxsize
u_long m_maxsize
Definition FileLogger.h:69
ASSA::FileLogger::m_sink
std::ofstream m_sink
Definition FileLogger.h:68
ASSA::FileLogger::m_state
state_t m_state
Definition FileLogger.h:70
ASSA::FileLogger::log_raw_msg
int log_raw_msg(const string &msg_)
Log message as it is (raw) without indentation or timestamping, but still perform byte counting and t...
Definition FileLogger.cpp:184
ASSA::FileLogger::m_bytecount
u_long m_bytecount
Definition FileLogger.h:71
ASSA::Logger_Impl::format_msg
char * format_msg(size_t expected_sz_, const char *fmt_, va_list vap_, bool &release_)
Format and put the message in the buffer.
Definition Logger_Impl.cpp:86
ASSA::Logger_Impl::indent_func_name
virtual u_short indent_func_name(ostream &sink_, const string &funcname_, size_t indent_level_, marker_t type_)
Definition Logger_Impl.cpp:54
ASSA::Logger_Impl::group_enabled
bool group_enabled(Group g_) const
Definition Logger_Impl.h:164
ASSA::Logger_Impl::m_indent_step
u_short m_indent_step
Indentation step.
Definition Logger_Impl.h:236
ASSA::Logger_Impl::m_groups
u_long m_groups
Enabled groups.
Definition Logger_Impl.h:239
ASSA::Logger_Impl::m_logfname
string m_logfname
Log file name.
Definition Logger_Impl.h:242
ASSA::Logger_Impl::add_timestamp
virtual u_short add_timestamp(ostream &sink_)
Definition Logger_Impl.cpp:35
ASSA::Logger_Impl::m_tmflg
bool m_tmflg
Timestamp on/off flag.
Definition Logger_Impl.h:245
ASSA
Definition Acceptor.h:40
ASSA::marker_t
marker_t
Definition LogMask.h:67
ASSA::FUNC_ENTRY
@ FUNC_ENTRY
Definition LogMask.h:69
ASSA::FUNC_MSG
@ FUNC_MSG
Definition LogMask.h:68
ASSA::Group
Group
Definition LogMask.h:25