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