plptools
Loading...
Searching...
No Matches
log.cc
Go to the documentation of this file.
1/*
2 * This file is part of plptools.
3 *
4 * Copyright (C) 1999 Philip Proudman <philip.proudman@btinternet.com>
5 * Copyright (C) 1999-2001 Fritz Elfert <felfert@to.com>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * along with this program; if not, see <https://www.gnu.org/licenses/>.
19 *
20 */
21#include "config.h"
22
23#include "log.h"
24
25#include "ignore-value.h"
26
27#include <unistd.h>
28
29logbuf::logbuf(int loglevel, int fd) {
30 ptr = buf;
31 len = 0;
32 _use_syslog = true;
33 _level = loglevel;
34 _fd = fd;
35}
36
37int logbuf::overflow(int c) {
38 if (c == '\n') {
39 *ptr++ = '\n';
40 *ptr = '\0';
41 if (_use_syslog)
42 syslog(_level, "%s", buf);
43 else if (_fd != -1)
44 ignore_value(write(_fd, buf, len + 1));
45 ptr = buf;
46 len = 0;
47 return 0;
48 }
49 if ((len + 2) >= sizeof(buf))
50 return EOF;
51 *ptr++ = c;
52 len++;
53 return 0;
54}
bool _use_syslog
Log flag.
Definition: log.h:121
logbuf(int loglevel, int fd)
Constructs a new instance.
Definition: log.cc:29
char * ptr
Pointer to next char in buffer.
Definition: log.h:100
char buf[1024]
The internal buffer for holding messages.
Definition: log.h:127
unsigned int len
Current length of buffer.
Definition: log.h:105
int _fd
File descriptor to use when switched off.
Definition: log.h:116
int overflow(int c=EOF)
Called by the associated ostream to write a character.
Definition: log.cc:37
int _level
The log level to use with syslog.
Definition: log.h:110