コード例 #1
0
        static IMessage MakeMessageInternal(
            TextMessageCapture capture,
            IRegex headRe,
            IRegex bodyRe,
            ref IMatch bodyMatch,
            IFieldsProcessor fieldsProcessor,
            MakeMessageFlags makeMessageFlags,
            DateTime sourceTime,
            ITimeOffsets timeOffsets,
            MessagesBuilderCallback threadLocalCallbackImpl
            )
        {
            if (bodyRe != null)
            {
                if (!bodyRe.Match(capture.BodyBuffer, capture.BodyIndex, capture.BodyLength, ref bodyMatch))
                {
                    return(null);
                }
            }

            int idx = 0;

            Group[] groups;

            fieldsProcessor.Reset();
            fieldsProcessor.SetSourceTime(sourceTime);
            fieldsProcessor.SetPosition(capture.BeginPosition);
            fieldsProcessor.SetTimeOffsets(timeOffsets);

            groups = capture.HeaderMatch.Groups;
            for (int i = 1; i < groups.Length; ++i)
            {
                var g = groups[i];
                fieldsProcessor.SetInputField(idx++, new StringSlice(capture.HeaderBuffer, g.Index, g.Length));
            }

            if (bodyRe != null)
            {
                groups = bodyMatch.Groups;
                for (int i = 1; i < groups.Length; ++i)
                {
                    var g = groups[i];
                    fieldsProcessor.SetInputField(idx++, new StringSlice(capture.BodyBuffer, g.Index, g.Length));
                }
            }
            else
            {
                fieldsProcessor.SetInputField(idx++, new StringSlice(capture.BodyBuffer, capture.BodyIndex, capture.BodyLength));
            }

            threadLocalCallbackImpl.SetCurrentPosition(capture.BeginPosition, capture.EndPosition);

            IMessage ret;

            ret = fieldsProcessor.MakeMessage(threadLocalCallbackImpl, makeMessageFlags);

            ret.SetRawText(StringSlice.Concat(capture.MessageHeaderSlice, capture.MessageBodySlice).Trim());

            return(ret);
        }
コード例 #2
0
ファイル: XmlLogReader.cs プロジェクト: pnelson786/logjoint
        static IMessage MakeMessageInternal(TextMessageCapture capture, XmlFormatInfo formatInfo, IRegex bodyRe, ref IMatch bodyReMatch,
                                            MessagesBuilderCallback callback, XsltArgumentList transformArgs, DateTime sourceTime, ITimeOffsets timeOffsets)
        {
            int nrOfSequentialFailures    = 0;
            int maxNrOfSequentialFailures = 10;

            for (; ;)
            {
                StringBuilder messageBuf = new StringBuilder();
                messageBuf.AppendFormat("<root {0}>", formatInfo.NSDeclaration.ToString());
                messageBuf.Append(capture.HeaderBuffer, capture.HeaderMatch.Index, capture.HeaderMatch.Length);
                if (bodyRe != null)
                {
                    if (!bodyRe.Match(capture.BodyBuffer, capture.BodyIndex, capture.BodyLength, ref bodyReMatch))
                    {
                        return(null);
                    }
                    messageBuf.Append(capture.BodyBuffer, bodyReMatch.Index, bodyReMatch.Length);
                }
                else
                {
                    messageBuf.Append(capture.BodyBuffer, capture.BodyIndex, capture.BodyLength);
                }
                messageBuf.Append("</root>");

                callback.SetCurrentPosition(capture.BeginPosition, capture.EndPosition);

                //this.owner.xslExt.SetSourceTime(this.owner.MediaLastModified); todo?

                string messageStr = messageBuf.ToString();

                using (FactoryWriter factoryWriter = new FactoryWriter(callback, timeOffsets))
                    using (XmlReader xmlReader = XmlReader.Create(new StringReader(messageStr), xmlReaderSettings))
                    {
                        try
                        {
                            if (formatInfo.IsNativeFormat)
                            {
                                factoryWriter.WriteNode(xmlReader, false);
                            }
                            else
                            {
                                formatInfo.Transform.Transform(xmlReader, transformArgs, factoryWriter);
                            }
                            nrOfSequentialFailures = 0;
                        }
                        catch (XmlException)
                        {
                            if (capture.IsLastMessage)
                            {
                                // There might be incomplete XML at the end of the stream. Ignore it.
                                return(null);
                            }
                            else
                            {
                                if (nrOfSequentialFailures < maxNrOfSequentialFailures)
                                {
                                    ++nrOfSequentialFailures;
                                    // Try to parse the next messsage if it's not the end of the stream
                                    continue;
                                }
                                else
                                {
                                    throw;
                                }
                            }
                        }

                        var ret = factoryWriter.GetOutput();
                        if (ret == null)
                        {
                            throw new XsltException(
                                      "Normalization XSLT produced no output");
                        }

                        if (formatInfo.ViewOptions.RawViewAllowed)
                        {
                            ret.SetRawText(StringSlice.Concat(capture.MessageHeaderSlice, capture.MessageBodySlice).Trim());
                        }

                        if (formatInfo.ViewOptions.WrapLineLength.HasValue)
                        {
                            ret.WrapsTexts(formatInfo.ViewOptions.WrapLineLength.Value);
                        }

                        return(ret);
                    }
            }
        }
コード例 #3
0
        static IMessage MakeMessageInternal(TextMessageCapture capture, JsonFormatInfo formatInfo, IRegex bodyRe, ref IMatch bodyReMatch,
                                            MessagesBuilderCallback callback, DateTime sourceTime, ITimeOffsets timeOffsets)
        {
            StringBuilder messageBuf = new StringBuilder();

            messageBuf.Append(capture.HeaderBuffer, capture.HeaderMatch.Index, capture.HeaderMatch.Length);
            if (bodyRe != null)
            {
                if (!bodyRe.Match(capture.BodyBuffer, capture.BodyIndex, capture.BodyLength, ref bodyReMatch))
                {
                    return(null);
                }
                messageBuf.Append(capture.BodyBuffer, bodyReMatch.Index, bodyReMatch.Length);
            }
            else
            {
                messageBuf.Append(capture.BodyBuffer, capture.BodyIndex, capture.BodyLength);
            }

            callback.SetCurrentPosition(capture.BeginPosition, capture.EndPosition);

            string messageStr = messageBuf.ToString();

            var transfromed = JsonTransformer.Transform(
                formatInfo.Transform.DeepClone() as JObject,
                JObject.Parse(messageStr)
                );
            var      d = transfromed.Property("d")?.Value;
            DateTime date;

            if (d != null && d.Type == JTokenType.String)
            {
                date = DateTime.Parse(d.ToString(), null, System.Globalization.DateTimeStyles.RoundtripKind);
            }
            else if (d != null && d.Type == JTokenType.Date)
            {
                date = (DateTime)((JValue)d).Value;
            }
            else
            {
                throw new Exception("Bad time property \"d\"");
            }

            var t = transfromed.Property("t")?.Value?.ToString();

            var m   = transfromed.Property("m")?.Value;
            var msg = "";

            if (m != null)
            {
                msg = m.ToString();
            }

            var s   = transfromed.Property("s")?.Value?.ToString();
            var sev = !string.IsNullOrEmpty(s) ? char.ToLower(s[0]) : 'i';

            IMessage ret = new Content(
                capture.BeginPosition, capture.EndPosition,
                callback.GetThread(new StringSlice(t ?? "")),
                new MessageTimestamp(date),
                new StringSlice(msg),
                sev == 'i' ? SeverityFlag.Info :
                sev == 'e' ? SeverityFlag.Error :
                sev == 'w' ? SeverityFlag.Warning :
                SeverityFlag.Info
                );

            if (formatInfo.ViewOptions.RawViewAllowed)
            {
                ret.SetRawText(StringSlice.Concat(capture.MessageHeaderSlice, capture.MessageBodySlice).Trim());
            }
            if (formatInfo.ViewOptions.WrapLineLength.HasValue)
            {
                ret.WrapsTexts(formatInfo.ViewOptions.WrapLineLength.Value);
            }

            return(ret);
        }