mirror of
https://gitlab.com/sortix/sortix.git
synced 2023-02-13 20:55:38 -05:00
Add syslog(3).
This commit is contained in:
parent
e677c455d8
commit
9771f29138
8 changed files with 366 additions and 0 deletions
|
@ -387,6 +387,12 @@ stdlib/unsetenv.o \
|
|||
sys/display/dispmsg_issue.o \
|
||||
sys/ioctl/ioctl.o \
|
||||
sys/kernelinfo/kernelinfo.o \
|
||||
syslog/closelog.o \
|
||||
syslog/connectlog.o \
|
||||
syslog/openlog.o \
|
||||
syslog/setlogmask.o \
|
||||
syslog/syslog.o \
|
||||
syslog/vsyslog.o \
|
||||
sys/mman/mmap.o \
|
||||
sys/mman/mprotect.o \
|
||||
sys/mman/munmap.o \
|
||||
|
|
107
libc/include/syslog.h
Normal file
107
libc/include/syslog.h
Normal file
|
@ -0,0 +1,107 @@
|
|||
/*******************************************************************************
|
||||
|
||||
Copyright(C) Jonas 'Sortie' Termansen 2014.
|
||||
|
||||
This file is part of the Sortix C Library.
|
||||
|
||||
The Sortix C 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.
|
||||
|
||||
The Sortix C 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 the Sortix C Library. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
syslog.h
|
||||
System error logging facility.
|
||||
|
||||
*******************************************************************************/
|
||||
|
||||
#ifndef INCLUDE_SYSLOG_H
|
||||
#define INCLUDE_SYSLOG_H
|
||||
|
||||
#include <sys/cdefs.h>
|
||||
|
||||
#include <stdarg.h>
|
||||
|
||||
__BEGIN_DECLS
|
||||
|
||||
#define LOG_PRISHIFT 0
|
||||
#define LOG_PRIBITS 3
|
||||
#define LOG_FACSHIFT LOG_PRIBITS
|
||||
#define LOG_FACBITS (31 - LOG_PRIBITS)
|
||||
|
||||
#define LOG_PRIMASK (((1 << LOG_PRIBITS) - 1) << LOG_PRISHIFT)
|
||||
#define LOG_FACMASK (((1 << LOG_FACBITS) - 1) << LOG_FACSHIFT)
|
||||
#define LOG_PRI(x) ((x) >> LOG_PRISHIFT & LOG_PRIMASK)
|
||||
#define LOG_FAC(x) ((x) >> LOG_FACSHIFT & LOG_FACMASK)
|
||||
|
||||
#define LOG_MAKEPRI(fac, pri) ((fac) << LOG_FACSHIFT | (pri) << LOG_PRISHIFT)
|
||||
|
||||
#define LOG_EMERG 0
|
||||
#define LOG_ALERT 1
|
||||
#define LOG_CRIT 2
|
||||
#define LOG_ERR 3
|
||||
#define LOG_WARNING 4
|
||||
#define LOG_NOTICE 5
|
||||
#define LOG_INFO 6
|
||||
#define LOG_DEBUG 7
|
||||
|
||||
#define LOG_MASK(pri) (1 << ((pri) >> LOG_PRISHIFT))
|
||||
#define LOG_UPTO(pri) ((1 << (((pri) >> LOG_PRISHIFT) + 1)) - 1)
|
||||
|
||||
#define LOG_KERN (0 << LOG_FACSHIFT)
|
||||
#define LOG_USER (1 << LOG_FACSHIFT)
|
||||
#define LOG_MAIL (2 << LOG_FACSHIFT)
|
||||
#define LOG_DAEMON (3 << LOG_FACSHIFT)
|
||||
#define LOG_AUTH (4 << LOG_FACSHIFT)
|
||||
#define LOG_SYSLOG (5 << LOG_FACSHIFT)
|
||||
#define LOG_LPR (6 << LOG_FACSHIFT)
|
||||
#define LOG_NEWS (7 << LOG_FACSHIFT)
|
||||
#define LOG_UUCP (8 << LOG_FACSHIFT)
|
||||
#define LOG_CRON (9 << LOG_FACSHIFT)
|
||||
#define LOG_AUTHPRIV (10 << LOG_FACSHIFT)
|
||||
#define LOG_FTP (11 << LOG_FACSHIFT)
|
||||
/* 12 through 15 are reserved */
|
||||
#define LOG_LOCAL0 (16 << LOG_FACSHIFT)
|
||||
#define LOG_LOCAL1 (17 << LOG_FACSHIFT)
|
||||
#define LOG_LOCAL2 (18 << LOG_FACSHIFT)
|
||||
#define LOG_LOCAL3 (19 << LOG_FACSHIFT)
|
||||
#define LOG_LOCAL4 (20 << LOG_FACSHIFT)
|
||||
#define LOG_LOCAL5 (21 << LOG_FACSHIFT)
|
||||
#define LOG_LOCAL6 (22 << LOG_FACSHIFT)
|
||||
#define LOG_LOCAL7 (23 << LOG_FACSHIFT)
|
||||
#define LOG_NFACILITIES 24
|
||||
|
||||
#define LOG_CONS (1 << 0)
|
||||
#define LOG_NDELAY (1 << 1)
|
||||
#define LOG_NOWAIT (1 << 2)
|
||||
#define LOG_ODELAY (1 << 3)
|
||||
#define LOG_PERROR (1 << 4)
|
||||
#define LOG_PID (1 << 5)
|
||||
|
||||
#if defined(__is_sortix_libc)
|
||||
extern char* __syslog_identity;
|
||||
extern int __syslog_facility;
|
||||
extern int __syslog_fd;
|
||||
extern int __syslog_mask;
|
||||
extern int __syslog_option;
|
||||
#endif
|
||||
|
||||
void closelog(void);
|
||||
int connectlog(void);
|
||||
void openlog(const char*, int, int);
|
||||
int setlogmask(int);
|
||||
__attribute__ ((__format__ (__printf__, 2, 3)))
|
||||
void syslog(int, const char*, ...);
|
||||
__attribute__ ((__format__ (__printf__, 2, 0)))
|
||||
void vsyslog(int, const char*, va_list);
|
||||
|
||||
__END_DECLS
|
||||
|
||||
#endif
|
35
libc/syslog/closelog.cpp
Normal file
35
libc/syslog/closelog.cpp
Normal file
|
@ -0,0 +1,35 @@
|
|||
/*******************************************************************************
|
||||
|
||||
Copyright(C) Jonas 'Sortie' Termansen 2014.
|
||||
|
||||
This file is part of the Sortix C Library.
|
||||
|
||||
The Sortix C 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.
|
||||
|
||||
The Sortix C 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 the Sortix C Library. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
syslog/closelog.cpp
|
||||
Closes the connection to the system log.
|
||||
|
||||
*******************************************************************************/
|
||||
|
||||
#include <syslog.h>
|
||||
#include <unistd.h>
|
||||
|
||||
extern "C" void closelog(void)
|
||||
{
|
||||
if ( 0 <= __syslog_fd )
|
||||
{
|
||||
close(__syslog_fd);
|
||||
__syslog_fd = -1;
|
||||
}
|
||||
}
|
31
libc/syslog/connectlog.cpp
Normal file
31
libc/syslog/connectlog.cpp
Normal file
|
@ -0,0 +1,31 @@
|
|||
/*******************************************************************************
|
||||
|
||||
Copyright(C) Jonas 'Sortie' Termansen 2014.
|
||||
|
||||
This file is part of the Sortix C Library.
|
||||
|
||||
The Sortix C 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.
|
||||
|
||||
The Sortix C 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 the Sortix C Library. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
syslog/connectlog.cpp
|
||||
Returns a file descriptor to the system log.
|
||||
|
||||
*******************************************************************************/
|
||||
|
||||
#include <fcntl.h>
|
||||
#include <syslog.h>
|
||||
|
||||
extern "C" int connectlog(void)
|
||||
{
|
||||
return open("/dev/tty", O_WRONLY | O_CLOEXEC);
|
||||
}
|
42
libc/syslog/openlog.cpp
Normal file
42
libc/syslog/openlog.cpp
Normal file
|
@ -0,0 +1,42 @@
|
|||
/*******************************************************************************
|
||||
|
||||
Copyright(C) Jonas 'Sortie' Termansen 2014.
|
||||
|
||||
This file is part of the Sortix C Library.
|
||||
|
||||
The Sortix C 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.
|
||||
|
||||
The Sortix C 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 the Sortix C Library. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
syslog/openlog.cpp
|
||||
Opens the connection to the system log.
|
||||
|
||||
*******************************************************************************/
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <syslog.h>
|
||||
|
||||
extern "C" void openlog(const char* identity, int option, int facility)
|
||||
{
|
||||
// Remember the identity to report in the system log events.
|
||||
free(__syslog_identity);
|
||||
__syslog_identity = identity ? strdup(identity) : (char*) NULL;
|
||||
|
||||
// Remember the option and facility parameters for later use.
|
||||
__syslog_option = option;
|
||||
__syslog_facility = facility;
|
||||
|
||||
// Connect to the system more immediately if we are asked to and need to.
|
||||
if ( (option & LOG_NDELAY) && __syslog_fd < 0 )
|
||||
__syslog_fd = connectlog();
|
||||
}
|
33
libc/syslog/setlogmask.cpp
Normal file
33
libc/syslog/setlogmask.cpp
Normal file
|
@ -0,0 +1,33 @@
|
|||
/*******************************************************************************
|
||||
|
||||
Copyright(C) Jonas 'Sortie' Termansen 2014.
|
||||
|
||||
This file is part of the Sortix C Library.
|
||||
|
||||
The Sortix C 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.
|
||||
|
||||
The Sortix C 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 the Sortix C Library. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
syslog/setlogmask.cpp
|
||||
Sets the log priority mask.
|
||||
|
||||
*******************************************************************************/
|
||||
|
||||
#include <syslog.h>
|
||||
|
||||
extern "C" int setlogmask(int mask)
|
||||
{
|
||||
int previous_mask = __syslog_mask;
|
||||
if ( mask )
|
||||
__syslog_mask = mask;
|
||||
return previous_mask;
|
||||
}
|
34
libc/syslog/syslog.cpp
Normal file
34
libc/syslog/syslog.cpp
Normal file
|
@ -0,0 +1,34 @@
|
|||
/*******************************************************************************
|
||||
|
||||
Copyright(C) Jonas 'Sortie' Termansen 2014.
|
||||
|
||||
This file is part of the Sortix C Library.
|
||||
|
||||
The Sortix C 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.
|
||||
|
||||
The Sortix C 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 the Sortix C Library. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
syslog/syslog.cpp
|
||||
Logs a condition to the system log.
|
||||
|
||||
*******************************************************************************/
|
||||
|
||||
#include <stdarg.h>
|
||||
#include <syslog.h>
|
||||
|
||||
extern "C" void syslog(int priority, const char* format, ...)
|
||||
{
|
||||
va_list ap;
|
||||
va_start(ap, format);
|
||||
vsyslog(priority, format, ap);
|
||||
va_end(ap);
|
||||
}
|
78
libc/syslog/vsyslog.cpp
Normal file
78
libc/syslog/vsyslog.cpp
Normal file
|
@ -0,0 +1,78 @@
|
|||
/*******************************************************************************
|
||||
|
||||
Copyright(C) Jonas 'Sortie' Termansen 2014.
|
||||
|
||||
This file is part of the Sortix C Library.
|
||||
|
||||
The Sortix C 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.
|
||||
|
||||
The Sortix C 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 the Sortix C Library. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
syslog/vsyslog.cpp
|
||||
Logs a condition to the system log.
|
||||
|
||||
*******************************************************************************/
|
||||
|
||||
#include <stdarg.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <syslog.h>
|
||||
#include <time.h>
|
||||
#include <unistd.h>
|
||||
|
||||
extern "C" { char* __syslog_identity = NULL; }
|
||||
extern "C" { int __syslog_facility = LOG_USER; }
|
||||
extern "C" { int __syslog_fd = -1; }
|
||||
extern "C" { int __syslog_mask = LOG_UPTO(LOG_DEBUG); }
|
||||
extern "C" { int __syslog_option = 0; }
|
||||
|
||||
// TODO: The transmitted string might be delivered in chunks using this dprintf
|
||||
// approach (plus there are multiple dprintf calls). We should buffer the
|
||||
// string fully before transmitting it.
|
||||
|
||||
// TODO: The log entry format here is just whatever some other system does. We
|
||||
// should work out the Sortix system log semantics and adapt this to that.
|
||||
|
||||
extern "C" void vsyslog(int priority, const char* format, va_list ap)
|
||||
{
|
||||
// Drop the event if it doesn't fit the current priority mask.
|
||||
if ( !(LOG_MASK(LOG_PRI(priority)) & __syslog_mask) )
|
||||
return;
|
||||
|
||||
// Connect to the system log if a connection hasn't yet been established.
|
||||
if ( __syslog_fd < 0 && (__syslog_fd = connectlog()) < 0 )
|
||||
return;
|
||||
|
||||
// If no facility is given we'll use the default facility from openlog.
|
||||
if ( !LOG_FAC(priority) )
|
||||
priority |= __syslog_facility;
|
||||
|
||||
// Prepare a timestamp for the log event.
|
||||
struct timespec now;
|
||||
clock_gettime(CLOCK_REALTIME, &now);
|
||||
struct tm tm;
|
||||
gmtime_r(&now.tv_sec, &tm);
|
||||
char timestamp[32];
|
||||
strftime(timestamp, sizeof(timestamp), "%b %e %T", &tm);
|
||||
|
||||
// Include the process id of the current process if requested.
|
||||
pid_t pid = (__syslog_option & LOG_PID) ? getpid() : 0;
|
||||
|
||||
// Transmit the event to the system log.
|
||||
dprintf(__syslog_fd, "<%d>%s %s%s%.0jd%s: ",
|
||||
priority,
|
||||
timestamp,
|
||||
__syslog_identity ? __syslog_identity : "",
|
||||
"[" + !pid, (intmax_t) pid, "]" + !pid);
|
||||
vdprintf(__syslog_fd, format, ap);
|
||||
dprintf(__syslog_fd, "\n");
|
||||
}
|
Loading…
Reference in a new issue