1 //
2 // Copyright (c) 2010-2024 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 Antmicro.Renode.UserInterface.Tokenizer;
9 using AntShell.Commands;
10 using Antmicro.Renode.Logging;
11 using Antmicro.Renode.Core;
12 using System.IO;
13 using Antmicro.Renode.Utilities;
14 
15 namespace Antmicro.Renode.UserInterface.Commands
16 {
17     public class LoggerFileCommand : AutoLoadCommand
18     {
PrintHelp(ICommandInteraction writer)19         public override void PrintHelp(ICommandInteraction writer)
20         {
21             base.PrintHelp(writer);
22             writer.WriteLine();
23             writer.WriteError("\nYou must specify the filename (full path or relative) for output file.");
24         }
25 
26         [Runnable]
Run(StringToken path)27         public void Run(StringToken path)
28         {
29             InnerRun(path.Value, false);
30         }
31 
32         [Runnable]
Run(StringToken path, BooleanToken token)33         public void Run(StringToken path, BooleanToken token)
34         {
35             InnerRun(path.Value, token.Value);
36         }
37 
InnerRun(string path, bool flushAfterEveryWrite)38         private void InnerRun(string path, bool flushAfterEveryWrite)
39         {
40             if(Logger.GetBackends().TryGetValue(BackendName, out var backend))
41             {
42                 // We are explicitly removing existing backend to close
43                 // file opened by it. When using the same path twice,
44                 // this allows for the previous file to be moved by
45                 // SequencedFilePath.
46                 Logger.RemoveBackend(backend);
47             }
48             Logger.AddBackend(new FileBackend(path, flushAfterEveryWrite), BackendName, true);
49         }
50 
LoggerFileCommand(Monitor monitor)51         public LoggerFileCommand(Monitor monitor) : base(monitor, "logFile", "sets the output file for logger.", "logF")
52         {
53         }
54 
55         private readonly string BackendName = "file";
56     }
57 }
58 
59