Richard S. Hall | 930fecc | 2005-08-16 18:33:34 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2005 The Apache Software Foundation |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | * |
| 16 | */ |
Richard S. Hall | 5a03159 | 2005-08-19 19:53:58 +0000 | [diff] [blame] | 17 | package org.apache.felix.framework; |
Richard S. Hall | 930fecc | 2005-08-16 18:33:34 +0000 | [diff] [blame] | 18 | |
| 19 | import org.osgi.framework.BundleException; |
| 20 | import org.osgi.framework.ServiceReference; |
| 21 | |
| 22 | /** |
| 23 | * <p> |
| 24 | * This class mimics the standard OSGi <tt>LogService</tt> interface. An |
| 25 | * instance of this class will be used by the framework for all logging. Currently, |
| 26 | * the implementation of this class just sends log messages to standard output, |
| 27 | * but in the future it will be modified to use a log service if one is |
| 28 | * installed in the framework. To do so, it will need to use reflection to |
| 29 | * call the log service methods, since it will not have access to the |
| 30 | * <tt>LogService</tt> class. |
| 31 | * </p> |
| 32 | **/ |
| 33 | // TODO: Modify LogWrapper to get LogService service object and invoke with reflection. |
| 34 | public class LogWrapper |
| 35 | { |
| 36 | public static final int LOG_ERROR = 1; |
| 37 | public static final int LOG_WARNING = 2; |
| 38 | public static final int LOG_INFO = 3; |
| 39 | public static final int LOG_DEBUG = 4; |
| 40 | |
| 41 | private Object m_logObj = null; |
| 42 | |
| 43 | public LogWrapper() |
| 44 | { |
| 45 | } |
| 46 | |
| 47 | public void log(int level, String msg) |
| 48 | { |
| 49 | synchronized (this) |
| 50 | { |
| 51 | if (m_logObj != null) |
| 52 | { |
| 53 | // Will use reflection. |
| 54 | // m_logObj.log(level, msg); |
| 55 | } |
| 56 | else |
| 57 | { |
| 58 | _log(null, level, msg, null); |
| 59 | } |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | public void log(int level, String msg, Throwable ex) |
| 64 | { |
| 65 | synchronized (this) |
| 66 | { |
| 67 | if (m_logObj != null) |
| 68 | { |
| 69 | // Will use reflection. |
| 70 | // m_logObj.log(level, msg); |
| 71 | } |
| 72 | else |
| 73 | { |
| 74 | _log(null, level, msg, ex); |
| 75 | } |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | public void log(ServiceReference sr, int level, String msg) |
| 80 | { |
| 81 | synchronized (this) |
| 82 | { |
| 83 | if (m_logObj != null) |
| 84 | { |
| 85 | // Will use reflection. |
| 86 | // m_logObj.log(level, msg); |
| 87 | } |
| 88 | else |
| 89 | { |
| 90 | _log(sr, level, msg, null); |
| 91 | } |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | public void log(ServiceReference sr, int level, String msg, Throwable ex) |
| 96 | { |
| 97 | synchronized (this) |
| 98 | { |
| 99 | if (m_logObj != null) |
| 100 | { |
| 101 | // Will use reflection. |
| 102 | // m_logObj.log(level, msg); |
| 103 | } |
| 104 | else |
| 105 | { |
| 106 | _log(sr, level, msg, ex); |
| 107 | } |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | private void _log(ServiceReference sr, int level, String msg, Throwable ex) |
| 112 | { |
| 113 | String s = (sr == null) ? null : "SvcRef " + sr; |
| 114 | s = (s == null) ? msg : s + " " + msg; |
| 115 | s = (ex == null) ? s : s + " (" + ex + ")"; |
| 116 | switch (level) |
| 117 | { |
| 118 | case LOG_DEBUG: |
| 119 | System.out.println("DEBUG: " + s); |
| 120 | break; |
| 121 | case LOG_ERROR: |
| 122 | System.out.println("ERROR: " + s); |
| 123 | if (ex != null) |
| 124 | { |
| 125 | if ((ex instanceof BundleException) && |
| 126 | (((BundleException) ex).getNestedException() != null)) |
| 127 | { |
| 128 | ex = ((BundleException) ex).getNestedException(); |
| 129 | } |
| 130 | ex.printStackTrace(); |
| 131 | } |
| 132 | break; |
| 133 | case LOG_INFO: |
| 134 | System.out.println("INFO: " + s); |
| 135 | break; |
| 136 | case LOG_WARNING: |
| 137 | System.out.println("WARNING: " + s); |
| 138 | break; |
| 139 | default: |
| 140 | System.out.println("UNKNOWN[" + level + "]: " + s); |
| 141 | } |
| 142 | } |
| 143 | } |