david_williams | 425ffe7 | 2004-12-07 21:46:39 +0000 | [diff] [blame] | 1 | /******************************************************************************* |
| 2 | * Copyright (c) 2004 IBM Corporation and others. |
| 3 | * All rights reserved. This program and the accompanying materials |
| 4 | * are made available under the terms of the Eclipse Public License v1.0 |
| 5 | * which accompanies this distribution, and is available at |
| 6 | * http://www.eclipse.org/legal/epl-v10.html |
| 7 | * |
| 8 | * Contributors: |
| 9 | * IBM Corporation - initial API and implementation |
| 10 | *******************************************************************************/ |
| 11 | package org.eclipse.jst.jsp.core.internal; |
| 12 | |
amywu | fecd7a9 | 2006-05-10 07:22:58 +0000 | [diff] [blame] | 13 | import com.ibm.icu.util.StringTokenizer; |
david_williams | 425ffe7 | 2004-12-07 21:46:39 +0000 | [diff] [blame] | 14 | |
| 15 | import org.eclipse.core.runtime.IStatus; |
| 16 | import org.eclipse.core.runtime.Platform; |
| 17 | import org.eclipse.core.runtime.Status; |
| 18 | import org.osgi.framework.Bundle; |
| 19 | |
| 20 | /** |
| 21 | * Small convenience class to log messages to plugin's log file and also, if desired, |
| 22 | * the console. This class should only be used by classes in this plugin. Other |
| 23 | * plugins should make their own copy, with appropriate ID. |
| 24 | */ |
| 25 | public class Logger { |
| 26 | private static final String PLUGIN_ID = "org.eclipse.jst.jsp.core"; //$NON-NLS-1$ |
| 27 | |
| 28 | private static final String TRACEFILTER_LOCATION = "/debug/tracefilter"; //$NON-NLS-1$ |
| 29 | |
| 30 | public static final int OK = IStatus.OK; // 0 |
| 31 | public static final int INFO = IStatus.INFO; // 1 |
| 32 | public static final int WARNING = IStatus.WARNING; // 2 |
| 33 | public static final int ERROR = IStatus.ERROR; // 4 |
| 34 | |
| 35 | public static final int OK_DEBUG = 200 + OK; |
| 36 | public static final int INFO_DEBUG = 200 + INFO; |
| 37 | public static final int WARNING_DEBUG = 200 + WARNING; |
| 38 | public static final int ERROR_DEBUG = 200 + ERROR; |
| 39 | |
| 40 | /** |
| 41 | * Adds message to log. |
| 42 | * @param level severity level of the message (OK, INFO, WARNING, ERROR, OK_DEBUG, INFO_DEBUG, WARNING_DEBUG, ERROR_DEBUG) |
| 43 | * @param message text to add to the log |
| 44 | * @param exception exception thrown |
| 45 | */ |
| 46 | protected static void _log(int level, String message, Throwable exception) { |
| 47 | if (level == OK_DEBUG || level == INFO_DEBUG || level == WARNING_DEBUG || level == ERROR_DEBUG) { |
| 48 | if (!isDebugging()) |
| 49 | return; |
| 50 | } |
| 51 | |
| 52 | int severity = IStatus.OK; |
| 53 | switch (level) { |
| 54 | case INFO_DEBUG : |
| 55 | case INFO : |
| 56 | severity = IStatus.INFO; |
| 57 | break; |
| 58 | case WARNING_DEBUG : |
| 59 | case WARNING : |
| 60 | severity = IStatus.WARNING; |
| 61 | break; |
| 62 | case ERROR_DEBUG : |
| 63 | case ERROR : |
| 64 | severity = IStatus.ERROR; |
| 65 | } |
| 66 | message = (message != null) ? message : "null"; //$NON-NLS-1$ |
| 67 | Status statusObj = new Status(severity, PLUGIN_ID, severity, message, exception); |
| 68 | Bundle bundle = Platform.getBundle(PLUGIN_ID); |
| 69 | if (bundle != null) |
| 70 | Platform.getLog(bundle).log(statusObj); |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * Prints message to log if category matches /debug/tracefilter option. |
| 75 | * @param message text to print |
| 76 | * @param category category of the message, to be compared with /debug/tracefilter |
| 77 | */ |
| 78 | protected static void _trace(String category, String message, Throwable exception) { |
| 79 | if (isTracing(category)) { |
| 80 | message = (message != null) ? message : "null"; //$NON-NLS-1$ |
| 81 | Status statusObj = new Status(IStatus.OK, PLUGIN_ID, IStatus.OK, message, exception); |
| 82 | Bundle bundle = Platform.getBundle(PLUGIN_ID); |
| 83 | if (bundle != null) |
| 84 | Platform.getLog(bundle).log(statusObj); |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * @return true if the platform is debugging |
| 90 | */ |
| 91 | public static boolean isDebugging() { |
| 92 | return Platform.inDebugMode(); |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Determines if currently tracing a category |
| 97 | * @param category |
| 98 | * @return true if tracing category, false otherwise |
| 99 | */ |
| 100 | public static boolean isTracing(String category) { |
| 101 | if (!isDebugging()) |
| 102 | return false; |
| 103 | |
| 104 | String traceFilter = Platform.getDebugOption(PLUGIN_ID + TRACEFILTER_LOCATION); |
| 105 | if (traceFilter != null) { |
| 106 | StringTokenizer tokenizer = new StringTokenizer(traceFilter, ","); //$NON-NLS-1$ |
| 107 | while (tokenizer.hasMoreTokens()) { |
| 108 | String cat = tokenizer.nextToken().trim(); |
| 109 | if (category.equals(cat)) { |
| 110 | return true; |
| 111 | } |
| 112 | } |
| 113 | } |
| 114 | return false; |
| 115 | } |
| 116 | |
| 117 | public static void log(int level, String message) { |
| 118 | _log(level, message, null); |
| 119 | } |
| 120 | |
| 121 | public static void log(int level, String message, Throwable exception) { |
| 122 | _log(level, message, exception); |
| 123 | } |
| 124 | |
| 125 | public static void logException(String message, Throwable exception) { |
| 126 | _log(ERROR, message, exception); |
| 127 | } |
| 128 | |
| 129 | public static void logException(Throwable exception) { |
| 130 | _log(ERROR, exception.getMessage(), exception); |
| 131 | } |
| 132 | |
| 133 | public static void traceException(String category, String message, Throwable exception) { |
| 134 | _trace(category, message, exception); |
| 135 | } |
| 136 | |
| 137 | public static void traceException(String category, Throwable exception) { |
| 138 | _trace(category, exception.getMessage(), exception); |
| 139 | } |
| 140 | |
| 141 | public static void trace(String category, String message) { |
| 142 | _trace(category, message, null); |
| 143 | } |
| 144 | } |