예제 #1
0
            public bool Update()
            {
                using (owner.trace.NewNamedFrame("Updating {0}", this.fileName))
                {
                    CheckDisposed();

                    try
                    {
                        if (simpleMedia == null)
                        {
                            owner.trace.Info("SimpleMedia object not created yet. Creating");
                            simpleMedia = new SimpleFileMedia(
                                owner.fileSystem,
                                SimpleFileMedia.CreateConnectionParamsFromFileName(Path.Combine(owner.baseDirectory, FileName))
                                );
                        }

                        owner.trace.Info("Updating simple media");
                        simpleMedia.Update();

                        if (!simpleMedia.IsAvailable)
                        {
                            owner.trace.Info("File is not avaliable (i.e. has been deleted)");
                            return(false);
                        }

                        if (firstMessageTime == null)
                        {
                            owner.trace.Info("First message time is unknown. Calcalating it");
                            using (IPositionedMessagesReader reader = (IPositionedMessagesReader)Activator.CreateInstance(
                                       owner.logReaderType, new MediaBasedReaderParams(owner.tempThreads, SimpleMedia, owner.tempFilesManager), owner.logFormatInfo))
                            {
                                owner.trace.Info("Reader created");

                                reader.UpdateAvailableBounds(false);
                                owner.trace.Info("Bounds found");

                                IMessage first = PositionedMessagesUtils.ReadNearestMessage(reader, reader.BeginPosition);
                                if (first == null)
                                {
                                    owner.trace.Warning("No messages found");
                                    return(false);
                                }

                                owner.trace.Info("First message: {0} '{1}'", first.Time, first.Text);
                                firstMessageTime = first.Time;
                            }
                        }

                        owner.trace.Info("Part updated OK");
                        return(true);
                    }
                    catch (Exception e)
                    {
                        owner.trace.Error(e, "Failure during part update");
                        return(false);
                    }
                }
            }
예제 #2
0
        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);
        }
예제 #3
0
        public static async Task <SimpleFileMedia> Create(IFileSystem fileSystem, IConnectionParams connectParams)
        {
            var media = new SimpleFileMedia(fileSystem, connectParams);

            try
            {
                await media.Init();

                await media.Update();

                return(media);
            }
            catch
            {
                media.Dispose();
                throw;
            }
        }
예제 #4
0
        public StreamLogProvider(
            ILogProviderHost host,
            ILogProviderFactory factory,
            IConnectionParams connectParams,
            Func <MediaBasedReaderParams, IPositionedMessagesReader> readerCreator
            ) :
            base(host, factory, connectParams)
        {
            using (tracer.NewFrame)
            {
                StartAsyncReader(async() => {
                    if (connectionParams[ConnectionParamsKeys.RotatedLogFolderPathConnectionParam] != null)
                    {
                        media = new RollingFilesMedia(
                            host.FileSystem,
                            readerCreator,
                            tracer,
                            new GenericRollingMediaStrategy(
                                connectionParams[ConnectionParamsKeys.RotatedLogFolderPathConnectionParam],
                                ConnectionParamsUtils.GetRotatedLogPatterns(connectParams)
                                )
                            );
                    }
                    else
                    {
                        media = await SimpleFileMedia.Create(host.FileSystem, connectParams);
                    }

                    reader = readerCreator(new MediaBasedReaderParams(this.threads, media,
                                                                      settingsAccessor: host.GlobalSettings, parentLoggingPrefix: tracer.Prefix));

                    ITimeOffsets initialTimeOffset;
                    if (LogJoint.TimeOffsets.TryParse(
                            connectionParams[ConnectionParamsKeys.TimeOffsetConnectionParam] ?? "", out initialTimeOffset))
                    {
                        reader.TimeOffsets = initialTimeOffset;
                    }

                    return(reader);
                });

                InitPathDependentMembers(connectParams);
            }
        }
예제 #5
0
        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);
        }
예제 #6
0
        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);
        }