1 /*
2  * Syslog.xs
3  *
4  * XS wrapper for the syslog(3) facility.
5  *
6  */
7 
8 #if defined(_WIN32)
9 #  include <windows.h>
10 #endif
11 
12 #include "EXTERN.h"
13 #include "perl.h"
14 #include "XSUB.h"
15 #ifdef USE_PPPORT_H
16 #  include "ppport.h"
17 #endif
18 
19 #ifndef HAVE_SYSLOG
20 #define HAVE_SYSLOG 1
21 #endif
22 
23 #if defined(_WIN32) && !defined(__CYGWIN__)
24 #  undef HAVE_SYSLOG
25 #  include "fallback/syslog.h"
26 #else
27 #  if defined(I_SYSLOG) || PATCHLEVEL < 6
28 #    include <syslog.h>
29 #  else
30 #    undef HAVE_SYSLOG
31 #    include "fallback/syslog.h"
32 #  endif
33 #endif
34 
35 static SV *ident_svptr;
36 
37 
38 #ifndef LOG_FAC
39 #define LOG_FACMASK     0x03f8
40 #define LOG_FAC(p)      (((p) & LOG_FACMASK) >> 3)
41 #endif
42 
43 #ifndef LOG_PRIMASK
44 #define LOG_PRIMASK     0x07
45 #endif
46 
47 #ifndef	LOG_PRI
48 #define	LOG_PRI(p)	((p) & LOG_PRIMASK)
49 #endif
50 
51 #ifndef	LOG_MAKEPRI
52 #define	LOG_MAKEPRI(fac, pri)	(((fac) << 3) | (pri))
53 #endif
54 
55 #ifndef LOG_MASK
56 #define	LOG_MASK(pri)	(1 << (pri))
57 #endif
58 
59 #ifndef LOG_UPTO
60 #define	LOG_UPTO(pri)	((1 << ((pri)+1)) - 1)
61 #endif
62 
63 #include "const-c.inc"
64 
65 
66 MODULE = Sys::Syslog		PACKAGE = Sys::Syslog
67 
68 INCLUDE: const-xs.inc
69 
70 int
71 LOG_FAC(p)
72     INPUT:
73 	int		p
74 
75 int
76 LOG_PRI(p)
77     INPUT:
78 	int		p
79 
80 int
81 LOG_MAKEPRI(fac,pri)
82     INPUT:
83 	int		fac
84 	int		pri
85 
86 int
87 LOG_MASK(pri)
88     INPUT:
89 	int		pri
90 
91 int
92 LOG_UPTO(pri)
93     INPUT:
94 	int		pri
95 
96 #ifdef HAVE_SYSLOG
97 
98 void
99 openlog_xs(ident, option, facility)
100     INPUT:
101         SV*   ident
102         int   option
103         int   facility
104     PREINIT:
105         STRLEN len;
106         char*  ident_pv;
107     CODE:
108         ident_svptr = newSVsv(ident);
109         ident_pv    = SvPV(ident_svptr, len);
110         openlog(ident_pv, option, facility);
111 
112 void
113 syslog_xs(priority, message)
114     INPUT:
115         int   priority
116         const char * message
117     CODE:
118         syslog(priority, "%s", message);
119 
120 int
121 setlogmask_xs(mask)
122     INPUT:
123         int mask
124     CODE:
125         RETVAL = setlogmask(mask);
126     OUTPUT:
127         RETVAL
128 
129 void
130 closelog_xs()
131     PREINIT:
132         U32 refcnt;
133     CODE:
134         if (!ident_svptr)
135             return;
136         closelog();
137         refcnt = SvREFCNT(ident_svptr);
138         if (refcnt) {
139             SvREFCNT_dec(ident_svptr);
140             if (refcnt == 1)
141                 ident_svptr = NULL;
142         }
143 
144 #else  /* HAVE_SYSLOG */
145 
146 void
147 openlog_xs(ident, option, facility)
148     INPUT:
149         SV*   ident
150         int   option
151         int   facility
152     CODE:
153 
154 void
155 syslog_xs(priority, message)
156     INPUT:
157         int   priority
158         const char * message
159     CODE:
160 
161 int
162 setlogmask_xs(mask)
163     INPUT:
164         int mask
165     CODE:
166 
167 void
168 closelog_xs()
169     CODE:
170 
171 #endif /* HAVE_SYSLOG */
172