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;
}
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);
}
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";
}
Aggregations