Francesco Furfari | ec7e175 | 2006-10-02 13:37:04 +0000 | [diff] [blame] | 1 | /*
|
| 2 | * Licensed to the Apache Software Foundation (ASF) under one
|
| 3 | * or more contributor license agreements. See the NOTICE file
|
| 4 | * distributed with this work for additional information
|
| 5 | * regarding copyright ownership. The ASF licenses this file
|
| 6 | * to you under the Apache License, Version 2.0 (the
|
| 7 | * "License"); you may not use this file except in compliance
|
| 8 | * with the License. You may obtain a copy of the License at
|
Francesco Furfari | d8bdb64 | 2006-04-04 23:33:40 +0000 | [diff] [blame] | 9 | *
|
Francesco Furfari | ec7e175 | 2006-10-02 13:37:04 +0000 | [diff] [blame] | 10 | * http://www.apache.org/licenses/LICENSE-2.0
|
Francesco Furfari | d8bdb64 | 2006-04-04 23:33:40 +0000 | [diff] [blame] | 11 | *
|
Francesco Furfari | ec7e175 | 2006-10-02 13:37:04 +0000 | [diff] [blame] | 12 | * Unless required by applicable law or agreed to in writing,
|
| 13 | * software distributed under the License is distributed on an
|
| 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
| 15 | * KIND, either express or implied. See the License for the
|
| 16 | * specific language governing permissions and limitations
|
| 17 | * under the License.
|
Francesco Furfari | d8bdb64 | 2006-04-04 23:33:40 +0000 | [diff] [blame] | 18 | */
|
| 19 |
|
| 20 | package org.apache.felix.upnp.basedriver.tool;
|
| 21 |
|
| 22 | import java.io.PrintStream;
|
| 23 |
|
| 24 | import org.cybergarage.util.Debug;
|
| 25 |
|
| 26 | import org.osgi.framework.Constants;
|
| 27 | import org.osgi.framework.InvalidSyntaxException;
|
| 28 | import org.osgi.framework.ServiceEvent;
|
| 29 | import org.osgi.framework.ServiceListener;
|
| 30 | import org.osgi.framework.ServiceReference;
|
| 31 | import org.osgi.service.log.LogService;
|
| 32 |
|
| 33 | import org.apache.felix.upnp.basedriver.Activator;
|
| 34 |
|
Francesco Furfari | f2a6791 | 2006-07-17 17:08:02 +0000 | [diff] [blame] | 35 | /*
|
Karl Pauls | d312acc | 2007-06-18 20:38:33 +0000 | [diff] [blame^] | 36 | * @author <a href="mailto:dev@felix.apache.org">Felix Project Team</a>
|
Francesco Furfari | f2a6791 | 2006-07-17 17:08:02 +0000 | [diff] [blame] | 37 | */
|
Francesco Furfari | d8bdb64 | 2006-04-04 23:33:40 +0000 | [diff] [blame] | 38 | public class Logger implements ServiceListener {
|
| 39 |
|
| 40 | private ServiceReference rls;
|
| 41 | private LogService osgiLogService;
|
| 42 | private int level;
|
| 43 | private PrintStream out;
|
| 44 | public final static String NEWLINE = System.getProperty("line.separator");
|
| 45 | public static final String ROW ="\n================REQUEST=====================\n";
|
| 46 | public static final String END_ROW ="--------------------------------------------";
|
| 47 |
|
| 48 | private final static String LEVEL_TO_STRING[] = new String[]{
|
| 49 | "",
|
| 50 | "ERROR [ " + Activator.bc.getBundle().getBundleId() + " ] ",
|
| 51 | "WARNING [ " + Activator.bc.getBundle().getBundleId() + " ] ",
|
| 52 | "INFO [ " + Activator.bc.getBundle().getBundleId() + " ] ",
|
| 53 | "DEBUG [ " + Activator.bc.getBundle().getBundleId() + " ] ",
|
| 54 | };
|
| 55 |
|
| 56 |
|
| 57 | /**
|
| 58 | * Create a Logger with <code>System.out</code> as <tt>PrintStream</tt> and without
|
| 59 | * reporting message on both <tt>PrintStream</tt> and <tt>LogService</tt>
|
| 60 | *
|
| 61 | * @param log <tt>ServiceReference</tt> to the <tt>LogService</tt> to use,
|
| 62 | * or null to avoid the use of this service
|
| 63 | *
|
| 64 | * @see #Logger(LogService, PrintStream, boolean)
|
| 65 | */
|
| 66 | public Logger(String levelStr){
|
| 67 | this.out = System.out;
|
| 68 | try {
|
| 69 | this.level = Integer.parseInt(levelStr);
|
| 70 | } catch (Exception ex){
|
| 71 | out.println("WARNING [UPnPBaseDriver Log]: " + levelStr+" is not a valid value!");
|
| 72 | this.level=2;
|
| 73 | }
|
| 74 | findService();
|
| 75 | }
|
| 76 |
|
| 77 | public void setCyberDebug(String value){
|
| 78 | try {
|
| 79 | if (Boolean.valueOf(value).booleanValue()){
|
| 80 | Debug.on();
|
| 81 | out.println("INFO [UPnPBaseDriver] Started CyberLink Debug");
|
| 82 | }
|
| 83 | } catch (Exception ex){
|
| 84 | out.println("WARNING [UPnPBaseDriver CyberLog]: " + value +" is not a valid value!");
|
| 85 | }
|
| 86 | }
|
| 87 |
|
| 88 | ////////////////////////////////////////////////////////////////////////////////////
|
| 89 | //////////////////////////// programmatic interface ////////////////////////////////
|
| 90 | ////////////////////////////////////////////////////////////////////////////////////
|
| 91 |
|
| 92 | public void setLogLevel(int level){
|
| 93 | if (level < 0 || level >4 ) throw new IllegalArgumentException("Log Level must be [0-4]");
|
| 94 | this.level = level;
|
| 95 | }
|
| 96 |
|
| 97 | public int getLogLevel(){
|
| 98 | return this.level;
|
| 99 | }
|
| 100 |
|
| 101 |
|
| 102 | public void setCyberDebug(boolean value){
|
| 103 | if (value) Debug.on();
|
| 104 | else Debug.off();
|
| 105 | }
|
| 106 |
|
| 107 | public boolean getCyberDebug(){
|
| 108 | return Debug.isOn();
|
| 109 | }
|
| 110 | //////////////////////////// end programmatic interface ////////////////////////////
|
| 111 |
|
| 112 |
|
| 113 | public final void ERROR(String message) {
|
| 114 | log(1,message);
|
| 115 | }
|
| 116 |
|
| 117 | public final void WARNING(String message) {
|
| 118 | log(2,message);
|
| 119 | }
|
| 120 |
|
| 121 | public final void INFO(String message) {
|
| 122 | log(3,message);
|
| 123 | }
|
| 124 |
|
| 125 | public final void DEBUG(String message) {
|
| 126 | log(4,message);
|
| 127 | }
|
| 128 |
|
| 129 | public final void PACKET(String message) {
|
| 130 | log(4, new StringBuffer(ROW).append(message).append(END_ROW).toString());
|
| 131 | }
|
| 132 |
|
| 133 | /**
|
| 134 | * Logs a message.
|
| 135 | *
|
| 136 | * <p>The <tt>ServiceReference</tt> field and the <tt>Throwable</tt>
|
| 137 | * field of the <tt>LogEntry</tt> object will be set to <tt>null</tt>.
|
| 138 | * @param msglevel The severity of the message.
|
| 139 | * This should be one of the defined log levels
|
| 140 | * but may be any integer that is interpreted in a user defined way.
|
| 141 | * @param message Human readable string describing the condition or <tt>null</tt>.
|
| 142 | * @see #LOG_ERROR
|
| 143 | * @see #LOG_WARNING
|
| 144 | * @see #LOG_INFO
|
| 145 | * @see #LOG_DEBUG
|
| 146 | */
|
| 147 | public void log(int msglevel, String message) {
|
| 148 | synchronized (this) {
|
| 149 | if (msglevel <= this.level){
|
| 150 | if (this.osgiLogService != null ){
|
| 151 | osgiLogService.log(msglevel, message);
|
| 152 | }
|
| 153 | else {
|
| 154 | StringBuffer sb = new StringBuffer(Logger.LEVEL_TO_STRING[msglevel]);
|
| 155 | this.out.println(sb.append(message));
|
| 156 | }
|
| 157 | }
|
| 158 | }
|
| 159 |
|
| 160 | }
|
| 161 |
|
| 162 | /**
|
| 163 | * Logs a message with an exception.
|
| 164 | *
|
| 165 | * <p>The <tt>ServiceReference</tt> field of the <tt>LogEntry</tt> object will be set to <tt>null</tt>.
|
| 166 | * @param msglevel The severity of the message.
|
| 167 | * This should be one of the defined log levels
|
| 168 | * but may be any integer that is interpreted in a user defined way.
|
| 169 | * @param message The human readable string describing the condition or <tt>null</tt>.
|
| 170 | * @param exception The exception that reflects the condition or <tt>null</tt>.
|
| 171 | * @see #LOG_ERROR
|
| 172 | * @see #LOG_WARNING
|
| 173 | * @see #LOG_INFO
|
| 174 | * @see #LOG_DEBUG
|
| 175 | */
|
| 176 | public void log(int msglevel, String message, Throwable exception) {
|
| 177 | synchronized (this) {
|
| 178 | if (msglevel <= this.level){
|
| 179 | if(this.osgiLogService != null){
|
| 180 | osgiLogService.log(msglevel, message, exception);
|
| 181 | }
|
| 182 | else {
|
| 183 | StringBuffer sb = new StringBuffer(Logger.LEVEL_TO_STRING[msglevel]);
|
| 184 | this.out.println(sb.append(message).append(NEWLINE).append(exception));
|
| 185 | exception.printStackTrace(this.out);
|
| 186 | }
|
| 187 | }
|
| 188 | }
|
| 189 | }
|
| 190 |
|
| 191 | private synchronized void setLogService(ServiceReference reference){
|
| 192 | this.rls = reference;
|
| 193 | this.osgiLogService = (LogService) Activator.bc.getService(rls);
|
| 194 | }
|
| 195 | /**
|
| 196 | * This look for a <tt>LogService</tt> if it founds no <tt>LogService</tt> will register a new
|
| 197 | * Listener of LogService
|
| 198 | *
|
| 199 | */
|
| 200 | private synchronized void findService() {
|
| 201 | //PRE:Actually no LogService are setted and we are registered as ServiceListener
|
| 202 | // for LogService (unregisterin event)
|
| 203 | this.rls = Activator.bc.getServiceReference(LogService.class.getName());
|
| 204 | if (this.rls != null){
|
| 205 | this.osgiLogService = (LogService) Activator.bc.getService(rls);
|
| 206 | }
|
| 207 | try {
|
| 208 | Activator.bc.addServiceListener(this,
|
| 209 | "(" + Constants.OBJECTCLASS + "=" + LogService.class.getName() + ")"
|
| 210 | );
|
| 211 | } catch (InvalidSyntaxException ignore) {}
|
| 212 | //POST: We are connected to a LogService or we are registered as ServiceListener
|
| 213 | // for LogService(registering event)
|
| 214 | }
|
| 215 |
|
| 216 | private synchronized void releaseLogService() {
|
| 217 | if( osgiLogService != null)
|
| 218 | Activator.bc.ungetService(this.rls);
|
| 219 | this.rls = null;
|
| 220 | this.osgiLogService = null;
|
| 221 | }
|
| 222 |
|
| 223 | /**
|
| 224 | * Used to keep track the existence of a <tt>LogService</tt>
|
| 225 | *
|
| 226 | * @see ServiceListener#serviceChanged(org.osgi.framework.ServiceEvent)
|
| 227 | */
|
| 228 | public void serviceChanged(ServiceEvent e) {
|
| 229 | switch (e.getType()) {
|
| 230 | case ServiceEvent.REGISTERED: {
|
| 231 | // put here code check for serviceid
|
| 232 | setLogService(e.getServiceReference());
|
| 233 | }break;
|
| 234 |
|
| 235 | case ServiceEvent.MODIFIED:
|
| 236 | break;
|
| 237 |
|
| 238 | case ServiceEvent.UNREGISTERING: {
|
| 239 | // put here code check for serviceid
|
| 240 | releaseLogService();
|
| 241 | }break;
|
| 242 | }
|
| 243 | }
|
| 244 |
|
| 245 | /**
|
| 246 | * Stop using the <tt>LogService</tt> and listening for those service event
|
| 247 | *
|
| 248 | * NOTE: All the message will be reported to <tt>PrintStream</tt>
|
| 249 | *
|
| 250 | */
|
| 251 | public void close(){
|
| 252 | Activator.bc.removeServiceListener(this);
|
| 253 | releaseLogService();
|
| 254 | }
|
| 255 | }
|