crashanalysis/crashanalyser/com.nokia.s60tools.crashanalyser/src/com/nokia/s60tools/crashanalyser/model/TraceListener.java
changeset 0 5ad7ad99af01
child 4 615035072f7e
equal deleted inserted replaced
-1:000000000000 0:5ad7ad99af01
       
     1 /*
       
     2 * Copyright (c) 2008 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 */
       
    17 
       
    18 package com.nokia.s60tools.crashanalyser.model;
       
    19 
       
    20 import java.io.*;
       
    21 import java.text.SimpleDateFormat;
       
    22 import java.util.Calendar;
       
    23 import org.eclipse.core.runtime.IConfigurationElement;
       
    24 import org.eclipse.core.runtime.IExtension;
       
    25 import org.eclipse.core.runtime.IExtensionPoint;
       
    26 import org.eclipse.core.runtime.IExtensionRegistry;
       
    27 import org.eclipse.core.runtime.Platform;
       
    28 import org.eclipse.swt.widgets.Display;
       
    29 import com.nokia.s60tools.crashanalyser.data.*;
       
    30 import com.nokia.s60tools.crashanalyser.interfaces.IErrorLibraryObserver;
       
    31 import com.nokia.s60tools.crashanalyser.files.*;
       
    32 import com.nokia.s60tools.crashanalyser.export.*;
       
    33 import com.nokia.s60tools.crashanalyser.plugin.*;
       
    34 
       
    35 /**
       
    36  * This class listens MobileCrash files via TraceViewer 
       
    37  *
       
    38  */
       
    39 public class TraceListener implements ITraceDataProcessor, 
       
    40 										IErrorLibraryObserver  {
       
    41 	private final static String MOBILE_CRASH_STARTTAG = "<MB_CR_START>"; //$NON-NLS-1$
       
    42 	private final static String MOBILE_CRASH_LINE_TAG = "<MB_CD>"; //$NON-NLS-1$
       
    43 	private final static String MOBILE_CRASH_STOPTAG = "<MB_CR_STOP>"; //$NON-NLS-1$
       
    44 	private final static String MOBILECRASH_START = "MobileCrash_"; //$NON-NLS-1$
       
    45 	final String EXTENSION_TRACE_PROVIDER = "traceprovider"; //$NON-NLS-1$
       
    46 
       
    47 
       
    48 	boolean listening = false;
       
    49 	boolean mobileCrashStarted = false;
       
    50 	BufferedWriter mcFile = null;
       
    51 	String dumpFolder = ""; //$NON-NLS-1$
       
    52 	File dumpFile;
       
    53 	ErrorLibrary errorLibrary = null;
       
    54 	boolean decode = false;
       
    55 	private static ITraceProvider traceProvider = null;
       
    56 	
       
    57 	/**
       
    58 	 * Constructor
       
    59 	 */
       
    60 	public TraceListener() {
       
    61 		readTraceProvider();
       
    62 	}
       
    63 	
       
    64 	/**
       
    65 	 * Starts trace listening asynchronously
       
    66 	 */
       
    67 	public void errorLibraryReady() {
       
    68 		Runnable refreshRunnable = new Runnable(){
       
    69 			public void run(){
       
    70 				startListening();
       
    71 			}
       
    72 		};
       
    73 		
       
    74 		Display.getDefault().asyncExec(refreshRunnable);        		
       
    75 	}
       
    76 	
       
    77 	/**
       
    78 	 * Activates MobileCrash file listening
       
    79 	 */
       
    80 	public void startListening() {
       
    81 		if (errorLibrary == null) {
       
    82 			errorLibrary = ErrorLibrary.getInstance(this);
       
    83 			return;
       
    84 		}
       
    85 		
       
    86 		if (listening || traceProvider == null)
       
    87 			return;
       
    88 
       
    89 		if (traceProvider.start(this))
       
    90 			listening = true;
       
    91 	}
       
    92 	
       
    93 	/**
       
    94 	 * Sets whether we should decode imported files or just import them as undecoded state.
       
    95 	 * @param decodeFiles 
       
    96 	 */
       
    97 	public void setDecode(boolean decodeFiles) {
       
    98 		decode = decodeFiles;
       
    99 	}
       
   100 	
       
   101 	/**
       
   102 	 * De-activates MobileCrash file listening
       
   103 	 */
       
   104 	public void stopListening() {
       
   105 		if (!listening)
       
   106 			return;
       
   107 		
       
   108 		if (traceProvider != null)
       
   109 			traceProvider.stop();
       
   110 		listening = false;
       
   111 	}
       
   112 
       
   113 	/**
       
   114 	 * All lines in trace data will be passed to this method. This method
       
   115 	 * pics up MobileCrash file content from trace data.
       
   116 	 * @param line trace data line
       
   117 	 */
       
   118 	public void processDataLine(String line) {
       
   119 		int idx = line.indexOf(MOBILE_CRASH_STARTTAG);
       
   120 		
       
   121 		try {
       
   122 			// Line contained <MC_CR_START>
       
   123 			if (idx > -1) { 
       
   124 				mobileCrashStarted = true;
       
   125 				dumpFolder = FileOperations.addSlashToEnd(DecoderEngine.getNewCrashFolder());
       
   126 				
       
   127 				Calendar cal = Calendar.getInstance();
       
   128 			    SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd_HHmmssSSS"); //$NON-NLS-1$
       
   129 			    dumpFile = new File(dumpFolder +
       
   130 			    					MOBILECRASH_START + 
       
   131 			    					sdf.format(cal.getTime()) + 
       
   132 			    					"." + 
       
   133 			    					CrashAnalyserFile.TRACE_EXTENSION);		
       
   134 				
       
   135 			    // create a file for this mobilecrash
       
   136 			    mcFile = new BufferedWriter(new FileWriter(dumpFile));
       
   137 			    mcFile.write(line);
       
   138 				mcFile.newLine();
       
   139 				
       
   140 			// Line did not contain <MC_CR_START>, but <MB_CR_START> has been found previously (i.e we are reading a file) 
       
   141 			} else if (mobileCrashStarted){ 
       
   142 				
       
   143 				idx = line.indexOf(MOBILE_CRASH_LINE_TAG);
       
   144 				// Line contained <MB_CD>, add data to file
       
   145 				if (idx > -1) { 
       
   146 					mcFile.write(line);
       
   147 					mcFile.newLine();
       
   148 				// Line did not contain <MB_CD>
       
   149 				} else { 
       
   150 					idx = line.indexOf(MOBILE_CRASH_STOPTAG);
       
   151 					// Line contained <MC_CR_STOP>, we can finish reading this mobile crash file
       
   152 					if (idx > -1) {
       
   153 						mcFile.write(line);
       
   154 						mcFile.newLine();
       
   155 						mcFile.close();
       
   156 						mcFile = null;
       
   157 						mobileCrashStarted = false;
       
   158 						
       
   159 						// give this mobilecrash file for further processing
       
   160 						MobileCrashImporter tc = new MobileCrashImporter();
       
   161 						tc.importFrom(dumpFolder, dumpFile.getName(), errorLibrary, decode);
       
   162 					}
       
   163 				}
       
   164 			}
       
   165 		} catch (Exception e) {
       
   166 			if (mcFile != null) {
       
   167 				try {mcFile.close();}catch (Exception E) {E.printStackTrace();}
       
   168 			}
       
   169 			mcFile = null;
       
   170 			mobileCrashStarted = false;
       
   171 			e.printStackTrace();
       
   172 		}
       
   173 	}
       
   174 	
       
   175 	/**
       
   176 	 * Returns whether we have found trace provider plugins.
       
   177 	 * @return true if trace providers were found, false if not
       
   178 	 */
       
   179 	public static boolean traceProviderAvailable() {
       
   180 		if (traceProvider == null)
       
   181 			return false;
       
   182 		return true;
       
   183 	}
       
   184 	
       
   185 	/**
       
   186 	 * Tries to find plugins which are Trace Providers. Selected the first found
       
   187 	 * Trace provider plugin.
       
   188 	 */
       
   189 	void readTraceProvider() {
       
   190 		try {
       
   191 			IExtensionRegistry er = Platform.getExtensionRegistry();
       
   192 			IExtensionPoint ep = 
       
   193 				er.getExtensionPoint(CrashAnalyserPlugin.PLUGIN_ID, EXTENSION_TRACE_PROVIDER);
       
   194 			IExtension[] extensions = ep.getExtensions();
       
   195 			
       
   196 			// if plug-ins were found.
       
   197 			if (extensions != null && extensions.length > 0) {
       
   198 				
       
   199 				// read all found trace providers
       
   200 				for (int i = 0; i < extensions.length; i++) {
       
   201 					IConfigurationElement[] ce = extensions[i].getConfigurationElements();
       
   202 					if (ce != null && ce.length > 0) {
       
   203 						try {
       
   204 							ITraceProvider provider = (ITraceProvider)ce[0].createExecutableExtension("class");
       
   205 							// we support only one trace provider
       
   206 							if (provider != null) {
       
   207 								traceProvider = provider;
       
   208 								break;
       
   209 							}
       
   210 						} catch (Exception e) {
       
   211 							e.printStackTrace();
       
   212 						}
       
   213 					}
       
   214 				}
       
   215 			}
       
   216 		} catch (Exception e) {
       
   217 			e.printStackTrace();
       
   218 		}
       
   219 	}
       
   220 }