예제 #1
0
        public void DeserializesRecord()
        {
            const string lex = @"
                timestamp   [0-9]{2}[.][0-9]{2}[.][0-9]{4}[T][0-9]{2}[:][0-9]{2}[:][0-9]{2}
                level       TRACE|DEBUG|INFO|WARN|ERROR|FATAL
                message     [^\r\n]+

                %x matched_level
                %%

                <INITIAL,matched_level> {
                    {timestamp} { this.Timestamp = TextAsTimestamp(""MM.dd.yyyyTHH:mm:ss""); BEGIN(INITIAL); }
                    {level} { this.Level = yytext; BEGIN(matched_level); }
                }
                <matched_level>{message} { this.Message = yytext; BEGIN(INITIAL); }
                ";

            var compiler = new LexCompiler {Diagnostics = Console.Out};
            var scanners = compiler.Compile("%%", lex);
            var taskScheduler = new TestTaskScheduler();

            var format = new LexLogFormat {
                RecordsScannerType = scanners.RecordsScannerType,
                TaskScheduler = taskScheduler
            };

            var record = format.DeserializeRecord(
                new ArraySegment<byte>(Encoding.UTF8.GetBytes("01.01.2012T15:41:23 DEBUG Hello world!\r\n")));

            taskScheduler.ExecuteAll();

            Assert.Equal(LogLevel.Debug, record.Level);
            Assert.Equal(" Hello world!", record.Message);
        }
예제 #2
0
        public void ReadsSegments()
        {
            var stream = CreateStream("01.01.2012T15:41:23 DEBUG Hello world!\r\n02.01.2012T10:23:03 WARN Bye bye!");

            const string lex = @"
                start   [0-9]{2}[.][0-9]{2}[.][0-9]{4}[T][0-9]{2}[:][0-9]{2}[:][0-9]{2}
                end     \r\n
                %%
                {start} BeginSegment();
                {end} EndSegment();
                <<EOF>> EndSegment();
                ";

            var compiler = new LexCompiler {Diagnostics = Console.Out};

            var scanners = compiler.Compile(lex, "%%");

            Assert.True(scanners.Success);

            var taskScheduler = new TestTaskScheduler();

            var format = new LexLogFormat {
                SegmentsScannerType = scanners.SegmentsScannerType,
                Diagnostics = Console.Out,
                TaskScheduler = taskScheduler
            };

            var segments = new List<RecordSegment>();
            var subject = new Subject<RecordSegment>();

            subject.Subscribe(segments.Add);

            format.ReadSegments(subject, stream, CancellationToken.None);

            taskScheduler.ExecuteAll();

            var segment = segments.FirstOrDefault();

            Assert.NotNull(segment);

            stream.Position = segment.Offset;

            var buffer = new byte[segment.Length];

            stream.Read(buffer, 0, buffer.Length);

            var str = Encoding.UTF8.GetString(buffer);

            Assert.Equal("01.01.2012T15:41:23 DEBUG Hello world!", str);
        }
예제 #3
0
        private static ILogFormat Compile(LexPreset preset)
        {
            var compiler = new LexCompiler();

            var directory = GetCacheDirectory();

            LexCompiler.LexFormatScanners result;

            var presetAssembly = GetCachedAssembly(preset, directory);

            if (directory.Exists)
            {
                if (presetAssembly.Exists)
                {
                    using (var fileStream = presetAssembly.OpenRead())
                        result = compiler.LoadCompiled(fileStream);

                    if (result.Success)
                    {
                        return new LexLogFormat {
                                   SegmentsScannerType = result.SegmentsScannerType,
                                   RecordsScannerType  = result.RecordsScannerType
                        }
                    }
                    ;
                }
            }
            else
            {
                directory.Create();
            }

            using (var fileStream = presetAssembly.Create())
                result = compiler.Compile(
                    string.Concat(preset.CommonCode, Environment.NewLine, preset.SegmentCode),
                    string.Concat(preset.CommonCode, Environment.NewLine, preset.RecordCode),
                    fileStream);

            if (result.Success)
            {
                return new LexLogFormat {
                           SegmentsScannerType = result.SegmentsScannerType,
                           RecordsScannerType  = result.RecordsScannerType
                }
            }
            ;

            return(null);
        }
예제 #4
0
        private static ILogFormat Compile(LexPreset preset)
        {
            var compiler = new LexCompiler();

            var directory = GetCacheDirectory();

            LexCompiler.LexFormatScanners result;

            var presetAssembly = GetCachedAssembly(preset, directory);

            if (directory.Exists) {
                if (presetAssembly.Exists) {
                    using (var fileStream = presetAssembly.OpenRead())
                        result = compiler.LoadCompiled(fileStream);

                    if (result.Success)
                        return new LexLogFormat {
                            SegmentsScannerType = result.SegmentsScannerType,
                            RecordsScannerType = result.RecordsScannerType
                        };
                }
            } else
                directory.Create();

            using (var fileStream = presetAssembly.Create())
                result = compiler.Compile(
                    string.Concat(preset.CommonCode, Environment.NewLine, preset.SegmentCode),
                    string.Concat(preset.CommonCode, Environment.NewLine, preset.RecordCode),
                    fileStream);

            if (result.Success)
                return new LexLogFormat {
                    SegmentsScannerType = result.SegmentsScannerType,
                    RecordsScannerType = result.RecordsScannerType
                };

            return null;
        }
예제 #5
0
        public LexPresetViewModel() {
            this.CommonCode = new TextDocument();
            this.SegmentCode = new TextDocument();
            this.RecordCode = new TextDocument();

            this.SegmentCodeCompletion = new LexCodeCompletionData[0];
            this.RecordCodeCompletion = new LexCodeCompletionData[0];

            this.CommonCode.TextChanged += (sender, args) => {
                this.IsCompiled = false;
                this.IsChanged = true;
                this.SegmentCodeCompletion = this.CreateCodeCompletion(this.SegmentCode.Text);
                this.RecordCodeCompletion = this.CreateCodeCompletion(this.RecordCode.Text);
            };

            this.SegmentCode.TextChanged += (sender, args) => {
                this.IsCompiled = false;
                this.IsChanged = true;
                this.SegmentCodeCompletion = this.CreateCodeCompletion(this.SegmentCode.Text);
            };

            this.RecordCode.TextChanged += (sender, args) => {
                this.IsCompiled = false;
                this.IsChanged = true;
                this.RecordCodeCompletion = this.CreateCodeCompletion(this.RecordCode.Text);
            };

            this.RunCommand = new RelayCommand(this.Preview, () => this.isBusy == false);

            this.SaveAndCloseCommand = new RelayCommand(
                () => EditCompleted(), 
                () => this.IsCompiled && !this.IsBusy);

            if (this.IsInDesignMode) {
                this.Name = "Test preset";

                this.CommonCode.Text =
                    "timestamp [^;\\r\\n]+\n" +
                    "level     [^;\\r\\n]+\n" +
                    "logger    [^;\\r\\n]+\n" +
                    "message   [^;\\r\\n]+\n" +
                    "exception [^;\\r\\n]*";

                this.SegmentCode.Text =
                    "record {timestamp}[;]{message}[;]{logger}[;]{level}[;]{exception}\\r\\n\n" +
                    "%%\n" +
                    "{record} Segment();";

                this.RecordCode.Text =
                    "%x MATCHED_TIMESTAMP\n" +
                    "%x MATCHED_MESSAGE\n" +
                    "%x MATCHED_LEVEL\n" +
                    "%x MATCHED_LOGGER\n" +
                    "%%\n" +
                    "<INITIAL>{timestamp} Timestamp = yytext; BEGIN(MATCHED_TIMESTAMP);\n" +
                    "<MATCHED_TIMESTAMP>{message} this.Message = yytext; BEGIN(MATCHED_MESSAGE);\n" +
                    "<MATCHED_MESSAGE>{logger} this.Logger = yytext; BEGIN(MATCHED_LOGGER);\n" +
                    "<MATCHED_LOGGER>{level} this.Level = yytext; BEGIN(MATCHED_LEVEL);\n" +
                    "<MATCHED_LEVEL>{exception} this.Exception = yytext; BEGIN(INITIAL);";
                return;
            }

            this.format = new LexLogFormat();
            this.compiler = new LexCompiler();

            this.Name = "(Current Preset)";
        }
예제 #6
0
        public LexPresetViewModel()
        {
            this.CommonCode  = new TextDocument();
            this.SegmentCode = new TextDocument();
            this.RecordCode  = new TextDocument();

            this.SegmentCodeCompletion = new LexCodeCompletionData[0];
            this.RecordCodeCompletion  = new LexCodeCompletionData[0];

            this.CommonCode.TextChanged += (sender, args) => {
                this.IsCompiled            = false;
                this.IsChanged             = true;
                this.SegmentCodeCompletion = this.CreateCodeCompletion(this.SegmentCode.Text);
                this.RecordCodeCompletion  = this.CreateCodeCompletion(this.RecordCode.Text);
            };

            this.SegmentCode.TextChanged += (sender, args) => {
                this.IsCompiled            = false;
                this.IsChanged             = true;
                this.SegmentCodeCompletion = this.CreateCodeCompletion(this.SegmentCode.Text);
            };

            this.RecordCode.TextChanged += (sender, args) => {
                this.IsCompiled           = false;
                this.IsChanged            = true;
                this.RecordCodeCompletion = this.CreateCodeCompletion(this.RecordCode.Text);
            };

            this.RunCommand = new RelayCommand(this.Preview, () => this.isBusy == false);

            this.SaveAndCloseCommand = new RelayCommand(
                () => EditCompleted(),
                () => this.IsCompiled && !this.IsBusy);

            if (this.IsInDesignMode)
            {
                this.Name = "Test preset";

                this.CommonCode.Text =
                    "timestamp [^;\\r\\n]+\n" +
                    "level     [^;\\r\\n]+\n" +
                    "logger    [^;\\r\\n]+\n" +
                    "message   [^;\\r\\n]+\n" +
                    "exception [^;\\r\\n]*";

                this.SegmentCode.Text =
                    "record {timestamp}[;]{message}[;]{logger}[;]{level}[;]{exception}\\r\\n\n" +
                    "%%\n" +
                    "{record} Segment();";

                this.RecordCode.Text =
                    "%x MATCHED_TIMESTAMP\n" +
                    "%x MATCHED_MESSAGE\n" +
                    "%x MATCHED_LEVEL\n" +
                    "%x MATCHED_LOGGER\n" +
                    "%%\n" +
                    "<INITIAL>{timestamp} Timestamp = yytext; BEGIN(MATCHED_TIMESTAMP);\n" +
                    "<MATCHED_TIMESTAMP>{message} this.Message = yytext; BEGIN(MATCHED_MESSAGE);\n" +
                    "<MATCHED_MESSAGE>{logger} this.Logger = yytext; BEGIN(MATCHED_LOGGER);\n" +
                    "<MATCHED_LOGGER>{level} this.Level = yytext; BEGIN(MATCHED_LEVEL);\n" +
                    "<MATCHED_LEVEL>{exception} this.Exception = yytext; BEGIN(INITIAL);";
                return;
            }

            this.format   = new LexLogFormat();
            this.compiler = new LexCompiler();

            this.Name = "(Current Preset)";
        }