aboutsummaryrefslogtreecommitdiffstats
path: root/vespalog/src/vespa/log/log-target-fd.cpp
blob: 74f761152792618216ea50475351cd6c6350c975 (plain) (blame)
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
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#include <sys/types.h>
#include <unistd.h>
#include <fcntl.h>
#include <cstring>
#include <cstdlib>

#include "log.h"
LOG_SETUP(".log");
#include "log-target-fd.h"
#include "internal.h"

namespace ns_log {

LogTargetFd::LogTargetFd(int fd_spec, const char *target)
    : LogTarget(target),
      _fd(-1), _istty(false)
{
    _fd = dup(fd_spec);
    if (_fd == -1) {
        throwInvalid("Bad target for LogTargetFd: '%s'", target);
    }
    if (isatty(_fd) == 1) {
        _istty = true;
    }
    fcntl(_fd, F_SETFD, FD_CLOEXEC);
}

LogTargetFd::~LogTargetFd()
{
    close(_fd);
}


// When writing to file descriptors, there is really not much to do.
// No log rotation is supported (at least not directly).
int
LogTargetFd::write(const char *buf, int bufLen)
{
    return ::write(_fd, buf, bufLen);
}

} // end namespace ns_log