Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

Tagged as

Collect and Compare Log Statistics using LogJoin

, 22 Oct 2013 BSD
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

Share

About the Author

Yuriy Nelipovich
Software Developer CactusSoft
Belarus Belarus
No Biography provided

| Advertise | Privacy | Mobile
Web02 | 2.8.141022.2 | Last Updated 22 Oct 2013
Article Copyright 2013 by Yuriy Nelipovich
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid