libassa 3.5.1
Loading...
Searching...
No Matches
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
29using namespace ASSA;
30
31char Logger_Impl::m_msgbuf [LOGGER_MAXLINE];
32
35add_timestamp (ostream& sink_)
36{
37 /*--- 'DD/MM/CC HH:MM:SS.MMMM ' - 23 chars ---*/
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
54indent_func_name (ostream& sink_,
55 const string& func_name_,
56 size_t indent_level_,
58{
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 }
80 }
81 return bytecount;
82}
83
84char*
87 const char* fmt_,
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
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
Class TimeVal is a wrapper around UNIX timeval structure.
A wrapper class to provide AutoPtr with reference semantics.
Definition AutoPtr.h:32
char * format_msg(size_t expected_sz_, const char *fmt_, va_list vap_, bool &release_)
Format and put the message in the buffer.
virtual u_short indent_func_name(ostream &sink_, const string &funcname_, size_t indent_level_, marker_t type_)
u_short m_indent_step
Indentation step.
int m_tz
Timezone: 0-GMT, 1-Local.
bool timestamp_enabled(void) const
static char m_msgbuf[LOGGER_MAXLINE]
Static buffer for formatted message.
static const unsigned int LOGGER_MAXLINE
Maximum length of the formatted message.
virtual u_short add_timestamp(ostream &sink_)
static TimeVal gettimeofday()
Shields off underlying OS differences in getting current time.
Definition TimeVal.cpp:44
marker_t
Definition LogMask.h:67
@ FUNC_ENTRY
Definition LogMask.h:69
@ FUNC_MSG
Definition LogMask.h:68
@ FUNC_EXIT
Definition LogMask.h:70