Skip to content

Commit

Permalink
buglabs-osgi: code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
kgilmer committed Jul 28, 2011
1 parent c533cf3 commit 07befda
Showing 1 changed file with 19 additions and 12 deletions.
31 changes: 19 additions & 12 deletions com.buglabs.common/com/buglabs/util/osgi/LogServiceUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,13 @@
* @author kgilmer
*
*/
public class LogServiceUtil {
public final class LogServiceUtil {

/**
* Utility class.
*/
private LogServiceUtil() {
}
/**
* @param context
* if context is null, it will return a log service that uses
Expand Down Expand Up @@ -80,7 +85,8 @@ public void log(int level, String message, Throwable exception) {
public void log(ServiceReference sr, int level, String message) {
System.out.println(levelString(level) + "Service Reference: " + sr.toString() + " " + message);
if (level == LogService.LOG_ERROR) {
System.err.println(levelString(level) + "Service Reference: " + sr.toString() + " " + message);
System.err.println(levelString(level) + "Service Reference: "
+ sr.toString() + " " + message);
}
}

Expand Down Expand Up @@ -123,39 +129,40 @@ private String levelString(int level) {
/**
* Log a bundle exception and print nested exception if it exists.
*
* @param logService
* @param message
* @param exception
* @param logService LogService
* @param message message
* @param exception Exception to log
*/
public static void logBundleException(LogService logService, String message, BundleException exception) {
// Add error handling to be specific about what exactly happened.
logService.log(LogService.LOG_ERROR, message + ": " + exception.getMessage() + "\n" + stackTraceToString(exception));
stackTraceToString(exception);

if (exception.getNestedException() != null) {
logService.log(LogService.LOG_ERROR, "Nested Exception: " + exception.getNestedException().getMessage() + "\n" + stackTraceToString(exception.getNestedException()));
logService.log(LogService.LOG_ERROR, "Nested Exception: " + exception.getNestedException().getMessage()
+ "\n" + stackTraceToString(exception.getNestedException()));
}
}

/**
* Log an exception and print nested exception if it exists.
*
* @param logService
* @param message
* @param exception
* @param logService LogService
* @param message message
* @param exception Exception to log
*/
public static void logBundleException(LogService logService, String message, Exception exception) {
// Add error handling to be specific about what exactly happened.
logService.log(LogService.LOG_ERROR, message + ": " + exception.getMessage() + "\n" + stackTraceToString(exception));
}

/**
* @param t
* @param throwable Throwable
* @return A stack trace as a string.
*/
private static String stackTraceToString(Throwable t) {
private static String stackTraceToString(Throwable throwable) {
StringWriter sw = new StringWriter();
t.printStackTrace(new PrintWriter(sw));
throwable.printStackTrace(new PrintWriter(sw));
return sw.getBuffer().toString();
}
}

0 comments on commit 07befda

Please sign in to comment.