Esempio n. 1
0
 public TimestampTokenRenderer(KonsoleTheme theme, PropertyToken token, IFormatProvider formatProvider, ConcurrentWriter concurrentWriter)
 {
     _theme            = theme;
     _token            = token;
     _formatProvider   = formatProvider;
     _concurrentWriter = concurrentWriter;
 }
Esempio n. 2
0
        /// <summary>
        /// Writes log events to <see cref="System.Console"/>.
        /// </summary>
        /// <param name="sinkConfiguration">Logger sink configuration.</param>
        /// <param name="restrictedToMinimumLevel">The minimum level for
        /// events passed through the sink. Ignored when <paramref name="levelSwitch"/> is specified.</param>
        /// <param name="outputTemplate">A message template describing the format used to write to the sink.
        /// The default is <code>"[{Timestamp:HH:mm:ss} {Level:u3}] {Message:lj}{NewLine}{Exception}"</code>.</param>
        /// <param name="syncRoot">An object that will be used to `lock` (sync) access to the console output. If you specify this, you
        /// will have the ability to lock on this object, and guarantee that the console sink will not be about to output anything while
        /// the lock is held.</param>
        /// <param name="formatProvider">Supplies culture-specific formatting information, or null.</param>
        /// <param name="levelSwitch">A switch allowing the pass-through minimum level
        /// to be changed at runtime.</param>
        /// <param name="standardErrorFromLevel">Specifies the level at which events will be written to standard error.</param>
        /// <param name="theme">The theme to apply to the styled output. If not specified,
        /// uses <see cref="DefaultKonsoleTheme.Literate"/>.</param>
        /// <param name="applyThemeToRedirectedOutput">Applies the selected or default theme even when output redirection is detected.</param>
        /// <returns>Configuration object allowing method chaining.</returns>
        public static LoggerConfiguration Konsole(
            this LoggerSinkConfiguration sinkConfiguration,
            LogEventLevel restrictedToMinimumLevel = LevelAlias.Minimum,
            string outputTemplate                = DefaultConsoleOutputTemplate,
            IFormatProvider formatProvider       = null,
            LoggingLevelSwitch levelSwitch       = null,
            LogEventLevel?standardErrorFromLevel = null,
            KonsoleTheme theme = null,
            bool applyThemeToRedirectedOutput = false,
            object syncRoot = null,
            ConcurrentWriter concurrentWriter = null)
        {
            if (sinkConfiguration == null)
            {
                throw new ArgumentNullException(nameof(sinkConfiguration));
            }
            if (outputTemplate == null)
            {
                throw new ArgumentNullException(nameof(outputTemplate));
            }

            concurrentWriter ??= new Window(Window.OpenBox("Logs")).Concurrent();

            var appliedTheme = !applyThemeToRedirectedOutput && (System.Console.IsOutputRedirected || System.Console.IsErrorRedirected) ?
                               KonsoleTheme.None :
                               theme ?? DefaultKonsoleThemes.Literate;

            syncRoot ??= DefaultSyncRoot;

            var formatter = new OutputTemplateRenderer(appliedTheme, outputTemplate, formatProvider, concurrentWriter);

            return(sinkConfiguration.Sink(new KonsoleSink(appliedTheme, formatter, standardErrorFromLevel, syncRoot, concurrentWriter), restrictedToMinimumLevel, levelSwitch));
        }
Esempio n. 3
0
 public ThemedMessageTemplateRenderer(KonsoleTheme theme, ThemedValueFormatter valueFormatter, bool isLiteral, ConcurrentWriter concurrentWriter)
 {
     _theme                  = theme ?? throw new ArgumentNullException(nameof(theme));
     _valueFormatter         = valueFormatter;
     _isLiteral              = isLiteral;
     _concurrentWriter       = concurrentWriter;
     _unthemedValueFormatter = valueFormatter.SwitchTheme(NoTheme);
 }
Esempio n. 4
0
 public KonsoleSink(
     KonsoleTheme theme,
     ITextFormatter formatter,
     LogEventLevel?standardErrorFromLevel,
     object syncRoot, ConcurrentWriter concurrentWriter)
 {
     _standardErrorFromLevel = standardErrorFromLevel;
     _theme            = theme ?? throw new ArgumentNullException(nameof(theme));
     _formatter        = formatter;
     _syncRoot         = syncRoot ?? throw new ArgumentNullException(nameof(syncRoot));
     _concurrentWriter = concurrentWriter;
     _writer           = new KonsoleTextWriter(_concurrentWriter);
 }
Esempio n. 5
0
        /// <inheritdoc/>
        public override int Set(TextWriter output, KonsoleThemeStyle style, ConcurrentWriter concurrentWriter)
        {
            if (Styles.TryGetValue(style, out var wcts))
            {
                if (wcts.Foreground.HasValue)
                {
                    concurrentWriter.ForegroundColor = wcts.Foreground.Value;
                }
                if (wcts.Background.HasValue)
                {
                    concurrentWriter.BackgroundColor = wcts.Background.Value;
                }
            }

            return(0);
        }
Esempio n. 6
0
        /// <summary>
        /// Writes log events to <see cref="System.Console"/>.
        /// </summary>
        /// <param name="sinkConfiguration">Logger sink configuration.</param>
        /// <param name="formatter">Controls the rendering of log events into text, for example to log JSON. To
        /// control plain text formatting, use the overload that accepts an output template.</param>
        /// <param name="syncRoot">An object that will be used to `lock` (sync) access to the console output. If you specify this, you
        /// will have the ability to lock on this object, and guarantee that the console sink will not be about to output anything while
        /// the lock is held.</param>
        /// <param name="restrictedToMinimumLevel">The minimum level for
        /// events passed through the sink. Ignored when <paramref name="levelSwitch"/> is specified.</param>
        /// <param name="levelSwitch">A switch allowing the pass-through minimum level
        /// to be changed at runtime.</param>
        /// <param name="standardErrorFromLevel">Specifies the level at which events will be written to standard error.</param>
        /// <returns>Configuration object allowing method chaining.</returns>
        public static LoggerConfiguration Konsole(
            this LoggerSinkConfiguration sinkConfiguration,
            ITextFormatter formatter,
            LogEventLevel restrictedToMinimumLevel = LevelAlias.Minimum,
            LoggingLevelSwitch levelSwitch         = null,
            LogEventLevel?standardErrorFromLevel   = null,
            object syncRoot = null,
            ConcurrentWriter concurrentWriter = null)
        {
            if (sinkConfiguration == null)
            {
                throw new ArgumentNullException(nameof(sinkConfiguration));
            }
            if (formatter == null)
            {
                throw new ArgumentNullException(nameof(formatter));
            }

            concurrentWriter ??= new Window(Window.OpenBox("Logs")).Concurrent();
            syncRoot ??= DefaultSyncRoot;
            return(sinkConfiguration.Sink(new KonsoleSink(KonsoleTheme.None, formatter, standardErrorFromLevel, syncRoot, concurrentWriter), restrictedToMinimumLevel, levelSwitch));
        }
Esempio n. 7
0
 public StyleReset(KonsoleTheme theme, TextWriter output, ConcurrentWriter concurrentWriter)
 {
     _theme            = theme;
     _output           = output;
     _concurrentWriter = concurrentWriter;
 }
Esempio n. 8
0
        public PropertiesTokenRenderer(KonsoleTheme theme, PropertyToken token, MessageTemplate outputTemplate, IFormatProvider formatProvider, ConcurrentWriter concurrentWriter)
        {
            _outputTemplate = outputTemplate;
            _theme          = theme ?? throw new ArgumentNullException(nameof(theme));
            _token          = token ?? throw new ArgumentNullException(nameof(token));
            var isJson = false;

            if (token.Format != null)
            {
                for (var i = 0; i < token.Format.Length; ++i)
                {
                    if (token.Format[i] == 'j')
                    {
                        isJson = true;
                    }
                }
            }

            _valueFormatter = isJson
                ? (ThemedValueFormatter) new ThemedJsonValueFormatter(theme, formatProvider, concurrentWriter)
                : new ThemedDisplayValueFormatter(theme, formatProvider, concurrentWriter);
        }
Esempio n. 9
0
        public OutputTemplateRenderer(KonsoleTheme theme, string outputTemplate, IFormatProvider formatProvider, ConcurrentWriter concurrentWriter)
        {
            if (outputTemplate == null)
            {
                throw new ArgumentNullException(nameof(outputTemplate));
            }
            var template = new MessageTemplateParser().Parse(outputTemplate);

            var renderers = new List <OutputTemplateTokenRenderer>();

            foreach (var token in template.Tokens)
            {
                if (token is TextToken tt)
                {
                    renderers.Add(new TextTokenRenderer(theme, tt.Text, concurrentWriter));
                    continue;
                }

                var pt = (PropertyToken)token;
                if (pt.PropertyName == OutputProperties.LevelPropertyName)
                {
                    renderers.Add(new LevelTokenRenderer(theme, pt, concurrentWriter));
                }
                else if (pt.PropertyName == OutputProperties.NewLinePropertyName)
                {
                    renderers.Add(new NewLineTokenRenderer(pt.Alignment));
                }
                else if (pt.PropertyName == OutputProperties.ExceptionPropertyName)
                {
                    renderers.Add(new ExceptionTokenRenderer(theme, pt, concurrentWriter));
                }
                else if (pt.PropertyName == OutputProperties.MessagePropertyName)
                {
                    renderers.Add(new MessageTemplateOutputTokenRenderer(theme, pt, formatProvider, concurrentWriter));
                }
                else if (pt.PropertyName == OutputProperties.TimestampPropertyName)
                {
                    renderers.Add(new TimestampTokenRenderer(theme, pt, formatProvider, concurrentWriter));
                }
                else if (pt.PropertyName == "Properties")
                {
                    renderers.Add(new PropertiesTokenRenderer(theme, pt, template, formatProvider, concurrentWriter));
                }
                else
                {
                    renderers.Add(new EventPropertyTokenRenderer(theme, pt, formatProvider, concurrentWriter));
                }
            }

            _renderers = renderers.ToArray();
        }
Esempio n. 10
0
        internal StyleReset Apply(TextWriter output, KonsoleThemeStyle style, ref int invisibleCharacterCount, ConcurrentWriter concurrentWriter)
        {
            invisibleCharacterCount += Set(output, style, concurrentWriter);
            invisibleCharacterCount += ResetCharCount;

            return(new StyleReset(this, output, concurrentWriter));
        }
Esempio n. 11
0
 /// <summary>
 /// Begin a span of text in the specified <paramref name="style"/>.
 /// </summary>
 /// <param name="output">Output destination.</param>
 /// <param name="style">Style to apply.</param>
 /// <param name="concurrentWriter"></param>
 /// <returns> The number of characters written to <paramref name="output"/>. </returns>
 public abstract int Set(TextWriter output, KonsoleThemeStyle style, ConcurrentWriter concurrentWriter);
Esempio n. 12
0
 public ThemedJsonValueFormatter(KonsoleTheme theme, IFormatProvider formatProvider, ConcurrentWriter concurrentWriter)
     : base(theme, concurrentWriter)
 {
     _displayFormatter = new ThemedDisplayValueFormatter(theme, formatProvider, concurrentWriter);
     _formatProvider   = formatProvider;
 }
Esempio n. 13
0
 /// <inheritdoc/>
 public override void Reset(TextWriter output, ConcurrentWriter concurrentWriter)
 {
     concurrentWriter.ForegroundColor = ConsoleColor.White;
     concurrentWriter.BackgroundColor = ConsoleColor.Black;
 }
Esempio n. 14
0
 public KonsoleTextWriter(ConcurrentWriter concurrentWriter)
 {
     _concurrentWriter = concurrentWriter;
 }
Esempio n. 15
0
 public LevelTokenRenderer(KonsoleTheme theme, PropertyToken levelToken, ConcurrentWriter concurrentWriter)
 {
     _theme            = theme;
     _levelToken       = levelToken;
     _concurrentWriter = concurrentWriter;
 }
        public MessageTemplateOutputTokenRenderer(KonsoleTheme theme, PropertyToken token, IFormatProvider formatProvider, ConcurrentWriter concurrentWriter)
        {
            _theme = theme ?? throw new ArgumentNullException(nameof(theme));
            _token = token ?? throw new ArgumentNullException(nameof(token));
            bool isLiteral = false, isJson = false;

            if (token.Format != null)
            {
                for (var i = 0; i < token.Format.Length; ++i)
                {
                    if (token.Format[i] == 'l')
                    {
                        isLiteral = true;
                    }
                    else if (token.Format[i] == 'j')
                    {
                        isJson = true;
                    }
                }
            }

            var valueFormatter = isJson
                ? (ThemedValueFormatter) new ThemedJsonValueFormatter(theme, formatProvider, concurrentWriter)
                : new ThemedDisplayValueFormatter(theme, formatProvider, concurrentWriter);

            _renderer = new ThemedMessageTemplateRenderer(theme, valueFormatter, isLiteral, concurrentWriter);
        }
Esempio n. 17
0
 public override int Set(TextWriter output, KonsoleThemeStyle style, ConcurrentWriter concurrentWriter) => 0;
Esempio n. 18
0
 /// <summary>
 /// Reset the output to un-styled colors.
 /// </summary>
 /// <param name="output">Output destination.</param>
 /// <param name="concurrentWriter"></param>
 public abstract void Reset(TextWriter output, ConcurrentWriter concurrentWriter);
Esempio n. 19
0
 public override void Reset(TextWriter output, ConcurrentWriter concurrentWriter)
 {
 }
Esempio n. 20
0
 public ExceptionTokenRenderer(KonsoleTheme theme, PropertyToken pt, ConcurrentWriter concurrentWriter)
 {
     _theme            = theme;
     _concurrentWriter = concurrentWriter;
 }
Esempio n. 21
0
 protected ThemedValueFormatter(KonsoleTheme theme, ConcurrentWriter concurrentWriter)
 {
     _theme            = theme ?? throw new ArgumentNullException(nameof(theme));
     _concurrentWriter = concurrentWriter;
 }
Esempio n. 22
0
        /// <summary>
        /// Split a large PGN file into smaller batches.
        /// </summary>
        /// <param name="file">The PGN file.</param>
        /// <param name="batchSize">The batch size.</param>
        /// <param name="skipParse">Skip parsing, just split.</param>
        /// <param name="destPath">The output directory.</param>
        static void Main(
            string file,
            int batchSize   = 1000,
            bool skipParse  = false,
            string destPath = null)
        {
            var console = new ConcurrentWriter();

            string realDestDir = null;

            if (destPath == null)
            {
                realDestDir = Path.Join(Path.GetDirectoryName(file), "batches");
                Directory.CreateDirectory(realDestDir);
            }
            else if (!Directory.Exists(destPath))
            {
                console.WriteLine(DarkYellow, $"Destination directory '{destPath}' does not exist, creating...");
                try
                {
                    Directory.CreateDirectory(destPath);
                    realDestDir = destPath;
                }
                catch (Exception e)
                {
                    console.WriteLine(Red, $"Unable to create directory '{destPath}': {e.Message}");
                    return;
                }
            }
            else if (!IsDirectoryEmpty(destPath))
            {
                console.WriteLine(Red, $"Destination directory '{destPath}' is not empty.");
                return;
            }

            if (!File.Exists(file))
            {
                console.WriteLine(Red, $"Input PGN file '{file}' does not exist.");
                return;
            }

            PgnGameStream gameStream;

            try
            {
                gameStream = CreateGameStream(file);
            }
            catch (ArgumentException)
            {
                console.WriteLine(Red, $"Input PGN file '{file}' does not exist");
                return;
            }

            int batchCount = 0;

            using (gameStream)
            {
                var batch = new List <string>();
                while (!gameStream.EndOfStream)
                {
                    if (skipParse)
                    {
                        batch.Add(gameStream.GetNextGameString());
                    }
                    else
                    {
                        var nextGame = gameStream.ParseNextGame();
                        batch.Add(nextGame.OriginalPgnText);
                    }


                    if (batch.Count >= batchSize)
                    {
                        string fileName = Path.Join(realDestDir, $"batch{batchCount}.pgn");
                        WriteBatch(batch, fileName);
                        batchCount++;
                        batch.Clear();
                        GC.Collect();
                    }
                }

                if (batch.Count > 0)
                {
                    string fileName = Path.Join(destPath, $"batch{batchCount}.pgn");
                    WriteBatch(batch, fileName);
                    batch.Clear();
                    GC.Collect();
                }
            }
        }
Esempio n. 23
0
 public TextTokenRenderer(KonsoleTheme theme, string text, ConcurrentWriter concurrentWriter)
 {
     _theme            = theme;
     _text             = text;
     _concurrentWriter = concurrentWriter;
 }