Loading...
Searching...
No Matches
ASSA::FileLogger Class Reference

`#include <FileLogger.h>`

Inheritance diagram for ASSA::FileLogger:

## 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)
Public Member Functions inherited from ASSA::Logger_Impl
| Logger_Impl ()
virtual | ~Logger_Impl ()
void | enable_group (Group g_)
void | disable_group (Group g_)
void | enable_groups (u_long g_)
void | disable_groups (u_long g_)
void | enable_all_groups (void)
void | disable_all_groups (void)
bool | group_enabled (Group g_) const
void | enable_timestamp (void)
void | disable_timestamp (void)
bool | timestamp_enabled (void) const
void | set_timezone (int zone_)
void | set_indent_step (u_short step_)
u_short | get_indent_step (void) const
virtual int | log_open (u_long groups_)
| Open StdErr Logger.
virtual int | log_open (const char *appname_, const char *logfname_, u_long groups_, u_long maxsize_, Reactor *reactor_)
| Open connection with Log Server.

## Private Types

enum | state_t { opened , closed }

## Private Member Functions

| FileLogger (const FileLogger &)
FileLogger & | operator= (const FileLogger &)
int | handle_rollover ()

## Private Attributes

std::ofstream | m_sink
u_long | m_maxsize
state_t | m_state
u_long | m_bytecount

## Additional Inherited Members

Static Public Attributes inherited from ASSA::Logger_Impl
static const unsigned int | LOGGER_MAXLINE = 6660
| Maximum length of the formatted message.
Protected Member Functions inherited from ASSA::Logger_Impl
virtual u_short | add_timestamp (ostream &sink_)
virtual u_short | indent_func_name (ostream &sink_, const string &funcname_, size_t indent_level_, marker_t type_)
char * | format_msg (size_t expected_sz_, const char *fmt_, va_list vap_, bool &release_)
| Format and put the message in the buffer.
Protected Attributes inherited from ASSA::Logger_Impl
u_short | m_indent_step
| Indentation step.
u_long | m_groups
| Enabled groups.
string | m_logfname
| Log file name.
bool | m_tmflg
| Timestamp on/off flag.
int | m_tz
| Timezone: 0-GMT, 1-Local.
Static Protected Attributes inherited from ASSA::Logger_Impl
static char | m_msgbuf [LOGGER_MAXLINE]
| Static buffer for formatted message.

## Detailed Description

Definition at line 30 of file FileLogger.h.

## Member Enumeration Documentation

## state_t

enum ASSA::FileLogger::state_t

private

Enumerator
opened |
closed |

Definition at line 60 of file FileLogger.h.

{ opened, closed };
ASSA::FileLogger::closed
@ closed
Definition FileLogger.h:60
ASSA::FileLogger::opened
@ opened
Definition FileLogger.h:60

## Constructor & Destructor Documentation

## FileLogger() [1/2]

ASSA::FileLogger::FileLogger | ( |  | ) |

inline

Definition at line 74 of file FileLogger.h.

: m_maxsize (1048576),
m_state (closed),
m_bytecount (0)
{
/*--- empty ---*/
}
ASSA::FileLogger::m_maxsize
u_long m_maxsize
Definition FileLogger.h:69
ASSA::FileLogger::m_state
state_t m_state
Definition FileLogger.h:70
ASSA::FileLogger::m_bytecount
u_long m_bytecount
Definition FileLogger.h:71

## FileLogger() [2/2]

ASSA::FileLogger::FileLogger | ( | const FileLogger & |  | ) |

private

## Member Function Documentation

## dump()

void FileLogger::dump | ( | void |  | ) |

Definition at line 234 of file FileLogger.cpp.

{
#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
}
ASSA::AutoPtrRef
A wrapper class to provide AutoPtr with reference semantics.
Definition AutoPtr.h:32
ASSA::FileLogger::m_sink
std::ofstream m_sink
Definition FileLogger.h:68
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::m_tmflg
bool m_tmflg
Timestamp on/off flag.
Definition Logger_Impl.h:245

References 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.

## handle_rollover()

int FileLogger::handle_rollover | ( |  | ) |

private

Definition at line 198 of file FileLogger.cpp.

{
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;
}
Assure_exit
#define Assure_exit(exp_)
Macro that makes program exit if assert fails.
Definition Assure.h:39

References Assure_exit, closed, 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().

## log_close()

int FileLogger::log_close | ( | void |  | ) |

virtual

Implements ASSA::Logger_Impl.

Definition at line 69 of file FileLogger.cpp.

{
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;
}

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

## log_func()

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 161 of file FileLogger.cpp.

{
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 ();
}
ASSA::FileLogger::handle_rollover
int handle_rollover()
Definition FileLogger.cpp:199
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::add_timestamp
virtual u_short add_timestamp(ostream &sink_)
Definition Logger_Impl.cpp:35
ASSA::FUNC_ENTRY
@ FUNC_ENTRY
Definition LogMask.h:69
ASSA::flush
Socket & flush(Socket &os_)
flush manipulator.
Definition Socket.h:587

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

## log_msg()

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 116 of file FileLogger.cpp.

{
// 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 ();
}
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::FUNC_MSG
@ FUNC_MSG
Definition LogMask.h:68

References ASSA::Logger_Impl::add_timestamp(), closed, 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.

## log_open()

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 32 of file FileLogger.cpp.

{
// 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;
}

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

## log_raw_msg()

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 183 of file FileLogger.cpp.

{
if (m_state == closed) {
errno = EPERM;
return -1;
}
m_sink << msg_ << std::flush;
m_bytecount += msg_.length ();
return handle_rollover ();
}

References closed, handle_rollover(), m_bytecount, m_sink, and m_state.

## log_resync()

void ASSA::FileLogger::log_resync | ( | void |  | ) |

inline virtual

Reimplemented from ASSA::Logger_Impl.

Definition at line 84 of file FileLogger.h.

{
m_sink << std::flush;
}

References m_sink.

## operator=()

FileLogger & ASSA::FileLogger::operator= | ( | const FileLogger & |  | ) |

private

## Member Data Documentation

## m_bytecount

u_long ASSA::FileLogger::m_bytecount

private

Definition at line 71 of file FileLogger.h.

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

## m_maxsize

u_long ASSA::FileLogger::m_maxsize

private

Definition at line 69 of file FileLogger.h.

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

## m_sink

std::ofstream ASSA::FileLogger::m_sink

private

Definition at line 68 of file FileLogger.h.

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

## m_state

state_t ASSA::FileLogger::m_state

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:

- FileLogger.h
- FileLogger.cpp