R
Rainer Weikusat
I finally grew somewhat tired of the Sys::Syslog code which does too
many weird things it shouldn't be doing (such as interfacing with the
Windows event log :->) in Perl code, including downright stupid things
such as using run-time translated strings for the priority constants
and things which change the 'global' execution environment (bad thing
for a routine supposed to generate diagnostic output). I've had a look
at Unix::Syslog but don't quite like that, either, because of 'weird
things' in the XS code ('hand-written' constant access functions,
mapping constants to names with for loops) and things written in Perl
which could equally well be implemented in C (message formatting).
Since this should really be an exercise in non-coding aka "just call
the C library routines and assume anything else is someone else's
business" and I had an otherwise spare Sunday evening in front of me,
I thought I could just run h2xs on the syslog.h file and see where
that would get me. Things never turning out to be quite as beautiful
as they should, some code does need to be written for that, in
particular, there's no (sane) way to invoke a C 'variadic
format-string function' with an a priori unknown set of arguments
which came from perl.
Perlapi(1) documents a function named
sv_vsetpvfn which seemed suitable for the formatting part which can
either work with a va_list or an array of SVs. At first, I thought of
copying the arguments on the stack into an array but then, it occurred
to me that the perl stack really ought to be such an array, already
laying in front of my feet and ready to be used. The code I'm
presently using for syslog which seems to be working fine is
void
syslog(__pri, __fmt, ...)
int __pri
char * __fmt
INIT:
char *msg;
SV *msg_sv;
bool dummy;
CODE:
if (items > 2) {
msg_sv = sv_newmortal();
sv_vsetpvfn(msg_sv, __fmt, sv_len(ST(1)), NULL,
PL_stack_base + ax + 2, items - 2,
&dummy);
msg = SvPV_nolen(msg_sv);
} else {
msg = __fmt;
}
syslog(__pri, "%s", msg);
Is there, excluding aesthetic/ philosophic aspects, a reason why I shouldn't be
doing this?
many weird things it shouldn't be doing (such as interfacing with the
Windows event log :->) in Perl code, including downright stupid things
such as using run-time translated strings for the priority constants
and things which change the 'global' execution environment (bad thing
for a routine supposed to generate diagnostic output). I've had a look
at Unix::Syslog but don't quite like that, either, because of 'weird
things' in the XS code ('hand-written' constant access functions,
mapping constants to names with for loops) and things written in Perl
which could equally well be implemented in C (message formatting).
Since this should really be an exercise in non-coding aka "just call
the C library routines and assume anything else is someone else's
business" and I had an otherwise spare Sunday evening in front of me,
I thought I could just run h2xs on the syslog.h file and see where
that would get me. Things never turning out to be quite as beautiful
as they should, some code does need to be written for that, in
particular, there's no (sane) way to invoke a C 'variadic
format-string function' with an a priori unknown set of arguments
which came from perl.
Perlapi(1) documents a function named
sv_vsetpvfn which seemed suitable for the formatting part which can
either work with a va_list or an array of SVs. At first, I thought of
copying the arguments on the stack into an array but then, it occurred
to me that the perl stack really ought to be such an array, already
laying in front of my feet and ready to be used. The code I'm
presently using for syslog which seems to be working fine is
void
syslog(__pri, __fmt, ...)
int __pri
char * __fmt
INIT:
char *msg;
SV *msg_sv;
bool dummy;
CODE:
if (items > 2) {
msg_sv = sv_newmortal();
sv_vsetpvfn(msg_sv, __fmt, sv_len(ST(1)), NULL,
PL_stack_base + ax + 2, items - 2,
&dummy);
msg = SvPV_nolen(msg_sv);
} else {
msg = __fmt;
}
syslog(__pri, "%s", msg);
Is there, excluding aesthetic/ philosophic aspects, a reason why I shouldn't be
doing this?