/home/vlg/develop/libASSA/libassa/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 <iostream>
00016 #include <iomanip>
00017 #include <string.h>             // strerror(3)
00018 
00019 #include "assa/TimeVal.h"
00020 #include "assa/Logger_Impl.h"
00021 
00022 #if defined (WIN32)
00023 #  include <windows.h>          // for vsnprintf() bug
00024 #endif
00025 
00026 using namespace ASSA;
00027 
00028 char Logger_Impl::m_msgbuf [LOGGER_MAXLINE];
00029 
00030 u_short 
00031 Logger_Impl::
00032 add_timestamp (ostream& sink_)
00033 {
00034     /*--- 'DD/MM/CC HH:MM:SS.MMMM ' - 23 chars ---*/
00035     u_short bytecount = 0;
00036 
00037     if (timestamp_enabled ()) {
00038         TimeVal tv = TimeVal::gettimeofday ();
00039         tv.tz (m_tz);
00040         sink_ << tv.fmtString ("%m/%d/%Y %H:%M:%S") << '.';
00041         char oldfill = sink_.fill('0');
00042         sink_ << std::setw (3) << (tv.msec () % 1000000)/1000 << ' ';
00043         sink_.fill (oldfill);
00044         bytecount = 23;
00045     }
00046     return bytecount;
00047 }
00048 
00049 u_short 
00050 Logger_Impl::
00051 indent_func_name (ostream&      sink_,  
00052                   const string& func_name_,
00053                   size_t        indent_level_,
00054                   marker_t      type_)
00055 {
00056     u_short bytecount = 0;
00057 
00058     if (func_name_.size ()) {
00059         u_int i = 1; 
00060         while (i < indent_level_) { 
00061             sink_ << '|';
00062             for (u_short j = 0; j < m_indent_step-1; j++) {
00063                 sink_ << ' ';
00064             }
00065             i++;
00066         }
00067         if (type_ == FUNC_ENTRY) {
00068             sink_ << '/' << func_name_ << "  ";
00069         }
00070         else if (type_ == FUNC_EXIT) {
00071             sink_ << '\\' << func_name_ << "  ";
00072         }
00073         else if (type_ == FUNC_MSG) {
00074             sink_ << '[' << func_name_ << "] ";
00075         }
00076         bytecount += indent_level_ * m_indent_step + func_name_.size () + 3;
00077     }
00078     return bytecount;
00079 }
00080 
00081 char* 
00082 Logger_Impl::
00083 format_msg (size_t      expected_sz_, 
00084             const char* fmt_, 
00085             va_list     vap_, 
00086             bool&       release_) // tell the caller it needs to release memory
00087 {
00088     char* msg = m_msgbuf;       // Use internal buffer
00089     int ret = 0;
00090 
00091     release_ = false;
00092     expected_sz_++;             // Expected size includes '\0'
00093 
00094     if (expected_sz_ >= LOGGER_MAXLINE) { // Allocate temporary buffer
00095         msg = new char [expected_sz_];
00096         release_ = true;
00097     }
00098 
00099     ret = ::vsnprintf (msg, expected_sz_, fmt_, vap_);
00100 #if NEVER
00101     if (ret < 0) {
00102         std::cout << "Logger_Impl: format_mg(expected_sz=" << expected_sz_
00103                   << ")=-1 failed! errno=" << errno << " ("
00104                   << strerror(errno) << "\n" << std::flush;
00105     }
00106 #endif
00107 
00108     return (ret < 0 ? NULL : msg);
00109 }

Generated on Sun Aug 13 15:08:00 2006 for libassa by  doxygen 1.4.6