Beispiel #1
0
        public File(string file, TailPbl bl, int fileIndex)
        {
            if (string.IsNullOrEmpty(file))
            {
                throw new ArgumentException(@"Param cannot be empty", nameof(file));
            }
            _bl        = bl ?? throw new ArgumentNullException(nameof(bl));
            _file      = file;
            _fileIndex = fileIndex;
            _logicalLinesHistoryQueue = new LogicalLinesHistoryQueue(Math.Max(1, Configs.ContextBefore));
            _startFromNum             = Configs.LinesStartNumber;

            if (_file == Constants.CONSOLE_FILENAME)
            {
                _fileType = FileTypes.Console;
            }
            else if (ArchiveSupport.TryGetArchivePath(file, out var archive, out var finalFile) &&
                     ArchiveSupport.IsValidArchive(archive))
            {
                _fileType = string.IsNullOrWhiteSpace(finalFile)
                            ? FileTypes.Archive
                            : FileTypes.ArchivedFile;
            }

            UpdateFileInfo();
            ResetCounters();
        }
Beispiel #2
0
 public FilesMonitorEntry(string path, TailPbl bl)
 {
     if (path == Constants.CONSOLE_FILENAME)
     {
         FileType = FileTypes.Console;
         Folder   = string.Empty;
         Mask     = Constants.CONSOLE_FILENAME;
     }
     else if (ArchiveSupport.TryGetArchivePath(path, out var archive, out var file))
     {
         FileType = FileTypes.Archive;
         Folder   = archive;
         Mask     = file;
     }
Beispiel #3
0
        private void ShowError(string error)
        {
            bool showError;

            lock (_errorShownLock)
            {
                showError = !_errorShown;
                if (!_errorShown)
                {
                    _errorShown = true;
                }
            }
            if (showError)
            {
                error += $". [{FileName}]";
                _bl.NewLineCallback(TailPbl.GetErrorLine(error), 0);
            }
        }
Beispiel #4
0
        private static void Main(string[] args)
        {
            // reset colors on Ctrl+C
            Console.CancelKeyPress += (s, a) => ResetConsoleColors();

            NewLine();

            _bl = new TailPbl(WriteLine);
            try
            {
                _bl.ParseArgs(args);

                StartUpdatingStatus();

                _bl.StartProcess();

                if (Configs.Follow)
                {
                    Console.ReadLine();
                }

                _statusTimer.Dispose();
                UpdateStatus();
            }
            catch (TailPHelpException)
            {
                WriteMessage(TailPbl.GetHelp(), Types.None);
            }
            catch (TailPArgsException ex)
            {
                WriteMessage(ex.ToString(), Types.Error);
                WriteMessage(TailPbl.GetHelp(), Types.None);
            }
#pragma warning disable CA1031 // Do not catch general exception types
            catch (Exception ex)
#pragma warning restore CA1031 // Do not catch general exception types
            {
                WriteMessage(ex.ToString(), Types.Error);
            }
            finally
            {
                ResetConsoleColors();
            }
        }
Beispiel #5
0
        public static void Add(string path, bool follow, TailPbl bl)
        {
            var entry = new FilesMonitorEntry(path, bl);

            bool added;

            lock (MonitorEntriesLock)
            {
                added = MonitorEntries.Add(entry);
            }

            if (added)
            {
                entry.Created += Created;
                entry.Deleted += Deleted;
                entry.Changed += Changed;

                if (follow)
                {
                    entry.BeginMonitor();
                }
            }
        }
Beispiel #6
0
        private void PrintLogicalLines(LogicalLinesHistoryQueue logicalLines)
        {
            lock (_bl.PrintLock)
            {
                PrintFileName();

                while (logicalLines.Any())
                {
                    var logicalLine = logicalLines.Dequeue();

                    if (!logicalLine.IsPrinted)
                    {
                        if (Configs.IsContextUsed &&
                            Math.Abs(logicalLine.LineNumber - _lastPrintedLine) > 1)
                        {
                            _bl.PrintLogicalLine(TailPbl.GetContextDelimiter(), _fileIndex);
                        }

                        _bl.PrintLogicalLine(logicalLine, _fileIndex);
                        _lastPrintedLine = logicalLine.LineNumber;
                    }
                }
            }
        }