Click here to Skip to main content
15,884,934 members
Articles / General Programming / Regular Expressions

Collect and Compare Log Statistics using LogJoin

Rate me:
Please Sign up or sign in to vote.
4.83/5 (3 votes)
22 Oct 2013BSD4 min read 7.8K   88   5  
The LogJoin tool helps to collect any unstructured data from text files and join it to a simple table representation for easy analysis.
/*
 * Copyright (c) 2013, Yuriy Nelipovich
 * 
 * If you find this code useful or in case of any questions, suggestions
 * bug reports, donation, please email me: dev.yuriy.n@gmail.com
 */

using System;
using System.Collections.Generic;
using System.Text;

namespace LogJoin
{
    /// <summary>
    /// Sequence of text portions, each portion is composed of a queue of text lines (called frame).
    /// When moving forward, the frame is shifted by one line.
    /// </summary>
    public class MultilineInput : Input
    {
        private readonly int _frameSize;

        private readonly Queue<string> _lines;

        /// <summary>
        /// Constructs new input
        /// </summary>
        /// <param name="filePaths">Sequence of files to read</param>
        /// <param name="frameSize">Maximum size of text frame (i.e. maximum count of lines)</param>
        public MultilineInput(string[] filePaths, int frameSize) : base(filePaths)
        {
            if (frameSize <= 0)
                throw new ArgumentOutOfRangeException("frameSize",
                                                      string.Format("Lines count {0} is invalid", frameSize));
            this._frameSize = frameSize;
            this._lines = new Queue<string>(frameSize);
        }

        /// <summary>
        /// Moves the input to the start position
        /// </summary>
        public override void ResetPosition()
        {
            base.ResetPosition();
            this._lines.Clear();
        }

        /// <summary>
        /// Rads the next text line, pushes it to the frame and moves to the next position
        /// </summary>
        /// <returns>Frame contents</returns>
        public override string ReadTextPortion()
        {
            if (this._lines.Count == 0)
                this.FillFrameWithInitialText();
            else
                this.ReadNextLineAndRoll();
            if (this._lines.Count == 0)
                return null;
            var result = new StringBuilder();
            foreach (var line in this._lines)
            {
                result.AppendLine(line);
            }
            return result.ToString();
        }

        /// <summary>
        /// Fills the frame with initial text
        /// </summary>
        private void FillFrameWithInitialText()
        {
            string nextLine;
            do
            {
                nextLine = base.ReadTextPortion();
                if (nextLine != null)
                    this._lines.Enqueue(nextLine);
            }
            while (nextLine != null && this._lines.Count < this._frameSize);
        }

        /// <summary>
        /// Rads the next text line, pushes it to the frame
        /// and ensures that the frame size is not exceeded
        /// </summary>
        private void ReadNextLineAndRoll()
        {
            var next = base.ReadTextPortion();
            if (next != null)
                this._lines.Enqueue(next);
            if (this._lines.Count > 0)
                this._lines.Dequeue();
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The BSD License


Written By
Software Developer CactusSoft
Belarus Belarus
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions