/*
 * Copyright 2011 Avid Technology, Inc.
 * 
 * Licensed  under the  Apache License,  Version 2.0  (the "License");
 * you may not use  this file  except in  compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed  under the  License is distributed on an "AS IS" BASIS,
 * WITHOUT  WARRANTIES OR CONDITIONS  OF ANY KIND, either  express  or
 * implied.
 *
 * See the License for the specific language governing permissions and
 * limitations under the License. 
 */
package org.ops4j.pax.logging.logback.internal;

import ch.qos.logback.classic.Level;

import org.ops4j.pax.logging.PaxLogger;
import org.ops4j.pax.logging.spi.PaxLevel;

/**
 * A straightforward facade to make a Logback Level instance look like a PaxLevel.
 * @author Chris Dolan
 */
public class PaxLevelForLogback implements PaxLevel {
    private Level m_delegate;

    public PaxLevelForLogback(Level delegate) {
        m_delegate = delegate;
    }

    public boolean isGreaterOrEqual( PaxLevel r ) {
        if (r instanceof PaxLevelForLogback) {
            return m_delegate.isGreaterOrEqual( ((PaxLevelForLogback) r).m_delegate );
        } else {
            // fallback case: the syslog numbers are portable
            return getSyslogEquivalent() <= r.getSyslogEquivalent();
        }
    }

    public int toInt() {
    	if (m_delegate.isGreaterOrEqual(Level.ERROR))
    		return PaxLogger.LEVEL_ERROR;
    	if (m_delegate.isGreaterOrEqual(Level.WARN))
    		return PaxLogger.LEVEL_WARNING;
    	if (m_delegate.isGreaterOrEqual(Level.INFO))
    		return PaxLogger.LEVEL_INFO;
    	if (m_delegate.isGreaterOrEqual(Level.DEBUG))
    		return PaxLogger.LEVEL_DEBUG;
    	return PaxLogger.LEVEL_TRACE;
    }

    public int getSyslogEquivalent() {
        // this data comes from the log4j level class
        if (m_delegate == Level.TRACE)
            return 7;
        if (m_delegate == Level.DEBUG)
            return 7;
        if (m_delegate == Level.INFO)
            return 6;
        if (m_delegate == Level.WARN)
            return 4;
        if (m_delegate == Level.ERROR)
            return 3;
        if (m_delegate == Level.OFF)
            return 0;
        //noinspection IfStatementWithIdenticalBranches
        if (m_delegate == Level.ALL)
            return 7;
        return 7; // fallback case...
    }

    @Override
    public String toString() {
        return m_delegate.toString();
    }
}

@@ -956,16 +956,293 @@
 Level %7B%0A
+    // this data comes from the log4j level class%0A    public static final int SYSLOG_DEBUG = 7;%0A    public static final int SYSLOG_INFO = 6;%0A    public static final int SYSLOG_WARN = 4;%0A    public static final int SYSLOG_ERROR = 3;%0A    public static final int SYSLOG_OFF = 0;%0A%0A
     priv
@@ -2158,62 +2158,8 @@
 ) %7B%0A
-        // this data comes from the log4j level class%0A
     
@@ -2204,33 +2204,44 @@
          return 
-7
+SYSLOG_DEBUG
 ;%0A        if (m_
@@ -2276,33 +2276,44 @@
          return 
-7
+SYSLOG_DEBUG
 ;%0A        if (m_
@@ -2355,17 +2355,27 @@
  return 
-6
+SYSLOG_INFO
 ;%0A      
@@ -2425,17 +2425,27 @@
  return 
-4
+SYSLOG_WARN
 ;%0A      
@@ -2496,17 +2496,28 @@
  return 
-3
+SYSLOG_ERROR
 ;%0A      
@@ -2566,17 +2566,26 @@
  return 
-0
+SYSLOG_OFF
 ;%0A      
@@ -2690,17 +2690,28 @@
  return 
-7
+SYSLOG_DEBUG
 ;%0A      
@@ -2719,17 +2719,28 @@
  return 
-7
+SYSLOG_DEBUG
 ; // fal

use constants to document syslog level numbers