001: /*_############################################################################
002: _##
003: _## SNMP4J - LogLevel.java
004: _##
005: _## Copyright (C) 2003-2008 Frank Fock and Jochen Katz (SNMP4J.org)
006: _##
007: _## Licensed under the Apache License, Version 2.0 (the "License");
008: _## you may not use this file except in compliance with the License.
009: _## You may obtain a copy of the License at
010: _##
011: _## http://www.apache.org/licenses/LICENSE-2.0
012: _##
013: _## Unless required by applicable law or agreed to in writing, software
014: _## distributed under the License is distributed on an "AS IS" BASIS,
015: _## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016: _## See the License for the specific language governing permissions and
017: _## limitations under the License.
018: _##
019: _##########################################################################*/
020:
021: package org.snmp4j.log;
022:
023: public class LogLevel {
024:
025: public static final int LEVEL_NONE = 0;
026: public static final int LEVEL_OFF = 1;
027: public static final int LEVEL_ALL = 2;
028: public static final int LEVEL_TRACE = 3;
029: public static final int LEVEL_DEBUG = 4;
030: public static final int LEVEL_INFO = 5;
031: public static final int LEVEL_WARN = 6;
032: public static final int LEVEL_ERROR = 7;
033: public static final int LEVEL_FATAL = 8;
034:
035: private static final String[] LEVEL_STRINGS = { "NONE", "OFF",
036: "ALL", "TRACE", "DEBUG", "INFO", "WARN", "INFO", "ERROR",
037: "FATAL" };
038:
039: public static final LogLevel NONE = new LogLevel(LEVEL_NONE);
040: public static final LogLevel OFF = new LogLevel(LEVEL_OFF);
041: public static final LogLevel ALL = new LogLevel(LEVEL_ALL);
042: public static final LogLevel TRACE = new LogLevel(LEVEL_TRACE);
043: public static final LogLevel DEBUG = new LogLevel(LEVEL_DEBUG);
044: public static final LogLevel INFO = new LogLevel(LEVEL_INFO);
045: public static final LogLevel WARN = new LogLevel(LEVEL_WARN);
046: public static final LogLevel ERROR = new LogLevel(LEVEL_ERROR);
047: public static final LogLevel FATAL = new LogLevel(LEVEL_FATAL);
048:
049: private int level;
050:
051: public LogLevel(int level) {
052: if ((level < 0) || (level > 8)) {
053: throw new IllegalArgumentException("Unknown log level "
054: + level);
055: }
056: this .level = level;
057: }
058:
059: public LogLevel(String levelString) {
060: this (levelFromString(levelString));
061: }
062:
063: /**
064: * Convert a level string "OFF", "ALL", "DEBUG", etc. into a level number.
065: * @param levelString
066: * one of the level strings "OFF", "ALL", "TRACE", "DEBUG", "INFO", "WARN",
067: * "INFO", "ERROR", and "FATAL".
068: * @return
069: * a number > 0 if the level string could be converted into a level,
070: * 0 otherwise.
071: */
072: public static int levelFromString(String levelString) {
073: int ind = -1;
074: for (int i = 0; i < LEVEL_STRINGS.length; i++) {
075: if (LEVEL_STRINGS[i].equals(levelString)) {
076: ind = i;
077: break;
078: }
079: }
080: return ind;
081: }
082:
083: /**
084: * Returns a <code>LogLevel</code> object for the specified level string.
085: * @param levelString
086: * one of the level strings "OFF", "ALL", "TRACE", "DEBUG", "INFO", "WARN",
087: * "INFO", "ERROR", and "FATAL".
088: * @return
089: * one of the <code>LogLevel</code> constants defined by this class.
090: * @since 1.7.2
091: */
092: public static LogLevel toLevel(String levelString) {
093: return new LogLevel(levelFromString(levelString));
094: }
095:
096: public int getLevel() {
097: return level;
098: }
099:
100: public String toString() {
101: return LEVEL_STRINGS[level];
102: }
103:
104: }
|