Ejemplo n.º 1
0
 private void DetermineDateTimePart(string line)
 {
     foreach (var parser in _subParsers)
     {
         for (var firstIndex = 0; firstIndex < line.Length; ++firstIndex)
         {
             for (var lastIndex = firstIndex + parser.MinimumLength; lastIndex <= line.Length; ++lastIndex)
             {
                 var dateTimeString = line.Substring(firstIndex, lastIndex - firstIndex);
                 try
                 {
                     DateTime unused;
                     if (parser.TryParse(dateTimeString, out unused))
                     {
                         var length = lastIndex - firstIndex;
                         DateTimeColumn    = firstIndex;
                         DateTimeLength    = length;
                         _determinedParser = parser;
                         return;
                     }
                 }
                 catch (Exception e)
                 {
                     ++_numExceptions;
                     Log.ErrorFormat("Caught unexpected exception: {0}", e);
                 }
             }
         }
     }
 }
        public void Constructor_NullParser_Throws()
        {
            ITimestampParser nullParser = null;

            Assert.Throws <ArgumentNullException>(
                () => new SubtitleTimestampsParser(nullParser));
        }
Ejemplo n.º 3
0
        /// <summary>
        ///     Initializes this parser.
        /// </summary>
        /// <param name="parser"></param>
        public NoThrowTimestampParser(ITimestampParser parser)
        {
            if (parser == null)
            {
                throw new ArgumentNullException(nameof(parser));
            }

            _parser = parser;
        }
        public SubtitleTimestampsParser(ITimestampParser timestampParser)
        {
            if (timestampParser == null)
            {
                throw new ArgumentNullException(nameof(timestampParser));
            }

            this.timestampParser = timestampParser;
        }
        internal InfluxRowContent(InfluxClient client, bool isBasedOnInterface, IEnumerable dataPoints, Func <TInfluxRow, string> getMeasurementName, InfluxWriteOptions options)
        {
            _isBasedOnInterface = isBasedOnInterface;
            _dataPoints         = dataPoints;
            _getMeasurementName = getMeasurementName;
            _options            = options;
            _timestampParser    = client.TimestampParserRegistry.FindTimestampParserOrNull <TTimestamp>();

            Headers.ContentType = TextMediaType;
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Initializes this text log file.
        /// </summary>
        /// <param name="scheduler"></param>
        /// <param name="fileName"></param>
        /// <param name="timestampParser">An optional timestamp parser that is used to find timestamps in log messages. If none is specified, then <see cref="TimestampParser"/> is used</param>
        /// <param name="translator">An optional translator that is used to translate each log line in memory. If none is specified, then log lines are displayed as they are in the file on disk</param>
        /// <param name="encoding">The encoding to use to interpet the file, if none is specified, then <see cref="Encoding.UTF8"/> is used</param>
        public TextLogFile(ITaskScheduler scheduler,
                           string fileName,
                           ITimestampParser timestampParser = null,
                           ILogLineTranslator translator    = null,
                           Encoding encoding = null)
            : base(scheduler)
        {
            if (fileName == null)
            {
                throw new ArgumentNullException(nameof(fileName));
            }

            _fileName     = fileName;
            _fullFilename = fileName;
            if (!Path.IsPathRooted(_fullFilename))
            {
                _fullFilename = Path.Combine(Directory.GetCurrentDirectory(), fileName);
            }

            if (translator != null)
            {
                _translator = new NoThrowLogLineTranslator(translator);
            }

            _entries    = new List <LogLine>();
            _properties = new LogFilePropertyList(LogFileProperties.Minimum);
            _properties.SetValue(LogFileProperties.Name, _fileName);
            _syncRoot = new object();
            _encoding = encoding ?? Encoding.UTF8;

            Log.DebugFormat("Log File '{0}' is interpreted using {1}", _fileName, _encoding.EncodingName);

            if (timestampParser != null)
            {
                _timestampParser = new NoThrowTimestampParser(timestampParser);
            }
            else
            {
                _timestampParser = new TimestampParser();
            }

            StartTask();
        }
Ejemplo n.º 7
0
        private void DetermineDateTimePart(string line)
        {
            if (SkipLine(line))
            {
                return;
            }

            int lineLength = line.Length;

            if (line.Length > MaxLineLength)
            {
                Log.WarnFormat("Line has a length ({1}) greater than {0} characters. A timestamp will only be looked for in the first {0} characters!", MaxLineLength, line.Length);
                lineLength = MaxLineLength;
            }

            foreach (var parser in _subParsers)
            {
                for (var firstIndex = 0; firstIndex < lineLength; ++firstIndex)
                {
                    for (var lastIndex = firstIndex + parser.MinimumLength; lastIndex <= lineLength; ++lastIndex)
                    {
                        var dateTimeString = line.Substring(firstIndex, lastIndex - firstIndex);
                        try
                        {
                            DateTime unused;
                            if (parser.TryParse(dateTimeString, out unused))
                            {
                                var length = lastIndex - firstIndex;
                                DateTimeColumn    = firstIndex;
                                DateTimeLength    = length;
                                _determinedParser = parser;
                                return;
                            }
                        }
                        catch (Exception e)
                        {
                            ++_numExceptions;
                            Log.ErrorFormat("Caught unexpected exception: {0}", e);
                        }
                    }
                }
            }
        }
Ejemplo n.º 8
0
        private static async Task AddValuesToInfluxSeriesByInterfaceAsync <TInfluxRow, TTimestamp>(
            InfluxSeries <TInfluxRow> influxSerie,
            SeriesResult series,
            InfluxClient client,
            string db,
            bool allowMetadataQuerying,
            InfluxRowTypeInfo <TInfluxRow> propertyMap,
            InfluxQueryOptions options,
            ITimestampParser <TTimestamp> timestampParser)
            where TInfluxRow : IInfluxRow <TTimestamp>, new()
        {
            // Values will be null, if there are no entries in the result set
            if (series.Values != null)
            {
                var precision  = options.Precision;
                var name       = series.Name;
                var columns    = series.Columns;
                var setters    = new Action <TInfluxRow, string, object> [columns.Count];
                var dataPoints = new List <TInfluxRow>();
                // Get metadata information about the measurement we are querying, as we dont know
                // which columns are tags/fields otherwise

                DatabaseMeasurementInfo meta = null;
                if (allowMetadataQuerying)
                {
                    meta = await client.GetMetaInformationAsync(db, name, options.MetadataExpiration).ConfigureAwait(false);
                }

                for (int i = 0; i < columns.Count; i++)
                {
                    var columnName = columns[i];

                    if (!allowMetadataQuerying)
                    {
                        setters[i] = (row, fieldName, value) => row.SetField(fieldName, value);
                    }
                    else if (columnName == InfluxConstants.TimeColumn)
                    {
                        setters[i] = (row, timeName, value) => row.SetTimestamp(timestampParser.ToTimestamp(options.Precision, value));
                    }
                    else if (meta.Tags.Contains(columnName))
                    {
                        setters[i] = (row, tagName, value) => row.SetTag(tagName, (string)value);
                    }
                    else
                    {
                        setters[i] = (row, fieldName, value) => row.SetField(fieldName, value);
                    }
                }

                // constructs the IInfluxRows using the IInfluxRow interface
                foreach (var values in series.Values)
                {
                    var dataPoint = new TInfluxRow();
                    propertyMap.SetMeasurementName(name, dataPoint);

                    // go through all values that are stored as a List<List<object>>
                    for (int i = 0; i < values.Count; i++)
                    {
                        var value = values[i]; // TODO: What about NULL values? Are they treated as empty strings or actual nulls?
                        if (value != null)
                        {
                            setters[i](dataPoint, columns[i], value);
                        }
                    }

                    dataPoints.Add(dataPoint);
                }

                influxSerie.Rows.AddRange(dataPoints);
            }
        }
Ejemplo n.º 9
0
        private static void AddValuesToInfluxSeriesByAttributes <TInfluxRow, TTimestamp>(
            InfluxSeries <TInfluxRow> influxSerie,
            SeriesResult series,
            InfluxRowTypeInfo <TInfluxRow> propertyMap,
            InfluxQueryOptions options,
            ITimestampParser <TTimestamp> timestampParser)
            where TInfluxRow : new()
        {
            // Values will be null, if there are no entries in the result set
            if (series.Values != null)
            {
                var dataPoints = new List <TInfluxRow>();

                var precision = options.Precision;
                var columns   = series.Columns;
                var name      = series.Name;

                // construct an array of properties based on the same indexing as the columns returned by the query
                var properties = new PropertyExpressionInfo <TInfluxRow> [columns.Count];
                for (int i = 0; i < columns.Count; i++)
                {
                    PropertyExpressionInfo <TInfluxRow> propertyInfo;
                    if (propertyMap.All.TryGetValue(columns[i], out propertyInfo))
                    {
                        properties[i] = propertyInfo;
                    }
                }

                foreach (var values in series.Values)
                {
                    // construct the data points based on the attributes
                    var dataPoint = new TInfluxRow();
                    propertyMap.SetMeasurementName(name, dataPoint);

                    for (int i = 0; i < values.Count; i++)
                    {
                        var value    = values[i]; // TODO: What about NULL values? Are they treated as empty strings or actual nulls?
                        var property = properties[i];

                        // set the value based on the property, if both the value and property is not null
                        if (property != null)
                        {
                            if (value != null)
                            {
                                if (property.Key == InfluxConstants.TimeColumn)
                                {
                                    property.SetValue(dataPoint, timestampParser.ToTimestamp(options.Precision, value));
                                }
                                else if (property.IsDateTime)
                                {
                                    property.SetValue(dataPoint, DateTime.Parse((string)value, CultureInfo.InvariantCulture, OnlyUtcStyles));
                                }
                                else if (property.IsEnum)
                                {
                                    property.SetValue(dataPoint, property.GetEnumValue(value));
                                }
                                else
                                {
                                    // will throw exception if types does not match up
                                    property.SetValue(dataPoint, Convert.ChangeType(value, property.Type, CultureInfo.InvariantCulture));
                                }
                            }
                        }
                    }

                    dataPoints.Add(dataPoint);
                }

                influxSerie.Rows.AddRange(dataPoints);
            }
        }
Ejemplo n.º 10
0
 private TextLogSource Create(string fileName,
                              ITimestampParser timestampParser = null)
 {
     return(new TextLogSource(_taskScheduler, fileName, LogFileFormats.GenericText, Encoding.Default));
 }
Ejemplo n.º 11
0
        public ILogEntryParser CreateParser(IServiceContainer services, ILogFileFormat format)
        {
            ITimestampParser timestampParser = services.TryRetrieve <ITimestampParser>() ?? new TimestampParser();

            return(new GenericTextLogEntryParser(timestampParser));
        }
 /// <summary>
 ///
 /// </summary>
 /// <param name="timestampParser"></param>
 public GenericTextLogEntryParser(ITimestampParser timestampParser)
 {
     _timestampParser = timestampParser ?? throw new ArgumentNullException(nameof(timestampParser));
     _logLevelParser  = new LogLevelParser();
     _columns         = new IColumnDescriptor[] { Core.Columns.Timestamp, Core.Columns.LogLevel };
 }
Ejemplo n.º 13
0
 /// <summary>
 ///     Initializes this parser.
 /// </summary>
 /// <param name="parser"></param>
 public NoThrowTimestampParser(ITimestampParser parser)
 {
     _parser = parser ?? throw new ArgumentNullException(nameof(parser));
 }
 private SubtitleTimestampsParser CreateParser(ITimestampParser timestampParser)
 {
     return(new SubtitleTimestampsParser(timestampParser));
 }