mu_logger.c
3.76 KB
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/* GNU Mailutils -- a suite of utilities for electronic mail
Copyright (C) 1999, 2000, 2001, 2006, 2007, 2010 Free Software
Foundation, Inc.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 3 of the License, or (at your option) any later version.
This library 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
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General
Public License along with this library; if not, write to the
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301 USA */
#include "mu_scm.h"
#include <syslog.h>
SCM_DEFINE (scm_mu_openlog, "mu-openlog", 3, 0, 0,
(SCM IDENT, SCM OPTION, SCM FACILITY),
"Opens a connection to the system logger for Guile program.\n"
"IDENT, OPTION and FACILITY have the same meaning as in openlog(3)")
#define FUNC_NAME s_scm_mu_openlog
{
char *ident, *ident_mem = NULL;
int option, facility;
if (IDENT == SCM_BOOL_F)
ident = "libmu_scm";
else
{
SCM_ASSERT (scm_is_string (IDENT), IDENT, SCM_ARG1, FUNC_NAME);
ident = ident_mem = scm_to_locale_string (IDENT);
}
if (scm_is_integer (OPTION))
option = scm_to_int32 (OPTION);
else if (SCM_BIGP (OPTION))
option = (int) scm_i_big2dbl (OPTION);
else
SCM_ASSERT (0, OPTION, SCM_ARG2, FUNC_NAME);
if (scm_is_integer (FACILITY))
facility = scm_to_int32 (FACILITY);
else if (SCM_BIGP (FACILITY))
facility = (int) scm_i_big2dbl (FACILITY);
else
SCM_ASSERT (0, FACILITY, SCM_ARG3, FUNC_NAME);
openlog (ident, option, facility);
free (ident_mem);
return SCM_UNSPECIFIED;
}
#undef FUNC_NAME
SCM_DEFINE (scm_mu_logger, "mu-logger", 2, 0, 0,
(SCM PRIO, SCM TEXT),
"Distributes TEXT via syslogd priority PRIO.")
#define FUNC_NAME s_scm_mu_logger
{
int prio;
char *str;
if (PRIO == SCM_BOOL_F)
prio = LOG_INFO;
else if (scm_is_integer (PRIO))
prio = scm_to_int32 (PRIO);
else if (SCM_BIGP (PRIO))
prio = (int) scm_i_big2dbl (PRIO);
else
SCM_ASSERT (0, PRIO, SCM_ARG1, FUNC_NAME);
SCM_ASSERT (scm_is_string (TEXT), TEXT, SCM_ARG2, FUNC_NAME);
str = scm_to_locale_string (TEXT);
syslog (prio, "%s", str);
return SCM_UNSPECIFIED;
}
#undef FUNC_NAME
SCM_DEFINE (scm_mu_closelog, "mu-closelog", 0, 0, 0,
(),
"Closes the channel to the system logger opened by @code{mu-openlog}.")
#define FUNC_NAME s_scm_mu_closelog
{
closelog ();
return SCM_UNSPECIFIED;
}
#undef FUNC_NAME
static struct
{
char *name;
int facility;
} syslog_kw[] = {
{ "LOG_USER", LOG_USER },
{ "LOG_DAEMON", LOG_DAEMON },
{ "LOG_AUTH", LOG_AUTH },
{ "LOG_LOCAL0", LOG_LOCAL0 },
{ "LOG_LOCAL1", LOG_LOCAL1 },
{ "LOG_LOCAL2", LOG_LOCAL2 },
{ "LOG_LOCAL3", LOG_LOCAL3 },
{ "LOG_LOCAL4", LOG_LOCAL4 },
{ "LOG_LOCAL5", LOG_LOCAL5 },
{ "LOG_LOCAL6", LOG_LOCAL6 },
{ "LOG_LOCAL7", LOG_LOCAL7 },
/* severity */
{ "LOG_EMERG", LOG_EMERG },
{ "LOG_ALERT", LOG_ALERT },
{ "LOG_CRIT", LOG_CRIT },
{ "LOG_ERR", LOG_ERR },
{ "LOG_WARNING", LOG_WARNING },
{ "LOG_NOTICE", LOG_NOTICE },
{ "LOG_INFO", LOG_INFO },
{ "LOG_DEBUG", LOG_DEBUG },
/* options */
{ "LOG_CONS", LOG_CONS },
{ "LOG_NDELAY", LOG_NDELAY },
{ "LOG_PID", LOG_PID }
};
void
mu_scm_logger_init ()
{
int i;
for (i = 0; i < sizeof (syslog_kw)/sizeof (syslog_kw[0]); i++)
scm_c_define (syslog_kw[i].name, scm_from_int (syslog_kw[i].facility));
#include <mu_logger.x>
}