Update Term-ANSIColor to CPAN version 4.05
[perl.git] / cpan / Sys-Syslog / Syslog.xs
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 #  endif
30 #endif
31
32 static SV *ident_svptr;
33
34
35 #ifndef LOG_FAC
36 #define LOG_FACMASK     0x03f8
37 #define LOG_FAC(p)      (((p) & LOG_FACMASK) >> 3)
38 #endif
39
40 #ifndef LOG_PRIMASK
41 #define LOG_PRIMASK     0x07
42 #endif
43
44 #ifndef LOG_PRI
45 #define LOG_PRI(p)      ((p) & LOG_PRIMASK)
46 #endif
47
48 #ifndef LOG_MAKEPRI
49 #define LOG_MAKEPRI(fac, pri)   (((fac) << 3) | (pri))
50 #endif
51
52 #ifndef LOG_MASK
53 #define LOG_MASK(pri)   (1 << (pri))
54 #endif
55
56 #ifndef LOG_UPTO
57 #define LOG_UPTO(pri)   ((1 << ((pri)+1)) - 1)
58 #endif
59
60 #include "const-c.inc"
61
62
63 MODULE = Sys::Syslog            PACKAGE = Sys::Syslog           
64
65 INCLUDE: const-xs.inc
66
67 int
68 LOG_FAC(p)
69     INPUT:
70         int             p
71
72 int
73 LOG_PRI(p)
74     INPUT:
75         int             p
76
77 int
78 LOG_MAKEPRI(fac,pri)
79     INPUT:
80         int             fac
81         int             pri
82
83 int
84 LOG_MASK(pri)
85     INPUT:
86         int             pri
87
88 int
89 LOG_UPTO(pri)
90     INPUT:
91         int             pri
92
93 #ifdef HAVE_SYSLOG
94
95 void
96 openlog_xs(ident, option, facility)
97     INPUT:
98         SV*   ident
99         int   option
100         int   facility
101     PREINIT:
102         STRLEN len;
103         char*  ident_pv;
104     CODE:
105         ident_svptr = newSVsv(ident);
106         ident_pv    = SvPV(ident_svptr, len);
107         openlog(ident_pv, option, facility);
108
109 void
110 syslog_xs(priority, message)
111     INPUT:
112         int   priority
113         const char * message
114     CODE:
115         syslog(priority, "%s", message);
116
117 int
118 setlogmask_xs(mask)
119     INPUT:
120         int mask
121     CODE:
122         RETVAL = setlogmask(mask);
123     OUTPUT:
124         RETVAL
125
126 void
127 closelog_xs()
128     PREINIT:
129         U32 refcnt;
130     CODE:
131         if (!ident_svptr)
132             return;
133         closelog();
134         refcnt = SvREFCNT(ident_svptr);
135         if (refcnt) {
136             SvREFCNT_dec(ident_svptr);
137             if (refcnt == 1)
138                 ident_svptr = NULL;
139         }
140
141 #else  /* HAVE_SYSLOG */
142
143 void
144 openlog_xs(ident, option, facility)
145     INPUT:
146         SV*   ident
147         int   option
148         int   facility
149     CODE:
150
151 void
152 syslog_xs(priority, message)
153     INPUT:
154         int   priority
155         const char * message
156     CODE:
157
158 int
159 setlogmask_xs(mask)
160     INPUT:
161         int mask
162     CODE:
163
164 void
165 closelog_xs()
166     CODE:
167
168 #endif /* HAVE_SYSLOG */