00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015 #include <iostream>
00016 #include <iomanip>
00017 #include <string.h>
00018
00019 #include "assa/TimeVal.h"
00020 #include "assa/Logger_Impl.h"
00021
00022 #if defined (WIN32)
00023 # include <windows.h>
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
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_)
00089 {
00090 char* msg = m_msgbuf;
00091 int ret = 0;
00092
00093 release_ = false;
00094 expected_sz_++;
00095
00096 if (expected_sz_ >= LOGGER_MAXLINE) {
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 }