libassa
3.5.1
Main Page
Namespaces
Classes
Files
File List
File Members
All
Classes
Namespaces
Files
Functions
Variables
Typedefs
Enumerations
Enumerator
Friends
Macros
assa
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
34
Logger_Impl::
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
()) {
41
TimeVal
tv =
TimeVal::gettimeofday
();
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
53
Logger_Impl::
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
*
85
Logger_Impl::
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
}
Generated on Wed Jun 13 2012 09:34:05 for libassa by
1.8.1