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 * Copyright 2003 Sun Microsystems, Inc. All rights reserved. 24 * Use is subject to license terms. 25 */ 26 27 /* ident "%Z%%M% %I% %E% SMI" */ 28 29 package com.sun.solaris.service.logging; 30 31 import java.util.*; 32 import java.util.logging.*; 33 34 /** 35 * <code>syslog(3C)</code> severity levels defined in 36 * <code>sys/syslog.h</code>. 37 */ 38 public final class Severity extends Level { 39 /** 40 * LOG_EMERG from <code>sys/syslog.h</code> 41 */ 42 private static final int LOG_EMERG = 0; 43 44 /** 45 * LOG_ALERT from <code>sys/syslog.h</code> 46 */ 47 private static final int LOG_ALERT = 1; 48 49 /** 50 * LOG_CRIT from <code>sys/syslog.h</code> 51 */ 52 private static final int LOG_CRIT = 2; 53 54 /** 55 * LOG_ERR from <code>sys/syslog.h</code> 56 */ 57 private static final int LOG_ERR = 3; 58 59 /** 60 * LOG_WARNING from <code>sys/syslog.h</code> 61 */ 62 private static final int LOG_WARNING = 4; 63 64 /** 65 * LOG_NOTICE from <code>sys/syslog.h</code> 66 */ 67 private static final int LOG_NOTICE = 5; 68 69 /** 70 * LOG_INFO from <code>sys/syslog.h</code> 71 */ 72 private static final int LOG_INFO = 6; 73 74 /** 75 * LOG_DEBUG from <code>sys/syslog.h</code> 76 */ 77 private static final int LOG_DEBUG = 7; 78 79 /** 80 * Maps static instances by name. 81 */ 82 private static final HashMap severityMap = new HashMap(); 83 84 /** 85 * LOG_EMERG 86 */ 87 public static final Severity EMERG = new Severity(LOG_EMERG, 88 Level.SEVERE.intValue() - 1, "EMERG"); 89 90 /** 91 * LOG_ALERT 92 */ 93 public static final Severity ALERT = new Severity(LOG_ALERT, 94 Level.SEVERE.intValue() - 2, "ALERT"); 95 96 /** 97 * LOG_CRIT 98 */ 99 public static final Severity CRIT = new Severity(LOG_CRIT, 100 Level.SEVERE.intValue() - 3, "CRIT"); 101 102 /** 103 * LOG_ERR 104 */ 105 public static final Severity ERR = new Severity(LOG_ERR, 106 Level.SEVERE.intValue() - 4, "ERR"); 107 108 /** 109 * LOG_WARNING 110 */ 111 public static final Severity WARNING = new Severity(LOG_WARNING, 112 Level.WARNING.intValue() - 1, "WARNING"); 113 114 /** 115 * LOG_NOTICE 116 */ 117 public static final Severity NOTICE = new Severity(LOG_NOTICE, 118 Level.INFO.intValue() - 1, "NOTICE"); 119 120 /** 121 * LOG_INFO 122 */ 123 public static final Severity INFO = new Severity(LOG_INFO, 124 Level.INFO.intValue() - 2, "INFO"); 125 126 /** 127 * LOG_DEBUG 128 */ 129 public static final Severity DEBUG = new Severity(LOG_DEBUG, 130 Level.FINE.intValue() - 1, "DEBUG"); 131 132 /** 133 * Add aliases. 134 */ 135 static { 136 severityMap.put("WARN", Severity.WARNING); 137 severityMap.put("ERROR", Severity.ERR); 138 } 139 140 /** 141 * Disallowed overidden constant Level 142 */ 143 private static final Level SEVERE = null; 144 145 /** 146 * Disallowed overidden constant Level 147 */ 148 private static final Level CONFIG = null; 149 150 /** 151 * Disallowed overidden constant Level 152 */ 153 private static final Level FINE = null; 154 155 /** 156 * Disallowed overidden constant Level 157 */ 158 private static final Level FINER = null; 159 160 /** 161 * Disallowed overidden constant Level 162 */ 163 private static final Level FINEST = null; 164 165 /** 166 * See getNative(). 167 */ 168 private int severity; 169 170 /** 171 * Constructs a Severity with the given native severity, Java 172 * logging level, and name. 173 */ Severity(int severity, int level, String name)174 private Severity(int severity, int level, String name) 175 { 176 super(name, level); 177 this.severity = severity; 178 179 Object displaced = severityMap.put(name, this); 180 assert (displaced == null); 181 } 182 183 /** 184 * Returns the Severity closest in meaning to the given Level. 185 * This is meant to be used by SyslogHandler to determine a 186 * proper Severity for Records which only specify a Level. 187 * 188 * <ul> 189 * <li>Level.SEVERE or higher becomes Severity.ERR 190 * <li>Level.WARNING becomes Severity.WARNING 191 * <li>Level.INFO becomes Severity.INFO 192 * <li>Level.FINE becomes Severity.DEBUG 193 * </ul> 194 * 195 * If the level is below Level.FINE, i.e. the level is too low 196 * for syslog, then null is returned. 197 */ severityForLevel(Level l)198 public static Severity severityForLevel(Level l) 199 { 200 if (l instanceof Severity) 201 return (Severity)l; 202 else { 203 if (l.intValue() >= Level.SEVERE.intValue()) 204 return (ERR); 205 if (l.intValue() >= Level.WARNING.intValue()) 206 return (WARNING); 207 if (l.intValue() >= Level.INFO.intValue()) 208 return (INFO); 209 if (l.intValue() >= Level.CONFIG.intValue()) 210 return (INFO); 211 if (l.intValue() >= Level.FINE.intValue()) 212 return (DEBUG); 213 return (null); 214 } 215 } 216 217 /** 218 * Returns the native <code>syslog(3C)</code> severity. 219 */ getNative()220 public int getNative() 221 { 222 return (severity); 223 } 224 225 /** 226 * Returns the Severity object with the given name, interpreted 227 * case-insensitively. 228 * 229 * @throws IllegalArgumentException if the name isn't of a known 230 * severity. 231 */ parse(String name)232 public static Level parse(String name) 233 { 234 Severity severity = (Severity)severityMap.get( 235 name.toUpperCase()); 236 237 if (severity == null) 238 throw new IllegalArgumentException(); 239 else 240 return (severity); 241 } 242 243 /** 244 * Returns the Severity object with the given name, interpreted 245 * case-insensitively. 246 * 247 * @throws IllegalArgumentException if the name isn't of a known 248 * severity. 249 */ getSeverityWithName(String name)250 public static Severity getSeverityWithName(String name) 251 { 252 return ((Severity)parse(name)); 253 } 254 } 255