org.osgi.service.log.LogService Java Examples

The following examples show how to use org.osgi.service.log.LogService. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: UserService.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
public void login(final String name) {
  final File f = new File(fileName);

  AccessController.doPrivileged(new PrivilegedAction() {
    public Object run() {
      if (f.exists()) {
        throw new IllegalStateException("User already logged in");
      }

      try {
        OutputStream os = new FileOutputStream(f);
        os.write(name.getBytes("UTF-8"));
        os.close();
        log(LogService.LOG_INFO, "User " + name + " logged in");
      } catch (IOException ioe) {
        log(LogService.LOG_WARNING, "Problem logging user in: " + ioe);
      }
      return null;
    }
  });
}
 
Example #2
Source File: ConsoleLogFormatter.java    From vespa with Apache License 2.0 6 votes vote down vote up
private StringBuilder formatLevel(LogEntry entry, StringBuilder out) {
    switch (entry.getLevel()) {
    case LogService.LOG_ERROR:
        out.append("error");
        break;
    case LogService.LOG_WARNING:
        out.append("warning");
        break;
    case LogService.LOG_INFO:
        out.append("info");
        break;
    case LogService.LOG_DEBUG:
        out.append("debug");
        break;
    default:
        out.append("unknown");
        break;
    }
    return out;
}
 
Example #3
Source File: SwingIO.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
void restoreSystemIO() {

    //    synchronized(grabLock)
      {
      if(bGrabbed) {
        ConsoleSwing.log(LogService.LOG_DEBUG, "restoring system I/O...");
        try {
          if(origIn != null) {
            System.setIn(origIn);
          }
          if(origOut != null) {
            System.setOut(origOut);
          }
          if(origIn != null) {
            System.setErr(origErr);
          }
          ConsoleSwing.log(LogService.LOG_DEBUG, "...restored system I/O");
          bGrabbed = false;
        } catch (Exception e) {
          ConsoleSwing.log(LogService.LOG_ERROR, "Failed to restore IO", e);
        }
      }

    }

  }
 
Example #4
Source File: OsgiLogHandler.java    From vespa with Apache License 2.0 6 votes vote down vote up
public static int toServiceLevel(Level level) {
    int val = level.intValue();
    if (val >= Level.SEVERE.intValue()) {
        return LogService.LOG_ERROR;
    }
    if (val >= Level.WARNING.intValue()) {
        return LogService.LOG_WARNING;
    }
    if (val >= Level.INFO.intValue()) {
        return LogService.LOG_INFO;
    }
    // Level.CONFIG
    // Level.FINE
    // Level.FINER
    // Level.FINEST
    return LogService.LOG_DEBUG;
}
 
Example #5
Source File: LoggerDelegate.java    From google-cloud-eclipse with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("deprecation")
@Override
public boolean isLoggable(int level) {
  // Oddly LogService#LOG_xxx are deprecated in favour of LogLevel enum, but
  // the LogLevel enum does not provide integer mappings.
  switch (level) {
    case LogService.LOG_ERROR:
      return isErrorEnabled();
    case LogService.LOG_WARNING:
      return isWarnEnabled();
    case LogService.LOG_INFO:
      return isInfoEnabled();
    case LogService.LOG_DEBUG:
      return isDebugEnabled();
    default:
      return false;
  }
}
 
Example #6
Source File: LoggerDelegate.java    From google-cloud-eclipse with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("deprecation")
@Override
public void log(Object context, int level, String message, Throwable exception) {
  // Oddly LogService#LOG_xxx are deprecated in favour of LogLevel enum, but
  // the LogLevel enum does not provide integer mappings.
  switch (level) {
    case LogService.LOG_ERROR:
      logger.error(message, exception);
      break;
    case LogService.LOG_WARNING:
      logger.warn(message, exception);
      break;
    case LogService.LOG_INFO:
      logger.info(message, exception);
      break;
    case LogService.LOG_DEBUG:
      logger.debug(message, exception);
      break;
    default:
      logger.debug(message, exception);
      break;
  }
}
 
Example #7
Source File: Activator.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Log message via specified BundleContext
 */
static void logBC(BundleContext bc, int level, String msg, Throwable t) {
  try {
    ServiceReference<LogService> sr = bc.getServiceReference(LogService.class);
    if (sr != null) {
      LogService log = bc.getService(sr);
      if (log != null) {
        log.log(level, msg, t);
        bc.ungetService(sr);
      }
    }
  } catch (IllegalStateException ise) {
    log(level, "Logging message for " + bc.getBundle() 
        + " since it was inactive: " + msg, t);
  }
}
 
Example #8
Source File: ConsoleLogFormatter.java    From vespa with Apache License 2.0 6 votes vote down vote up
private StringBuilder formatException(LogEntry entry, StringBuilder out) {
    Throwable t = entry.getException();
    if (t != null) {
        if (entry.getLevel() == LogService.LOG_INFO) {
            out.append(": ");
            String msg = t.getMessage();
            if (msg != null) {
                formatString(msg, out);
            } else {
                out.append(t.getClass().getName());
            }
        } else {
            Writer buf = new StringWriter();
            t.printStackTrace(new PrintWriter(buf));
            formatString("\n" + buf, out);
        }
    }
    return out;
}
 
Example #9
Source File: CommandTty.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
public void log(int level, String msg, Exception e) {
  if(logTracker != null) {
    final LogService sLog = logTracker.getService();
    if (sLog != null) {
      if (e == null) {
        sLog.log(level, msg);
      } else {
        sLog.log(level, msg, e);
      }
      return;
    }
  }
  System.out.println("LOG " + level + ": " + msg);
  if(e != null) {
    e.printStackTrace();
  }
}
 
Example #10
Source File: OsgiLogServiceTestCase.java    From vespa with Apache License 2.0 6 votes vote down vote up
@Test
public void requireThatLogServiceIsRegistered() throws BundleException, InterruptedException {
    OsgiFramework osgi = TestDriver.newOsgiFramework();
    osgi.start();

    ServiceTracker<?,?> logs = newTracker(osgi, LogService.class);
    ServiceTracker<?,?> logReaders = newTracker(osgi, LogReaderService.class);
    assertEquals(1, logs.getTrackingCount());
    assertEquals(1, logReaders.getTrackingCount());

    OsgiLogService service = new OsgiLogService();
    service.start(osgi.bundleContext());

    assertEquals(2, logs.getTrackingCount());
    assertEquals(2, logReaders.getTrackingCount());
    osgi.stop();
}
 
Example #11
Source File: ConsoleTcp.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Utility method used for logging.
 *
 * @param level
 *          Log level
 * @param msg
 *          Log message
 */
void log(int level, String msg)
{
  try {
    final ServiceReference<LogService> srLog =
      bc.getServiceReference(LogService.class);
    if (srLog != null) {
      final LogService sLog = bc.getService(srLog);
      if (sLog != null) {
        sLog.log(level, msg);
      }
      bc.ungetService(srLog);
    }
  } catch (final IllegalStateException exp) {
    // if the thread has survied the stop of the bundle we get this
    // which we unfortunately has to ignore
  }
}
 
Example #12
Source File: BundleImpl.java    From concierge with Eclipse Public License 1.0 6 votes vote down vote up
public long retrieveFileLength(final String classpath,
		final String filename) throws IOException {
	final Object res = findFile(classpath, filename,
			GET_CONTENT_LENGTH);
	if (res == null) {
		if (framework.DEBUG_CLASSLOADING) {
			framework.logger.log(LogService.LOG_DEBUG,
					"Could not retrieveFileLength for filename="
							+ filename + " from bundle="
							+ this.toString());
		}
		return -1;
	} else {
		return (Long) res;
	}
}
 
Example #13
Source File: LogFrameworkListener.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * The service event callback method inserts all service events into the
 * log.
 * 
 * Event of types REGISTERED, UNREGISTERED are assigned the log
 * level info.
 * 
 * Events of type MODIFIED are assigned the log level DEBUG.
 * 
 * @param se
 *            the service event that has occurred.
 */
public void serviceChanged(ServiceEvent se) {
    ServiceReference<?> sr = se.getServiceReference();
    Bundle bundle = sr.getBundle();
    String msg = null;
    int level = LogService.LOG_INFO;
    switch (se.getType()) {
    case ServiceEvent.REGISTERED:
        msg = "ServiceEvent REGISTERED";
        break;
    case ServiceEvent.UNREGISTERING:
        msg = "ServiceEvent UNREGISTERING";
        break;
    case ServiceEvent.MODIFIED:
        msg = "ServiceEvent MODIFIED";
        level = LogService.LOG_DEBUG;
        break;
    }
    lrsf.log(new LogEntryImpl(bundle, sr, level, msg));
}
 
Example #14
Source File: BrowserWatchFilter.java    From wisdom with Apache License 2.0 6 votes vote down vote up
public Result call(Route route, RequestContext context) throws Exception {
	Result returned = context.proceed();
	// Only augment elements in DEV mode
	if (configuration.isDev()) {
		// Only "augment" HTML results
		Renderable<?> renderable = returned.getRenderable();
		if(renderable!=null) {
			if ("text/html".equals(renderable.mimetype())) {
				if (renderable instanceof RenderableString) {
					RenderableString text = (RenderableString) renderable;
					log.log(LogService.LOG_INFO, String.format("Intercepting query %s", context.context().path()));
					// Now add the magic javascript !
					returned.html().render(addJavascript(text.content()));
				}
			}
		}
	}
	return returned;
}
 
Example #15
Source File: LogUtil.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Converts a string representing a log severity level to an int.
 *
 * @param level
 *            The string to convert.
 * @param def
 *            Default value to use if the string is not recognized
 *            as a log level.
 * @return the log level, or the default value if the string can not
 *         be recognized.
 */
static public int toLevel(String level, int def) {
  if (level==null) {
    return def;
  }

  level = level.trim();
  if (level.equalsIgnoreCase("INFO")) {
    return LogService.LOG_INFO;
  } else if (level.equalsIgnoreCase("DEBUG")) {
    return LogService.LOG_DEBUG;
  } else if (level.equalsIgnoreCase("WARNING")) {
    return LogService.LOG_WARNING;
  } else if (level.equalsIgnoreCase("ERROR")) {
    return LogService.LOG_ERROR;
  } else if (level.equalsIgnoreCase("DEFAULT")) {
    return 0;
  }
  return def;
}
 
Example #16
Source File: LoggingApplication.java    From osgi.enroute.examples with Apache License 2.0 6 votes vote down vote up
@Reference
void setLogReader(LogReaderService reader) {
	reader.addLogListener(e -> {
		switch (e.getLevel()) {
		case LogService.LOG_DEBUG:
			System.out.println("DEBUG:::: " + e.getMessage());
			break;

		case LogService.LOG_INFO:
			System.out.println("INFO::::: " + e.getMessage());
			break;
		case LogService.LOG_WARNING:
			System.out.println("WARNING:: " + e.getMessage());
			break;
		case LogService.LOG_ERROR:
			System.out.println("ERROR:::: " + e.getMessage());
			break;
		}
	});
}
 
Example #17
Source File: OsgiSl4fjLoggingAdapter.java    From elexis-3-core with Eclipse Public License 1.0 6 votes vote down vote up
@Override
public void logged(LogEntry entry){
	
	Bundle bundle = entry.getBundle();
	String message =
		(bundle != null) ? "[" + bundle.getSymbolicName() + "] " + entry.getMessage()
				: entry.getMessage();
	Throwable exception = entry.getException();
	
	switch (entry.getLevel()) {
	case LogService.LOG_ERROR:
		logger.error(message, exception);
		break;
	case LogService.LOG_WARNING:
		logger.warn(message, exception);
		break;
	case LogService.LOG_INFO:
		logger.info(message, exception);
		break;
	default:
		logger.debug(message, exception);
		break;
	}
	
}
 
Example #18
Source File: CommandTty.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
public void start(BundleContext bc) throws Exception {
  this.bc = bc;

  log(LogService.LOG_INFO, "Starting");

  // Get config
  Dictionary<String,String> p = new Hashtable<String,String>();
  p.put(Constants.SERVICE_PID, getClass().getName());
  bc.registerService(ManagedService.class, this, p);

  inStream  = new SystemIn(bc);
  outStream = System.out;
  errStream = System.err;

  cmdProcTracker = new ServiceTracker(bc, CommandProcessor.class, this);
  cmdProcTracker.open();

  logTracker = new ServiceTracker(bc, LogService.class, null);
  logTracker.open();

}
 
Example #19
Source File: ConsoleTty.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public void log(int level, String msg, Exception e) {
    ServiceReference srLog = bc.getServiceReference(logServiceName);
    if (srLog != null) {
        LogService sLog = (LogService) bc.getService(srLog);
        if (sLog != null) {
            if (e == null) {
                sLog.log(level, msg);
            } else {
                sLog.log(level, msg, e);
            }
        }
        bc.ungetService(srLog);
    }
}
 
Example #20
Source File: Activator.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Called by the framework when this bundle is started.
 *
 * @param bc Bundle context.
 */
public void start(BundleContext bc) {
    this.bc = bc;

    log(LogService.LOG_INFO, "Starting version "
            + bc.getBundle().getHeaders().get("Bundle-Version"));

    consoleReg = bc.registerService(ConsoleService.class,
            new ConsoleServiceImpl(bc), null);
}
 
Example #21
Source File: BrowserWatchController.java    From wisdom with Apache License 2.0 5 votes vote down vote up
public void serviceChanged(ServiceEvent event) {
	Bundle bundle = event.getServiceReference().getBundle();
	switch(event.getType()) {
	case ServiceEvent.REGISTERED:
		Object service = context.getService(event.getServiceReference());
		if(service instanceof  org.wisdom.api.Controller) {
			String serviceClassName = service.getClass().getName();
			triggerReloadFor(serviceClassName);
			lazyGetInfos(bundle).addController(serviceClassName);
			log.log(LogService.LOG_INFO, String.format("Controller %s is registered !", serviceClassName));
		}
	}
}
 
Example #22
Source File: ConsoleSwing.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public void removedService(ServiceReference<ConsoleService> reference,
                           ConsoleService service) {
  if (consoleService == service) {
    if (null!=consoleSession) {
      log(LogService.LOG_INFO, "Console service closed.");
      closeSession();
      swingIO.disableInput("No Console Service Available.");
    }
    consoleService = null;
  }
}
 
Example #23
Source File: LogUtil.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Converts from a numeric log severity level to a left justified
 * string of at least the given length.
 *
 * @param level
 *            is the log severity level.
 * @param length
 *            the minimum length of the resulting string.
 * @return left justified string representation of a numeric log level.
 */
static public String fromLevel(int level, int length) {
  final StringBuffer sb = new StringBuffer(length > 7 ? length : 7);
  switch (level) {
  case LogService.LOG_INFO:
    sb.append("info");
    break;
  case LogService.LOG_DEBUG:
    sb.append("debug");
    break;
  case LogService.LOG_WARNING:
    sb.append("Warning");
    break;
  case LogService.LOG_ERROR:
    sb.append("ERROR");
    break;
  case 0:
    sb.append("DEFAULT");
    break;
  default:
    sb.append("[");
    sb.append(level);
    sb.append("]");
  }
  for (int i = sb.length(); i < length; i++) {
    sb.append(" ");
  }
  return sb.toString();
}
 
Example #24
Source File: Activator.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private void error(String msg)
{
  try {
    log.log(LogService.LOG_ERROR, msg);
  } catch (final Exception e) {
    System.err.println("[Device Manager] ERROR: " + msg);
  }
}
 
Example #25
Source File: Activator.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private void info(String msg)
{
  try {
    log.log(LogService.LOG_INFO, msg);
  } catch (final Exception e) {
    System.err.println("[Device Manager] info: " + msg);
  }
}
 
Example #26
Source File: ConsoleSwing.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public static void log(int level, String msg, Exception e) {
  ServiceReference<LogService> srLog = bc.getServiceReference(LogService.class);
  if (srLog != null) {
    LogService sLog = bc.getService(srLog);
    if (sLog != null) {
      if(e != null) {
        sLog.log(level, msg, e);
      } else {
        sLog.log(level, msg);
      }
    }
    bc.ungetService(srLog);
  }
}
 
Example #27
Source File: BrowserWatchController.java    From wisdom with Apache License 2.0 5 votes vote down vote up
private org.wisdom.api.router.Route getRoute(String url) {
	for(HttpMethod m : HttpMethod.values()) {
		org.wisdom.api.router.Route r = router.getRouteFor(m, url);
		if(!r.isUnbound()) {
			log.log(LogService.LOG_INFO, String.format("Url %s is mapped to route %s", url, r));
			return r;
		}
	}
	return null;
}
 
Example #28
Source File: UserService.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public void logout() {
  final File f = new File(fileName);

  AccessController.doPrivileged(new PrivilegedAction() {
    public Object run() {
      if (!f.exists()) {
        throw new IllegalStateException("No user logged in");
      }
  
      f.delete();
      log(LogService.LOG_INFO, "User logged out");
      return null;
    }
  });
}
 
Example #29
Source File: Activator.java    From neoscada with Eclipse Public License 1.0 5 votes vote down vote up
protected void log ( final int level, final String message )
{
    final BundleContext context = this.context;
    if ( context == null )
    {
        return;
    }

    final ServiceReference<LogService> ref = context.getServiceReference ( LogService.class );
    if ( ref == null )
    {
        return;
    }

    final LogService service = context.getService ( ref );
    if ( service == null )
    {
        return;
    }

    try
    {
        service.log ( level, message );
    }
    finally
    {
        context.ungetService ( ref );
    }
}
 
Example #30
Source File: ConsoleSwing.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public void start() {

    log(LogService.LOG_INFO, "Starting");

    swingIO = new SwingIO();
    swingIO.start();
    swingIO.disableInput("No Console Service Available.");
    consoleTracker.open();
  }