summaryrefslogtreecommitdiff
path: root/src/KM_log.cpp
blob: 82d67d38ce734965ea3a8d475f9cfe9966f7e5e7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
/*
Copyright (c) 2004-2011, John Hurst
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
   notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
   notice, this list of conditions and the following disclaimer in the
   documentation and/or other materials provided with the distribution.
3. The name of the author may not be used to endorse or promote products
   derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
  /*! \file    KM_log.cpp
    \version $Id: KM_log.cpp,v 1.17 2012/09/20 21:19:23 jhurst Exp $
    \brief   message logging API
  */

#include <asdcp/KM_util.h>
#include <asdcp/KM_log.h>
#include <asdcp/KM_mutex.h>
#include <sys/types.h>
#include <string.h>
#include <stdarg.h>
#include <iostream>
#include <sstream>

#ifdef KM_WIN32
#define getpid GetCurrentProcessId
#else
#include <unistd.h>
#endif

//------------------------------------------------------------------------------------------
//

void
Kumu::ILogSink::vLogf(LogType_t type, const char* fmt, va_list* list)
{
  char buf[MaxLogLength];
  vsnprintf(buf, MaxLogLength, fmt, *list);

  WriteEntry(LogEntry(getpid(), type, buf));
}

//------------------------------------------------------------------------------------------
//

static Kumu::Mutex     s_DefaultLogSinkLock;
static Kumu::ILogSink* s_DefaultLogSink = 0;
static Kumu::StdioLogSink s_StderrLogSink;

//
void
Kumu::SetDefaultLogSink(ILogSink* Sink)
{
  AutoMutex L(s_DefaultLogSinkLock);
  s_DefaultLogSink = Sink;
}

// Returns the internal default sink.
Kumu::ILogSink&
Kumu::DefaultLogSink()
{
  AutoMutex L(s_DefaultLogSinkLock);

  if ( s_DefaultLogSink == 0 )
    s_DefaultLogSink = &s_StderrLogSink;

  return *s_DefaultLogSink;
}


//------------------------------------------------------------------------------------------
//

void
Kumu::EntryListLogSink::WriteEntry(const LogEntry& Entry)
{
  AutoMutex L(m_lock);
  WriteEntryToListeners(Entry);

  if ( Entry.TestFilter(m_filter) )
    m_Target.push_back(Entry);
}

//------------------------------------------------------------------------------------------
//

void
Kumu::StdioLogSink::WriteEntry(const LogEntry& Entry)
{
  std::string buf;
  AutoMutex L(m_lock);
  WriteEntryToListeners(Entry);

  if ( Entry.TestFilter(m_filter) )
    {
      Entry.CreateStringWithOptions(buf, m_options);
      fputs(buf.c_str(), m_stream);
      fflush(m_stream);
    }
}

//---------------------------------------------------------------------------------

#ifdef KM_WIN32
//
// http://www.codeguru.com/forum/showthread.php?t=231165
//
void
Kumu::WinDbgLogSink::WriteEntry(const LogEntry& Entry)
{
  std::string buf;
  AutoMutex L(m_lock);
  WriteEntryToListeners(Entry);

  if ( Entry.TestFilter(m_filter) )
    {
      Entry.CreateStringWithOptions(buf, m_options);
      ::OutputDebugStringA(buf.c_str());
    }
}
#endif

//------------------------------------------------------------------------------------------
//

#ifndef KM_WIN32
//
void
Kumu::StreamLogSink::WriteEntry(const LogEntry& Entry)
{
  std::string buf;
  AutoMutex L(m_lock);
  WriteEntryToListeners(Entry);

  if ( Entry.TestFilter(m_filter) )
    {
      Entry.CreateStringWithOptions(buf, m_options);
      write(m_fd, buf.c_str(), buf.size());
    }
}

// foolin with symbols
//------------------------------------------------------------------------------------------
#include <syslog.h>
int const SYSLOG_ALERT = LOG_ALERT;
int const SYSLOG_CRIT = LOG_CRIT;
int const SYSLOG_ERR = LOG_ERR;
int const SYSLOG_WARNING = LOG_WARNING;
int const SYSLOG_NOTICE = LOG_NOTICE;
int const SYSLOG_INFO = LOG_INFO;
int const SYSLOG_DEBUG = LOG_DEBUG;
#undef LOG_ALERT
#undef LOG_CRIT
#undef LOG_ERR
#undef LOG_WARNING
#undef LOG_NOTICE
#undef LOG_INFO
#undef LOG_DEBUG
//------------------------------------------------------------------------------------------

Kumu::SyslogLogSink::SyslogLogSink(const std::string& source_name, int facility)
{
  if ( facility == 0 )
    facility = LOG_DAEMON;

  openlog(source_name.c_str(), LOG_CONS|LOG_NDELAY||LOG_PID, facility);
}

Kumu::SyslogLogSink::~SyslogLogSink()
{
  closelog();
}

//
void
Kumu::SyslogLogSink::WriteEntry(const LogEntry& Entry)
{
  int priority;

  switch ( Entry.Type )
    {
    case Kumu::LOG_ALERT:   priority = SYSLOG_ALERT; break;
    case Kumu::LOG_CRIT:    priority = SYSLOG_CRIT; break;
    case Kumu::LOG_ERROR:   priority = SYSLOG_ERR; break;
    case Kumu::LOG_WARN:    priority = SYSLOG_WARNING; break;
    case Kumu::LOG_NOTICE:  priority = SYSLOG_NOTICE; break;
    case Kumu::LOG_INFO:    priority = SYSLOG_INFO; break;
    case Kumu::LOG_DEBUG:   priority = SYSLOG_DEBUG; break;
    default: break;
    }

  AutoMutex L(m_lock);
  WriteEntryToListeners(Entry);

  if ( Entry.TestFilter(m_filter) )
    {
      syslog(priority, "%s", Entry.Msg.substr(0, Entry.Msg.size() - 1).c_str());
    }
}

//
int
Kumu::SyslogNameToFacility(const std::string& facility_name)
{
  if ( facility_name == "LOG_DAEMON" ) return LOG_DAEMON;
  if ( facility_name == "LOG_LOCAL0" ) return LOG_LOCAL0;
  if ( facility_name == "LOG_LOCAL1" ) return LOG_LOCAL1;
  if ( facility_name == "LOG_LOCAL2" ) return LOG_LOCAL2;
  if ( facility_name == "LOG_LOCAL3" ) return LOG_LOCAL3;
  if ( facility_name == "LOG_LOCAL4" ) return LOG_LOCAL4;
  if ( facility_name == "LOG_LOCAL5" ) return LOG_LOCAL5;
  if ( facility_name == "LOG_LOCAL6" ) return LOG_LOCAL6;
  if ( facility_name == "LOG_LOCAL7" ) return LOG_LOCAL7;

  DefaultLogSink().Error("Unsupported facility name: %s, using default value LOG_DAEMON\n", facility_name.c_str());
  return LOG_DAEMON;
}

#endif

//------------------------------------------------------------------------------------------

//
std::basic_ostream<char, std::char_traits<char> >&
Kumu::operator<<(std::basic_ostream<char, std::char_traits<char> >& strm, LogEntry const& Entry)
{
  std::basic_ostringstream<char, std::char_traits<char> > s;
  s.copyfmt(strm);
  s.width(0);
  std::string buf;

  s << Entry.CreateStringWithOptions(buf, LOG_OPTION_ALL);

  strm << s.str();
  return strm;
}

//------------------------------------------------------------------------------------------


//
bool
Kumu::LogEntry::TestFilter(i32_t filter) const
{
  switch ( Type )
    {
    case LOG_CRIT:
      if ( (filter & LOG_ALLOW_CRIT) == 0 )
	return false;
      break;

    case LOG_ALERT:
      if ( (filter & LOG_ALLOW_ALERT) == 0 )
	return false;
      break;

    case LOG_NOTICE:
      if ( (filter & LOG_ALLOW_NOTICE) == 0 )
	return false;
      break;

    case LOG_ERROR:
      if ( (filter & LOG_ALLOW_ERROR) == 0 )
	return false;
      break;

    case LOG_WARN:
      if ( (filter & LOG_ALLOW_WARN) == 0 )
	return false;
      break;

    case LOG_INFO:
      if ( (filter & LOG_ALLOW_INFO) == 0 )
	return false;
      break;

    case LOG_DEBUG:
      if ( (filter & LOG_ALLOW_DEBUG) == 0 )
	return false;
      break;

    default:
      break;
    }

 return true;
}

//
std::string&
Kumu::LogEntry::CreateStringWithOptions(std::string& out_buf, i32_t opt) const
{
  out_buf.erase();

  if ( opt != 0 )
    {
      char buf[64];

      if ( (opt & LOG_OPTION_TIMESTAMP) != 0 )
	{
	  Timestamp Now;
	  out_buf += Now.EncodeString(buf, 64);
	}

      if ( (opt & LOG_OPTION_PID) != 0 )
	{
	  if ( ! out_buf.empty() )  out_buf += " ";
	  snprintf(buf, 64, "%d", PID);
	  out_buf += buf;
	}

      if ( (opt & LOG_OPTION_TYPE) != 0 )
	{
	  if ( ! out_buf.empty() )  out_buf += " ";

	  switch ( Type )
	    {
	    case LOG_CRIT:   out_buf += "CRT";      break;
	    case LOG_ALERT:  out_buf += "ALR";      break;
	    case LOG_NOTICE: out_buf += "NTC";      break;
	    case LOG_ERROR:  out_buf += "ERR";      break;
	    case LOG_WARN:   out_buf += "WRN";      break;
	    case LOG_INFO:   out_buf += "INF";      break;
	    case LOG_DEBUG:  out_buf += "DBG";      break;
	    default:	     out_buf += "DFL";      break;
	    }
	}

      out_buf.insert(0, "[");
      out_buf += "]: ";
    }

  out_buf += Msg;
  return out_buf;
}


//
ui32_t
Kumu::LogEntry::ArchiveLength() const
{
  return sizeof(ui32_t)
    + EventTime.ArchiveLength()
    + sizeof(ui32_t)
    + sizeof(ui32_t) + Msg.size();
}

//
bool
Kumu::LogEntry::Archive(Kumu::MemIOWriter* Writer) const
{
  if ( ! Writer->WriteUi32BE(PID) ) return false;
  if ( ! EventTime.Archive(Writer) ) return false;
  if ( ! Writer->WriteUi32BE(Type) ) return false;
  if ( ! ArchiveString(*Writer, Msg) ) return false;
  return true;
}

//
bool
Kumu::LogEntry::Unarchive(Kumu::MemIOReader* Reader)
{
  if ( ! Reader->ReadUi32BE(&PID) ) return false;
  if ( ! EventTime.Unarchive(Reader) ) return false;
  if ( ! Reader->ReadUi32BE((ui32_t*)&Type) ) return false;
  if ( ! UnarchiveString(*Reader, Msg) ) return false;
  return true;
}

//
// end
//