trace/traceviewer/com.nokia.traceviewer/src/com/nokia/traceviewer/action/LogSaveAsciiAction.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  * Handler for save ascii log command
       
    17  *
       
    18  */
       
    19 package com.nokia.traceviewer.action;
       
    20 
       
    21 import java.io.FileNotFoundException;
       
    22 import java.io.FileWriter;
       
    23 import java.io.IOException;
       
    24 import java.io.PrintWriter;
       
    25 import java.net.URL;
       
    26 import java.util.Iterator;
       
    27 import java.util.List;
       
    28 
       
    29 import org.eclipse.jface.resource.ImageDescriptor;
       
    30 import org.eclipse.ui.PlatformUI;
       
    31 
       
    32 import com.nokia.traceviewer.TraceViewerHelpContextIDs;
       
    33 import com.nokia.traceviewer.TraceViewerPlugin;
       
    34 import com.nokia.traceviewer.dialog.ProgressBarDialog;
       
    35 import com.nokia.traceviewer.engine.DataProcessorAccess;
       
    36 import com.nokia.traceviewer.engine.TraceProperties;
       
    37 import com.nokia.traceviewer.engine.TraceViewerGlobals;
       
    38 import com.nokia.traceviewer.engine.TraceViewerDialogInterface.Dialog;
       
    39 
       
    40 /**
       
    41  * Handler for save ascii log command
       
    42  */
       
    43 public final class LogSaveAsciiAction extends TraceViewerAction {
       
    44 
       
    45 	/**
       
    46 	 * Number of traces to process once
       
    47 	 */
       
    48 	private static final int NUMBER_OF_TRACES_TO_PROCESS = 400;
       
    49 
       
    50 	/**
       
    51 	 * Filters in save file dialog
       
    52 	 */
       
    53 	private static final String[] FILTER_NAMES = { Messages
       
    54 			.getString("LogSaveAsciiAction.FilterNames") }; //$NON-NLS-1$
       
    55 
       
    56 	/**
       
    57 	 * Extension filters in save file dialog
       
    58 	 */
       
    59 	private static final String[] FILTER_EXTS = { "*.txt", "*.*" }; //$NON-NLS-1$ //$NON-NLS-2$
       
    60 
       
    61 	/**
       
    62 	 * Image for the action
       
    63 	 */
       
    64 	private static ImageDescriptor image;
       
    65 
       
    66 	/**
       
    67 	 * Progressbar
       
    68 	 */
       
    69 	private ProgressBarDialog progressBar;
       
    70 
       
    71 	/**
       
    72 	 * Saving file boolean
       
    73 	 */
       
    74 	private boolean savingFile;
       
    75 
       
    76 	/**
       
    77 	 * Writer to be used for writing
       
    78 	 */
       
    79 	private PrintWriter plainOutput;
       
    80 
       
    81 	/**
       
    82 	 * End line character \n
       
    83 	 */
       
    84 	private static final char ENDLINE_N = '\n';
       
    85 
       
    86 	/**
       
    87 	 * Tabulator character
       
    88 	 */
       
    89 	private static final char TABULATOR = '\t';
       
    90 
       
    91 	static {
       
    92 		URL url = TraceViewerPlugin.getDefault().getBundle().getEntry(
       
    93 				"/icons/logsaveascii.gif"); //$NON-NLS-1$
       
    94 		image = ImageDescriptor.createFromURL(url);
       
    95 	}
       
    96 
       
    97 	/**
       
    98 	 * Constructor
       
    99 	 */
       
   100 	LogSaveAsciiAction() {
       
   101 		setText(Messages.getString("LogSaveAsciiAction.Title")); //$NON-NLS-1$
       
   102 		setToolTipText(Messages.getString("LogSaveAsciiAction.Tooltip")); //$NON-NLS-1$
       
   103 		setImageDescriptor(image);
       
   104 		setActionDefinitionId("com.nokia.traceviewer.command.save_ascii_log"); //$NON-NLS-1$
       
   105 
       
   106 		// Set help
       
   107 		PlatformUI.getWorkbench().getHelpSystem().setHelp(this,
       
   108 				TraceViewerHelpContextIDs.LOGGING);
       
   109 	}
       
   110 
       
   111 	/*
       
   112 	 * (non-Javadoc)
       
   113 	 * 
       
   114 	 * @see com.nokia.traceviewer.action.TraceViewerAction#doRun()
       
   115 	 */
       
   116 	@Override
       
   117 	protected void doRun() {
       
   118 		TraceViewerGlobals.postUiEvent("LogSaveAsciiAction", "1"); //$NON-NLS-1$ //$NON-NLS-2$
       
   119 
       
   120 		// Bring up a file save dialog
       
   121 		String[] files = TraceViewerActionUtils.openFileDialog(FILTER_NAMES,
       
   122 				FILTER_EXTS, null, null, false, false);
       
   123 
       
   124 		// Check if a file name was given
       
   125 		if (files != null && files.length > 0) {
       
   126 			String fileName = files[0];
       
   127 			final int numberOfTraces = TraceViewerGlobals.getTraceViewer()
       
   128 					.getDataReaderAccess().getMainDataReader().getTraceCount();
       
   129 
       
   130 			// Create file writer
       
   131 			boolean writerOk = buildPlainFileWriter(fileName);
       
   132 
       
   133 			if (writerOk) {
       
   134 
       
   135 				// Create progressbar
       
   136 				progressBar = (ProgressBarDialog) TraceViewerGlobals
       
   137 						.getTraceViewer().getDialogs().createDialog(
       
   138 								Dialog.PROGRESSBAR);
       
   139 
       
   140 				// Create new thread
       
   141 				new Thread() {
       
   142 
       
   143 					/*
       
   144 					 * (non-Javadoc)
       
   145 					 * 
       
   146 					 * @see java.lang.Thread#run()
       
   147 					 */
       
   148 					@Override
       
   149 					public void run() {
       
   150 						try {
       
   151 							int traceNumberToProcess = 0;
       
   152 
       
   153 							// Get block of traces from the binary file
       
   154 							while (traceNumberToProcess < numberOfTraces) {
       
   155 
       
   156 								// From 0 to 399.
       
   157 								List<TraceProperties> traces = TraceViewerGlobals
       
   158 										.getTraceViewer()
       
   159 										.getTraces(
       
   160 												traceNumberToProcess,
       
   161 												traceNumberToProcess
       
   162 														+ NUMBER_OF_TRACES_TO_PROCESS
       
   163 														- 1);
       
   164 								Iterator<TraceProperties> i = traces.iterator();
       
   165 
       
   166 								// Iterate through the block of traces
       
   167 								while (i.hasNext()) {
       
   168 									TraceProperties trace = i.next();
       
   169 									String processedTrace = processTrace(trace);
       
   170 
       
   171 									if (processedTrace != null) {
       
   172 										plainOutput.write(processedTrace);
       
   173 									}
       
   174 								}
       
   175 
       
   176 								// Increase the counter value and update
       
   177 								// progressbar
       
   178 								traceNumberToProcess += NUMBER_OF_TRACES_TO_PROCESS;
       
   179 								notifyFilePosition(traceNumberToProcess);
       
   180 							}
       
   181 
       
   182 						} catch (Exception e) {
       
   183 							e.printStackTrace();
       
   184 
       
   185 							// Close the progressbar and the file writer
       
   186 						} finally {
       
   187 							TraceViewerGlobals.getTraceViewer().getView()
       
   188 									.closeProgressBar(progressBar);
       
   189 							plainOutput.flush();
       
   190 							plainOutput.close();
       
   191 						}
       
   192 					}
       
   193 
       
   194 				}.start();
       
   195 
       
   196 				savingFile = true;
       
   197 				progressBar.open(numberOfTraces, Messages
       
   198 						.getString("LogSaveAsciiAction.SavingText")); //$NON-NLS-1$
       
   199 				savingFile = false;
       
   200 
       
   201 				// Export possible comments
       
   202 				TraceViewerGlobals.getTraceViewer().getDataProcessorAccess()
       
   203 						.getTraceCommentHandler().exportTraceComments(fileName);
       
   204 
       
   205 			} else {
       
   206 				System.out
       
   207 						.println(Messages
       
   208 								.getString("LogSaveAsciiAction.CannotCreateFileWriter")); //$NON-NLS-1$
       
   209 			}
       
   210 		}
       
   211 
       
   212 		TraceViewerGlobals.postUiEvent("LogSaveAsciiAction", "0"); //$NON-NLS-1$ //$NON-NLS-2$
       
   213 	}
       
   214 
       
   215 	/**
       
   216 	 * Processes trace
       
   217 	 * 
       
   218 	 * @param properties
       
   219 	 *            trace properties
       
   220 	 * @return the processed trace
       
   221 	 */
       
   222 	protected String processTrace(TraceProperties properties) {
       
   223 		String ret = null;
       
   224 		StringBuffer trace = null;
       
   225 
       
   226 		DataProcessorAccess dpa = TraceViewerGlobals.getTraceViewer()
       
   227 				.getDataProcessorAccess();
       
   228 
       
   229 		// If binary trace, decode it
       
   230 		if (properties.binaryTrace) {
       
   231 			dpa.getDecoder().processData(properties);
       
   232 		}
       
   233 
       
   234 		// Null timestamp from first trace
       
   235 		if (properties.traceNumber == 1) {
       
   236 			TraceViewerGlobals.getTraceViewer().getDataProcessorAccess()
       
   237 					.getTimestampParser().nullPreviousTimestamp();
       
   238 		}
       
   239 
       
   240 		// Parse timestamp
       
   241 		dpa.getTimestampParser().processData(properties);
       
   242 
       
   243 		// Normal ASCII trace log
       
   244 		if (!properties.binaryTrace) {
       
   245 
       
   246 			// No timestamp
       
   247 			if (properties.timestampString == null) {
       
   248 				int traceLen = properties.traceString.length() + 1;
       
   249 				trace = new StringBuffer(traceLen);
       
   250 				trace.append(properties.traceString);
       
   251 				trace.append(ENDLINE_N);
       
   252 
       
   253 				// With timestamp
       
   254 			} else {
       
   255 				StringBuffer timeFromPreviousSB = TraceViewerGlobals
       
   256 						.getTraceViewer().getDataProcessorAccess()
       
   257 						.getTimestampParser().getTimeFromPreviousString(
       
   258 								properties.timeFromPreviousTrace);
       
   259 				int traceLen = properties.timestampString.length() + 1
       
   260 						+ timeFromPreviousSB.length()
       
   261 						+ properties.traceString.length() + 1;
       
   262 				trace = new StringBuffer(traceLen);
       
   263 				trace.append(properties.timestampString);
       
   264 				trace.append(timeFromPreviousSB);
       
   265 				trace.append(TABULATOR);
       
   266 				trace.append(properties.traceString);
       
   267 				trace.append(ENDLINE_N);
       
   268 			}
       
   269 			ret = trace.toString();
       
   270 		}
       
   271 		return ret;
       
   272 	}
       
   273 
       
   274 	/**
       
   275 	 * Is saving file
       
   276 	 * 
       
   277 	 * @return true if saving file is going on
       
   278 	 */
       
   279 	public boolean isSavingFile() {
       
   280 		return savingFile;
       
   281 	}
       
   282 
       
   283 	/**
       
   284 	 * Update progress bar with file position
       
   285 	 * 
       
   286 	 * @param traceNumber
       
   287 	 *            trace number
       
   288 	 */
       
   289 	public void notifyFilePosition(int traceNumber) {
       
   290 
       
   291 		// Only update every third time to get more speed
       
   292 		if (progressBar != null) {
       
   293 			progressBar.updateProgressBar(traceNumber);
       
   294 		}
       
   295 	}
       
   296 
       
   297 	/**
       
   298 	 * Builds the plain file writer
       
   299 	 * 
       
   300 	 * @param filePath
       
   301 	 *            file path to save the log
       
   302 	 * @return status of building plain text file
       
   303 	 */
       
   304 	public boolean buildPlainFileWriter(String filePath) {
       
   305 		boolean success = false;
       
   306 
       
   307 		try {
       
   308 			plainOutput = new PrintWriter(new FileWriter(filePath));
       
   309 			success = true;
       
   310 		} catch (FileNotFoundException e) {
       
   311 			e.printStackTrace();
       
   312 		} catch (IOException e) {
       
   313 			e.printStackTrace();
       
   314 		}
       
   315 
       
   316 		return success;
       
   317 	}
       
   318 }