From 1f48b082d1e33e851a4e19376e17278a44ed5103 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Tue, 30 May 2017 15:04:28 +0200 Subject: [PATCH] CLI: Add --output-file flag so results can be written to file directly. --- cli/cmdlineparser.cpp | 5 +++++ cli/cppcheckexecutor.cpp | 8 ++++++-- cli/cppcheckexecutor.h | 5 +++++ lib/settings.h | 5 ++++- 4 files changed, 20 insertions(+), 3 deletions(-) diff --git a/cli/cmdlineparser.cpp b/cli/cmdlineparser.cpp index b4b9f65e7..a1da7f79c 100644 --- a/cli/cmdlineparser.cpp +++ b/cli/cmdlineparser.cpp @@ -272,6 +272,10 @@ bool CmdLineParser::ParseFromArgs(int argc, const char* const argv[]) } } + // Write results in file + else if (std::strncmp(argv[i], "--output-file=", 14) == 0) + _settings->outputFile = Path::simplifyPath(Path::fromNativeSeparators(argv[i] + 14)); + // Write results in results.plist else if (std::strncmp(argv[i], "--plist-output=", 15) == 0) { _settings->plistOutput = Path::simplifyPath(Path::fromNativeSeparators(argv[i] + 15)); @@ -923,6 +927,7 @@ void CmdLineParser::PrintHelp() " distributed with Cppcheck is loaded automatically.\n" " For more information about library files, read the\n" " manual.\n" + " --output-file= Write results to file, rather than standard error.\n" " --project= Run Cppcheck on project. The can be a Visual\n" " Studio Solution (*.sln), Visual Studio Project\n" " (*.vcxproj), or compile database\n" diff --git a/cli/cppcheckexecutor.cpp b/cli/cppcheckexecutor.cpp index 7f286846c..3412aa79b 100644 --- a/cli/cppcheckexecutor.cpp +++ b/cli/cppcheckexecutor.cpp @@ -78,7 +78,7 @@ /*static*/ FILE* CppCheckExecutor::exceptionOutput = stdout; CppCheckExecutor::CppCheckExecutor() - : _settings(0), time1(0), errorlist(false) + : _settings(0), time1(0), errorOutput(stderr), errorlist(false) { } @@ -808,6 +808,10 @@ int CppCheckExecutor::check_internal(CppCheck& cppcheck, int /*argc*/, const cha if (settings.reportProgress) time1 = std::time(0); + if (!settings.outputFile.empty()) { + errorOutput = fopen(settings.outputFile.c_str(), "wt"); + } + if (settings.xml) { reportErr(ErrorLogger::ErrorMessage::getXMLHeader(settings.xml_version)); } @@ -923,7 +927,7 @@ void CppCheckExecutor::reportErr(const std::string &errmsg) return; _errorList.insert(errmsg); - std::cerr << errmsg << std::endl; + std::fprintf(errorOutput, "%s\n", errmsg.c_str()); } void CppCheckExecutor::reportOut(const std::string &outmsg) diff --git a/cli/cppcheckexecutor.h b/cli/cppcheckexecutor.h index 6b9d1d25b..50ae1a76e 100644 --- a/cli/cppcheckexecutor.h +++ b/cli/cppcheckexecutor.h @@ -176,6 +176,11 @@ private: */ static FILE* exceptionOutput; + /** + * Error output (default stderr) + */ + FILE* errorOutput; + /** * Has --errorlist been given? */ diff --git a/lib/settings.h b/lib/settings.h index 5bc9b1261..81cae42e7 100644 --- a/lib/settings.h +++ b/lib/settings.h @@ -132,7 +132,10 @@ public: /** @brief Paths used as base for conversion to relative paths. */ std::vector basePaths; - /** @brief write XML results (--plist-output=) */ + /** @brief write results (--output-file=) */ + std::string outputFile; + + /** @brief plist output (--plist-output=) */ std::string plistOutput; /** @brief write XML results (--xml) */