summaryrefslogtreecommitdiff
path: root/src/syslogdevice.cc
blob: 49162a4e4bb6d6baffd59d5ec8b408d466d0ee24 (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
/**
 *  @file syslogdevice.cc
 *  @brief  Implementation of the SyslogDevice class.
 *  @author Andreas Aardal Hanssen
 *  @date 2002, 2003
 */

#include "syslogdevice.h"

#include <string>

#include <syslog.h>

using namespace Binc;
using std::string;

string SyslogDevice::ident;

SyslogDevice::SyslogDevice(int f, const char *i, int o, int fa)
  : IODevice(f)
  , option(o)
  , facility(fa)
  , priority(LOG_INFO)
{
  ident = i;
  openlog(ident.c_str(), option, facility);
}

SyslogDevice::~SyslogDevice(void)
{
  closelog();
}

string SyslogDevice::service(void) const
{
  return "log";
}

bool SyslogDevice::waitForWrite(void) const
{
  return true;
}

bool SyslogDevice::waitForRead(void) const
{
  return false;
}

IODevice::WriteResult SyslogDevice::write(void)
{
  string out;
  string::const_iterator i = outputBuffer.str().begin();
  string::const_iterator ie = outputBuffer.str().end();

  for (; i != ie; ++i) {
    if (*i == '\n') {
      syslog(priority, out.c_str(), out.size());
      out = "";
    } else if (*i != '\r') {
      out += *i;
    }
  }

  if (out != "") syslog(priority, out.c_str(), out.size());

  outputBuffer.clear();
  return WriteDone;
}

bool SyslogDevice::fillInputBuffer(void)
{
  return false;
}