1 //
2 // Copyright (c) 2010-2025 Antmicro
3 // Copyright (c) 2011-2015 Realtime Embedded
4 //
5 // This file is licensed under the MIT License.
6 // Full license text is available in 'licenses/MIT.txt'.
7 //
8 using System;
9 using System.IO;
10 
11 namespace Antmicro.Renode.Logging
12 {
13     public class ConsoleBackend : FormattedTextBackend
14     {
ConsoleBackend()15         static ConsoleBackend()
16         {
17             Instance = new ConsoleBackend();
18         }
19 
20         public static ConsoleBackend Instance { get; private set; }
21 
Dispose()22         public override void Dispose()
23         {
24 
25         }
26 
Flush()27         public override void Flush()
28         {
29             output.Flush();
30         }
31 
32         public string WindowTitle
33         {
34             // Console.Title *getter* is only supported on Windows,
35             // so we always return an empty string on other platforms.
36             get
37             {
38 #if PLATFORM_WINDOWS
39                 return PlainMode
40                     ? string.Empty
41                     : Console.Title;
42 #else
43                 return string.Empty;
44 #endif
45             }
46             // The setter is supported on all the platforms we target,
47             // so it doesn't need the if clause.
48             set
49             {
50                 if(!PlainMode)
51                 {
52                     Console.Title = value;
53                 }
54             }
55         }
56 
57         // do not generate color output
58         // do not set window title
59         // minimize the use of VT100 codes
60         public override bool PlainMode
61         {
62             get => plainMode || output != Console.Out || isRedirected;
63             set => plainMode = value;
64         }
65 
SetColor(ConsoleColor color)66         protected override void SetColor(ConsoleColor color)
67         {
68             Console.ForegroundColor = color;
69         }
70 
ResetColor()71         protected override void ResetColor()
72         {
73             Console.ResetColor();
74         }
75 
WriteLine(string line)76         protected override void WriteLine(string line)
77         {
78             output.WriteLine(line);
79         }
80 
ConsoleBackend()81         private ConsoleBackend()
82         {
83             isRedirected = Console.IsOutputRedirected;
84         }
85 
86         private TextWriter output = Console.Out;
87         private bool plainMode;
88         private readonly bool isRedirected;
89     }
90 }
91