Loading...
Searching...
No Matches
Logger_Impl.cpp
Go to the documentation of this file.
// -*- c++ -*-
//------------------------------------------------------------------------------
// assa/Logger_Impl.cpp
//------------------------------------------------------------------------------
// $Id: Logger_Impl.cpp,v 1.7 2012/05/21 03:20:39 vlg Exp $
//------------------------------------------------------------------------------
// Copyright (c) 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 <cstdio>
#include <iostream>
#include <iomanip>
#include <string.h> // strerror(3)
#include "assa/TimeVal.h"
#include "assa/Logger_Impl.h"
#if defined (WIN32)
# include <windows.h> // for vsnprintf() bug
#else
# include <stdio.h>
#endif
using namespace ASSA;
char Logger_Impl::m_msgbuf [LOGGER_MAXLINE];
u_short
Logger_Impl::
add_timestamp (ostream& sink_)
{
/*--- 'DD/MM/CC HH:MM:SS.MMMM ' - 23 chars ---*/
u_short bytecount = 0;
if (timestamp_enabled ()) {
TimeVal tv = TimeVal::gettimeofday ();
tv.tz (m_tz);
sink_ << tv.fmtString ("%m/%d/%Y %H:%M:%S") << '.';
char oldfill = sink_.fill('0');
sink_ << std::setw (3) << (tv.msec () % 1000000)/1000 << ' ';
sink_.fill (oldfill);
bytecount = 23;
}
return bytecount;
}
u_short
Logger_Impl::
indent_func_name (ostream& sink_,
const string& func_name_,
size_t indent_level_,
marker_t type_)
{
u_short bytecount = 0;
if (func_name_.size ()) {
u_int i = 1;
while (i < indent_level_) {
sink_ << '|';
for (u_short j = 0; j < m_indent_step-1; j++) {
sink_ << ' ';
}
i++;
}
if (type_ == FUNC_ENTRY) {
sink_ << '/' << func_name_ << " ";
}
else if (type_ == FUNC_EXIT) {
sink_ << '\\' << func_name_ << " ";
}
else if (type_ == FUNC_MSG) {
sink_ << '[' << func_name_ << "] ";
}
bytecount += indent_level_ * m_indent_step + func_name_.size () + 3;
}
return bytecount;
}
char*
Logger_Impl::
format_msg (size_t expected_sz_,
const char* fmt_,
va_list vap_,
bool& release_) // tell the caller it needs to release memory
{
char* msg = m_msgbuf; // Use internal buffer
int ret = 0;
release_ = false;
expected_sz_++; // Expected size includes '\0'
if (expected_sz_ >= LOGGER_MAXLINE) { // Allocate temporary buffer
msg = new char [expected_sz_];
release_ = true;
}
ret = ::vsnprintf (msg, expected_sz_, fmt_, vap_);
#if NEVER
if (ret < 0) {
std::cout << "Logger_Impl: format_mg(expected_sz=" << expected_sz_
<< ")=-1 failed! errno=" << errno << " ("
<< strerror(errno) << "\n" << std::flush;
}
#endif
return (ret < 0 ? NULL : msg);
}
Logger_Impl.h
u_short
unsigned short u_short
Definition Logger_Impl.h:39
u_int
unsigned int u_int
Definition Logger_Impl.h:40
TimeVal.h
Class TimeVal is a wrapper around UNIX timeval structure.
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::m_indent_step
u_short m_indent_step
Indentation step.
Definition Logger_Impl.h:236
ASSA::Logger_Impl::m_tz
int m_tz
Timezone: 0-GMT, 1-Local.
Definition Logger_Impl.h:248
ASSA::Logger_Impl::timestamp_enabled
bool timestamp_enabled(void) const
Definition Logger_Impl.h:168
ASSA::Logger_Impl::m_msgbuf
static char m_msgbuf[LOGGER_MAXLINE]
Static buffer for formatted message.
Definition Logger_Impl.h:233
ASSA::Logger_Impl::LOGGER_MAXLINE
static const unsigned int LOGGER_MAXLINE
Maximum length of the formatted message.
Definition Logger_Impl.h:149
ASSA::Logger_Impl::add_timestamp
virtual u_short add_timestamp(ostream &sink_)
Definition Logger_Impl.cpp:35
ASSA::TimeVal
Definition TimeVal.h:31
ASSA::TimeVal::tz
void tz(int tz_)
Set timezone.
Definition TimeVal.h:81
ASSA::TimeVal::fmtString
string fmtString(const char *fmt_=NULL) const
Format timeval structure into readable format.
Definition TimeVal.cpp:146
ASSA::TimeVal::gettimeofday
static TimeVal gettimeofday()
Shields off underlying OS differences in getting current time.
Definition TimeVal.cpp:44
ASSA::TimeVal::msec
void msec(long msec_)
Set microseconds.
Definition TimeVal.h:70
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::FUNC_EXIT
@ FUNC_EXIT
Definition LogMask.h:70