1//
2//  ========================================================================
3//  Copyright (c) 1995-2014 Mort Bay Consulting Pty. Ltd.
4//  ------------------------------------------------------------------------
5//  All rights reserved. This program and the accompanying materials
6//  are made available under the terms of the Eclipse Public License v1.0
7//  and Apache License v2.0 which accompanies this distribution.
8//
9//      The Eclipse Public License is available at
10//      http://www.eclipse.org/legal/epl-v10.html
11//
12//      The Apache License v2.0 is available at
13//      http://www.opensource.org/licenses/apache2.0.php
14//
15//  You may elect to redistribute this code under either of these licenses.
16//  ========================================================================
17//
18
19package org.eclipse.jetty.util.log;
20
21import java.lang.reflect.Method;
22
23/**
24 *
25 */
26public class LoggerLog extends AbstractLogger
27{
28    private final Object _logger;
29    private final Method _debugMT;
30    private final Method _debugMAA;
31    private final Method _infoMT;
32    private final Method _infoMAA;
33    private final Method _warnMT;
34    private final Method _warnMAA;
35    private final Method _setDebugEnabledE;
36    private final Method _getLoggerN;
37    private final Method _getName;
38    private volatile boolean _debug;
39
40    public LoggerLog(Object logger)
41    {
42        try
43        {
44            _logger = logger;
45            Class<?> lc = logger.getClass();
46            _debugMT = lc.getMethod("debug", new Class[]{String.class, Throwable.class});
47            _debugMAA = lc.getMethod("debug", new Class[]{String.class, Object[].class});
48            _infoMT = lc.getMethod("info", new Class[]{String.class, Throwable.class});
49            _infoMAA = lc.getMethod("info", new Class[]{String.class, Object[].class});
50            _warnMT = lc.getMethod("warn", new Class[]{String.class, Throwable.class});
51            _warnMAA = lc.getMethod("warn", new Class[]{String.class, Object[].class});
52            Method _isDebugEnabled = lc.getMethod("isDebugEnabled");
53            _setDebugEnabledE = lc.getMethod("setDebugEnabled", new Class[]{Boolean.TYPE});
54            _getLoggerN = lc.getMethod("getLogger", new Class[]{String.class});
55            _getName = lc.getMethod("getName");
56
57            _debug = (Boolean)_isDebugEnabled.invoke(_logger);
58        }
59        catch(Exception x)
60        {
61            throw new IllegalStateException(x);
62        }
63    }
64
65    public String getName()
66    {
67        try
68        {
69            return (String)_getName.invoke(_logger);
70        }
71        catch (Exception e)
72        {
73            e.printStackTrace();
74            return null;
75        }
76    }
77
78    public void warn(String msg, Object... args)
79    {
80        try
81        {
82            _warnMAA.invoke(_logger, args);
83        }
84        catch (Exception e)
85        {
86            e.printStackTrace();
87        }
88    }
89
90    public void warn(Throwable thrown)
91    {
92        warn("", thrown);
93    }
94
95    public void warn(String msg, Throwable thrown)
96    {
97        try
98        {
99            _warnMT.invoke(_logger, msg, thrown);
100        }
101        catch (Exception e)
102        {
103            e.printStackTrace();
104        }
105    }
106
107    public void info(String msg, Object... args)
108    {
109        try
110        {
111            _infoMAA.invoke(_logger, args);
112        }
113        catch (Exception e)
114        {
115            e.printStackTrace();
116        }
117    }
118
119    public void info(Throwable thrown)
120    {
121        info("", thrown);
122    }
123
124    public void info(String msg, Throwable thrown)
125    {
126        try
127        {
128            _infoMT.invoke(_logger, msg, thrown);
129        }
130        catch (Exception e)
131        {
132            e.printStackTrace();
133        }
134    }
135
136    public boolean isDebugEnabled()
137    {
138        return _debug;
139    }
140
141    public void setDebugEnabled(boolean enabled)
142    {
143        try
144        {
145            _setDebugEnabledE.invoke(_logger, enabled);
146            _debug = enabled;
147        }
148        catch (Exception e)
149        {
150            e.printStackTrace();
151        }
152    }
153
154    public void debug(String msg, Object... args)
155    {
156        if (!_debug)
157            return;
158
159        try
160        {
161            _debugMAA.invoke(_logger, args);
162        }
163        catch (Exception e)
164        {
165            e.printStackTrace();
166        }
167    }
168
169    public void debug(Throwable thrown)
170    {
171        debug("", thrown);
172    }
173
174    public void debug(String msg, Throwable th)
175    {
176        if (!_debug)
177            return;
178
179        try
180        {
181            _debugMT.invoke(_logger, msg, th);
182        }
183        catch (Exception e)
184        {
185            e.printStackTrace();
186        }
187    }
188
189    public void ignore(Throwable ignored)
190    {
191        if (Log.isIgnored())
192        {
193            warn(Log.IGNORED, ignored);
194        }
195    }
196
197    /**
198     * Create a Child Logger of this Logger.
199     */
200    protected Logger newLogger(String fullname)
201    {
202        try
203        {
204            Object logger=_getLoggerN.invoke(_logger, fullname);
205            return new LoggerLog(logger);
206        }
207        catch (Exception e)
208        {
209            e.printStackTrace();
210            return this;
211        }
212    }
213}
214