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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
|
/* This file is part of Gamma.
Copyright (C) 2002-2018 Sergey Poznyakoff
Gamma 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.
Gamma 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 Gamma. If not, see <http://www.gnu.org/licenses/>. */
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <libguile.h>
#include <stdio.h>
#include <string.h>
#include <syslog.h>
#ifndef HAVE_SCM_T_OFF
typedef off_t scm_t_off;
#endif
#define GAMMA_SYSLOG_PORT_BUFSIZE 1024
static scm_t_port_type *scm_syslog_port_type;
struct _gamma_syslog_port {
int prio;
};
static SCM
_make_syslog_port(int prio)
{
struct _gamma_syslog_port *dp;
dp = scm_gc_typed_calloc (struct _gamma_syslog_port);
dp->prio = prio;
return scm_c_make_port(scm_syslog_port_type,
SCM_OPN | SCM_WRTNG | SCM_BUFLINE,
(scm_t_bits) dp);
}
#define SYSLOG_PORT(x) ((struct _gamma_syslog_port *) SCM_STREAM (x))
static void
_syslog_port_close(SCM port)
{
struct _gamma_syslog_port *dp = SYSLOG_PORT(port);
/* nothing */
}
static size_t
_syslog_port_write(SCM port, SCM src, size_t start, size_t count)
{
struct _gamma_syslog_port *dp = SYSLOG_PORT(port);
syslog(dp->prio, "%*.*s",
(int) count,
(int) count,
SCM_BYTEVECTOR_CONTENTS(src) + start);
return count;
}
static scm_t_off
_syslog_port_seek(SCM port, scm_t_off offset, int whence)
{
return (scm_t_off) -1;
}
static int
_syslog_port_print(SCM exp, SCM port, scm_print_state *pstate)
{
scm_puts("#<", port);
scm_print_port_mode(exp, port);
scm_puts("Gamma syslog port>", port);
return 1;
}
SCM_DEFINE_PUBLIC(scm_open_syslog_port, "open-syslog-port", 1, 0, 0,
(SCM prio),
"Create and open an output port connected to syslog priority "
"@code{prio}.")
#define FUNC_NAME s_scm_open_syslog_port
{
SCM_ASSERT(scm_is_integer(prio), prio, SCM_ARG1, FUNC_NAME);
return _make_syslog_port(scm_to_int(prio));
}
#undef FUNC_NAME
void
_gamma_init_syslog_port()
{
scm_syslog_port_type = scm_make_port_type("syslog-port",
NULL,
_syslog_port_write);
scm_set_port_print(scm_syslog_port_type, _syslog_port_print);
scm_set_port_close(scm_syslog_port_type, _syslog_port_close);
scm_set_port_seek(scm_syslog_port_type, _syslog_port_seek);
#include <syslog-port.x>
}
|