Search in sources :

Example 31 with TechnicalException

use of org.eclipse.titan.log.viewer.exceptions.TechnicalException in project titan.EclipsePlug-ins by eclipse.

the class LogFileHandler method autoDetect.

/**
 * This method looks in a log and populate a meta data object
 * with data from the log file. Throws exception if no valid log
 * file format is found
 * @return FileMetaInfo
 * @throws TechnicalException invalid log file
 */
public LogFileMetaData autoDetect() throws TechnicalException {
    String logLine = null;
    BufferedReader bufferedReader = null;
    File logFile = new File(this.fileMetaInfo.getFilePath());
    // check if the log file exists
    if (!logFile.exists()) {
        // $NON-NLS-1$
        throw new TechnicalException(Messages.getString("LogFileHandler.6"));
    }
    // check if .log file extension
    int fileSize = logFile.getName().length();
    // $NON-NLS-1$
    String logExt = "." + Constants.LOG_EXTENSION;
    if (fileSize > logExt.length()) {
        // ".log".length -> 4
        String fileExtension = logFile.getName().substring(fileSize - logExt.length(), fileSize);
        if (!fileExtension.equalsIgnoreCase(logExt)) {
            // $NON-NLS-1$
            throw new TechnicalException(Messages.getString("LogFileHandler.4"));
        }
    }
    // check if the file is empty
    if (logFile.length() == 0) {
        // $NON-NLS-1$
        throw new TechnicalException(Messages.getString("LogFileHandler.0"));
    }
    // check if the file is readable
    if (!logFile.canRead()) {
        // $NON-NLS-1$
        throw new TechnicalException(Messages.getString("LogFileHandler.5"));
    }
    // read first line in the log file
    try {
        bufferedReader = new BufferedReader(new FileReader(logFile));
        try {
            logLine = bufferedReader.readLine();
        } catch (IOException e) {
            ErrorReporter.logExceptionStackTrace(e);
            // $NON-NLS-1$
            throw new TechnicalException(Messages.getString("LogFileHandler.5"));
        }
        // check if a valid time format can be found
        String timeStampFormat = logLine != null ? getTimeStampFormat(logLine) : null;
        if (timeStampFormat == null) {
            // $NON-NLS-1$
            throw new TechnicalException(Messages.getString("LogFileHandler.1"));
        }
        this.fileMetaInfo.setTimeStampFormat(timeStampFormat);
        // remove timestamp from the log line
        logLine = logLine.substring(timeStampFormat.length(), logLine.length()).trim();
        // $NON-NLS-1$
        String[] logLineArray = logLine.split(" ");
        // only timestamp is present in the log
        if (logLineArray.length < 1) {
            // $NON-NLS-1$
            throw new TechnicalException(Messages.getString("LogFileHandler.2"));
        }
        // check for event types
        this.fileMetaInfo.setHasLoggedEventTypes(isEventTypePresent(logLine));
        // check for execution mode, single or parallel
        // (parallel merged is left out)
        String executionMode = getExecutionMode(logLine);
        if (executionMode == null) {
            // $NON-NLS-1$
            throw new TechnicalException(Messages.getString("LogFileHandler.3"));
        }
        this.fileMetaInfo.setExecutionMode(executionMode);
        // set file size
        this.fileMetaInfo.setSize(logFile.length());
        // set last modified
        this.fileMetaInfo.setLastModified(logFile.lastModified());
        // set file format
        this.fileMetaInfo.setFileFormat(Constants.FILEFORMAT_1);
    } catch (FileNotFoundException e) {
        throw new TechnicalException("File not found: " + this.fileMetaInfo.getFilePath(), e);
    } finally {
        IOUtils.closeQuietly(bufferedReader);
    }
    return this.fileMetaInfo;
}
Also used : TechnicalException(org.eclipse.titan.log.viewer.exceptions.TechnicalException) BufferedReader(java.io.BufferedReader) FileNotFoundException(java.io.FileNotFoundException) FileReader(java.io.FileReader) IOException(java.io.IOException) File(java.io.File) IFile(org.eclipse.core.resources.IFile)

Example 32 with TechnicalException

use of org.eclipse.titan.log.viewer.exceptions.TechnicalException in project titan.EclipsePlug-ins by eclipse.

the class LogSearchQuery method run.

@Override
public IStatus run(final IProgressMonitor monitor) {
    result.removeAll();
    int numOfRecords = 0;
    for (IFile logFile : files) {
        File indexFile = LogFileCacheHandler.getLogRecordIndexFileForLogFile(logFile);
        numOfRecords += LogFileCacheHandler.getNumberOfLogRecordIndexes(indexFile);
    }
    monitor.beginTask("Searching", numOfRecords);
    for (IFile logFile : files) {
        if (monitor.isCanceled()) {
            break;
        }
        if (LogFileCacheHandler.hasLogFileChanged(logFile) && !LogFileCacheHandler.processLogFile(logFile, new NullProgressMonitor(), true)) {
            continue;
        }
        File indexFile = LogFileCacheHandler.getLogRecordIndexFileForLogFile(logFile);
        try {
            LogRecordIndex[] indexes = LogFileCacheHandler.readLogRecordIndexFile(indexFile, 0, LogFileCacheHandler.getNumberOfLogRecordIndexes(indexFile));
            SequentialLogFileReader reader = new SequentialLogFileReader(logFile.getLocationURI(), indexes);
            monitor.subTask("Filtering");
            filterRecords(monitor, logFile, reader);
            monitor.done();
        } catch (ParseException e) {
            ErrorReporter.logExceptionStackTrace(e);
            TitanLogExceptionHandler.handleException(new TechnicalException(logFile.getName() + ": Could not parse the log file. Reason: " + e.getMessage()));
        } catch (IOException e) {
            ErrorReporter.logExceptionStackTrace(e);
            TitanLogExceptionHandler.handleException(new TechnicalException(logFile.getName() + ": Error while searching. Reason: " + e.getMessage()));
        }
    }
    monitor.done();
    return new Status(IStatus.OK, Constants.PLUGIN_ID, IStatus.OK, "Search done.", null);
}
Also used : Status(org.eclipse.core.runtime.Status) IStatus(org.eclipse.core.runtime.IStatus) NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) IFile(org.eclipse.core.resources.IFile) TechnicalException(org.eclipse.titan.log.viewer.exceptions.TechnicalException) SequentialLogFileReader(org.eclipse.titan.log.viewer.readers.SequentialLogFileReader) LogRecordIndex(org.eclipse.titan.log.viewer.models.LogRecordIndex) ParseException(java.text.ParseException) IOException(java.io.IOException) File(java.io.File) IFile(org.eclipse.core.resources.IFile)

Example 33 with TechnicalException

use of org.eclipse.titan.log.viewer.exceptions.TechnicalException in project titan.EclipsePlug-ins by eclipse.

the class SearchLabelProvider method getText.

@Override
public String getText(final Object element) {
    if (element instanceof IResource) {
        return WorkbenchLabelProvider.getDecoratingWorkbenchLabelProvider().getText(element);
    }
    if (element instanceof Match) {
        Match match = (Match) element;
        IFile logFile = (IFile) match.getElement();
        RandomAccessFile file = null;
        String result = "";
        try {
            // TODO: This should be cached in some way
            File indexFile = LogFileCacheHandler.getLogRecordIndexFileForLogFile(logFile);
            LogRecordIndex[] indexes = LogFileCacheHandler.readLogRecordIndexFile(indexFile, match.getOffset(), 1);
            file = new RandomAccessFile(new File(logFile.getLocationURI()), "r");
            int length = indexes[0].getRecordLength() > MAX_TEXT_LENGTH ? MAX_TEXT_LENGTH : indexes[0].getRecordLength();
            byte[] record = new byte[length];
            file.seek(indexes[0].getFileOffset());
            file.read(record);
            result = new String(record);
        } catch (FileNotFoundException e) {
            ErrorReporter.logExceptionStackTrace(e);
            // $NON-NLS-1$
            TitanLogExceptionHandler.handleException(new TechnicalException(logFile.getName() + ": File not found " + e.getMessage()));
            return logFile.getName() + ": File not found";
        } catch (IOException e) {
            ErrorReporter.logExceptionStackTrace(e);
            // $NON-NLS-1$
            TitanLogExceptionHandler.handleException(new TechnicalException("Error while reading the log file." + e.getMessage()));
            return "Error while reading the log file.";
        } finally {
            IOUtils.closeQuietly(file);
        }
        return result;
    }
    return "Unexpected element";
}
Also used : IFile(org.eclipse.core.resources.IFile) TechnicalException(org.eclipse.titan.log.viewer.exceptions.TechnicalException) LogRecordIndex(org.eclipse.titan.log.viewer.models.LogRecordIndex) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) Match(org.eclipse.search.ui.text.Match) RandomAccessFile(java.io.RandomAccessFile) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File) IFile(org.eclipse.core.resources.IFile) IResource(org.eclipse.core.resources.IResource)

Aggregations

TechnicalException (org.eclipse.titan.log.viewer.exceptions.TechnicalException)33 IOException (java.io.IOException)18 IFile (org.eclipse.core.resources.IFile)16 UserException (org.eclipse.titan.log.viewer.exceptions.UserException)10 IWorkbenchPage (org.eclipse.ui.IWorkbenchPage)10 File (java.io.File)9 IViewReference (org.eclipse.ui.IViewReference)8 CoreException (org.eclipse.core.runtime.CoreException)7 PartInitException (org.eclipse.ui.PartInitException)7 ParseException (java.text.ParseException)6 IProgressMonitor (org.eclipse.core.runtime.IProgressMonitor)6 IProject (org.eclipse.core.resources.IProject)5 PreferencesHolder (org.eclipse.titan.log.viewer.preferences.PreferencesHolder)5 FileNotFoundException (java.io.FileNotFoundException)4 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)4 WorkspaceJob (org.eclipse.core.resources.WorkspaceJob)4 LogFileMetaData (org.eclipse.titan.log.viewer.models.LogFileMetaData)4 TestCase (org.eclipse.titan.log.viewer.parsers.data.TestCase)4 InvocationTargetException (java.lang.reflect.InvocationTargetException)3 Map (java.util.Map)3