001: /*******************************************************************************
002: * Copyright (c) 2004, 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.ui.internal.intro.universal.util;
011:
012: import org.eclipse.core.runtime.ILog;
013: import org.eclipse.core.runtime.IStatus;
014: import org.eclipse.core.runtime.MultiStatus;
015: import org.eclipse.core.runtime.Platform;
016: import org.eclipse.core.runtime.Status;
017: import org.eclipse.ui.internal.intro.universal.IUniversalIntroConstants;
018: import org.eclipse.ui.internal.intro.universal.UniversalIntroPlugin;
019:
020: /**
021: * Utility class for logging, based on Platform logging classes. The log
022: * listerner used is the base one supplied by the platform. Error messages are
023: * always logged. Warning messages are only logged when the plugin is in debug
024: * mode. Info messages are only logged when the /trace/logInfo debug option is
025: * set to true. Performance reports are only logged when /trace/performance is
026: * set to true.
027: *
028: */
029: public class Log implements IUniversalIntroConstants {
030:
031: /**
032: * This MUST be set to <b>false </b> in production. <br>
033: * Used to compile out developement debug messages. <br>
034: * Compiler compiles out code warpped wit this flag as an optimization.
035: */
036: public static final boolean DEBUG = false;
037:
038: // Use these flags to filter out code that may be a performance hit.
039: // Flag that controls logging of warning message
040: public static boolean logWarning = false;
041: // Flag that controls logging of information messages
042: public static boolean logInfo = false;
043: // Flag that controls logging of performance messages
044: public static boolean logPerformance = false;
045:
046: private final static ILog pluginLog = UniversalIntroPlugin
047: .getDefault().getLog();
048:
049: static {
050: // init debug options based on settings defined in ".options" file. If
051: // the plugin is not in debug mode, no point setting debug options.
052: if (UniversalIntroPlugin.getDefault().isDebugging()) {
053: logWarning = true;
054: logInfo = getDebugOption("/trace/logInfo"); //$NON-NLS-1$
055: logPerformance = getDebugOption("/trace/logPerformance"); //$NON-NLS-1$
056: }
057:
058: }
059:
060: private static boolean getDebugOption(String option) {
061: return "true".equalsIgnoreCase(//$NON-NLS-1$
062: Platform.getDebugOption(PLUGIN_ID + option));
063: }
064:
065: /**
066: * Log an Error message with an exception. Note that the message should
067: * already be localized to proper local. Errors are always logged.
068: */
069: public static synchronized void error(String message, Throwable ex) {
070: if (message == null)
071: message = ""; //$NON-NLS-1$
072: Status errorStatus = new Status(IStatus.ERROR, PLUGIN_ID,
073: IStatus.OK, message, ex);
074: pluginLog.log(errorStatus);
075: }
076:
077: /**
078: * Log an Information message. Note that the message should already be
079: * localized to proper local. Info messages are only logged when the
080: * /trace/logInfo debug option is true.
081: */
082: public static synchronized void info(String message) {
083: if (!logInfo)
084: // logging of info messages is not enabled.
085: return;
086:
087: if (message == null)
088: message = ""; //$NON-NLS-1$
089: Status infoStatus = new Status(IStatus.INFO, PLUGIN_ID,
090: IStatus.OK, message, null);
091: pluginLog.log(infoStatus);
092: }
093:
094: /**
095: * Log an Information message. Note that the message should already be
096: * localized to proper local. These messages are always logged. They are not
097: * controlled by any debug flags. Logging of these messages can be
098: * controlled by the public flags in this class.
099: */
100: public static synchronized void forcedInfo(String message) {
101: if (message == null)
102: message = ""; //$NON-NLS-1$
103: Status infoStatus = new Status(IStatus.INFO, PLUGIN_ID,
104: IStatus.OK, message, null);
105: pluginLog.log(infoStatus);
106: }
107:
108: /**
109: * Log a Warning message. Note that the message should already be localized
110: * to proper local. Warning messages are only logged when the plugin is in
111: * debug mode.
112: */
113: public static synchronized void warning(String message) {
114: if (!logWarning)
115: // no warning messages (ie: plugin is not in debug mode). Default is
116: // to not log warning messages.
117: return;
118:
119: if (message == null)
120: message = ""; //$NON-NLS-1$
121: Status warningStatus = new Status(IStatus.WARNING, PLUGIN_ID,
122: IStatus.OK, message, null);
123: pluginLog.log(warningStatus);
124: }
125:
126: /**
127: * Log a development debug message. Debug messages are compiled out.
128: */
129: public static synchronized void debugMessage(String className,
130: String message) {
131: if (DEBUG) {
132: MultiStatus debugStatus = new MultiStatus(PLUGIN_ID,
133: IStatus.OK, className, null);
134: Status infoStatus = new Status(IStatus.OK, PLUGIN_ID,
135: IStatus.OK, message, null);
136: debugStatus.add(infoStatus);
137: pluginLog.log(debugStatus);
138: }
139: }
140: }
|