Javolution 6.0.0 java
LogServiceImpl.java
Go to the documentation of this file.
1 /*
2  * Javolution - Java(TM) Solution for Real-Time and Embedded Systems
3  * Copyright (C) 2012 - Javolution (http://javolution.org/)
4  * All rights reserved.
5  *
6  * Permission to use, copy, modify, and distribute this software is
7  * freely granted, provided that this notice is preserved.
8  */
9 package javolution.osgi.internal;
10 
12 
13 import org.osgi.framework.ServiceReference;
14 import org.osgi.service.log.LogService;
15 
20 public final class LogServiceImpl extends Thread implements LogService {
21 
22  private static class LogEvent {
23  Throwable exception;
24  int level;
25  String message;
26  }
27 
29 
30  public LogServiceImpl() {
31  super("Logging-Thread");
32  setDaemon(true);
33  this.start();
34  Thread hook = new Thread(new Runnable() {
35  @Override
36  public void run() { // Maintains the VM alive until the event queue is flushed
37  synchronized (eventQueue) {
38  try {
39  while (!eventQueue.isEmpty())
40  eventQueue.wait();
41  } catch (InterruptedException e) {}
42  }
43  }
44  });
45  Runtime.getRuntime().addShutdownHook(hook);
46  }
47 
48  @Override
49  public void log(int level, String message) {
50  log(level, message, null);
51  }
52 
53  @Override
54  public void log(int level, String message, Throwable exception) {
55  LogEvent event = new LogEvent();
56  event.level = level;
57  event.message = message;
58  event.exception = exception;
59  synchronized (eventQueue) {
60  eventQueue.addFirst(event);
61  eventQueue.notify();
62  }
63  }
64 
65  @SuppressWarnings("rawtypes")
66  @Override
67  public void log(ServiceReference sr, int level, String message) {
68  throw new UnsupportedOperationException();
69  }
70 
71  @SuppressWarnings("rawtypes")
72  @Override
73  public void log(ServiceReference sr, int level, String message,
74  Throwable exception) {
75  throw new UnsupportedOperationException();
76  }
77 
78  @Override
79  public void run() {
80  while (true) {
81  try {
82  LogEvent event;
83  synchronized (eventQueue) {
84  while (eventQueue.isEmpty())
85  eventQueue.wait();
86  event = eventQueue.pollLast();
87  eventQueue.notify();
88  }
89  switch (event.level) {
90  case LogService.LOG_DEBUG:
91  System.out.println("[DEBUG] " + event.message);
92  break;
93  case LogService.LOG_INFO:
94  System.out.println("[INFO] " + event.message);
95  break;
96  case LogService.LOG_WARNING:
97  System.out.println("[WARNING] " + event.message);
98  break;
99  case LogService.LOG_ERROR:
100  System.out.println("[ERROR] " + event.message);
101  break;
102  default:
103  System.out.println("[UNKNOWN] " + event.message);
104  break;
105  }
106  if (event.exception != null) {
107  event.exception.printStackTrace(System.out);
108  }
109  } catch (InterruptedException error) {
110  error.printStackTrace(System.err);
111  }
112  }
113  }
114 
115 }
javolution.osgi.internal.LogServiceImpl.LogEvent.exception
Throwable exception
Definition: LogServiceImpl.java:23
javolution
javolution.osgi.internal.LogServiceImpl.LogEvent.message
String message
Definition: LogServiceImpl.java:25
javolution.osgi.internal.LogServiceImpl.eventQueue
final FastTable< LogEvent > eventQueue
Definition: LogServiceImpl.java:28
javolution.osgi.internal.LogServiceImpl.log
void log(int level, String message, Throwable exception)
Definition: LogServiceImpl.java:54
javolution.osgi.internal.LogServiceImpl.LogEvent.level
int level
Definition: LogServiceImpl.java:24
javolution.osgi.internal.LogServiceImpl
Definition: LogServiceImpl.java:20
javolution.osgi.internal.LogServiceImpl.run
void run()
Definition: LogServiceImpl.java:79
javolution.osgi.internal.LogServiceImpl.LogServiceImpl
LogServiceImpl()
Definition: LogServiceImpl.java:30
javolution.osgi.internal.LogServiceImpl.log
void log(int level, String message)
Definition: LogServiceImpl.java:49
javolution.util.FastTable.service
final TableService< E > service
Definition: FastTable.java:95
javolution.osgi.internal.LogServiceImpl.LogEvent
Definition: LogServiceImpl.java:22
javolution.osgi.internal.LogServiceImpl.log
void log(ServiceReference sr, int level, String message, Throwable exception)
Definition: LogServiceImpl.java:73
Thread
javolution.util
Definition: FastBitSet.java:9
javolution.osgi.internal.LogServiceImpl.log
void log(ServiceReference sr, int level, String message)
Definition: LogServiceImpl.java:67
javolution.util.FastTable
Definition: FastTable.java:88