Example #1
0
        private static ConsoleRowHighlightingRule MakeRule(LogLevel level, ConsoleOutputColor fg)
        {
            ConsoleOutputColor  bg        = ConsoleOutputColor.Black;
            ConditionExpression condition = ConditionParser.ParseExpression("level == LogLevel." + level);

            return(new ConsoleRowHighlightingRule(condition, fg, bg));
        }
Example #2
0
        /// <summary>
        /// Writes the specified text, breaking the line at the end of the text.
        /// </summary>
        /// <param name="text">The text.</param>
        /// <param name="consoleOutputColor">Color of the console output.</param>
        public void WriteLine(string text, ConsoleOutputColor consoleOutputColor)
        {
            // Gets current console color to properly restore it after writting text.
            ConsoleColor currentConsoleColor = Console.ForegroundColor;

            Console.ForegroundColor = consoleOutputColor.ToConsoleColor();
            Console.WriteLine(text);
            Console.ForegroundColor = currentConsoleColor;
        }
Example #3
0
        private ushort ColorFromForegroundAndBackground(ushort current, ConsoleOutputColor foregroundColor, ConsoleOutputColor backgroundColor)
        {
            ushort newColor = current;

            if (foregroundColor != ConsoleOutputColor.NoChange)
            {
                newColor  = (ushort)(newColor & ~0xF);
                newColor |= (ushort )foregroundColor;
            }

            if (backgroundColor != ConsoleOutputColor.NoChange)
            {
                newColor  = (ushort)(newColor & ~0xF0);
                newColor |= (ushort)(((int)backgroundColor) << 4);
            }

            return(newColor);
        }
        /// <summary>
        /// Converts the console output color to console color.
        /// </summary>
        /// <param name="consoleOutputColor">Color of the console output.</param>
        /// <returns></returns>
        /// <exception cref="InvalidOperationException">Unrecognized console output color.</exception>
        public static ConsoleColor ToConsoleColor(this ConsoleOutputColor consoleOutputColor)
        {
            switch (consoleOutputColor)
            {
            case ConsoleOutputColor.Gray:
                return(ConsoleColor.Gray);

            case ConsoleOutputColor.Red:
                return(ConsoleColor.Red);

            case ConsoleOutputColor.Green:
                return(ConsoleColor.DarkGreen);

            case ConsoleOutputColor.Yellow:
                return(ConsoleColor.DarkYellow);

            case ConsoleOutputColor.Blue:
                return(ConsoleColor.DarkBlue);

            default:
                throw new InvalidOperationException("Unrecognized console output color.");
            }
        }
 /// <summary>
 /// Initializes a new instance of the <see cref="ConsoleWordHighlightingRule" /> class.
 /// </summary>
 /// <param name="text">The text to be matched..</param>
 /// <param name="foregroundColor">Color of the foreground.</param>
 /// <param name="backgroundColor">Color of the background.</param>
 public ConsoleWordHighlightingRule(string text, ConsoleOutputColor foregroundColor, ConsoleOutputColor backgroundColor)
 {
     _regexHelper.SearchText = text;
     ForegroundColor         = foregroundColor;
     BackgroundColor         = backgroundColor;
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="ConsoleRowHighlightingRule" /> class.
 /// </summary>
 /// <param name="condition">The condition.</param>
 /// <param name="foregroundColor">Color of the foreground.</param>
 /// <param name="backgroundColor">Color of the background.</param>
 public ConsoleRowHighlightingRule(ConditionExpression condition, ConsoleOutputColor foregroundColor, ConsoleOutputColor backgroundColor)
 {
     this.Condition = condition;
     this.ForegroundColor = foregroundColor;
     this.BackgroundColor = backgroundColor;
 }
 private static bool IsColorChange(ConsoleOutputColor targetColor, ConsoleColor oldColor)
 {
     return (targetColor != ConsoleOutputColor.NoChange) && ((ConsoleColor)targetColor != oldColor);
 }
 /// <summary>
 ///     Initializes a new instance of the <see cref="ConsoleWordHighlightingRule" /> class.
 /// </summary>
 /// <param name="text">The text to be matched..</param>
 /// <param name="foregroundColor">Color of the foreground.</param>
 /// <param name="backgroundColor">Color of the background.</param>
 public ConsoleWordHighlightingRule(string text, ConsoleOutputColor foregroundColor, ConsoleOutputColor backgroundColor)
 {
     Text = text;
     ForegroundColor = foregroundColor;
     BackgroundColor = backgroundColor;
 }
 private static bool IsColorChange(ConsoleOutputColor targetColor, ConsoleColor oldColor)
 {
     return((targetColor != ConsoleOutputColor.NoChange) && ((ConsoleColor)targetColor != oldColor));
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="ConsoleRowHighlightingRule" /> class.
 /// </summary>
 /// <param name="condition">The condition.</param>
 /// <param name="foregroundColor">Color of the foreground.</param>
 /// <param name="backgroundColor">Color of the background.</param>
 public ConsoleRowHighlightingRule(ConditionExpression condition, ConsoleOutputColor foregroundColor, ConsoleOutputColor backgroundColor)
 {
     Condition       = condition;
     ForegroundColor = foregroundColor;
     BackgroundColor = backgroundColor;
 }
Example #11
0
        private ushort ColorFromForegroundAndBackground(ushort current, ConsoleOutputColor foregroundColor, ConsoleOutputColor backgroundColor)
        {
            ushort newColor = current;

            if (foregroundColor != ConsoleOutputColor.NoChange)
            {
                newColor = (ushort)(newColor & ~0xF);
                newColor |= (ushort )foregroundColor;
            }

            if (backgroundColor != ConsoleOutputColor.NoChange)
            {
                newColor = (ushort)(newColor & ~0xF0);
                newColor |= (ushort)(((int)backgroundColor) << 4);
            }

            return newColor;
        }
Example #12
0
 /// <summary>
 /// Initializes a new instance of the <see cref="ConsoleWordHighlightingRule" /> class.
 /// </summary>
 /// <param name="text">The text to be matched..</param>
 /// <param name="foregroundColor">Color of the foreground.</param>
 /// <param name="backgroundColor">Color of the background.</param>
 public ConsoleWordHighlightingRule(string text, ConsoleOutputColor foregroundColor, ConsoleOutputColor backgroundColor)
 {
     this.Text            = text;
     this.ForegroundColor = foregroundColor;
     this.BackgroundColor = backgroundColor;
 }
Example #13
0
 private static void ChangeConsoleColorForLevel(ColoredConsoleTarget consoleTarget, string level, ConsoleOutputColor foregroundColor)
 {
     consoleTarget.RowHighlightingRules.Add(new ConsoleRowHighlightingRule
     {
         Condition       = ConditionParser.ParseExpression("level == LogLevel." + level),
         ForegroundColor = foregroundColor
     });
 }
Example #14
0
        private void ColorizeEscapeSequences(TextWriter output, IntPtr hConsole, string message, ushort startingAttribute, ushort defaultAttribute)
        {
            lock (this)
            {
                int colorStackLength = 0;
                _colorStack[colorStackLength++] = startingAttribute;

                ConsoleWin32Api.SetConsoleTextAttribute(hConsole, startingAttribute);

                int p0 = 0;

                while (p0 < message.Length)
                {
                    int p1 = p0;
                    while (p1 < message.Length && message[p1] >= 32)
                    {
                        p1++;
                    }
                    // text
                    if (p1 != p0)
                    {
                        output.Write(message.Substring(p0, p1 - p0));
                    }
                    if (p1 >= message.Length)
                    {
                        p0 = p1;
                        break;
                    }

                    // control characters
                    char c1 = message[p1];
                    char c2 = (char)0;

                    if (p1 + 1 < message.Length)
                    {
                        c2 = message[p1 + 1];
                    }

                    if (c1 == '\a' && c2 == '\a')
                    {
                        output.Write('\a');
                        p0 = p1 + 2;
                        continue;
                    }
                    if (c1 == '\r' || c1 == '\n')
                    {
                        ConsoleWin32Api.SetConsoleTextAttribute(hConsole, defaultAttribute);
                        output.Write(c1);
                        ConsoleWin32Api.SetConsoleTextAttribute(hConsole, _colorStack[colorStackLength - 1]);
                        p0 = p1 + 1;
                        continue;
                    }
                    if (c1 == '\a')
                    {
                        if (c2 == 'X')
                        {
                            colorStackLength--;
                            ConsoleWin32Api.SetConsoleTextAttribute(hConsole, _colorStack[colorStackLength - 1]);
                            p0 = p1 + 2;
                            continue;
                        }
                        else
                        {
                            ConsoleOutputColor foreground = (ConsoleOutputColor)(int)(c2 - 'A');
                            ConsoleOutputColor background = (ConsoleOutputColor)(int)(message[p1 + 2] - 'A');
                            ushort             newColor   = ColorFromForegroundAndBackground(
                                _colorStack[colorStackLength - 1], foreground, background);

                            _colorStack[colorStackLength++] = newColor;
                            ConsoleWin32Api.SetConsoleTextAttribute(hConsole, newColor);
                            p0 = p1 + 3;
                            continue;
                        }
                    }

                    output.Write(c1);
                    p0 = p1 + 1;
                }
                if (p0 < message.Length)
                {
                    output.Write(message.Substring(p0));
                }
            }
        }
Example #15
0
File: Log.cs Project: ItsKaa/Ditto
        public static void Setup(bool logToConsole = true, bool logToFile = false, bool?logDebugLevel = null)
        {
            _mutex.WaitOne();
            Ready = false;
            try
            {
                var logConfig = new LoggingConfiguration();
                ColoredConsoleTarget consoleTarget = null;

                // Setup console logging
                if (logToConsole)
                {
                    consoleTarget = new ColoredConsoleTarget()
                    {
                        Layout = @"${date:format=HH\:mm\:ss} ${logger}${message}"
                    };

                    // Modify default colours
                    foreach (var level in LogLevel.AllLoggingLevels)
                    {
                        ConsoleOutputColor fgColour = ConsoleOutputColor.NoChange;
                        if (level == LogLevel.Info)
                        {
                            fgColour = ConsoleOutputColor.Gray;
                        }
                        else if (level == LogLevel.Debug)
                        {
                            fgColour = ConsoleOutputColor.Yellow;
                        }
                        else if (level == LogLevel.Trace)
                        {
                            fgColour = ConsoleOutputColor.DarkYellow;
                        }
                        else if (level == LogLevel.Warn)
                        {
                            fgColour = ConsoleOutputColor.Magenta;
                        }
                        else if (level == LogLevel.Error)
                        {
                            fgColour = ConsoleOutputColor.Red;
                        }
                        else if (level == LogLevel.Fatal)
                        {
                            fgColour = ConsoleOutputColor.Red;                               //DarkRed;
                        }
                        consoleTarget.RowHighlightingRules.Add(new ConsoleRowHighlightingRule()
                        {
                            Condition       = ConditionParser.ParseExpression("level == LogLevel." + level.Name),
                            ForegroundColor = fgColour
                        });
                    }
                }

                // Setup file logging
                FileTarget fileTarget       = null;
                FileTarget fileTarget_error = null;
                if (logToFile)
                {
                    fileTarget = new FileTarget()
                    {
                        Layout       = @"${longdate} ${logger} | ${uppercase:${level:format}}${message}",
                        FileName     = "${basedir}/logs/ditto.log",
                        KeepFileOpen = false,
                        CreateDirs   = true,
                        Encoding     = Encoding.UTF8,
                    };

                    fileTarget_error = new FileTarget()
                    {
                        Layout       = @"${longdate} ${logger} | ${uppercase:${level:format}}${message}",
                        FileName     = "${basedir}/logs/ditto_error.log",
                        KeepFileOpen = false,
                        CreateDirs   = true,
                        Encoding     = Encoding.UTF8,
                    };
                }


                // Add targets & rules
                LogLevel minLogLevel = (logDebugLevel == true ? LogLevel.Trace : LogLevel.Info);
                if (logDebugLevel == null)
                {
                    #if DEBUG
                    minLogLevel = LogLevel.Trace;
                    #else
                    minLogLevel = LogLevel.Info;
                    #endif
                }

                if (logToConsole)
                {
                    logConfig.AddTarget("Console", consoleTarget);
                    logConfig.LoggingRules.Add(new LoggingRule("*", minLogLevel, consoleTarget));
                }
                if (logToFile)
                {
                    logConfig.LoggingRules.Add(new LoggingRule("*", LogLevel.Error, fileTarget_error));
                    logConfig.LoggingRules.Add(new LoggingRule("*", minLogLevel, fileTarget));
                }

                LogManager.Configuration = logConfig;
                Ready         = true;
                LogToConsole  = logToConsole;
                LogToFile     = logToFile;
                LogDebugLevel = (minLogLevel == LogLevel.Trace || minLogLevel == LogLevel.Debug);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }
            finally
            {
                _mutex.ReleaseMutex();
            }
        }
 /// <summary>
 /// Creates a new instance of <see cref="ConsoleRowHighlightingRule"/> and
 /// assigns Condition, ForegroundColor and BackgroundColor properties.
 /// </summary>
 public ConsoleRowHighlightingRule(string condition, ConsoleOutputColor foregroundColor, ConsoleOutputColor backgroundColor)
 {
     Condition = condition;
     ForegroundColor = foregroundColor;
     BackgroundColor = backgroundColor;
 }