summaryrefslogtreecommitdiff
path: root/src/multilogdevice.cc
blob: 75317972c26266c94c1ce6298ed8b8dbc7dbc6e0 (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
/**
 *  @file multilogdevice.cc
 *  @brief  Implementation of the MultilogDevice class
 *  @author Andreas Aardal Hanssen
 *  @date 2003/2023
 */

#include "multilogdevice.h"

#include <string>

#include <errno.h>

#include <sys/select.h>
#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>

using namespace Binc;

MultilogDevice::MultilogDevice(int f) : IODevice(f) {}

MultilogDevice::~MultilogDevice() {}

std::string MultilogDevice::service() const
{
  return "log";
}

bool MultilogDevice::waitForWrite() const
{
  fd_set writeMask;
  FD_ZERO(&writeMask);
  FD_SET(fileno(stderr), &writeMask);

  struct timeval tv;
  tv.tv_sec = getTimeout();
  tv.tv_usec = 0;

  int result = select(fileno(stderr) + 1,
                      nullptr,
                      &writeMask,
                      nullptr,
                      tv.tv_sec ? &tv : nullptr);

  return result > 0;
}

bool MultilogDevice::waitForRead() const
{
  return false;
}

IODevice::WriteResult MultilogDevice::write()
{
  for (;;) {
    ssize_t wrote = ::write(fileno(stderr),
                            outputBuffer.str().c_str(),
                            outputBuffer.getSize());

    if (wrote == -1) {
      if (errno == EINTR)
        continue;
      else
        return WriteError;
    }

    if ((unsigned int)wrote == outputBuffer.getSize()) {
      outputBuffer.clear();
      return WriteDone;
    }

    outputBuffer.popString(wrote);
    return WriteWait;
  }
}

bool MultilogDevice::fillInputBuffer()
{
  return false;
}