crashanalysis/crashanalyser/com.nokia.s60tools.crashanalyser/src/com/nokia/s60tools/crashanalyser/containers/OstTrace.java
changeset 4 615035072f7e
child 16 72f198be1c1d
equal deleted inserted replaced
3:431bbaccaec8 4:615035072f7e
       
     1 /*
       
     2 * Copyright (c) 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 */package com.nokia.s60tools.crashanalyser.containers;
       
    17 
       
    18 import java.io.BufferedWriter;
       
    19 import java.io.IOException;
       
    20 import java.util.ArrayList;
       
    21 import java.util.List;
       
    22 
       
    23 import org.w3c.dom.Element;
       
    24 import org.w3c.dom.Node;
       
    25 import org.w3c.dom.NodeList;
       
    26 
       
    27 public final class OstTrace {
       
    28 
       
    29 	private List<OstTraceLine> ostTraces;
       
    30 	
       
    31 	/**
       
    32 	 * Private constructor.
       
    33 	 * @param traceLines Trace lines.
       
    34 	 */
       
    35 	private OstTrace(List<OstTraceLine> traceLines) {
       
    36 		ostTraces = traceLines;
       
    37 	}
       
    38 	
       
    39 	/**
       
    40 	 * Writes traces in to a buffer (e.g. a text file)
       
    41 	 * @param out where to write
       
    42 	 * @throws IOException
       
    43 	 */
       
    44 	public void writeTo(BufferedWriter out) throws IOException {
       
    45 		writeLine(out,"");
       
    46 		writeLine(out, "OST TRACE LOG:");
       
    47 		writeLine(out, "-----------------");
       
    48 		// if there are any events
       
    49 		if (ostTraces != null && !ostTraces.isEmpty()) {
       
    50 			// print events
       
    51 			for (OstTraceLine traceLine : ostTraces) {
       
    52 				String format = "%s : %s : %s : %s : %s : %s : %s : %s : %s : %s";
       
    53 				String line = String.format(format, traceLine.getTimestamp(), traceLine.getTraceText(), 
       
    54 						traceLine.getFile(), traceLine.getLineNumber(), traceLine.getType(), 
       
    55 						traceLine.getContextId(), traceLine.getPrefix(),
       
    56 						traceLine.getComponent(), traceLine.getGroup(), traceLine.getId());
       
    57 				writeLine(out, line);
       
    58 			}
       
    59 		}
       
    60 	}
       
    61 
       
    62 	/**
       
    63 	 * Writes given line plus a line break.
       
    64 	 * @param out where to write
       
    65 	 * @param line what to write
       
    66 	 * @throws IOException
       
    67 	 */
       
    68 	void writeLine(BufferedWriter out, String line) throws IOException {
       
    69 		out.write(line);
       
    70 		out.newLine();
       
    71 	}
       
    72 
       
    73 	/**
       
    74 	 * Reads OST traces from an XML element
       
    75 	 * @param elementSegTraces seg_traces tag
       
    76 	 * @return a created OstTrace class or null
       
    77 	 */
       
    78 	public static OstTrace read(Element elementSegOstTrace) {
       
    79 		List<OstTraceLine> traces = new ArrayList<OstTraceLine>();
       
    80 		try {
       
    81 			
       
    82 			// get all trace nodes
       
    83 			NodeList children = elementSegOstTrace.getChildNodes();
       
    84 			if (children != null && children.getLength() > 0) {
       
    85 				// go through all line nodes
       
    86 				for (int i = 0; i < children.getLength(); i++) {
       
    87 					Node traceNode = children.item(i);
       
    88 					OstTraceLine traceLine = OstTraceLine.read(traceNode);
       
    89 					if(traceLine != null) {
       
    90 						traces.add(traceLine);
       
    91 					}
       
    92 				}
       
    93 			}
       
    94 			
       
    95 		} catch (Exception e) {
       
    96 			// Do nothing.
       
    97 		}
       
    98 		return new OstTrace(traces);
       
    99 	}
       
   100 
       
   101 
       
   102 	/**
       
   103 	 * Returns the trace lines.
       
   104 	 * @return Trace lines
       
   105 	 */
       
   106 	public List<OstTraceLine> getTraces() {
       
   107 		return ostTraces;
       
   108 	}
       
   109 }