libassa  3.5.0
/build/buildd/libassa-3.5.0/assa/Logger_Impl.cpp
Go to the documentation of this file.
00001 // -*- c++ -*-
00002 //------------------------------------------------------------------------------
00003 //                              assa/Logger_Impl.cpp
00004 //------------------------------------------------------------------------------
00005 // $Id: Logger_Impl.cpp,v 1.5 2006/07/20 02:30:54 vlg Exp $
00006 //------------------------------------------------------------------------------
00007 //  Copyright (c) 2001 by Vladislav Grinchenko
00008 //
00009 //  This library is free software; you can redistribute it and/or
00010 //  modify it under the terms of the GNU Library General Public
00011 //  License as published by the Free Software Foundation; either
00012 //  version 2 of the License, or (at your option) any later version.
00013 //------------------------------------------------------------------------------
00014 
00015 #include <cstdio>
00016 #include <iostream>
00017 #include <iomanip>
00018 #include <string.h>             // strerror(3)
00019 
00020 #include "assa/TimeVal.h"
00021 #include "assa/Logger_Impl.h"
00022 
00023 #if defined (WIN32)
00024 #  include <windows.h>          // for vsnprintf() bug
00025 #endif
00026 
00027 using namespace ASSA;
00028 
00029 char Logger_Impl::m_msgbuf [LOGGER_MAXLINE];
00030 
00031 u_short 
00032 Logger_Impl::
00033 add_timestamp (ostream& sink_)
00034 {
00035     /*--- 'DD/MM/CC HH:MM:SS.MMMM ' - 23 chars ---*/
00036     u_short bytecount = 0;
00037 
00038     if (timestamp_enabled ()) {
00039         TimeVal tv = TimeVal::gettimeofday ();
00040         tv.tz (m_tz);
00041         sink_ << tv.fmtString ("%m/%d/%Y %H:%M:%S") << '.';
00042         char oldfill = sink_.fill('0');
00043         sink_ << std::setw (3) << (tv.msec () % 1000000)/1000 << ' ';
00044         sink_.fill (oldfill);
00045         bytecount = 23;
00046     }
00047     return bytecount;
00048 }
00049 
00050 u_short 
00051 Logger_Impl::
00052 indent_func_name (ostream&      sink_,  
00053                   const string& func_name_,
00054                   size_t        indent_level_,
00055                   marker_t      type_)
00056 {
00057     u_short bytecount = 0;
00058 
00059     if (func_name_.size ()) {
00060         u_int i = 1; 
00061         while (i < indent_level_) { 
00062             sink_ << '|';
00063             for (u_short j = 0; j < m_indent_step-1; j++) {
00064                 sink_ << ' ';
00065             }
00066             i++;
00067         }
00068         if (type_ == FUNC_ENTRY) {
00069             sink_ << '/' << func_name_ << "  ";
00070         }
00071         else if (type_ == FUNC_EXIT) {
00072             sink_ << '\\' << func_name_ << "  ";
00073         }
00074         else if (type_ == FUNC_MSG) {
00075             sink_ << '[' << func_name_ << "] ";
00076         }
00077         bytecount += indent_level_ * m_indent_step + func_name_.size () + 3;
00078     }
00079     return bytecount;
00080 }
00081 
00082 char* 
00083 Logger_Impl::
00084 format_msg (size_t      expected_sz_, 
00085             const char* fmt_, 
00086             va_list     vap_, 
00087             bool&       release_) // tell the caller it needs to release memory
00088 {
00089     char* msg = m_msgbuf;       // Use internal buffer
00090     int ret = 0;
00091 
00092     release_ = false;
00093     expected_sz_++;             // Expected size includes '\0'
00094 
00095     if (expected_sz_ >= LOGGER_MAXLINE) { // Allocate temporary buffer
00096         msg = new char [expected_sz_];
00097         release_ = true;
00098     }
00099 
00100     ret = ::vsnprintf (msg, expected_sz_, fmt_, vap_);
00101 #if NEVER
00102     if (ret < 0) {
00103         std::cout << "Logger_Impl: format_mg(expected_sz=" << expected_sz_
00104                   << ")=-1 failed! errno=" << errno << " ("
00105                   << strerror(errno) << "\n" << std::flush;
00106     }
00107 #endif
00108 
00109     return (ret < 0 ? NULL : msg);
00110 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines