001: /*
002: * ====================================================================
003: * Copyright (c) 2004-2008 TMate Software Ltd. All rights reserved.
004: *
005: * This software is licensed as described in the file COPYING, which
006: * you should have received as part of this distribution. The terms
007: * are also available at http://svnkit.com/license.html
008: * If newer versions of this license are posted there, you may use a
009: * newer version instead, at your option.
010: * ====================================================================
011: */
012: package org.tmatesoft.svn.core;
013:
014: import java.io.InputStream;
015: import java.io.OutputStream;
016: import java.io.UnsupportedEncodingException;
017:
018: import org.eclipse.core.runtime.ILog;
019: import org.eclipse.core.runtime.IStatus;
020: import org.eclipse.core.runtime.Platform;
021: import org.eclipse.core.runtime.Status;
022: import org.osgi.framework.Bundle;
023: import org.tmatesoft.svn.util.SVNDebugLogAdapter;
024:
025: /**
026: * @version 1.1.1
027: * @author TMate Software Ltd.
028: */
029: public class SVNKitLog extends SVNDebugLogAdapter {
030:
031: private static final String DEBUG_FINE = "/debug/fine";
032: private static final String DEBUG_INFO = "/debug/info";
033: private static final String DEBUG_ERROR = "/debug/error";
034:
035: private boolean myIsFineEnabled;
036: private boolean myIsInfoEnabled;
037: private boolean myIsErrorEnabled;
038:
039: private ILog myLog;
040: private String myPluginID;
041:
042: public SVNKitLog(Bundle bundle, boolean enabled) {
043: myLog = Platform.getLog(bundle);
044: myPluginID = bundle.getSymbolicName();
045:
046: // enabled even when not in debug mode
047: myIsErrorEnabled = Boolean.TRUE.toString().equals(
048: Platform.getDebugOption(myPluginID + DEBUG_ERROR));
049:
050: // debug mode have to be enabled
051: myIsFineEnabled = enabled
052: && Boolean.TRUE.toString()
053: .equals(
054: Platform.getDebugOption(myPluginID
055: + DEBUG_FINE));
056: myIsInfoEnabled = enabled
057: && Boolean.TRUE.toString()
058: .equals(
059: Platform.getDebugOption(myPluginID
060: + DEBUG_INFO));
061: }
062:
063: public boolean isFineEnabled() {
064: return myIsFineEnabled;
065: }
066:
067: public boolean isInfoEnabled() {
068: return myIsInfoEnabled;
069: }
070:
071: public boolean isErrorEnabled() {
072: return myIsErrorEnabled;
073: }
074:
075: private Status createStatus(int severity, String message,
076: Throwable th) {
077: return new Status(severity, myPluginID, IStatus.OK,
078: message == null ? "" : message, th);
079: }
080:
081: public void info(String message) {
082: if (isInfoEnabled()) {
083: myLog.log(createStatus(IStatus.INFO, message, null));
084: }
085: }
086:
087: public void info(Throwable th) {
088: if (isInfoEnabled()) {
089: myLog.log(createStatus(IStatus.INFO, th != null ? th
090: .getMessage() : "", th));
091: }
092: }
093:
094: public void error(String message) {
095: if (isErrorEnabled()) {
096: myLog.log(createStatus(IStatus.ERROR, message, null));
097: }
098: }
099:
100: public void error(Throwable th) {
101: if (isErrorEnabled()) {
102: myLog.log(createStatus(IStatus.ERROR, th != null ? th
103: .getMessage() : "", th));
104: }
105: }
106:
107: public void log(String message, byte[] data) {
108: if (isFineEnabled()) {
109: try {
110: myLog.log(createStatus(IStatus.INFO, message + " : "
111: + new String(data, "UTF-8"), null));
112: } catch (UnsupportedEncodingException e) {
113: myLog.log(createStatus(IStatus.INFO, message + " : "
114: + new String(data), null));
115: }
116: }
117: }
118:
119: public InputStream createLogStream(InputStream is) {
120: if (isFineEnabled()) {
121: return super .createLogStream(is);
122: }
123: return is;
124: }
125:
126: public OutputStream createLogStream(OutputStream os) {
127: if (isFineEnabled()) {
128: return super.createLogStream(os);
129: }
130: return os;
131: }
132: }
|