crashanalysis/crashanalyser/com.nokia.s60tools.crashanalyser/src/com/nokia/s60tools/crashanalyser/model/EmulatorListener.java
changeset 0 5ad7ad99af01
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 org.eclipse.core.runtime.IProgressMonitor;
       
    21 import org.eclipse.core.runtime.IStatus;
       
    22 import org.eclipse.core.runtime.Status;
       
    23 import org.eclipse.core.runtime.jobs.Job;
       
    24 import org.eclipse.swt.widgets.Display;
       
    25 import java.io.File;
       
    26 import java.io.FileInputStream;
       
    27 import java.io.InputStreamReader;
       
    28 import java.io.BufferedReader;
       
    29 import java.util.regex.Pattern;
       
    30 import java.util.regex.Matcher;
       
    31 import com.nokia.s60tools.crashanalyser.ui.views.MainView;
       
    32 import com.nokia.s60tools.crashanalyser.files.PanicFile;
       
    33 
       
    34 /**
       
    35  * This class listens emulator for panics. 
       
    36  *
       
    37  */
       
    38 public class EmulatorListener extends Job {
       
    39 
       
    40 	static final int POLL_INTERVAL = 10000; // 10 seconds
       
    41 	boolean listening = false;
       
    42 	String epocWindOutPath = "";
       
    43 	long previousFileSize = -1;
       
    44 	int readLines = 0;
       
    45 	Pattern panicParserPattern = Pattern.compile("^\\s*(\\S*)\\s*Thread\\s*(\\S*)\\s*Panic\\s*(\\S*)\\s*(\\S*).*");
       
    46 	
       
    47 	/**
       
    48 	 * Constructor
       
    49 	 */
       
    50 	public EmulatorListener() {
       
    51 		super("Crash Analyser - Emulator Listener");
       
    52 		epocWindOutPath = FileOperations.addSlashToEnd(System.getenv("TEMP"));
       
    53 		epocWindOutPath += "epocwind.out";
       
    54 	}
       
    55 	
       
    56 	/**
       
    57 	 * Starts listening emulator
       
    58 	 */
       
    59 	public void start() {
       
    60 		if (listening)
       
    61 			return;		
       
    62 		listening = true;
       
    63 		setPriority(Job.LONG);
       
    64 		setSystem(true);
       
    65 		setUser(false);
       
    66 		schedule();		
       
    67 	}
       
    68 	
       
    69 	/**
       
    70 	 * Stops listening emulator
       
    71 	 */
       
    72 	public void stop() {
       
    73 		listening = false;
       
    74 		previousFileSize = -1;
       
    75 		readLines = 0;
       
    76 		cancel();
       
    77 	}
       
    78 	
       
    79 	void reSchedule() {
       
    80 		Runnable refreshRunnable = new Runnable(){
       
    81 			public void run(){
       
    82 				cancel();
       
    83 				schedule(POLL_INTERVAL);
       
    84 			}
       
    85 		};
       
    86 		
       
    87 		Display.getDefault().asyncExec(refreshRunnable);        		
       
    88 	}
       
    89 	
       
    90 	@Override
       
    91 	protected IStatus run(IProgressMonitor arg0) {
       
    92 		try {
       
    93 			boolean filesAdded = false;
       
    94 			File epocwind = new File(epocWindOutPath);
       
    95 			if (epocwind.exists()) {
       
    96 				// epocwind.out has not yet been read
       
    97 				if (previousFileSize == -1) {
       
    98 					previousFileSize = epocwind.length();
       
    99 				// epocwind.out has changes
       
   100 				} else if (epocwind.length() > previousFileSize) {
       
   101 					FileInputStream fis = new FileInputStream(epocwind);
       
   102 					BufferedReader br = new BufferedReader(new InputStreamReader(fis));
       
   103 					br.skip(readLines);
       
   104 					
       
   105 					String line = "";
       
   106 					// read all new lines in epocwind.out
       
   107 					while ((line = br.readLine()) != null) {
       
   108 						readLines++;
       
   109 						if (handleLine(line))
       
   110 							filesAdded = true;
       
   111 					}
       
   112 					previousFileSize = epocwind.length();
       
   113 				// new epocwind.out file
       
   114 				} else if (epocwind.length() < previousFileSize){
       
   115 					previousFileSize = 0;
       
   116 					readLines = 0;
       
   117 				}
       
   118 				
       
   119 				// if panics were found, update main view
       
   120 				if (filesAdded)
       
   121 					MainView.showOrRefresh();
       
   122 			}
       
   123 		} catch (Exception e) {
       
   124 			e.printStackTrace();
       
   125 		}
       
   126 		
       
   127 		reSchedule();
       
   128 		return Status.OK_STATUS;
       
   129 	}
       
   130 	
       
   131 	/**
       
   132 	 * If line contains a panic, writes it to xml file
       
   133 	 * @param line epocwind.out file line
       
   134 	 * @return true if line contained a panic, false if not
       
   135 	 */
       
   136 	boolean handleLine(String line) {
       
   137 		Matcher matcher = panicParserPattern.matcher(line);
       
   138 		if (matcher.find()) {
       
   139 			PanicFile.WritePanicFile(matcher.group(1),
       
   140 									 matcher.group(2),
       
   141 									 matcher.group(3),
       
   142 									 matcher.group(4));
       
   143 			return true;
       
   144 		}	
       
   145 		return false;
       
   146 	}
       
   147 	
       
   148 }