Beispiel #1
0
        private static void WriteLinePrivate(int StackFramesToSkip, bool bWriteOnce, LogEventType Verbosity, LogFormatOptions FormatOptions, string Format, params object[] Args)
        {
            // if we want this message only written one time, check if it was already written out
            if (bWriteOnce)
            {
                string Formatted = string.Format(Format, Args);
                if (WriteOnceSet.Contains(Formatted))
                {
                    return;
                }

                WriteOnceSet.Add(Formatted);
            }

            if (Verbosity <= OutputLevel)
            {
                lock (SyncObject)
                {
                    // Output to all the other trace listeners
                    List <string> Lines = FormatMessage(StackFramesToSkip + 1, Verbosity, FormatOptions, false, Format, Args);
                    foreach (TraceListener Listener in Trace.Listeners)
                    {
                        foreach (string Line in Lines)
                        {
                            Listener.WriteLine(Line);
                        }
                        Listener.Flush();
                    }

                    // Handle the console output separately; we format things differently
                    if ((Verbosity != LogEventType.Log || OutputLevel >= LogEventType.Verbose) && (FormatOptions & LogFormatOptions.NoConsoleOutput) == 0)
                    {
                        FlushStatusHeading();

                        bool bResetConsoleColor = false;
                        if (ColorConsoleOutput)
                        {
                            if (Verbosity == LogEventType.Warning)
                            {
                                Console.ForegroundColor = ConsoleColor.Yellow;
                                bResetConsoleColor      = true;
                            }
                            if (Verbosity <= LogEventType.Error)
                            {
                                Console.ForegroundColor = ConsoleColor.Red;
                                bResetConsoleColor      = true;
                            }
                        }
                        try
                        {
                            List <string> ConsoleLines = FormatMessage(StackFramesToSkip + 1, Verbosity, FormatOptions, true, Format, Args);
                            foreach (string ConsoleLine in ConsoleLines)
                            {
                                Console.WriteLine(ConsoleLine);
                            }
                        }
                        finally
                        {
                            // make sure we always put the console color back.
                            if (bResetConsoleColor)
                            {
                                Console.ResetColor();
                            }
                        }

                        if (StatusMessageStack.Count > 0 && AllowStatusUpdates)
                        {
                            SetStatusText(StatusMessageStack.Peek().CurrentText);
                        }
                    }
                }
            }
        }
Beispiel #2
0
        private static List <string> FormatMessage(int StackFramesToSkip, LogEventType Verbosity, LogFormatOptions Options, bool bForConsole, string Format, params object[] Args)
        {
            string TimePrefix     = IncludeTimestamps? String.Format("[{0:hh\\:mm\\:ss\\.fff}] ", Timer.Elapsed) : "";
            string SourcePrefix   = (bForConsole ? IncludeCallingMethodForConsole : IncludeCallingMethod) ? string.Format("{0}: ", GetSource(StackFramesToSkip)) : "";
            string SeverityPrefix = (IncludeSeverityPrefix && ((Options & LogFormatOptions.NoSeverityPrefix) == 0)) ? GetSeverityPrefix(Verbosity) : "";

            // Include the executable name when running inside MSBuild. If unspecified, MSBuild re-formats them with an "EXEC :" prefix.
            if (SeverityPrefix.Length > 0 && IncludeProgramNameWithSeverityPrefix)
            {
                SeverityPrefix = String.Format("{0}: {1}", Path.GetFileNameWithoutExtension(Assembly.GetEntryAssembly().Location), SeverityPrefix);
            }

            // If there are no extra args, don't try to format the string, in case it has any format control characters in it (our LOCTEXT strings tend to).
            string[] Lines = ((Args.Length > 0) ? String.Format(Format, Args) : Format).TrimEnd(' ', '\t', '\r', '\n').Split('\n');

            List <string> FormattedLines = new List <string>();

            FormattedLines.Add(String.Format("{0}{1}{2}{3}{4}", TimePrefix, SourcePrefix, Indent, SeverityPrefix, Lines[0].TrimEnd('\r')));

            if (Lines.Length > 1)
            {
                int PaddingLength = 0;
                while (PaddingLength < Lines[0].Length && Char.IsWhiteSpace(Lines[0][PaddingLength]))
                {
                    PaddingLength++;
                }

                string Padding = new string(' ', SeverityPrefix.Length) + Lines[0].Substring(0, PaddingLength);
                for (int Idx = 1; Idx < Lines.Length; Idx++)
                {
                    FormattedLines.Add(String.Format("{0}{1}{2}{3}{4}", TimePrefix, SourcePrefix, Indent, Padding, Lines[Idx].TrimEnd('\r')));
                }
            }

            return(FormattedLines);
        }
Beispiel #3
0
 public static void WriteLineOnce(LogEventType Verbosity, LogFormatOptions Options, string Format, params object[] Args)
 {
     WriteLinePrivate(1, true, Verbosity, Options, Format, Args);
 }
Beispiel #4
0
 public static void WriteLine(int StackFramesToSkip, LogEventType Verbosity, LogFormatOptions FormatOptions, string Format, params object[] Args)
 {
     WriteLinePrivate(StackFramesToSkip + 1, false, Verbosity, FormatOptions, Format, Args);
 }
Beispiel #5
0
        private static void WriteLinePrivate(int StackFramesToSkip, bool bWriteOnce, LogEventType Verbosity, LogFormatOptions FormatOptions, string Format, params object[] Args)
        {
            if (!bIsInitialized)
            {
                throw new BuildException("Tried to using Logging system before it was ready");
            }

            // if we want this message only written one time, check if it was already written out
            if (bWriteOnce)
            {
                string Formatted = string.Format(Format, Args);
                if (WriteOnceSet.Contains(Formatted))
                {
                    return;
                }

                WriteOnceSet.Add(Formatted);
            }

            if (Verbosity <= LogLevel)
            {
                // Do console color highlighting here.
                ConsoleColor DefaultColor = ConsoleColor.Gray;
                bool         bIsWarning   = false;
                bool         bIsError     = false;
                // don't try to touch the console unless we are told to color the output.
                if (bColorConsoleOutput)
                {
                    DefaultColor = Console.ForegroundColor;
                    bIsWarning   = Verbosity == LogEventType.Warning;
                    bIsError     = Verbosity <= LogEventType.Error;
                    // @todo mono - mono doesn't seem to initialize the ForegroundColor properly, so we can't restore it properly.
                    // Avoid touching the console color unless we really need to.
                    if (bIsWarning || bIsError)
                    {
                        Console.ForegroundColor = bIsWarning ? ConsoleColor.Yellow : ConsoleColor.Red;
                    }
                }
                try
                {
                    // @todo mono: mono has some kind of bug where calling mono recursively by spawning
                    // a new process causes Trace.WriteLine to stop functioning (it returns, but does nothing for some reason).
                    // work around this by simulating Trace.WriteLine on mono.
                    // We use UAT to spawn UBT instances recursively a lot, so this bug can effectively
                    // make all build output disappear outside of the top level UAT process.
                    //					#if MONO
                    lock (((System.Collections.ICollection)Trace.Listeners).SyncRoot)
                    {
                        foreach (TraceListener l in Trace.Listeners)
                        {
                            bool bIsConsole = l is UEConsoleTraceListener;
                            if (Verbosity != LogEventType.Log || !bIsConsole || LogLevel >= LogEventType.Verbose)
                            {
                                List <string> Lines = FormatMessage(StackFramesToSkip + 1, Verbosity, FormatOptions, bIsConsole, Format, Args);
                                foreach (string Line in Lines)
                                {
                                    l.WriteLine(Line);
                                }
                            }
                            l.Flush();
                        }
                    }
                    //					#else
                    // Call Trace directly here. Trace ensures that our logging is threadsafe using the GlobalLock.
                    //                      Trace.WriteLine(FormatMessage(StackFramesToSkip + 1, CustomSource, Verbosity, Format, Args));
                    //					#endif
                }
                finally
                {
                    // make sure we always put the console color back.
                    if (bColorConsoleOutput && (bIsWarning || bIsError))
                    {
                        Console.ForegroundColor = DefaultColor;
                    }
                }
            }
        }
Beispiel #6
0
        private static List <string> FormatMessage(int StackFramesToSkip, LogEventType Verbosity, LogFormatOptions Options, bool bForConsole, string Format, params object[] Args)
        {
            string TimePrefix     = (Timer != null) ? String.Format("[{0:hh\\:mm\\:ss\\.fff}] ", Timer.Elapsed) : "";
            string SourcePrefix   = (bForConsole ? bLogSourcesToConsole : bLogSources) ? string.Format("{0}: ", GetSource(StackFramesToSkip)) : "";
            string SeverityPrefix = (bLogSeverity && ((Options & LogFormatOptions.NoSeverityPrefix) == 0)) ? GetSeverityPrefix(Verbosity) : "";

            // If there are no extra args, don't try to format the string, in case it has any format control characters in it (our LOCTEXT strings tend to).
            string[] Lines = ((Args.Length > 0) ? String.Format(Format, Args) : Format).TrimEnd(' ', '\t', '\r', '\n').Split('\n');

            List <string> FormattedLines = new List <string>();

            FormattedLines.Add(String.Format("{0}{1}{2}{3}{4}", TimePrefix, SourcePrefix, Indent, SeverityPrefix, Lines[0].TrimEnd('\r')));

            if (Lines.Length > 1)
            {
                string Padding = new string(' ', SeverityPrefix.Length);
                for (int Idx = 1; Idx < Lines.Length; Idx++)
                {
                    FormattedLines.Add(String.Format("{0}{1}{2}{3}{4}", TimePrefix, SourcePrefix, Indent, Padding, Lines[Idx].TrimEnd('\r')));
                }
            }

            return(FormattedLines);
        }