libassa  3.5.1
Logger_Impl.cpp
Go to the documentation of this file.
1 // -*- c++ -*-
2 //------------------------------------------------------------------------------
3 // assa/Logger_Impl.cpp
4 //------------------------------------------------------------------------------
5 // $Id: Logger_Impl.cpp,v 1.7 2012/05/21 03:20:39 vlg Exp $
6 //------------------------------------------------------------------------------
7 // Copyright (c) Vladislav Grinchenko
8 //
9 // This library is free software; you can redistribute it and/or
10 // modify it under the terms of the GNU Library General Public
11 // License as published by the Free Software Foundation; either
12 // version 2 of the License, or (at your option) any later version.
13 //------------------------------------------------------------------------------
14 
15 #include <cstdio>
16 #include <iostream>
17 #include <iomanip>
18 #include <string.h> // strerror(3)
19 
20 #include "assa/TimeVal.h"
21 #include "assa/Logger_Impl.h"
22 
23 #if defined (WIN32)
24 # include <windows.h> // for vsnprintf() bug
25 #else
26 # include <stdio.h>
27 #endif
28 
29 using namespace ASSA;
30 
31 char Logger_Impl::m_msgbuf [LOGGER_MAXLINE];
32 
33 u_short
35 add_timestamp (ostream& sink_)
36 {
37  /*--- 'DD/MM/CC HH:MM:SS.MMMM ' - 23 chars ---*/
38  u_short bytecount = 0;
39 
40  if (timestamp_enabled ()) {
42  tv.tz (m_tz);
43  sink_ << tv.fmtString ("%m/%d/%Y %H:%M:%S") << '.';
44  char oldfill = sink_.fill('0');
45  sink_ << std::setw (3) << (tv.msec () % 1000000)/1000 << ' ';
46  sink_.fill (oldfill);
47  bytecount = 23;
48  }
49  return bytecount;
50 }
51 
52 u_short
54 indent_func_name (ostream& sink_,
55  const string& func_name_,
56  size_t indent_level_,
57  marker_t type_)
58 {
59  u_short bytecount = 0;
60 
61  if (func_name_.size ()) {
62  u_int i = 1;
63  while (i < indent_level_) {
64  sink_ << '|';
65  for (u_short j = 0; j < m_indent_step-1; j++) {
66  sink_ << ' ';
67  }
68  i++;
69  }
70  if (type_ == FUNC_ENTRY) {
71  sink_ << '/' << func_name_ << " ";
72  }
73  else if (type_ == FUNC_EXIT) {
74  sink_ << '\\' << func_name_ << " ";
75  }
76  else if (type_ == FUNC_MSG) {
77  sink_ << '[' << func_name_ << "] ";
78  }
79  bytecount += indent_level_ * m_indent_step + func_name_.size () + 3;
80  }
81  return bytecount;
82 }
83 
84 char*
86 format_msg (size_t expected_sz_,
87  const char* fmt_,
88  va_list vap_,
89  bool& release_) // tell the caller it needs to release memory
90 {
91  char* msg = m_msgbuf; // Use internal buffer
92  int ret = 0;
93 
94  release_ = false;
95  expected_sz_++; // Expected size includes '\0'
96 
97  if (expected_sz_ >= LOGGER_MAXLINE) { // Allocate temporary buffer
98  msg = new char [expected_sz_];
99  release_ = true;
100  }
101 
102  ret = ::vsnprintf (msg, expected_sz_, fmt_, vap_);
103 #if NEVER
104  if (ret < 0) {
105  std::cout << "Logger_Impl: format_mg(expected_sz=" << expected_sz_
106  << ")=-1 failed! errno=" << errno << " ("
107  << strerror(errno) << "\n" << std::flush;
108  }
109 #endif
110 
111  return (ret < 0 ? NULL : msg);
112 }
unsigned short u_short
Definition: Logger_Impl.h:39
unsigned int u_int
Definition: Logger_Impl.h:40
Class TimeVal is a wrapper around UNIX timeval structure.
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
virtual u_short indent_func_name(ostream &sink_, const string &funcname_, size_t indent_level_, marker_t type_)
Definition: Logger_Impl.cpp:54
u_short m_indent_step
Indentation step.
Definition: Logger_Impl.h:236
int m_tz
Timezone: 0-GMT, 1-Local.
Definition: Logger_Impl.h:248
bool timestamp_enabled(void) const
Definition: Logger_Impl.h:168
static char m_msgbuf[LOGGER_MAXLINE]
Static buffer for formatted message.
Definition: Logger_Impl.h:233
static const unsigned int LOGGER_MAXLINE
Maximum length of the formatted message.
Definition: Logger_Impl.h:149
virtual u_short add_timestamp(ostream &sink_)
Definition: Logger_Impl.cpp:35
void tz(int tz_)
Set timezone.
Definition: TimeVal.h:81
string fmtString(const char *fmt_=NULL) const
Format timeval structure into readable format.
Definition: TimeVal.cpp:146
static TimeVal gettimeofday()
Shields off underlying OS differences in getting current time.
Definition: TimeVal.cpp:44
void msec(long msec_)
Set microseconds.
Definition: TimeVal.h:70
Definition: Acceptor.h:40
Socket & flush(Socket &os_)
flush manipulator.
Definition: Socket.h:587
marker_t
Definition: LogMask.h:67
@ FUNC_ENTRY
Definition: LogMask.h:69
@ FUNC_MSG
Definition: LogMask.h:68
@ FUNC_EXIT
Definition: LogMask.h:70