xref: /illumos-gate/usr/src/lib/libslp/javalib/com/sun/slp/Syslog.java (revision 45ede40b2394db7967e59f19288fae9b62efd4aa)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * ident	"%Z%%M%	%I%	%E% SMI"
24  *
25  * Copyright (c) 1999,2001 by Sun Microsystems, Inc.
26  * All rights reserved.
27  *
28  */
29 
30 package com.sun.slp;
31 
32 import java.io.*;
33 
34 /**
35  * A logging class which writes to UNIX syslog. This class can be dynamically
36  * loaded by SLPConfig and used as the log object by the writeLog and
37  * writeLogLine methods. Note that we need to use JNI here to call the
38  * native syslog. This is because syslog can be listening on any port
39  * mapped to 'syslog' in the services table, but Java provides no way to
40  * query this mapping.
41  *
42  * This class does not actually write anything until the flush() method
43  * in invoked; this will write the concatenation of all messages
44  * passed to the write() method since the last invokation of flush().
45  *
46  * The actual logging class used can be controlled via the
47  * sun.net.slp.loggerClass property.
48  *
49  * See also the SLPLog (in slpd.java) and StderrLog classes.
50  */
51 
52 class Syslog extends Writer {
53 
54     private StringBuffer buf;
55 
56     public Syslog() {
57 	buf = new StringBuffer();
58 
59 	// can't just get SLPConfig; causes stack recursion
60 	tracingOn = Boolean.getBoolean("sun.net.slp.traceALL") ||
61 	    Boolean.getBoolean("net.slp.traceReg") ||
62 	    Boolean.getBoolean("net.slp.traceMsg") ||
63 	    Boolean.getBoolean("net.slp.traceDrop") ||
64 	    Boolean.getBoolean("net.slp.traceDATraffic");
65     }
66 
67     public void write(char[] cbuf, int off, int len) throws IOException {
68 	buf.append(cbuf, off, len);
69     }
70 
71     public void flush() {
72 	int priority;
73 	if (tracingOn) {
74 	    priority = 6;	// LOG_INFO
75 	} else {
76 	    priority = 2;	// LOG_CRIT
77 	}
78 
79 	syslog(priority, buf.toString());
80 	buf = new StringBuffer();
81     }
82 
83     public void close() {
84     }
85 
86     private native void syslog(int priority, String message);
87 
88     // The JNI implementation is in libslp.so.
89     static {
90 	System.load("/usr/lib/libslp.so");
91     }
92 
93     private boolean tracingOn;
94 }
95