aboutsummaryrefslogtreecommitdiff
path: root/lib/syslogstr.c
blob: 0483581af7dc80ba18db89022b77ff5c0517cfb2 (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
/* This file is part of Smap.
   Copyright (C) 2006-2007, 2010, 2014 Sergey Poznyakoff

   Smap is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3, or (at your option)
   any later version.

   Smap is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with Smap.  If not, see <http://www.gnu.org/licenses/>. */

#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <syslog.h>
#include <errno.h>
#include <string.h>
#include "smap/stream.h"
#include "smap/streamdef.h"

struct syslog_stream {
	struct _smap_stream base;
	int prio;
	char *pfx;
};

static int
_syslog_stream_write(struct _smap_stream *stream, const char *buf,
		      size_t size, size_t *pret)
{
	struct syslog_stream *lsp = (struct syslog_stream *)stream;

	*pret = size;
	if (size > 0 && buf[size-1] == '\n')
		size--;
	if (size == 0)
		return 0;
	if (lsp->pfx) {
		syslog(lsp->prio, "[%s] %*.*s", lsp->pfx, (int) size,
		       (int) size, buf);
	} else
		syslog(lsp->prio, "%*.*s", (int) size, (int) size, buf);
	return 0;
}

static void
_syslog_stream_destroy(struct _smap_stream *stream)
{
	struct syslog_stream *lsp = (struct syslog_stream *)stream;
	free(lsp->pfx);
}

int
smap_syslog_stream_create(smap_stream_t *pstream, int prio,
			  const char *pfx)
{
	struct syslog_stream *str =
		(struct syslog_stream *)
		  _smap_stream_create(sizeof(*str), SMAP_STREAM_WRITE);
	if (!str)
		return ENOMEM;
	if (pfx) {
		str->pfx = strdup(pfx);
		if (!str->pfx) {
			free(str);
			return ENOMEM;
		}
	} else
		str->pfx = NULL;
	str->prio = prio;
	str->base.write = _syslog_stream_write;
	str->base.done = _syslog_stream_destroy;
	*pstream = (smap_stream_t) str;
	smap_stream_set_buffer(*pstream, smap_buffer_line, 1024);
	return 0;
}

Return to:

Send suggestions and report system problems to the System administrator.