17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate /*
237c478bd9Sstevel@tonic-gate  * Copyright 2003 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate /* ident	"%Z%%M%	%I%	%E% SMI" */
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate package com.sun.solaris.service.logging;
307c478bd9Sstevel@tonic-gate 
317c478bd9Sstevel@tonic-gate import java.util.*;
327c478bd9Sstevel@tonic-gate import java.util.logging.*;
337c478bd9Sstevel@tonic-gate 
347c478bd9Sstevel@tonic-gate /**
357c478bd9Sstevel@tonic-gate  * <code>syslog(3C)</code> severity levels defined in
367c478bd9Sstevel@tonic-gate  * <code>sys/syslog.h</code>.
377c478bd9Sstevel@tonic-gate  */
387c478bd9Sstevel@tonic-gate public final class Severity extends Level {
397c478bd9Sstevel@tonic-gate 	/**
407c478bd9Sstevel@tonic-gate 	 * LOG_EMERG from <code>sys/syslog.h</code>
417c478bd9Sstevel@tonic-gate 	 */
427c478bd9Sstevel@tonic-gate 	private static final int LOG_EMERG = 0;
437c478bd9Sstevel@tonic-gate 
447c478bd9Sstevel@tonic-gate 	/**
457c478bd9Sstevel@tonic-gate 	 * LOG_ALERT from <code>sys/syslog.h</code>
467c478bd9Sstevel@tonic-gate 	 */
477c478bd9Sstevel@tonic-gate 	private static final int LOG_ALERT = 1;
487c478bd9Sstevel@tonic-gate 
497c478bd9Sstevel@tonic-gate 	/**
507c478bd9Sstevel@tonic-gate 	 * LOG_CRIT from <code>sys/syslog.h</code>
517c478bd9Sstevel@tonic-gate 	 */
527c478bd9Sstevel@tonic-gate 	private static final int LOG_CRIT = 2;
537c478bd9Sstevel@tonic-gate 
547c478bd9Sstevel@tonic-gate 	/**
557c478bd9Sstevel@tonic-gate 	 * LOG_ERR from <code>sys/syslog.h</code>
567c478bd9Sstevel@tonic-gate 	 */
577c478bd9Sstevel@tonic-gate 	private static final int LOG_ERR = 3;
587c478bd9Sstevel@tonic-gate 
597c478bd9Sstevel@tonic-gate 	/**
607c478bd9Sstevel@tonic-gate 	 * LOG_WARNING from <code>sys/syslog.h</code>
617c478bd9Sstevel@tonic-gate 	 */
627c478bd9Sstevel@tonic-gate 	private static final int LOG_WARNING = 4;
637c478bd9Sstevel@tonic-gate 
647c478bd9Sstevel@tonic-gate 	/**
657c478bd9Sstevel@tonic-gate 	 * LOG_NOTICE from <code>sys/syslog.h</code>
667c478bd9Sstevel@tonic-gate 	 */
677c478bd9Sstevel@tonic-gate 	private static final int LOG_NOTICE = 5;
687c478bd9Sstevel@tonic-gate 
697c478bd9Sstevel@tonic-gate 	/**
707c478bd9Sstevel@tonic-gate 	 * LOG_INFO from <code>sys/syslog.h</code>
717c478bd9Sstevel@tonic-gate 	 */
727c478bd9Sstevel@tonic-gate 	private static final int LOG_INFO = 6;
737c478bd9Sstevel@tonic-gate 
747c478bd9Sstevel@tonic-gate 	/**
757c478bd9Sstevel@tonic-gate 	 * LOG_DEBUG from <code>sys/syslog.h</code>
767c478bd9Sstevel@tonic-gate 	 */
777c478bd9Sstevel@tonic-gate 	private static final int LOG_DEBUG = 7;
787c478bd9Sstevel@tonic-gate 
797c478bd9Sstevel@tonic-gate 	/**
807c478bd9Sstevel@tonic-gate 	 * Maps static instances by name.
817c478bd9Sstevel@tonic-gate 	 */
827c478bd9Sstevel@tonic-gate 	private static final HashMap severityMap = new HashMap();
837c478bd9Sstevel@tonic-gate 
847c478bd9Sstevel@tonic-gate 	/**
857c478bd9Sstevel@tonic-gate 	 * LOG_EMERG
867c478bd9Sstevel@tonic-gate 	 */
877c478bd9Sstevel@tonic-gate 	public static final Severity EMERG = new Severity(LOG_EMERG,
887c478bd9Sstevel@tonic-gate 	    Level.SEVERE.intValue() - 1, "EMERG");
897c478bd9Sstevel@tonic-gate 
907c478bd9Sstevel@tonic-gate 	/**
917c478bd9Sstevel@tonic-gate 	 * LOG_ALERT
927c478bd9Sstevel@tonic-gate 	 */
937c478bd9Sstevel@tonic-gate 	public static final Severity ALERT = new Severity(LOG_ALERT,
947c478bd9Sstevel@tonic-gate 	    Level.SEVERE.intValue() - 2, "ALERT");
957c478bd9Sstevel@tonic-gate 
967c478bd9Sstevel@tonic-gate 	/**
977c478bd9Sstevel@tonic-gate 	 * LOG_CRIT
987c478bd9Sstevel@tonic-gate 	 */
997c478bd9Sstevel@tonic-gate 	public static final Severity CRIT = new Severity(LOG_CRIT,
1007c478bd9Sstevel@tonic-gate 	    Level.SEVERE.intValue() - 3, "CRIT");
1017c478bd9Sstevel@tonic-gate 
1027c478bd9Sstevel@tonic-gate 	/**
1037c478bd9Sstevel@tonic-gate 	 * LOG_ERR
1047c478bd9Sstevel@tonic-gate 	 */
1057c478bd9Sstevel@tonic-gate 	public static final Severity ERR = new Severity(LOG_ERR,
1067c478bd9Sstevel@tonic-gate 	    Level.SEVERE.intValue() - 4, "ERR");
1077c478bd9Sstevel@tonic-gate 
1087c478bd9Sstevel@tonic-gate 	/**
1097c478bd9Sstevel@tonic-gate 	 * LOG_WARNING
1107c478bd9Sstevel@tonic-gate 	 */
1117c478bd9Sstevel@tonic-gate 	public static final Severity WARNING = new Severity(LOG_WARNING,
1127c478bd9Sstevel@tonic-gate 	    Level.WARNING.intValue() - 1, "WARNING");
1137c478bd9Sstevel@tonic-gate 
1147c478bd9Sstevel@tonic-gate 	/**
1157c478bd9Sstevel@tonic-gate 	 * LOG_NOTICE
1167c478bd9Sstevel@tonic-gate 	 */
1177c478bd9Sstevel@tonic-gate 	public static final Severity NOTICE = new Severity(LOG_NOTICE,
1187c478bd9Sstevel@tonic-gate 	    Level.INFO.intValue() - 1, "NOTICE");
1197c478bd9Sstevel@tonic-gate 
1207c478bd9Sstevel@tonic-gate 	/**
1217c478bd9Sstevel@tonic-gate 	 * LOG_INFO
1227c478bd9Sstevel@tonic-gate 	 */
1237c478bd9Sstevel@tonic-gate 	public static final Severity INFO = new Severity(LOG_INFO,
1247c478bd9Sstevel@tonic-gate 	    Level.INFO.intValue() - 2, "INFO");
1257c478bd9Sstevel@tonic-gate 
1267c478bd9Sstevel@tonic-gate 	/**
1277c478bd9Sstevel@tonic-gate 	 * LOG_DEBUG
1287c478bd9Sstevel@tonic-gate 	 */
1297c478bd9Sstevel@tonic-gate 	public static final Severity DEBUG = new Severity(LOG_DEBUG,
1307c478bd9Sstevel@tonic-gate 	    Level.FINE.intValue() - 1, "DEBUG");
1317c478bd9Sstevel@tonic-gate 
1327c478bd9Sstevel@tonic-gate 	/**
1337c478bd9Sstevel@tonic-gate 	 * Add aliases.
1347c478bd9Sstevel@tonic-gate 	 */
1357c478bd9Sstevel@tonic-gate 	static {
1367c478bd9Sstevel@tonic-gate 		severityMap.put("WARN", Severity.WARNING);
1377c478bd9Sstevel@tonic-gate 		severityMap.put("ERROR", Severity.ERR);
1387c478bd9Sstevel@tonic-gate 	}
1397c478bd9Sstevel@tonic-gate 
1407c478bd9Sstevel@tonic-gate 	/**
1417c478bd9Sstevel@tonic-gate 	 * Disallowed overidden constant Level
1427c478bd9Sstevel@tonic-gate 	 */
1437c478bd9Sstevel@tonic-gate 	private static final Level SEVERE = null;
1447c478bd9Sstevel@tonic-gate 
1457c478bd9Sstevel@tonic-gate 	/**
1467c478bd9Sstevel@tonic-gate 	 * Disallowed overidden constant Level
1477c478bd9Sstevel@tonic-gate 	 */
1487c478bd9Sstevel@tonic-gate 	private static final Level CONFIG = null;
1497c478bd9Sstevel@tonic-gate 
1507c478bd9Sstevel@tonic-gate 	/**
1517c478bd9Sstevel@tonic-gate 	 * Disallowed overidden constant Level
1527c478bd9Sstevel@tonic-gate 	 */
1537c478bd9Sstevel@tonic-gate 	private static final Level FINE = null;
1547c478bd9Sstevel@tonic-gate 
1557c478bd9Sstevel@tonic-gate 	/**
1567c478bd9Sstevel@tonic-gate 	 * Disallowed overidden constant Level
1577c478bd9Sstevel@tonic-gate 	 */
1587c478bd9Sstevel@tonic-gate 	private static final Level FINER = null;
1597c478bd9Sstevel@tonic-gate 
1607c478bd9Sstevel@tonic-gate 	/**
1617c478bd9Sstevel@tonic-gate 	 * Disallowed overidden constant Level
1627c478bd9Sstevel@tonic-gate 	 */
1637c478bd9Sstevel@tonic-gate 	private static final Level FINEST = null;
1647c478bd9Sstevel@tonic-gate 
1657c478bd9Sstevel@tonic-gate 	/**
1667c478bd9Sstevel@tonic-gate 	 * See getNative().
1677c478bd9Sstevel@tonic-gate 	 */
1687c478bd9Sstevel@tonic-gate 	private int severity;
1697c478bd9Sstevel@tonic-gate 
1707c478bd9Sstevel@tonic-gate 	/**
1717c478bd9Sstevel@tonic-gate 	 * Constructs a Severity with the given native severity, Java
1727c478bd9Sstevel@tonic-gate 	 * logging level, and name.
1737c478bd9Sstevel@tonic-gate 	 */
Severity(int severity, int level, String name)1747c478bd9Sstevel@tonic-gate 	private Severity(int severity, int level, String name)
1757c478bd9Sstevel@tonic-gate 	{
1767c478bd9Sstevel@tonic-gate 		super(name, level);
1777c478bd9Sstevel@tonic-gate 		this.severity = severity;
1787c478bd9Sstevel@tonic-gate 
1797c478bd9Sstevel@tonic-gate 		Object displaced = severityMap.put(name, this);
1807c478bd9Sstevel@tonic-gate 		assert (displaced == null);
1817c478bd9Sstevel@tonic-gate 	}
182*55fea89dSDan Cross 
1837c478bd9Sstevel@tonic-gate 	/**
184*55fea89dSDan Cross 	 * Returns the Severity closest in meaning to the given Level.
1857c478bd9Sstevel@tonic-gate 	 * This is meant to be used by SyslogHandler to determine a
1867c478bd9Sstevel@tonic-gate 	 * proper Severity for Records which only specify a Level.
1877c478bd9Sstevel@tonic-gate 	 *
1887c478bd9Sstevel@tonic-gate 	 * <ul>
1897c478bd9Sstevel@tonic-gate 	 * <li>Level.SEVERE or higher becomes Severity.ERR
1907c478bd9Sstevel@tonic-gate 	 * <li>Level.WARNING becomes Severity.WARNING
1917c478bd9Sstevel@tonic-gate 	 * <li>Level.INFO becomes Severity.INFO
1927c478bd9Sstevel@tonic-gate 	 * <li>Level.FINE becomes Severity.DEBUG
1937c478bd9Sstevel@tonic-gate 	 * </ul>
1947c478bd9Sstevel@tonic-gate 	 *
1957c478bd9Sstevel@tonic-gate 	 * If the level is below Level.FINE, i.e. the level is too low
1967c478bd9Sstevel@tonic-gate 	 * for syslog, then null is returned.
1977c478bd9Sstevel@tonic-gate 	 */
severityForLevel(Level l)1987c478bd9Sstevel@tonic-gate 	public static Severity severityForLevel(Level l)
1997c478bd9Sstevel@tonic-gate 	{
2007c478bd9Sstevel@tonic-gate 		if (l instanceof Severity)
2017c478bd9Sstevel@tonic-gate 			return (Severity)l;
2027c478bd9Sstevel@tonic-gate 		else {
2037c478bd9Sstevel@tonic-gate 			if (l.intValue() >= Level.SEVERE.intValue())
2047c478bd9Sstevel@tonic-gate 				return (ERR);
2057c478bd9Sstevel@tonic-gate 			if (l.intValue() >= Level.WARNING.intValue())
2067c478bd9Sstevel@tonic-gate 				return (WARNING);
2077c478bd9Sstevel@tonic-gate 			if (l.intValue() >= Level.INFO.intValue())
2087c478bd9Sstevel@tonic-gate 				return (INFO);
2097c478bd9Sstevel@tonic-gate 			if (l.intValue() >= Level.CONFIG.intValue())
2107c478bd9Sstevel@tonic-gate 				return (INFO);
2117c478bd9Sstevel@tonic-gate 			if (l.intValue() >= Level.FINE.intValue())
2127c478bd9Sstevel@tonic-gate 				return (DEBUG);
2137c478bd9Sstevel@tonic-gate 			return (null);
2147c478bd9Sstevel@tonic-gate 		}
2157c478bd9Sstevel@tonic-gate 	}
2167c478bd9Sstevel@tonic-gate 
2177c478bd9Sstevel@tonic-gate 	/**
2187c478bd9Sstevel@tonic-gate 	 * Returns the native <code>syslog(3C)</code> severity.
2197c478bd9Sstevel@tonic-gate 	 */
getNative()2207c478bd9Sstevel@tonic-gate 	public int getNative()
2217c478bd9Sstevel@tonic-gate 	{
2227c478bd9Sstevel@tonic-gate 		return (severity);
2237c478bd9Sstevel@tonic-gate 	}
2247c478bd9Sstevel@tonic-gate 
2257c478bd9Sstevel@tonic-gate 	/**
2267c478bd9Sstevel@tonic-gate 	 * Returns the Severity object with the given name, interpreted
2277c478bd9Sstevel@tonic-gate 	 * case-insensitively.
2287c478bd9Sstevel@tonic-gate 	 *
2297c478bd9Sstevel@tonic-gate 	 * @throws IllegalArgumentException if the name isn't of a known
2307c478bd9Sstevel@tonic-gate 	 * severity.
2317c478bd9Sstevel@tonic-gate 	 */
parse(String name)2327c478bd9Sstevel@tonic-gate 	public static Level parse(String name)
2337c478bd9Sstevel@tonic-gate 	{
2347c478bd9Sstevel@tonic-gate 		Severity severity = (Severity)severityMap.get(
2357c478bd9Sstevel@tonic-gate 		    name.toUpperCase());
2367c478bd9Sstevel@tonic-gate 
2377c478bd9Sstevel@tonic-gate 		if (severity == null)
2387c478bd9Sstevel@tonic-gate 			throw new IllegalArgumentException();
2397c478bd9Sstevel@tonic-gate 		else
2407c478bd9Sstevel@tonic-gate 			return (severity);
2417c478bd9Sstevel@tonic-gate 	}
2427c478bd9Sstevel@tonic-gate 
2437c478bd9Sstevel@tonic-gate 	/**
2447c478bd9Sstevel@tonic-gate 	 * Returns the Severity object with the given name, interpreted
2457c478bd9Sstevel@tonic-gate 	 * case-insensitively.
2467c478bd9Sstevel@tonic-gate 	 *
2477c478bd9Sstevel@tonic-gate 	 * @throws IllegalArgumentException if the name isn't of a known
2487c478bd9Sstevel@tonic-gate 	 * severity.
2497c478bd9Sstevel@tonic-gate 	 */
getSeverityWithName(String name)2507c478bd9Sstevel@tonic-gate 	public static Severity getSeverityWithName(String name)
2517c478bd9Sstevel@tonic-gate 	{
2527c478bd9Sstevel@tonic-gate 		return ((Severity)parse(name));
2537c478bd9Sstevel@tonic-gate 	}
2547c478bd9Sstevel@tonic-gate }
255