Exemple #1
0
        private static void UnityLog(string category, object message,
                                     LoggerVerbosity messageVerbosity = LoggerVerbosity.Info)
        {
            if (messageVerbosity == LoggerVerbosity.None)
            {
                return;
            }

            if (Verbosity > messageVerbosity)
            {
                return;
            }
            if (CategoryVerbosities != null)
            {
                if (!CategoryVerbosities.ContainsKey(category) && !CategoryVerbosities.ContainsKey(AllowAnyCategoryVerbosities))
                {
                    return;
                }
                if (CategoryVerbosities.ContainsKey(category))
                {
                    if (CategoryVerbosities[category] > messageVerbosity)
                    {
                        return;
                    }
                }
            }

            MessageCount++;

            string output = string.Concat(GetMessageHeader(messageVerbosity, category), message);

            switch (messageVerbosity)
            {
            case LoggerVerbosity.Info:
                UnityEngine.Debug.Log(output);
                break;

            case LoggerVerbosity.Warning:
                UnityEngine.Debug.LogWarning(output);
                break;

            case LoggerVerbosity.Error:
                UnityEngine.Debug.LogError(output);
                break;
            }
        }
Exemple #2
0
        /// <summary>
        /// Log a message in a category with a specified verbosity.
        /// </summary>
        /// <param name="category">The category to log in.</param>
        /// <param name="message">The message to log.</param>
        /// <param name="messageVerbosity">The verbosity to log with.</param>
        /// <param name="separateLineHere">Should this message be separated by a line?</param>
        public static void Log(string category, object message, LoggerVerbosity messageVerbosity = LoggerVerbosity.Info, bool separateLineHere = false)
        {
            // The none verbosity turns off logging.
            if (messageVerbosity == LoggerVerbosity.None)
            {
                return;
            }

            // Lock the output stream of the console for operation.
            lock (Console.Out)
            {
                if (Verbosity > messageVerbosity)
                {
                    return;
                }

                // Check if the category we are trying to log into allows the specified verbosity.
                if (CategoryVerbosities != null)
                {
                    bool allowAnyCategoryVerbosities = CategoryVerbosities.ContainsKey(AllowAnyCategoryVerbosities);
                    if (!CategoryVerbosities.ContainsKey(category) && !allowAnyCategoryVerbosities)
                    {
                        return;
                    }

                    if (CategoryVerbosities.ContainsKey(category))
                    {
                        if (CategoryVerbosities[category] > messageVerbosity)
                        {
                            return;
                        }
                    }
                }

                MessageCount++;

                string output           = string.Concat(GetMessageHeader(messageVerbosity, category), message, MessageSuffix);
                string messageSeperator = string.Empty;

                bool shouldSeparateLine = separateLineHere || LineSeperatorMessageInterval > 0 &&
                                          MessageCount % LineSeperatorMessageInterval == 0;

                if (!string.IsNullOrEmpty(LineSeperator) && shouldSeparateLine)
                {
                    messageSeperator = LineSeperator.Multiply(output.Length);
                }

                // Log to the file.
                if ((Destination & LoggerDestination.File) != 0)
                {
                    messageBuffer.AppendLine(output);
                    if (!string.IsNullOrEmpty(messageSeperator))
                    {
                        messageBuffer.AppendLine(messageSeperator);
                    }
                }

                // Log to the console.
                if ((Destination & LoggerDestination.Output) != 0)
                {
                    ConsoleColor oldConsoleColor = Console.ForegroundColor;
                    Console.ForegroundColor = GetVerbosityConsoleColour(messageVerbosity);

                    if (messageVerbosity == LoggerVerbosity.Plain)
                    {
                        Console.WriteLine(message);
                    }
                    else
                    {
                        Console.WriteLine(output);
                    }

                    if (!string.IsNullOrEmpty(messageSeperator))
                    {
                        Console.WriteLine(messageSeperator);
                    }

                    Console.ForegroundColor = oldConsoleColor;
                }

                if (Destination != LoggerDestination.None)
                {
                    OnMessageLogged(new MessagedLoggerEventArgs(message.ToString()));
                }

                // Flush the message buffer if it is time.
                messageCountSinceLastFlush++;
                if (messageVerbosity >= FlushVerbosity || messageCountSinceLastFlush == FlushBufferMessageCapacity)
                {
                    FlushMessageBuffer();
                }
            }
        }
Exemple #3
0
        private static void ConsoleLog(string category, object message,
                                       LoggerVerbosity messageVerbosity = LoggerVerbosity.Info, bool seperateLineHere = false)
        {
            if (messageVerbosity == LoggerVerbosity.None)
            {
                return;
            }
            lock (Console.Out)
            {
                if (Verbosity > messageVerbosity)
                {
                    return;
                }
                if (CategoryVerbosities != null)
                {
                    if (!CategoryVerbosities.ContainsKey(category) && !CategoryVerbosities.ContainsKey(AllowAnyCategoryVerbosities))
                    {
                        return;
                    }
                    if (CategoryVerbosities.ContainsKey(category))
                    {
                        if (CategoryVerbosities[category] > messageVerbosity)
                        {
                            return;
                        }
                    }
                }

                MessageCount++;

                string output           = string.Concat(GetMessageHeader(messageVerbosity, category), message);
                string messageSeperator = string.Empty;
                if (!string.IsNullOrEmpty(LineSeperator) && (seperateLineHere || LineSeperatorMessageInterval > 0 &&
                                                             MessageCount % LineSeperatorMessageInterval == 0))
                {
                    messageSeperator = LineSeperator.Multiply(output.Length);
                }

                if ((LogDestination & LoggerDestination.File) != 0)
                {
                    messageBuffer.AppendLine(output);
                    if (!string.IsNullOrEmpty(messageSeperator))
                    {
                        messageBuffer.AppendLine(messageSeperator);
                    }
                }

                if ((LogDestination & LoggerDestination.Output) != 0)
                {
                    ConsoleColor oldConsoleColor = Console.ForegroundColor;
                    Console.ForegroundColor = GetConsoleColour(messageVerbosity);

                    Console.WriteLine(output);
                    if (!string.IsNullOrEmpty(messageSeperator))
                    {
                        Console.WriteLine(messageSeperator);
                    }

                    Console.ForegroundColor = oldConsoleColor;
                }

                messageCountSinceLastFlush++;
                if (messageVerbosity >= FlushVerbosity || messageCountSinceLastFlush == FlushBufferMessageCapacity)
                {
                    FlushMessageBuffer();
                }
            }

            if (!isFirstLog)
            {
                return;
            }
            messageBufferFlushTimer.Start();
            isFirstLog = false;
        }
        /// <summary>
        /// Log a message in a category with a specified verbosity.
        /// </summary>
        /// <param name="category">The category to log in.</param>
        /// <param name="message">The message to log.</param>
        /// <param name="messageVerbosity">The verbosity to log with.</param>
        /// <param name="separateLineHere">Should this message be separated by a line?</param>
        /// <param name="destinationOverride">
        /// Overrides the message log destination.
        /// Defaults to <see cref="LoggerDestination.None"/> indicating no log destination override.
        /// </param>
        public static void Log(string category, object message, LoggerVerbosity messageVerbosity = LoggerVerbosity.Info,
                               LoggerDestination destinationOverride = LoggerDestination.None, bool separateLineHere = false)
        {
            // The none verbosity turns off logging.
            if (messageVerbosity == LoggerVerbosity.None)
            {
                return;
            }

            // Lock the output stream of the console for operation.
            lock (Console.Out)
            {
                if (Verbosity > messageVerbosity)
                {
                    return;
                }

                // Check if the category we are trying to log into allows the specified verbosity.
                if (CategoryVerbosities != null)
                {
                    bool allowAnyCategoryVerbosities = CategoryVerbosities.ContainsKey(AllowAnyCategoryVerbosities);
                    if (!CategoryVerbosities.ContainsKey(category) && !allowAnyCategoryVerbosities)
                    {
                        return;
                    }

                    if (CategoryVerbosities.ContainsKey(category))
                    {
                        if (CategoryVerbosities[category] > messageVerbosity)
                        {
                            return;
                        }
                    }
                }

                MessageCount++;

                string output           = string.Concat(GetMessageHeader(messageVerbosity, category), message, MessageSuffix);
                string messageSeperator = string.Empty;

                bool shouldSeparateLine = separateLineHere || LineSeperatorMessageInterval > 0 &&
                                          MessageCount % LineSeperatorMessageInterval == 0;

                if (!string.IsNullOrEmpty(LineSeperator) && shouldSeparateLine)
                {
                    messageSeperator = LineSeperator.Multiply(output.Length);
                }

                LoggerDestination destination = destinationOverride == LoggerDestination.None ? Destination : destinationOverride;

                // Log to the file.
                if (destination.HasFlag(LoggerDestination.File))
                {
                    messageBuffer.AppendLine(output);
                    if (!string.IsNullOrEmpty(messageSeperator))
                    {
                        messageBuffer.AppendLine(messageSeperator);
                    }
                }

                // Log to the console.
                if (destination.HasFlag(LoggerDestination.Output))
                {
                    ConsoleColor oldConsoleColor = Console.ForegroundColor;
                    Console.ForegroundColor = GetVerbosityConsoleColour(messageVerbosity);

                    if (messageVerbosity == LoggerVerbosity.Plain)
                    {
                        Console.WriteLine(message);
                    }
                    else
                    {
                        Console.WriteLine(output);
                    }

                    if (!string.IsNullOrEmpty(messageSeperator))
                    {
                        Console.WriteLine(messageSeperator);
                    }

                    Console.ForegroundColor = oldConsoleColor;
                }

                // Log using message box
                if (destination.HasFlag(LoggerDestination.Form))
                {
                    // The caption of the message box is either the category or assembly name.
                    string caption = string.IsNullOrEmpty(category) ? Assembly.GetEntryAssembly().GetName().Name : category;

                    // Show a message box with the appropriate icon corresponding to the message verbosity
                    switch (messageVerbosity)
                    {
                    case LoggerVerbosity.Plain:
                        MessageBox.Show(message.ToString(), caption, MessageBoxButton.OK, MessageBoxImage.None);
                        break;

                    case LoggerVerbosity.Info:
                        MessageBox.Show(message.ToString(), caption, MessageBoxButton.OK, MessageBoxImage.Information);
                        break;

                    case LoggerVerbosity.Warning:
                        MessageBox.Show(message.ToString(), caption, MessageBoxButton.OK, MessageBoxImage.Warning);
                        break;

                    case LoggerVerbosity.Error:
                        MessageBox.Show(message.ToString(), caption, MessageBoxButton.OK, MessageBoxImage.Error);
                        break;
                    }
                }

                if (!destination.HasFlag(LoggerDestination.None))
                {
                    OnMessageLogged(new MessagedLoggerEventArgs(message.ToString()));
                }

                // Flush the message buffer if it is time.
                messageCountSinceLastFlush++;
                if (messageVerbosity >= FlushVerbosity || messageCountSinceLastFlush == FlushBufferMessageCapacity)
                {
                    FlushMessageBuffer();
                }
            }
        }