EMMA Coverage Report (generated Tue Feb 12 22:23:49 ICT 2008)
[all classes][net.sourceforge.hivegui.application]

COVERAGE SUMMARY FOR SOURCE FILE [JavaLoggingToCommonLoggingRedirector.java]

nameclass, %method, %block, %line, %
JavaLoggingToCommonLoggingRedirector.java0%   (0/3)0%   (0/9)0%   (0/150)0%   (0/45)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class JavaLoggingToCommonLoggingRedirector0%   (0/1)0%   (0/3)0%   (0/67)0%   (0/18)
JavaLoggingToCommonLoggingRedirector (): void 0%   (0/1)0%   (0/3)0%   (0/2)
activate (): void 0%   (0/1)0%   (0/51)0%   (0/12)
deactivate (): void 0%   (0/1)0%   (0/13)0%   (0/4)
     
class JavaLoggingToCommonLoggingRedirector$10%   (0/1)100% (0/0)100% (0/0)100% (0/0)
     
class JavaLoggingToCommonLoggingRedirector$JDKLogHandler0%   (0/1)0%   (0/6)0%   (0/83)0%   (0/27)
JavaLoggingToCommonLoggingRedirector$JDKLogHandler (): void 0%   (0/1)0%   (0/8)0%   (0/2)
JavaLoggingToCommonLoggingRedirector$JDKLogHandler (JavaLoggingToCommonLoggin... 0%   (0/1)0%   (0/3)0%   (0/1)
close (): void 0%   (0/1)0%   (0/1)0%   (0/1)
flush (): void 0%   (0/1)0%   (0/1)0%   (0/1)
getLog (String): Log 0%   (0/1)0%   (0/19)0%   (0/5)
publish (LogRecord): void 0%   (0/1)0%   (0/51)0%   (0/18)

1//  Copyright 2004-2007 Jean-Francois Poilpret
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14 
15package net.sourceforge.hivegui.application;
16 
17import java.util.Map;
18import java.util.concurrent.ConcurrentHashMap;
19import java.util.logging.Handler;
20import java.util.logging.Level;
21import java.util.logging.LogManager;
22import java.util.logging.LogRecord;
23import java.util.logging.Logger;
24 
25import org.apache.commons.logging.Log;
26import org.apache.commons.logging.LogFactory;
27 
28/**
29 * Utility class to redirect JUL logs (from Swing App Framework) to 
30 * commons-logging (used by hivemind, hivemind-utilities).
31 * @author MarkusDoering
32 */
33public final class JavaLoggingToCommonLoggingRedirector
34{
35        private JavaLoggingToCommonLoggingRedirector()
36        {
37        }
38 
39        /**
40         * Activates this feature.
41         */
42        public static void activate()
43        {
44                // CSOFF: IllegalCatchCheck
45                try
46                {
47                        Logger rootLogger = LogManager.getLogManager().getLogger("");
48                        // remove old handlers
49                        for (Handler handler : rootLogger.getHandlers())
50                        {
51                                rootLogger.removeHandler(handler);
52                        }
53                        // add our own
54                        _activeHandler = new JDKLogHandler();
55                        _activeHandler.setLevel(Level.ALL);
56                        rootLogger.addHandler(_activeHandler);
57                        rootLogger.setLevel(Level.ALL);
58                        // done, let's check it right away!!!
59                        Logger.getLogger(JavaLoggingToCommonLoggingRedirector.class.getName())
60                                .info("activated: sending JDK log messages to Commons Logging");
61                }
62                catch (Exception exc)
63                {
64                        LogFactory.getLog(JavaLoggingToCommonLoggingRedirector.class)
65                                .error("activation failed", exc);
66                }
67                // CSON: IllegalCatchCheck
68        }
69 
70        public static void deactivate()
71        {
72                Logger rootLogger = LogManager.getLogManager().getLogger("");
73                rootLogger.removeHandler(_activeHandler);
74                Logger.getLogger(JavaLoggingToCommonLoggingRedirector.class.getName())
75                        .info("dactivated");
76        }
77 
78        static private class JDKLogHandler extends Handler
79        {
80                private Map<String, Log> _cachedLogs = new ConcurrentHashMap<String, Log>();
81                
82                private Log getLog(String logName)
83                {
84                        Log log = _cachedLogs.get(logName);
85                        if (log == null)
86                        {
87                                log = LogFactory.getLog(logName);
88                                _cachedLogs.put(logName, log);
89                        }
90                        return log;
91                }
92                
93                @Override public void publish(LogRecord record)
94                {
95                        Log log = getLog(record.getLoggerName());
96                        String message = record.getMessage();
97                        Throwable exception = record.getThrown();
98                        Level level = record.getLevel();
99                        if (level == Level.SEVERE)
100                        {
101                                log.error(message, exception);
102                        }
103                        else if (level == Level.WARNING)
104                        {
105                                log.warn(message, exception);
106                        }
107                        else if (level == Level.INFO)
108                        {
109                                log.info(message, exception);
110                        }
111                        else if (level == Level.CONFIG)
112                        {
113                                log.debug(message, exception);
114                        }
115                        else
116                        {
117                                log.trace(message, exception);
118                        }
119                }
120                
121                @Override public void flush()
122                {
123                        // nothing to do
124                }
125                
126                @Override public void close()
127                {
128                        // nothing to do
129                }
130        }
131 
132        static private JDKLogHandler _activeHandler;
133}

[all classes][net.sourceforge.hivegui.application]
EMMA 2.0.5312 (C) Vladimir Roubtsov