aboutsummaryrefslogtreecommitdiff
path: root/src/syslog.c
blob: b50762fb7a80fc5a38146f10508255042793178d (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
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/* This file is part of Gamma.
   Copyright (C) 2002, 2010 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 <syslog.h>

static char *log_tag;

SCM_DEFINE(scm_openlog, "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_openlog
{
	SCM_ASSERT(scm_is_string(IDENT), IDENT, SCM_ARG1, FUNC_NAME);
	if (log_tag)
		free(log_tag);
	log_tag = scm_to_locale_string(IDENT);
	SCM_ASSERT(scm_is_integer(OPTION), OPTION, SCM_ARG2, FUNC_NAME);
	SCM_ASSERT(scm_is_integer(FACILITY), FACILITY, SCM_ARG3, FUNC_NAME);
	openlog(log_tag, scm_to_int(OPTION), scm_to_int(FACILITY));
	return SCM_UNSPECIFIED;
}
#undef FUNC_NAME

SCM_DEFINE(scm_openlog_p, "openlog?", 0, 0, 0,
	   (),
	   "Returns #t if @code{openlog} was called.")
#define FUNC_NAME s_scm_openlog_p
{
	return log_tag ? SCM_BOOL_T : SCM_BOOL_F;
}
#undef FUNC_NAME

SCM_DEFINE(scm_syslog_tag, "syslog-tag", 0, 0, 0,
	   (),
	   "Returns the tag given to the recent @code{openlog}.")
#define FUNC_NAME s_scm_syslog_tag
{
	if (!log_tag)
		scm_misc_error("syslog-tag",
			       "openlog have not been called",
			       SCM_EOL);
	return scm_makfrom0str(log_tag);
}
#undef FUNC_NAME

SCM_DEFINE(scm_syslog, "syslog", 2, 0, 0,
	  (SCM PRIO, SCM TEXT),
	  "Distributes TEXT via syslogd priority PRIO.")
#define FUNC_NAME s_scm_syslog
{
	int prio;
	char *str;

	SCM_ASSERT(scm_is_integer(PRIO), PRIO, SCM_ARG1, FUNC_NAME);
	prio = scm_to_int(PRIO);
  
	SCM_ASSERT(scm_is_string(TEXT), TEXT, SCM_ARG2, FUNC_NAME);
	str = scm_to_locale_string(TEXT);
	syslog(prio, "%s", str);
	free(str);
	return SCM_UNSPECIFIED;
}
#undef FUNC_NAME

SCM_DEFINE(scm_closelog, "closelog", 0, 0, 0,
	   (),
	   "Closes the channel to the system logger opened by @code{openlog}.")
#define FUNC_NAME s_scm_closelog
{
	closelog();
	if (log_tag) {
		free(log_tag);
		log_tag = NULL;
	}
	return SCM_UNSPECIFIED;
}
#undef FUNC_NAME


static struct {
	int facility;
	char *name;
} syslog_kw[] = {
#define GAMMA_CONST(s) { s, #s }
	GAMMA_CONST(LOG_AUTH),
#ifdef LOG_AUTHPRIV
	GAMMA_CONST(LOG_AUTHPRIV),
#endif
	GAMMA_CONST(LOG_CRON),
	GAMMA_CONST(LOG_DAEMON),
#ifdef LOG_FTP
	GAMMA_CONST(LOG_FTP),
#endif
	GAMMA_CONST(LOG_USER),
	GAMMA_CONST(LOG_LPR),
	GAMMA_CONST(LOG_MAIL),
	GAMMA_CONST(LOG_NEWS),
	GAMMA_CONST(LOG_SYSLOG),
	GAMMA_CONST(LOG_UUCP),
	GAMMA_CONST(LOG_LOCAL0),
	GAMMA_CONST(LOG_LOCAL1),
	GAMMA_CONST(LOG_LOCAL2),
	GAMMA_CONST(LOG_LOCAL3),
	GAMMA_CONST(LOG_LOCAL4),
	GAMMA_CONST(LOG_LOCAL5),
	GAMMA_CONST(LOG_LOCAL6),
	GAMMA_CONST(LOG_LOCAL7),
  /* severity */
	GAMMA_CONST(LOG_EMERG),    
	GAMMA_CONST(LOG_ALERT),   
	GAMMA_CONST(LOG_CRIT),    
	GAMMA_CONST(LOG_ERR),     
	GAMMA_CONST(LOG_WARNING), 
	GAMMA_CONST(LOG_NOTICE),  
	GAMMA_CONST(LOG_INFO),    
	GAMMA_CONST(LOG_DEBUG),   
  /* options */
	GAMMA_CONST(LOG_CONS),   
	GAMMA_CONST(LOG_NDELAY), 
	GAMMA_CONST(LOG_PID),
	GAMMA_CONST(LOG_NOWAIT),
#ifdef LOG_ODELAY
	GAMMA_CONST(LOG_ODELAY),
#endif
#ifdef LOG_PERROR
	GAMMA_CONST(LOG_PERROR)
#endif
};

void
syslog_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 <syslog.x>
}

Return to:

Send suggestions and report system problems to the System administrator.