libassa  3.5.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
Public Member Functions | Private Types | Private Member Functions | Private Attributes
ASSA::FileLogger Class Reference

#include <FileLogger.h>

Inheritance diagram for ASSA::FileLogger:
ASSA::Logger_Impl

List of all members.

Public Member Functions

 FileLogger ()
virtual int log_open (const char *logfname_, u_long groups_, u_long maxsize_=10485760)
 Open File Logger.
virtual int log_close (void)
virtual void log_resync (void)
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.
virtual int log_func (Group g_, size_t indent_level_, const string &func_name_, marker_t type_)
int log_raw_msg (const string &msg_)
 Log message as it is (raw) without indentation or timestamping, but still perform byte counting and the logic associated with it.
void dump (void)

Private Types

enum  state_t { opened, closed }

Private Member Functions

 FileLogger (const FileLogger &)
FileLoggeroperator= (const FileLogger &)
int handle_rollover ()

Private Attributes

std::ofstream m_sink
u_long m_maxsize
state_t m_state
u_long m_bytecount

Detailed Description

Definition at line 30 of file FileLogger.h.


Member Enumeration Documentation

enum ASSA::FileLogger::state_t [private]
Enumerator:
opened 
closed 

Definition at line 60 of file FileLogger.h.

{ opened, closed };

Constructor & Destructor Documentation

Definition at line 75 of file FileLogger.h.

    : m_maxsize   (1048576), 
      m_state     (closed),
      m_bytecount (0)
{
    /*--- empty ---*/
}
ASSA::FileLogger::FileLogger ( const FileLogger ) [private]

Member Function Documentation

void FileLogger::dump ( void  )

Definition at line 235 of file FileLogger.cpp.

References ASSA::endl(), m_bytecount, ASSA::Logger_Impl::m_groups, ASSA::Logger_Impl::m_indent_step, ASSA::Logger_Impl::m_logfname, m_maxsize, m_sink, m_state, ASSA::Logger_Impl::m_tmflg, and opened.

{
#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
}
int FileLogger::handle_rollover ( ) [private]

Definition at line 199 of file FileLogger.cpp.

References Assure_exit, closed, ASSA::endl(), m_bytecount, ASSA::Logger_Impl::m_logfname, m_maxsize, m_sink, m_state, and opened.

Referenced by log_func(), log_msg(), and log_raw_msg().

{
    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;
}
int FileLogger::log_close ( void  ) [virtual]

Implements ASSA::Logger_Impl.

Definition at line 70 of file FileLogger.cpp.

References closed, ASSA::flush(), m_bytecount, ASSA::Logger_Impl::m_groups, ASSA::Logger_Impl::m_logfname, m_maxsize, m_sink, and m_state.

{
    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_func ( Group  g_,
size_t  indent_level_,
const string &  func_name_,
marker_t  type_ 
) [virtual]

Implements ASSA::Logger_Impl.

Definition at line 162 of file FileLogger.cpp.

References ASSA::Logger_Impl::add_timestamp(), closed, ASSA::flush(), ASSA::FUNC_ENTRY, ASSA::Logger_Impl::group_enabled(), handle_rollover(), ASSA::Logger_Impl::indent_func_name(), m_bytecount, m_sink, and m_state.

{
    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_msg ( Group  g_,
size_t  indent_level_,
const string &  func_name_,
size_t  expected_sz_,
const char *  fmt_,
va_list  msg_list_ 
) [virtual]

If output string is longer then LOGGER_MAXLINE-1, it is truncated to that size.

From printf(3) manpage:

"Upon successful return, these functions return the number of characters printed (not including the trailing '\0' used to end output to strings).

If the output was truncated due to this limit then the return value is the number of characters (not including the trailing '\0') which would have been written to the final string if enough space had been available.

Thus, a return value of size or more means that the output was truncated.

If an output error is encountered, a negative value is returned."

In other words, if you have attempted to write more then buffer can hold, the output is truncated by buffer size - 1, and the return value would be the number of bytes you have tried to write to the buffer.

If buffer size is 256, and you tried to write 340 bytes to it, the first 255 bytes are written to the buffer, and 340 is returns as the return value.

Implements ASSA::Logger_Impl.

Definition at line 117 of file FileLogger.cpp.

References ASSA::Logger_Impl::add_timestamp(), closed, ASSA::flush(), ASSA::Logger_Impl::format_msg(), ASSA::FUNC_MSG, ASSA::Logger_Impl::group_enabled(), handle_rollover(), ASSA::Logger_Impl::indent_func_name(), m_bytecount, m_sink, and m_state.

{
//  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_open ( const char *  logfname_,
u_long  groups_,
u_long  maxsize_ = 10485760 
) [virtual]

Open File Logger.

Reimplemented from ASSA::Logger_Impl.

Definition at line 33 of file FileLogger.cpp.

References ASSA::Logger_Impl::m_groups, ASSA::Logger_Impl::m_logfname, m_maxsize, m_sink, m_state, and opened.

{
//  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_raw_msg ( const string &  msg_)

Log message as it is (raw) without indentation or timestamping, but still perform byte counting and the logic associated with it.

Definition at line 184 of file FileLogger.cpp.

References closed, ASSA::flush(), handle_rollover(), m_bytecount, m_sink, and m_state.

{
    if (m_state == closed) {
        errno = EPERM;
        return -1;
    }

    m_sink << msg_ << std::flush;
    m_bytecount += msg_.length ();

    return handle_rollover ();
}
void ASSA::FileLogger::log_resync ( void  ) [inline, virtual]

Reimplemented from ASSA::Logger_Impl.

Definition at line 85 of file FileLogger.h.

References ASSA::flush(), and m_sink.

FileLogger& ASSA::FileLogger::operator= ( const FileLogger ) [private]

Member Data Documentation

Definition at line 71 of file FileLogger.h.

Referenced by dump(), handle_rollover(), log_close(), log_func(), log_msg(), and log_raw_msg().

Definition at line 69 of file FileLogger.h.

Referenced by dump(), handle_rollover(), log_close(), and log_open().

std::ofstream ASSA::FileLogger::m_sink [private]

Definition at line 70 of file FileLogger.h.

Referenced by dump(), handle_rollover(), log_close(), log_func(), log_msg(), log_open(), and log_raw_msg().


The documentation for this class was generated from the following files:
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines