An implementation of IResultLogWriter that writes the results as JSON to a TextWriter.
Inheritance: IResultLogWriter, IDisposable
Exemplo n.º 1
0
        private SarifLogger(
            TextWriter textWriter,
            LogFilePersistenceOptions logFilePersistenceOptions,
            bool closeWriterOnDipose,
            IEnumerable <FailureLevel> levels,
            IEnumerable <ResultKind> kinds) : base(failureLevels: levels, resultKinds: kinds)
        {
            _textWriter           = textWriter;
            _closeWriterOnDispose = closeWriterOnDipose;
            _jsonTextWriter       = new JsonTextWriter(_textWriter);

            _logFilePersistenceOptions = logFilePersistenceOptions;

            if (PrettyPrint)
            {
                // Indented output is preferable for debugging
                _jsonTextWriter.Formatting = Newtonsoft.Json.Formatting.Indented;
            }

            _jsonTextWriter.DateFormatString = DateTimeConverter.DateTimeFormat;
            _jsonTextWriter.CloseOutput      = _closeWriterOnDispose;

            _issueLogJsonWriter = new ResultLogJsonWriter(_jsonTextWriter);
            RuleToIndexMap      = new Dictionary <ReportingDescriptor, int>(ReportingDescriptor.ValueComparer);
        }
 public void ResultLogJsonWriter_CannotWriteToolToDisposedWriter()
 {
     using (var str = new StringWriter())
         using (var json = new JsonTextWriter(str))
             using (var uut = new ResultLogJsonWriter(json))
             {
                 uut.Dispose();
                 Assert.Throws <InvalidOperationException>(() => uut.WriteTool(DefaultTool));
             }
 }
 public void ResultLogJsonWriter_CannotWriteToolToDisposedWriter()
 {
     using (var str = new StringWriter())
         using (var json = new JsonTextWriter(str))
             using (var uut = new ResultLogJsonWriter(json))
             {
                 uut.Dispose();
                 uut.WriteTool(s_defaultTool);
             }
 }
Exemplo n.º 4
0
 public void ResultLogJsonWriter_CannotWriteIssuesToDisposedWriter()
 {
     using (var str = new StringWriter())
         using (var json = new JsonTextWriter(str))
             using (var uut = new ResultLogJsonWriter(json))
             {
                 uut.WriteToolAndRunInfo(s_defaultToolInfo, s_defaultRunInfo);
                 uut.Dispose();
                 uut.WriteResult(s_defaultIssue);
             }
 }
 public void ResultLogJsonWriter_CannotWriteConfigurationNotificationsTwice()
 {
     using (var str = new StringWriter())
         using (var json = new JsonTextWriter(str))
             using (var uut = new ResultLogJsonWriter(json))
             {
                 uut.WriteTool(DefaultTool);
                 uut.WriteConfigurationNotifications(s_notifications);
                 uut.WriteConfigurationNotifications(s_notifications);
             }
 }
 public void ResultLogJsonWriter_CannotWriteInvocationTwice()
 {
     using (var str = new StringWriter())
         using (var json = new JsonTextWriter(str))
             using (var uut = new ResultLogJsonWriter(json))
             {
                 uut.WriteTool(DefaultTool);
                 uut.WriteInvocation(s_invocation);
                 uut.WriteInvocation(s_invocation);
             }
 }
 public void ResultLogJsonWriter_CannotWriteConfigurationNotificationsTwice()
 {
     using (var str = new StringWriter())
         using (var json = new JsonTextWriter(str))
             using (var uut = new ResultLogJsonWriter(json))
             {
                 uut.WriteTool(DefaultTool);
                 uut.WriteConfigurationNotifications(s_notifications);
                 Assert.Throws <InvalidOperationException>(() => uut.WriteConfigurationNotifications(s_notifications));
             }
 }
Exemplo n.º 8
0
        protected static string GetJson(Action<ResultLogJsonWriter> testContent)
        {
            StringBuilder result = new StringBuilder();
            using (var str = new StringWriter(result))
            using (var json = new JsonTextWriter(str) { Formatting = Formatting.Indented })
            using (var uut = new ResultLogJsonWriter(json))
            {
                testContent(uut);
            }

            return result.ToString();
        }
 public void ResultLogJsonWriter_MultipleDisposeAllowed()
 {
     using (var str = new StringWriter())
         using (var json = new JsonTextWriter(str))
             using (var uut = new ResultLogJsonWriter(json))
             {
                 // Assert no exception thrown
                 uut.Dispose();
                 uut.Dispose();
                 uut.Dispose();
             }
 }
        private static string GetJson(Action <ResultLogJsonWriter> testContent)
        {
            StringBuilder result = new StringBuilder();

            using (var str = new StringWriter(result))
                using (var json = new JsonTextWriter(str))
                    using (var uut = new ResultLogJsonWriter(json))
                    {
                        testContent(uut);
                    }

            return(result.ToString());
        }
Exemplo n.º 11
0
        public ResultLogger(
            Assembly assembly, 
            string outputFilePath,
            bool verbose,
            IEnumerable<string> analysisTargets,
            bool computeTargetsHash,
            string prereleaseInfo)
        {
            Verbose = verbose;

            _fileStream = new FileStream(outputFilePath, FileMode.Create, FileAccess.Write, FileShare.None);
            _textWriter = new StreamWriter(_fileStream);
            _jsonTextWriter = new JsonTextWriter(_textWriter);

            // for debugging it is nice to have the following line added.
            _jsonTextWriter.Formatting = Newtonsoft.Json.Formatting.Indented;

            _issueLogJsonWriter = new ResultLogJsonWriter(_jsonTextWriter);

            var toolInfo = new ToolInfo();
            toolInfo.InitializeFromAssembly(assembly, prereleaseInfo);

            RunInfo runInfo = new RunInfo();
            runInfo.AnalysisTargets = new List<FileReference>();

            foreach (string target in analysisTargets)
            {
                var fileReference = new FileReference()
                {
                    Uri = target.CreateUriForJsonSerialization(),
                };

                if (computeTargetsHash)
                {
                    string sha256Hash = HashUtilities.ComputeSha256Hash(target) ?? "[could not compute file hash]";
                    fileReference.Hashes = new List<Hash>(new Hash[]
                    {
                            new Hash()
                            {
                                Value = sha256Hash,
                                Algorithm = AlgorithmKind.Sha256,
                            }
                    });
                }
                runInfo.AnalysisTargets.Add(fileReference);
            }
            runInfo.InvocationInfo = Environment.CommandLine;

            _issueLogJsonWriter.WriteToolAndRunInfo(toolInfo, runInfo);
        }
 public void ResultLogJsonWriter_CannotWriteToolTwice()
 {
     using (var str = new StringWriter())
         using (var json = new JsonTextWriter(str))
             using (var uut = new ResultLogJsonWriter(json))
             {
                 var run = new Run()
                 {
                 };
                 uut.Initialize(run);
                 uut.WriteTool(DefaultTool);
                 Assert.Throws <InvalidOperationException>(() => uut.WriteTool(DefaultTool));
             }
 }
 public void ResultLogJsonWriter_CannotWriteResultsToDisposedWriter()
 {
     using (var str = new StringWriter())
         using (var json = new JsonTextWriter(str))
             using (var uut = new ResultLogJsonWriter(json))
             {
                 var run = new Run()
                 {
                     Tool = DefaultTool
                 };
                 uut.Initialize(run);
                 uut.Dispose();
                 Assert.Throws <InvalidOperationException>(() => uut.WriteResult(DefaultResult));
             }
 }
Exemplo n.º 14
0
        private SarifLogger(TextWriter textWriter, LoggingOptions loggingOptions)
        {
            _textWriter     = textWriter;
            _jsonTextWriter = new JsonTextWriter(_textWriter);

            _loggingOptions = loggingOptions;

            if (PrettyPrint)
            {
                // Indented output is preferable for debugging
                _jsonTextWriter.Formatting = Newtonsoft.Json.Formatting.Indented;
            }

            _jsonTextWriter.DateFormatString = DateTimeConverter.DateTimeFormat;

            _issueLogJsonWriter = new ResultLogJsonWriter(_jsonTextWriter);
        }
Exemplo n.º 15
0
        public static string GetConverterJson(ToolFileConverterBase converter, byte[] inputData)
        {
            using (var input = new MemoryStream(inputData))
            {
                using (var output = new StringWriter())
                {
                    var json = new JsonTextWriter(output);
                    json.Formatting = Newtonsoft.Json.Formatting.Indented;
                    json.CloseOutput = false;
                    using (var outputWriter = new ResultLogJsonWriter(json))
                    {
                        converter.Convert(input, outputWriter);
                    }

                    return output.ToString();
                }
            }
        }
Exemplo n.º 16
0
        private SarifLogger(TextWriter textWriter, LoggingOptions loggingOptions)
        {
            _textWriter     = textWriter;
            _jsonTextWriter = new JsonTextWriter(_textWriter);

            _loggingOptions = loggingOptions;

            if (PrettyPrint)
            {
                // Indented output is preferable for debugging
                _jsonTextWriter.Formatting = Newtonsoft.Json.Formatting.Indented;
            }

            _jsonTextWriter.DateFormatString = DateTimeConverter.DateTimeFormat;

            _issueLogJsonWriter = new ResultLogJsonWriter(_jsonTextWriter);
            RuleToIndexMap      = new Dictionary <ReportingDescriptor, int>(ReportingDescriptor.ValueComparer);
        }
Exemplo n.º 17
0
        public static void ProcessLogFile(string filePath, ToolFormat toolFormat = ToolFormat.None)
        {
            SarifLog log;

            JsonSerializerSettings settings = new JsonSerializerSettings()
            {
                ContractResolver = SarifContractResolver.Instance,
            };

            string logText;

            if (toolFormat == ToolFormat.None)
            {
                logText = File.ReadAllText(filePath);
            }
            else if (toolFormat == ToolFormat.PREfast)
            {
                logText = ToolFormatConverter.ConvertPREfastToStandardFormat(filePath);
            }
            else
            {
                // We have conversion to do
                var converter = new ToolFormatConverter();
                var sb = new StringBuilder();

                using (var input = new MemoryStream(File.ReadAllBytes(filePath)))
                {
                    var outputTextWriter = new StringWriter(sb);
                    var outputJson = new JsonTextWriter(outputTextWriter);
                    var output = new ResultLogJsonWriter(outputJson);

                    input.Seek(0, SeekOrigin.Begin);
                    converter.ConvertToStandardFormat(toolFormat, input, output);

                    // This is serving as a flush mechanism
                    output.Dispose();

                    logText = sb.ToString();
                }
            }

            log = JsonConvert.DeserializeObject<SarifLog>(logText, settings);
            ProcessSarifLog(log, filePath);
        }
Exemplo n.º 18
0
 public void ResultLogJsonWriter_CannotWriteToolToDisposedWriter()
 {
     using (var str = new StringWriter())
     using (var json = new JsonTextWriter(str))
     using (var uut = new ResultLogJsonWriter(json))
     {
         uut.Dispose();
         uut.WriteTool(DefaultTool);
     }
 }
Exemplo n.º 19
0
 public void ResultLogJsonWriter_CannotWriteToolNotificationsTwice()
 {
     using (var str = new StringWriter())
     using (var json = new JsonTextWriter(str))
     using (var uut = new ResultLogJsonWriter(json))
     {
         uut.WriteTool(DefaultTool);
         uut.WriteToolNotifications(s_notifications);
         uut.WriteToolNotifications(s_notifications);
     }
 }
Exemplo n.º 20
0
 public void ResultLogJsonWriter_MultipleDisposeAllowed()
 {
     using (var str = new StringWriter())
     using (var json = new JsonTextWriter(str))
     using (var uut = new ResultLogJsonWriter(json))
     {
         // Assert no exception thrown
         uut.Dispose();
         uut.Dispose();
         uut.Dispose();
     }
 }
Exemplo n.º 21
0
        public SarifLogger(TextWriter textWriter, bool verbose)
        {
            Verbose = verbose;

            _textWriter = textWriter;

            _jsonTextWriter = new JsonTextWriter(_textWriter);

            // for debugging it is nice to have the following line added.
            _jsonTextWriter.Formatting = Newtonsoft.Json.Formatting.Indented;
            _jsonTextWriter.DateFormatString = DateTimeConverter.DateTimeFormat;

            _issueLogJsonWriter = new ResultLogJsonWriter(_jsonTextWriter);
        }