Coverage Report

Created: 2023-06-07 06:03

/src/resiprocate/rutil/SysLogBuf.cxx
Line
Count
Source (jump to first uncovered line)
1
#include <cstdio>
2
#include "rutil/ResipAssert.h"
3
#include "rutil/SysLogBuf.hxx"
4
#include "rutil/Log.hxx"
5
6
#ifndef EOF
7
# define EOF (-1)
8
#endif
9
10
using resip::SysLogBuf;
11
12
SysLogBuf::SysLogBuf ()
13
 : mLevel(Log::Debug),
14
   mAppName(""),
15
   mFacility(LOG_DAEMON)
16
0
{
17
0
   init();
18
0
}
19
20
SysLogBuf::SysLogBuf (const resip::Data& ident, int facility)
21
 : mLevel(Log::Debug),
22
   mAppName(ident),
23
   mFacility(facility)
24
0
{
25
0
   init();
26
0
}
27
28
void
29
SysLogBuf::init()
30
0
{
31
0
#if !defined(WIN32)
32
0
   setp(buffer,buffer+Size);
33
0
   const char* _ident = 0;
34
0
   if(!mAppName.empty())
35
0
   {
36
0
      _ident = mAppName.c_str();
37
0
   }
38
0
   openlog (_ident, LOG_NDELAY | LOG_PID, mFacility);
39
0
#endif
40
0
}
41
      
42
SysLogBuf::~SysLogBuf()
43
0
{
44
0
}
45
      
46
int 
47
SysLogBuf::sync()
48
0
{
49
0
#if !defined(WIN32)
50
   // Default to debug level for Stack, Debug and unrecognised values
51
0
   int _level = LOG_DEBUG;
52
   // For efficiency, we check mLevel in decreasing order of frequency,
53
   // anticipating that Stack is the most common and Crit is the least
54
   // common.
55
0
   switch(mLevel)
56
0
   {
57
0
      case Log::Stack:
58
0
      case Log::Debug:
59
         // This is just here to avoid traversing the rest
60
         // of the switch block for every Stack or Debug message.
61
         // They will just be logged with the default LOG_DEBUG
62
         // specified above.
63
0
         break;
64
0
      case Log::Info:
65
0
         _level = LOG_INFO;
66
0
         break;
67
0
      case Log::Warning:
68
0
         _level = LOG_WARNING;
69
0
         break;
70
0
      case Log::Err:
71
0
         _level = LOG_ERR;
72
0
         break;
73
0
      case Log::Crit:
74
0
         _level = LOG_CRIT;
75
0
         break;
76
0
      default:
77
         // just let it use the default value defined above
78
0
         break;
79
0
   }
80
0
   *(pptr()) = 0;
81
0
   syslog (mFacility | _level, "%s", pbase());
82
   // Set mLevel back to the default level for the next log entry
83
   // in case it is not explicitly specified next time.
84
0
   mLevel = Log::Debug;
85
0
   setp(buffer, buffer+Size);
86
#else
87
   resip_assert(0);
88
#endif
89
0
   return 0;
90
0
}
91
     
92
int 
93
SysLogBuf::overflow (int c)
94
0
{
95
0
   sync();
96
0
   if (c != EOF) 
97
0
   {
98
0
      *pptr() = static_cast<unsigned char>(c);
99
0
      pbump(1);
100
0
   }
101
0
   return c;
102
0
}
103
104
std::ostream& resip::operator<< (std::ostream& os, const resip::Log::Level& level)
105
0
{
106
   // FIXME - we should probably find a more precise way to make sure
107
   // that this can never be done for the wrong type of stream.
108
   // For now, we rely on Log.cxx checking if mLogger is of type Syslog
109
   // and not sending Level to the stream otherwise.
110
0
   static_cast<SysLogBuf *>(os.rdbuf())->mLevel = level;
111
0
   return os;
112
0
}
113
114
/* ====================================================================
115
 * The Vovida Software License, Version 1.0 
116
 * 
117
 * Copyright (c) 2000-2005 Vovida Networks, Inc.  All rights reserved.
118
 * 
119
 * Redistribution and use in source and binary forms, with or without
120
 * modification, are permitted provided that the following conditions
121
 * are met:
122
 * 
123
 * 1. Redistributions of source code must retain the above copyright
124
 *    notice, this list of conditions and the following disclaimer.
125
 * 
126
 * 2. Redistributions in binary form must reproduce the above copyright
127
 *    notice, this list of conditions and the following disclaimer in
128
 *    the documentation and/or other materials provided with the
129
 *    distribution.
130
 * 
131
 * 3. The names "VOCAL", "Vovida Open Communication Application Library",
132
 *    and "Vovida Open Communication Application Library (VOCAL)" must
133
 *    not be used to endorse or promote products derived from this
134
 *    software without prior written permission. For written
135
 *    permission, please contact vocal@vovida.org.
136
 *
137
 * 4. Products derived from this software may not be called "VOCAL", nor
138
 *    may "VOCAL" appear in their name, without prior written
139
 *    permission of Vovida Networks, Inc.
140
 * 
141
 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED
142
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
143
 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE AND
144
 * NON-INFRINGEMENT ARE DISCLAIMED.  IN NO EVENT SHALL VOVIDA
145
 * NETWORKS, INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT DAMAGES
146
 * IN EXCESS OF $1,000, NOR FOR ANY INDIRECT, INCIDENTAL, SPECIAL,
147
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
148
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
149
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
150
 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
151
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
152
 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
153
 * DAMAGE.
154
 * 
155
 * ====================================================================
156
 * 
157
 * This software consists of voluntary contributions made by Vovida
158
 * Networks, Inc. and many individuals on behalf of Vovida Networks,
159
 * Inc.  For more information on Vovida Networks, Inc., please see
160
 * <http://www.vovida.org/>.
161
 *
162
 */