Esempio n. 1
0
        public LogSource(ILogSourcesManagerInternal owner, int id,
                         ILogProviderFactory providerFactory, IConnectionParams connectionParams,
                         IModelThreads threads, ITempFilesManager tempFilesManager, Persistence.IStorageManager storageManager,
                         IInvokeSynchronization invoker, Settings.IGlobalSettingsAccessor globalSettingsAccess, IBookmarks bookmarks)
        {
            this.owner                = owner;
            this.tracer               = new LJTraceSource("LogSource", string.Format("ls{0:D2}", id));
            this.tempFilesManager     = tempFilesManager;
            this.invoker              = invoker;
            this.globalSettingsAccess = globalSettingsAccess;
            this.bookmarks            = bookmarks;

            try
            {
                this.logSourceThreads              = new LogSourceThreads(this.tracer, threads, this);
                this.timeGaps                      = new TimeGapsDetector(tracer, invoker, new LogSourceGapsSource(this));
                this.timeGaps.OnTimeGapsChanged   += timeGaps_OnTimeGapsChanged;
                this.logSourceSpecificStorageEntry = CreateLogSourceSpecificStorageEntry(providerFactory, connectionParams, storageManager);

                var extendedConnectionParams = connectionParams.Clone(true);
                this.LoadPersistedSettings(extendedConnectionParams);
                this.provider = providerFactory.CreateFromConnectionParams(this, extendedConnectionParams);
            }
            catch (Exception e)
            {
                tracer.Error(e, "Failed to initialize log source");
                ((ILogSource)this).Dispose();
                throw;
            }

            this.owner.Container.Add(this);
            this.owner.FireOnLogSourceAdded(this);

            this.LoadBookmarks();
        }
Esempio n. 2
0
        static void InitializePlatform(LJTraceSource tracer)
        {
            Thread.CurrentThread.Name = "Main thread";

            AppDomain.CurrentDomain.UnhandledException += delegate(object sender, UnhandledExceptionEventArgs e)
            {
                string msg = "Unhahdled domain exception occured";
                if (e.ExceptionObject is Exception)
                {
                    tracer.Error((Exception)e.ExceptionObject, msg);
                }
                else
                {
                    tracer.Error("{0}: ({1}) {2}", msg, e.ExceptionObject.GetType(), e.ExceptionObject);
                }
            };
        }
Esempio n. 3
0
 public static async Task LogAndThrowOnFailure(this HttpWebResponse rsp, LJTraceSource trace)
 {
     if ((int)rsp.StatusCode >= 200 && (int)rsp.StatusCode < 300)
     {
         return;
     }
     using (var responseStream = rsp.GetResponseStream())
         using (var responseReader = new StreamReader(responseStream))
         {
             trace.Error("http failed. {0} {1} {2}",
                         rsp.Method, rsp.ResponseUri, await responseReader.ReadToEndAsync());
         }
     throw new WebException("http request failed");
 }
Esempio n. 4
0
        public RollingFilesMedia(
            LogMedia.IFileSystem fileSystem,
            Type logReaderType,
            StreamBasedFormatInfo logFormatInfo,
            LJTraceSource traceSource,
            IRollingFilesMediaStrategy rollingStrategy,
            ITempFilesManager tempFilesManager,
            ITraceSourceFactory traceSourceFactory)
        {
            this.traceSourceFactory = traceSourceFactory;
            trace = traceSource;
            using (trace.NewFrame)
            {
                if (fileSystem == null)
                {
                    throw new ArgumentNullException("fileSystem");
                }

                this.rollingStrategy  = rollingStrategy;
                this.logReaderType    = logReaderType;
                this.logFormatInfo    = logFormatInfo;
                this.tempFilesManager = tempFilesManager;

                try
                {
                    this.fileSystem    = fileSystem;
                    this.baseDirectory = rollingStrategy.BaseDirectory;
                    trace.Info("Base file directory: {0}", baseDirectory);

                    this.concatStream = new ConcatReadingStream();
                    this.tempThreads  = new LogSourceThreads(LJTraceSource.EmptyTracer, new ModelThreads(), null);

                    this.fsWatcher                     = fileSystem.CreateWatcher();
                    this.fsWatcher.Path                = this.baseDirectory;
                    this.fsWatcher.Created            += new FileSystemEventHandler(fsWatcher_Created);
                    this.fsWatcher.Renamed            += new RenamedEventHandler(fsWatcher_Renamed);
                    this.fsWatcher.EnableRaisingEvents = true;

                    trace.Info("Watcher enabled");

                    this.folderNeedsRescan = 1;
                }
                catch
                {
                    trace.Error("Initialization failed. Disposing.");
                    Dispose();
                    throw;
                }
            }
        }
Esempio n. 5
0
        public LiveLogProvider(ILogProviderHost host, ILogProviderFactory factory, IConnectionParams originalConnectionParams, DejitteringParams?dejitteringParams = null)
            :
            base(
                host,
                factory,
                CreateConnectionParams(originalConnectionParams, host.TempFilesManager),
                @params => new XmlFormat.MessagesReader(
                    @params,
                    XmlFormat.XmlFormatInfo.MakeNativeFormatInfo(LiveLogXMLWriter.OutputEncoding.EncodingName, dejitteringParams, new FormatViewOptions(rawViewAllowed: false), host.RegexFactory),
                    host.RegexFactory,
                    host.TraceSourceFactory
                    )
                )
        {
            this.trace = base.tracer;
            this.originalConnectionParams = new ConnectionParamsReadOnlyView(originalConnectionParams);
            using (trace.NewFrame)
            {
                try
                {
                    string fileName = base.connectionParamsReadonlyView[ConnectionParamsKeys.PathConnectionParam];

                    XmlWriterSettings xmlSettings = new XmlWriterSettings();
                    xmlSettings.CloseOutput        = true;
                    xmlSettings.ConformanceLevel   = ConformanceLevel.Fragment;
                    xmlSettings.OmitXmlDeclaration = false;
                    xmlSettings.Indent             = true;

                    output = new LiveLogXMLWriter(
                        new FileStream(fileName, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.Read),
                        xmlSettings,
                        defaultBackupMaxFileSize
                        );
                    trace.Info("Output created");

                    stopEvt = new CancellationTokenSource();

                    listeningThread = new Thread(ListeningThreadProc);
                }
                catch (Exception e)
                {
                    trace.Error(e, "Failed to inistalize live log reader. Disposing what has been created so far.");
                    Dispose();
                    throw;
                }
            }
        }
Esempio n. 6
0
 void ListeningThreadProc()
 {
     using (trace.NewFrame)
     {
         try
         {
             LiveLogListen(this.stopEvt.Token, this.output);
         }
         catch (Exception e)
         {
             trace.Error(e, "Live log listening thread failed");
         }
         finally
         {
             ReportBackgroundActivityStatus(false);
         }
     }
 }
Esempio n. 7
0
        public RollingFilesMedia(
            LogMedia.IFileSystem fileSystem,
            Func <MediaBasedReaderParams, IPositionedMessagesReader> readerCreator,
            LJTraceSource traceSource,
            IRollingFilesMediaStrategy rollingStrategy)
        {
            trace = traceSource;
            using (trace.NewFrame)
            {
                if (fileSystem == null)
                {
                    throw new ArgumentNullException(nameof(fileSystem));
                }

                this.rollingStrategy = rollingStrategy;
                this.readerCreator   = readerCreator;

                try
                {
                    this.fileSystem    = fileSystem;
                    this.baseDirectory = rollingStrategy.BaseDirectory;
                    trace.Info("Base file directory: {0}", baseDirectory);

                    this.concatStream = new ConcatReadingStream();
                    this.tempThreads  = new LogSourceThreads(LJTraceSource.EmptyTracer, new ModelThreads(), null);

                    this.fsWatcher                     = fileSystem.CreateWatcher();
                    this.fsWatcher.Path                = this.baseDirectory;
                    this.fsWatcher.Created            += new FileSystemEventHandler(fsWatcher_Created);
                    this.fsWatcher.Renamed            += new RenamedEventHandler(fsWatcher_Renamed);
                    this.fsWatcher.EnableRaisingEvents = true;

                    trace.Info("Watcher enabled");

                    this.folderNeedsRescan = 1;
                }
                catch
                {
                    trace.Error("Initialization failed. Disposing.");
                    Dispose();
                    throw;
                }
            }
        }
Esempio n. 8
0
        public TempFilesManager()
        {
            var tracer = new LJTraceSource("App", "tmp");

            using (tracer.NewFrame)
            {
#if !SILVERLIGHT
                folder = Path.Combine(Path.GetTempPath(), "LogJoint");
#else
#endif
                bool thisIsTheOnlyInstance = false;
                runningInstanceMutex = new Mutex(true, "LogJoint/TempFilesManager", out thisIsTheOnlyInstance);

                tracer.Info("Temp directory: {0}", folder);

                if (!Directory.Exists(folder))
                {
                    tracer.Info("Temp directory doesn't exist. Creating it.");
                    Directory.CreateDirectory(folder);
                }
                else
                {
                    if (!thisIsTheOnlyInstance)
                    {
                        tracer.Info("Temp directory exists and I am NOT the only instance in the system. Skipping temp cleanup.");
                    }
                    else
                    {
                        tracer.Info("Temp directory exists. Deleting it first.");
                        try
                        {
                            Directory.Delete(folder, true);
                        }
                        catch (Exception e)
                        {
                            tracer.Error(e, "Failed to delete tempdir");
                        }
                    }
                    tracer.Info("Creating temp directory.");
                    Directory.CreateDirectory(folder);
                }
            }
        }
Esempio n. 9
0
 public BookmarkController(
     IBookmarks bookmarks,
     IModelThreads threads,
     IHeartBeatTimer heartbeat
     )
 {
     tracer = LJTraceSource.EmptyTracer;
     threads.OnThreadListChanged += (s, e) =>
     {
         bookmarksNeedPurgeFlag.Invalidate();
     };
     heartbeat.OnTimer += (sender, args) =>
     {
         if (args.IsNormalUpdate && bookmarksNeedPurgeFlag.Validate())
         {
             bookmarks.PurgeBookmarksForDisposedThreads();
         }
     };
     bookmarks.OnBookmarksChanged += (sender, e) =>
     {
         if (e.Type == BookmarksChangedEventArgs.ChangeType.Added || e.Type == BookmarksChangedEventArgs.ChangeType.Removed ||
             e.Type == BookmarksChangedEventArgs.ChangeType.RemovedAll || e.Type == BookmarksChangedEventArgs.ChangeType.Purged)
         {
             foreach (var affectedSource in
                      e.AffectedBookmarks
                      .Select(b => b.GetLogSource())
                      .Where(s => s.LogSourceStateIsOkToChangePersistentState())
                      .Distinct())
             {
                 try
                 {
                     affectedSource.StoreBookmarks();
                 }
                 catch (Persistence.StorageException storageException)
                 {
                     tracer.Error(storageException, "Failed to store bookmarks for log {0}",
                                  affectedSource.GetSafeConnectionId());
                 }
             }
         }
     };
 }
Esempio n. 10
0
        public LogSource(ILogSourcesManagerInternal owner, int id,
                         ILogProviderFactory providerFactory, IConnectionParams connectionParams,
                         IModelThreadsInternal threads, ITempFilesManager tempFilesManager, Persistence.IStorageManager storageManager,
                         ISynchronizationContext modelSyncContext, Settings.IGlobalSettingsAccessor globalSettingsAccess, IBookmarks bookmarks,
                         ITraceSourceFactory traceSourceFactory, RegularExpressions.IRegexFactory regexFactory, LogMedia.IFileSystem fileSystem)
        {
            this.owner                = owner;
            this.tracer               = traceSourceFactory.CreateTraceSource("LogSource", string.Format("ls{0:D2}", id));
            this.tempFilesManager     = tempFilesManager;
            this.modelSyncContext     = modelSyncContext;
            this.globalSettingsAccess = globalSettingsAccess;
            this.bookmarks            = bookmarks;
            this.traceSourceFactory   = traceSourceFactory;
            this.regexFactory         = regexFactory;
            this.fileSystem           = fileSystem;

            try
            {
                this.logSourceThreads              = new LogSourceThreads(this.tracer, threads, this);
                this.timeGaps                      = new TimeGapsDetector(tracer, modelSyncContext, new LogSourceGapsSource(this), traceSourceFactory);
                this.timeGaps.OnTimeGapsChanged   += timeGaps_OnTimeGapsChanged;
                this.logSourceSpecificStorageEntry = CreateLogSourceSpecificStorageEntry(providerFactory, connectionParams, storageManager);

                var extendedConnectionParams = connectionParams.Clone(true);
                this.LoadPersistedSettings(extendedConnectionParams);
                this.provider = providerFactory.CreateFromConnectionParams(this, extendedConnectionParams);
            }
            catch (Exception e)
            {
                tracer.Error(e, "Failed to initialize log source");
                ((ILogSource)this).Dispose();
                throw;
            }

            this.owner.Add(this);
            this.owner.FireOnLogSourceAdded(this);

            this.LoadBookmarks();
        }
Esempio n. 11
0
 public BookmarkController(
     IBookmarks bookmarks,
     IModelThreads threads,
     IHeartBeatTimer heartbeat,
     ISynchronizationContext synchronization
     )
 {
     tracer         = LJTraceSource.EmptyTracer;
     bookmarksPurge = new AsyncInvokeHelper(synchronization, bookmarks.PurgeBookmarksForDisposedThreads);
     threads.OnThreadListChanged += (s, e) =>
     {
         bookmarksPurge.Invoke();
     };
     bookmarks.OnBookmarksChanged += (sender, e) =>
     {
         if (e.Type == BookmarksChangedEventArgs.ChangeType.Added || e.Type == BookmarksChangedEventArgs.ChangeType.Removed ||
             e.Type == BookmarksChangedEventArgs.ChangeType.RemovedAll || e.Type == BookmarksChangedEventArgs.ChangeType.Purged)
         {
             foreach (var affectedSource in
                      e.AffectedBookmarks
                      .Select(b => b.GetLogSource())
                      .Where(s => s.LogSourceStateIsOkToChangePersistentState())
                      .Distinct())
             {
                 try
                 {
                     affectedSource.StoreBookmarks();
                 }
                 catch (Persistence.StorageException storageException)
                 {
                     tracer.Error(storageException, "Failed to store bookmarks for log {0}",
                                  affectedSource.GetSafeConnectionId());
                 }
             }
         }
     };
 }
Esempio n. 12
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);
        }