package posix; /** Instances of Syslog maintain a connection to the system error logger. A work in progress. FIXME: So far, this uses the C library calls. After a little reflection, it seems much more flexible to make this 100% Java and simply send datagrams to the syslog server. This would allow the syslog server to be on a remote machine. The C library only works for the local machine. @author Stuart D. Gathman Copyright (C) 1998 Business Management Systems, Inc.

This code is distributed under the GNU Library General Public License

This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.

This 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 Library General Public License for more details.

You should have received a copy of the GNU Library General Public License along with this library; if not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ public class Syslog { private long syslogData = CPtr.NULL; /** Allocate an initialized syslog_data_struct. */ private native long initdata(); /** Set options for a syslog_data_struct and optionally connect immediately to the syslog server. @return 0 or errno|0x80000000 */ private native int openlog(String id,int logopt,int facility,long data); /** All message expansion should be done on the Java end, and any '%' doubled to prevent expansion by the C library. NOOP if data is NULL. @return 0 or errno|0x80000000 */ private native int syslog(int prior,long data,String msg); /** Close the log and free the syslog_data_struct. */ private native void closelog(long data); /** Change the priority mask and return the old mask. NOOP if data is NULL. */ private native int setlogmask(int maskprior,long data); // FIXME: openlog option values might need to be determined at runtime. /** log the pid with each message */ public static final byte LOG_PID = 0x01; /** log on the console if errors in sending */ public static final byte LOG_CONS = 0x02; /** delay open until syslog() is called */ public static final byte LOG_ODELAY = 0x04; /** don't delay open */ public static final byte LOG_NDELAY = 0x08; /** if forking to log on console, don't wait(). This is always needed for a JVM because it typically uses SIGCLD to implement java.lang.Process. We might also want to ban LOG_CONS if an extra fork() might confuse the JVM. */ private static final byte LOG_NOWAIT = 0x10; /* priority levels should be constant across posix platforms. */ public static final byte LOG_EMERG = 0; // system is unusable public static final byte LOG_ALERT = 1; // action must be taken immediately public static final byte LOG_CRIT = 2; // critical conditions public static final byte LOG_ERR = 3; // error conditions public static final byte LOG_WARNING = 4; // warning conditions public static final byte LOG_NOTICE = 5; // normal but significant condition public static final byte LOG_INFO = 6; // informational public static final byte LOG_DEBUG = 7; // debug-level messages public Syslog(String id) throws IPCException { syslogData = initdata(); int rc = openlog(id,LOG_NOWAIT,LOG_USER,syslogData); if (rc < 0) { rc &= 0x7FFFFFFF; throw new IPCException("openlog",rc); } } /** Replace all '%' with "%%" to prevent expansion by C library. */ private static String escape(String s) { int pos = s.indexOf('%'); if (pos < 0) return s; // leave room for expanding up to 5 '%' without reallocating StringBuffer buf = new StringBuffer(s.length() + 5); do { buf.append(s.substring(0,++pos)); buf.append('%'); if (pos >= s.length()) break; s = s.substring(pos); pos = s.indexOf('%'); } while (pos >= 0); return buf.toString(); } public void log(String msg) throws IPCException { int rc = syslog(0,syslogData,escape(s)); if (rc < 0) { rc &= 0x7FFFFFFF; throw new IPCException("syslog",rc); } } public synchronized void dispose() { if (syslogData != CPtr.NULL) { closelog(syslogData); syslogData = CPtr.NULL; } } protected void finalize() { dispose(); } }