trace/traceviewer/com.nokia.traceviewer/src/com/nokia/traceviewer/engine/TraceViewerGlobals.java
changeset 11 5b9d4d8641ce
equal deleted inserted replaced
10:ed1c9f64298a 11:5b9d4d8641ce
       
     1 /*
       
     2  * Copyright (c) 2007-2010 Nokia Corporation and/or its subsidiary(-ies). 
       
     3  * All rights reserved.
       
     4  * This component and the accompanying materials are made available
       
     5  * under the terms of "Eclipse Public License v1.0"
       
     6  * which accompanies this distribution, and is available
       
     7  * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8  *
       
     9  * Initial Contributors:
       
    10  * Nokia Corporation - initial contribution.
       
    11  *
       
    12  * Contributors:
       
    13  *
       
    14  * Description:
       
    15  *
       
    16  * Access point to the exported interfaces of Trace Viewer engine
       
    17  *
       
    18  */
       
    19 package com.nokia.traceviewer.engine;
       
    20 
       
    21 import java.util.ArrayList;
       
    22 import java.util.List;
       
    23 
       
    24 import org.eclipse.ui.console.ConsolePlugin;
       
    25 import org.eclipse.ui.console.IConsole;
       
    26 import org.eclipse.ui.console.IConsoleManager;
       
    27 import org.eclipse.ui.console.MessageConsole;
       
    28 import org.eclipse.ui.console.MessageConsoleStream;
       
    29 
       
    30 import com.nokia.trace.eventrouter.PropertySource;
       
    31 import com.nokia.trace.eventrouter.TraceEvent;
       
    32 import com.nokia.trace.eventrouter.TraceEventRouter;
       
    33 import com.nokia.traceviewer.TraceViewerPlugin;
       
    34 
       
    35 /**
       
    36  * Access point to the exported interfaces of Trace Viewer engine
       
    37  * 
       
    38  */
       
    39 public final class TraceViewerGlobals {
       
    40 
       
    41 	/**
       
    42 	 * Action name for UI Event
       
    43 	 */
       
    44 	private static final String UI_EVENT_ACTION_NAME = "uievent"; //$NON-NLS-1$
       
    45 
       
    46 	/**
       
    47 	 * ID for statisticslogger plugin
       
    48 	 */
       
    49 	private static final String STATISTICSLOGGER_ID = "com.nokia.trace.statisticslogger"; //$NON-NLS-1$
       
    50 
       
    51 	/**
       
    52 	 * Number of lines in one block.
       
    53 	 */
       
    54 	public static final int blockSize = 200;
       
    55 
       
    56 	/**
       
    57 	 * If this value is true, debug prints are printed to console
       
    58 	 */
       
    59 	private static final boolean debugPrintsEnabled = false;
       
    60 
       
    61 	/**
       
    62 	 * Debug print level. None to get nothing
       
    63 	 */
       
    64 	private static final DebugLevel debugPrintLevel = DebugLevel.NONE;
       
    65 
       
    66 	/**
       
    67 	 * Trace viewer instance
       
    68 	 */
       
    69 	private static TraceViewer instance;
       
    70 
       
    71 	/**
       
    72 	 * TrimProvider
       
    73 	 */
       
    74 	private static TrimProvider trimProvider = new DummyTrim();
       
    75 
       
    76 	/**
       
    77 	 * List of TraceProviders
       
    78 	 */
       
    79 	private static List<TraceProvider> traceProviders = new ArrayList<TraceProvider>();
       
    80 
       
    81 	/**
       
    82 	 * Debug levels
       
    83 	 * 
       
    84 	 */
       
    85 	public enum DebugLevel {
       
    86 
       
    87 		/**
       
    88 		 * No prints
       
    89 		 */
       
    90 		NONE,
       
    91 
       
    92 		/**
       
    93 		 * Simple prints
       
    94 		 */
       
    95 		SIMPLE,
       
    96 
       
    97 		/**
       
    98 		 * Flow traces
       
    99 		 */
       
   100 		FLOW,
       
   101 
       
   102 		/**
       
   103 		 * Test traces
       
   104 		 */
       
   105 		TEST,
       
   106 
       
   107 		/**
       
   108 		 * All traces
       
   109 		 */
       
   110 		ALL
       
   111 	}
       
   112 
       
   113 	/**
       
   114 	 * Constructor is hidden
       
   115 	 */
       
   116 	private TraceViewerGlobals() {
       
   117 	}
       
   118 
       
   119 	/**
       
   120 	 * Starts Trace Viewer engine.
       
   121 	 */
       
   122 	public static void start() {
       
   123 		if (instance == null) {
       
   124 			instance = new TraceViewer();
       
   125 			instance.start();
       
   126 		}
       
   127 	}
       
   128 
       
   129 	/**
       
   130 	 * Gets the trace viewer interface
       
   131 	 * 
       
   132 	 * @return trace viewer
       
   133 	 */
       
   134 	public static TraceViewerInterface getTraceViewer() {
       
   135 		return instance.getTraceViewer();
       
   136 	}
       
   137 
       
   138 	/**
       
   139 	 * Called by the provider plug-in to set the provider
       
   140 	 * 
       
   141 	 * @param provider
       
   142 	 *            the trace provider
       
   143 	 * @param clearDataBefore
       
   144 	 *            if true, clear data file before changing the new provider
       
   145 	 */
       
   146 	public static void setTraceProvider(TraceProvider provider,
       
   147 			boolean clearDataBefore) {
       
   148 		// Add to list if doesn't exist yet
       
   149 		if (!traceProviders.contains(provider)) {
       
   150 
       
   151 			// Sort providers to alphabetic order. This is assuming there are
       
   152 			// only two providers.
       
   153 			if (!traceProviders.isEmpty()) {
       
   154 				TraceProvider prevProvider = traceProviders.get(0);
       
   155 				if (prevProvider.getName().compareTo(provider.getName()) < 0) {
       
   156 					traceProviders.add(provider);
       
   157 				} else {
       
   158 					traceProviders.add(0, provider);
       
   159 				}
       
   160 			} else {
       
   161 				traceProviders.add(provider);
       
   162 			}
       
   163 		}
       
   164 
       
   165 		// Tell the TraceViewer to set TraceProvider into use
       
   166 		instance.setTraceProvider(provider, clearDataBefore);
       
   167 	}
       
   168 
       
   169 	/**
       
   170 	 * Gets the trace provider
       
   171 	 * 
       
   172 	 * @return the trace provider
       
   173 	 */
       
   174 	public static TraceProvider getTraceProvider() {
       
   175 		return instance.getTraceProvider();
       
   176 	}
       
   177 
       
   178 	/**
       
   179 	 * Sets Trim provider
       
   180 	 * 
       
   181 	 * @param provider
       
   182 	 *            new trim provider
       
   183 	 */
       
   184 	public static void setTrimProvider(TrimProvider provider) {
       
   185 		trimProvider = provider;
       
   186 	}
       
   187 
       
   188 	/**
       
   189 	 * Gets the trim provider
       
   190 	 * 
       
   191 	 * @return the trimprovider
       
   192 	 */
       
   193 	public static TrimProvider getTrimProvider() {
       
   194 		return trimProvider;
       
   195 	}
       
   196 
       
   197 	/**
       
   198 	 * Gets list of available TraceProviders
       
   199 	 * 
       
   200 	 * @return the list of available TraceProviders
       
   201 	 */
       
   202 	public static List<TraceProvider> getListOfTraceProviders() {
       
   203 		return traceProviders;
       
   204 	}
       
   205 
       
   206 	/**
       
   207 	 * Called by the decoder plug-in to set the decoder
       
   208 	 * 
       
   209 	 * @param decoder
       
   210 	 *            the trace decoder
       
   211 	 */
       
   212 	public static void setDecodeProvider(DecodeProvider decoder) {
       
   213 		instance.setDecodeProvider(decoder);
       
   214 	}
       
   215 
       
   216 	/**
       
   217 	 * Gets the decode provider
       
   218 	 * 
       
   219 	 * @return the decode provider
       
   220 	 */
       
   221 	public static DecodeProvider getDecodeProvider() {
       
   222 		return instance.getDecodeProvider();
       
   223 	}
       
   224 
       
   225 	/**
       
   226 	 * Posts Error Event
       
   227 	 * 
       
   228 	 * @param description
       
   229 	 *            description of the event
       
   230 	 * @param category
       
   231 	 *            category of the event
       
   232 	 * @param source
       
   233 	 *            source of the event
       
   234 	 */
       
   235 	public static void postErrorEvent(String description, String category,
       
   236 			Object source) {
       
   237 		TraceEvent event = new TraceEvent(TraceEvent.ERROR, description);
       
   238 		event.setCategory(category);
       
   239 
       
   240 		// If source doesn't exist, insert current time
       
   241 		if (source == null) {
       
   242 			source = TraceViewerUtils.constructTimeString();
       
   243 		}
       
   244 		event.setSource(source);
       
   245 		TraceEventRouter.getInstance().postEvent(event);
       
   246 	}
       
   247 
       
   248 	/**
       
   249 	 * Posts Info Event
       
   250 	 * 
       
   251 	 * @param description
       
   252 	 *            description of the event
       
   253 	 * @param category
       
   254 	 *            category of the event
       
   255 	 * @param source
       
   256 	 *            source of the event
       
   257 	 */
       
   258 	public static void postInfoEvent(String description, String category,
       
   259 			Object source) {
       
   260 		TraceEvent event = new TraceEvent(TraceEvent.INFO, description);
       
   261 		event.setCategory(category);
       
   262 
       
   263 		// If source doesn't exist, insert current time
       
   264 		if (source == null) {
       
   265 			source = TraceViewerUtils.constructTimeString();
       
   266 		}
       
   267 		event.setSource(source);
       
   268 		TraceEventRouter.getInstance().postEvent(event);
       
   269 	}
       
   270 
       
   271 	/**
       
   272 	 * Posts UI Event
       
   273 	 * 
       
   274 	 * @param name
       
   275 	 *            name of the event
       
   276 	 * @param value
       
   277 	 *            value of the event
       
   278 	 */
       
   279 	public static void postUiEvent(String name, String value) {
       
   280 		TraceEvent event = new TraceEvent(TraceEvent.INFO, "UI Event"); //$NON-NLS-1$
       
   281 		PropertySource source = new PropertySource(UI_EVENT_ACTION_NAME,
       
   282 				STATISTICSLOGGER_ID, TraceViewerPlugin.PLUGIN_ID);
       
   283 		source.getProperties().put(name, value);
       
   284 		event.setSource(source);
       
   285 		TraceEventRouter.getInstance().postEvent(event);
       
   286 	}
       
   287 
       
   288 	/**
       
   289 	 * Print debug prints to console or do nothing if debug flag is not enabled
       
   290 	 * 
       
   291 	 * @param text
       
   292 	 *            text to print to console
       
   293 	 * @param level
       
   294 	 *            debug print level
       
   295 	 */
       
   296 	public static void debug(String text, DebugLevel level) {
       
   297 		if (debugPrintsEnabled) {
       
   298 			if (debugPrintLevel.compareTo(level) >= 0) {
       
   299 				MessageConsole myConsole = findConsole("TraceViewerDebug"); //$NON-NLS-1$
       
   300 				MessageConsoleStream out = myConsole.newMessageStream();
       
   301 				out.println(text);
       
   302 			}
       
   303 		}
       
   304 	}
       
   305 
       
   306 	/**
       
   307 	 * Find my console
       
   308 	 * 
       
   309 	 * @param name
       
   310 	 *            name for the console
       
   311 	 * @return my console
       
   312 	 */
       
   313 	private static MessageConsole findConsole(String name) {
       
   314 		ConsolePlugin plugin = ConsolePlugin.getDefault();
       
   315 		IConsoleManager conMan = plugin.getConsoleManager();
       
   316 		IConsole[] existing = conMan.getConsoles();
       
   317 		for (int i = 0; i < existing.length; i++)
       
   318 			if (name.equals(existing[i].getName()))
       
   319 				return (MessageConsole) existing[i];
       
   320 		// no console found, so create a new one
       
   321 		MessageConsole myConsole = new MessageConsole(name, null);
       
   322 		conMan.addConsoles(new IConsole[] { myConsole });
       
   323 		return myConsole;
       
   324 	}
       
   325 
       
   326 }