static DetectedFormat DetectFormat( string fileName, Func <ILogProviderFactory, int> mruIndexGetter, ILogProviderFactoryRegistry factoriesRegistry, CancellationToken cancellation, IFormatAutodetectionProgress progress, ITempFilesManager tempFilesManager) { if (string.IsNullOrEmpty(fileName)) { throw new ArgumentException("fileName"); } if (mruIndexGetter == null) { throw new ArgumentNullException("mru"); } var log = LJTraceSource.EmptyTracer; using (log.NewFrame) using (SimpleFileMedia fileMedia = new SimpleFileMedia( SimpleFileMedia.CreateConnectionParamsFromFileName(fileName))) using (ILogSourceThreads threads = new LogSourceThreads()) { foreach (ILogProviderFactory factory in GetOrderedListOfRelevantFactories(fileName, mruIndexGetter, factoriesRegistry)) { log.Info("Trying {0}", factory); if (progress != null) { progress.Trying(factory); } if (cancellation.IsCancellationRequested) { return(null); } try { using (var reader = ((IMediaBasedReaderFactory)factory).CreateMessagesReader( new MediaBasedReaderParams(threads, fileMedia, tempFilesManager, MessagesReaderFlags.QuickFormatDetectionMode))) { reader.UpdateAvailableBounds(false); using (var parser = reader.CreateParser(new CreateParserParams(0, null, MessagesParserFlag.DisableMultithreading, MessagesParserDirection.Forward))) { if (parser.ReadNext() != null) { log.Info("Autodetected format of {0}: {1}", fileName, factory); return(new DetectedFormat(factory, ((IFileBasedLogProviderFactory)factory).CreateParams(fileName))); } } } } catch (Exception e) { log.Error(e, "Failed to load '{0}' as {1}", fileName, factory); } } } return(null); }
static async Task <DetectedFormat> DetectFormat( string fileName, string loggableName, Func <ILogProviderFactory, int> mruIndexGetter, ILogProviderFactoryRegistry factoriesRegistry, CancellationToken cancellation, IFormatAutodetectionProgress progress, ITraceSourceFactory traceSourceFactory, LogMedia.IFileSystem fileSystem) { if (string.IsNullOrEmpty(fileName)) { throw new ArgumentException("fileName"); } if (mruIndexGetter == null) { throw new ArgumentNullException("mru"); } Func <Task <SimpleFileMedia> > createFileMedia = () => SimpleFileMedia.Create(fileSystem, SimpleFileMedia.CreateConnectionParamsFromFileName(fileName)); var log = traceSourceFactory.CreateTraceSource("App", string.Format("fdtc.{0}", Interlocked.Increment(ref lastPerfOp))); using (new Profiling.Operation(log, string.Format("format detection of {0}", loggableName))) using (ILogSourceThreadsInternal threads = new LogSourceThreads()) using (var localCancellation = CancellationTokenSource.CreateLinkedTokenSource(cancellation)) { var candidateFactories = GetOrderedListOfRelevantFactories(fileName, mruIndexGetter, factoriesRegistry).ToArray(); var ret = (await Task.WhenAll(candidateFactories.Select((factory, index) => (factory, index)).Select(async candidate => { var(factory, idx) = candidate; try { using (var perfOp = new Profiling.Operation(log, factory.ToString())) using (var fileMedia = await createFileMedia()) using (var reader = ((IMediaBasedReaderFactory)factory).CreateMessagesReader( new MediaBasedReaderParams(threads, fileMedia, MessagesReaderFlags.QuickFormatDetectionMode, parentLoggingPrefix: log.Prefix))) { if (progress != null) { progress.Trying(factory); } if (localCancellation.IsCancellationRequested) { perfOp.Milestone("cancelled"); return(fmt: (DetectedFormat)null, idx); } await reader.UpdateAvailableBounds(false); perfOp.Milestone("bounds detected"); var parser = await reader.CreateParser(new CreateParserParams(0, null, MessagesParserFlag.DisableMultithreading | MessagesParserFlag.DisableDejitter, MessagesParserDirection.Forward)); try { if (await parser.ReadNext() != null) { log.Info("Autodetected format of {0}: {1}", fileName, factory); localCancellation.Cancel(); return(fmt: new DetectedFormat(factory, ((IFileBasedLogProviderFactory)factory).CreateParams(fileName)), idx); } } finally { await parser.Dispose(); } } } catch (Exception e) { log.Error(e, "Failed to load '{0}' as {1}", fileName, factory); } return(fmt: (DetectedFormat)null, idx); }))).Where(x => x.fmt != null).OrderBy(x => x.idx).Select(x => x.fmt).FirstOrDefault(); if (ret != null) { return(ret); } using (var fileMedia = await createFileMedia()) { if (!await IOUtils.IsBinaryFile(fileMedia.DataStream)) { log.Info("File does not look binary"); var factory = factoriesRegistry.Find( PlainText.Factory.CompanyName, PlainText.Factory.FormatName) as IFileBasedLogProviderFactory; if (factory != null) { log.Info("Fall back to plaintext format"); return(new DetectedFormat(factory, factory.CreateParams(fileName))); } } } } return(null); }
static DetectedFormat DetectFormat( string fileName, string loggableName, Func <ILogProviderFactory, int> mruIndexGetter, ILogProviderFactoryRegistry factoriesRegistry, CancellationToken cancellation, IFormatAutodetectionProgress progress, ITempFilesManager tempFilesManager) { if (string.IsNullOrEmpty(fileName)) { throw new ArgumentException("fileName"); } if (mruIndexGetter == null) { throw new ArgumentNullException("mru"); } Func <SimpleFileMedia> createFileMedia = () => new SimpleFileMedia(SimpleFileMedia.CreateConnectionParamsFromFileName(fileName)); var log = new LJTraceSource("App", string.Format("fdtc.{0}", Interlocked.Increment(ref lastPerfOp))); using (new Profiling.Operation(log, string.Format("format detection of {0}", loggableName))) using (ILogSourceThreads threads = new LogSourceThreads()) using (var localCancellation = CancellationTokenSource.CreateLinkedTokenSource(cancellation)) { var ret = GetOrderedListOfRelevantFactories(fileName, mruIndexGetter, factoriesRegistry).AsParallel().Select(factory => { try { using (var perfOp = new Profiling.Operation(log, factory.ToString())) using (var fileMedia = createFileMedia()) using (var reader = ((IMediaBasedReaderFactory)factory).CreateMessagesReader( new MediaBasedReaderParams(threads, fileMedia, tempFilesManager, MessagesReaderFlags.QuickFormatDetectionMode, parentLoggingPrefix: log.Prefix))) { if (progress != null) { progress.Trying(factory); } if (localCancellation.IsCancellationRequested) { perfOp.Milestone("cancelled"); return(null); } reader.UpdateAvailableBounds(false); perfOp.Milestone("bounds detected"); using (var parser = reader.CreateParser(new CreateParserParams(0, null, MessagesParserFlag.DisableMultithreading | MessagesParserFlag.DisableDejitter, MessagesParserDirection.Forward))) { if (parser.ReadNext() != null) { log.Info("Autodetected format of {0}: {1}", fileName, factory); localCancellation.Cancel(); return(new DetectedFormat(factory, ((IFileBasedLogProviderFactory)factory).CreateParams(fileName))); } } } } catch (Exception e) { log.Error(e, "Failed to load '{0}' as {1}", fileName, factory); } return(null); }).FirstOrDefault(x => x != null); if (ret != null) { return(ret); } using (var fileMedia = createFileMedia()) { if (!IOUtils.IsBinaryFile(fileMedia.DataStream)) { log.Info("File does not look binary"); var factory = factoriesRegistry.Find( PlainText.Factory.CompanyName, PlainText.Factory.FormatName) as IFileBasedLogProviderFactory; if (factory != null) { log.Info("Fall back to plaintext format"); return(new DetectedFormat(factory, factory.CreateParams(fileName))); } } } } return(null); }