1 //
2 // Copyright (c) 2010-2024 Antmicro
3 //
4 // This file is licensed under the MIT License.
5 // Full license text is available in 'licenses/MIT.txt'.
6 //
7 
8 using System.Linq;
9 using Antmicro.Renode.Exceptions;
10 using Antmicro.Renode.Utilities;
11 
12 namespace Antmicro.Renode.Peripherals.CPU
13 {
14     public class TraceWriterBuilder
15     {
TraceWriterBuilder(TranslationCPU cpu, SequencedFilePath path, TraceFormat format, bool isBinary, bool compress)16         public TraceWriterBuilder(TranslationCPU cpu, SequencedFilePath path, TraceFormat format, bool isBinary, bool compress)
17         {
18             // SequencedFilePath ensures that file in given path doesn't exist
19             this.cpu = cpu;
20             this.path = path;
21             this.format = format;
22             this.isBinary = isBinary;
23             this.compress = compress;
24 
25             if(!AreArgumentsValid())
26             {
27                 throw new RecoverableException($"Tracing don't support {(this.isBinary ? "binary" : "text")} output file with the '{this.format}' formatting.");
28             }
29         }
30 
CreateWriter()31         public TraceWriter CreateWriter()
32         {
33             if(format == TraceFormat.TraceBasedModel)
34             {
35                 return new TraceBasedModelFlatBufferWriter(cpu, path, format, compress);
36             }
37             if(isBinary)
38             {
39                 return new TraceBinaryWriter(cpu, path, format, compress);
40             }
41             else
42             {
43                 return new TraceTextWriter(cpu, path, format, compress);
44             }
45         }
46 
AreArgumentsValid()47         private bool AreArgumentsValid()
48         {
49             if(format == TraceFormat.TraceBasedModel)
50             {
51                 return true;
52             }
53             if(isBinary)
54             {
55                 return TraceBinaryWriter.SupportedFormats.Contains(this.format);
56             }
57             else
58             {
59                 return TraceTextWriter.SupportedFormats.Contains(this.format);
60             }
61         }
62 
63         private readonly TranslationCPU cpu;
64         private readonly string path;
65         private readonly TraceFormat format;
66         private readonly bool isBinary;
67         private readonly bool compress;
68     }
69 }
70