Skip to content

Commit

Permalink
Add quiet mode.
Browse files Browse the repository at this point in the history
  • Loading branch information
OpenCppCoverage committed Sep 6, 2015
1 parent 4ddd160 commit cf8d54b
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 20 deletions.
31 changes: 23 additions & 8 deletions CppCoverage/Options.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,34 @@

#include "stdafx.h"
#include "Options.hpp"

#include "CppCoverageException.hpp"
namespace cov = CppCoverage;

namespace CppCoverage
{
namespace
{
//---------------------------------------------------------------------
std::wstring GetLogLevelStr(LogLevel logLevel)
{
switch (logLevel)
{
case LogLevel::Normal: return L"Normal";
case LogLevel::Quiet: return L"Quiet";
case LogLevel::Verbose: return L"Verbose";
}
THROW("Invalid Log level.");
}
}

//-------------------------------------------------------------------------
Options::Options(
const cov::Patterns& modulePatterns,
const cov::Patterns& sourcePatterns,
const cov::StartInfo* startInfo)
: modules_{modulePatterns}
, sources_{sourcePatterns}
, isVerboseModeEnabled_{false}
, logLevel_{ LogLevel::Normal }
, isPluginModeEnabled_{false}
, isCoverChildrenModeEnabled_{false}
, isAggregateByFileModeEnabled_{true}
Expand Down Expand Up @@ -56,17 +71,17 @@ namespace CppCoverage
}

//-------------------------------------------------------------------------
void Options::EnableVerboseMode()
void Options::SetLogLevel(LogLevel logLevel)
{
isVerboseModeEnabled_ = true;
logLevel_ = logLevel;
}

//-------------------------------------------------------------------------
bool Options::IsVerboseModeEnabled() const
LogLevel Options::GetLogLevel() const
{
return isVerboseModeEnabled_;
return logLevel_;
}

//-------------------------------------------------------------------------
void Options::EnablePlugingMode()
{
Expand Down Expand Up @@ -134,7 +149,7 @@ namespace CppCoverage
ostr << *options.optionalStartInfo_ << std::endl;
ostr << L"Modules: " << options.modules_ << std::endl;
ostr << L"Sources: " << options.sources_ << std::endl;
ostr << L"Verbose mode: " << options.isVerboseModeEnabled_ << std::endl;
ostr << L"Log Level: " << GetLogLevelStr(options.GetLogLevel()) << std::endl;
ostr << L"Cover Children: " << options.isCoverChildrenModeEnabled_ << std::endl;
ostr << L"Aggregate by file: " << options.isAggregateByFileModeEnabled_ << std::endl;

Expand Down
13 changes: 10 additions & 3 deletions CppCoverage/Options.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,13 @@ namespace CppCoverage
{
class Patterns;

enum class LogLevel
{
Quiet,
Normal,
Verbose
};

class CPPCOVERAGE_DLL Options
{
public:
Expand All @@ -42,8 +49,8 @@ namespace CppCoverage
const CppCoverage::Patterns& GetSourcePatterns() const;
const CppCoverage::StartInfo* GetStartInfo() const;

void EnableVerboseMode();
bool IsVerboseModeEnabled() const;
void SetLogLevel(LogLevel);
LogLevel GetLogLevel() const;

void EnablePlugingMode();
bool IsPlugingModeEnabled() const;
Expand Down Expand Up @@ -71,7 +78,7 @@ namespace CppCoverage
CppCoverage::Patterns sources_;
boost::optional<CppCoverage::StartInfo> optionalStartInfo_;

bool isVerboseModeEnabled_;
LogLevel logLevel_;
bool isPluginModeEnabled_;
bool isCoverChildrenModeEnabled_;
bool isAggregateByFileModeEnabled_;
Expand Down
13 changes: 11 additions & 2 deletions CppCoverage/OptionsParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -275,8 +275,17 @@ namespace CppCoverage
auto optionalStartInfo = GetStartInfo(variables);
Options options{ modulePatterns, sourcePatterns, optionalStartInfo.get_ptr() };

if (IsOptionSelected(variables, ProgramOptions::VerboseOption))
options.EnableVerboseMode();
bool isVerbose = IsOptionSelected(variables, ProgramOptions::VerboseOption);
bool isQuiet = IsOptionSelected(variables, ProgramOptions::QuietOption);

if (isVerbose && isQuiet)
throw OptionsParserException("--" + ProgramOptions::VerboseOption + " and --" + ProgramOptions::QuietOption + " cannot be used at the same time.");

if (isVerbose)
options.SetLogLevel(LogLevel::Verbose);
if (isQuiet)
options.SetLogLevel(LogLevel::Quiet);

if (IsOptionSelected(variables, ProgramOptions::CoverChildrenOption))
options.EnableCoverChildrenMode();
if (IsOptionSelected(variables, ProgramOptions::PluginOption))
Expand Down
5 changes: 4 additions & 1 deletion CppCoverage/ProgramOptions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ namespace CppCoverage
void FillGenericOptions(po::options_description& options)
{
options.add_options()
((ProgramOptions::VerboseOption + "," + ProgramOptions::VerboseShortOption).c_str(), "Show verbose log.")
((ProgramOptions::VerboseOption + "," + ProgramOptions::VerboseShortOption).c_str(), "Verbose mode.")
((ProgramOptions::QuietOption + "," + ProgramOptions::QuietShortOption).c_str(), "Quiet mode.")
((ProgramOptions::HelpOption + "," + ProgramOptions::HelpShortOption).c_str(), "Show help message.")
(ProgramOptions::ConfigFileOption.c_str(), po::value<std::string>(), "Filename of a configuration file.");
}
Expand Down Expand Up @@ -117,6 +118,8 @@ namespace CppCoverage
const std::string ProgramOptions::ExcludedSourcesOption = "excluded_sources";
const std::string ProgramOptions::VerboseOption = "verbose";
const std::string ProgramOptions::VerboseShortOption = "v";
const std::string ProgramOptions::QuietOption = "quiet";
const std::string ProgramOptions::QuietShortOption = "q";
const std::string ProgramOptions::PluginOption = "plugin";
const std::string ProgramOptions::HelpOption = "help";
const std::string ProgramOptions::HelpShortOption = "h";
Expand Down
2 changes: 2 additions & 0 deletions CppCoverage/ProgramOptions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,10 @@ namespace CppCoverage
static const std::string SelectedSourcesOption;
static const std::string ExcludedSourcesOption;
static const std::string VerboseOption;
static const std::string QuietOption;
static const std::string PluginOption;
static const std::string VerboseShortOption;
static const std::string QuietShortOption;
static const std::string HelpOption;
static const std::string HelpShortOption;
static const std::string ConfigFileOption;
Expand Down
21 changes: 16 additions & 5 deletions CppCoverageTest/OptionsParserTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ namespace CppCoverageTest

auto options = TestTools::Parse(parser, {});
ASSERT_TRUE(options);
ASSERT_FALSE(options->IsVerboseModeEnabled());
ASSERT_EQ(cov::LogLevel::Normal, options->GetLogLevel());
ASSERT_FALSE(options->IsPlugingModeEnabled());
ASSERT_FALSE(options->IsCoverChildrenModeEnabled());
ASSERT_TRUE(options->IsAggregateByFileModeEnabled());
Expand All @@ -67,12 +67,23 @@ namespace CppCoverageTest
{
cov::OptionsParser parser;

ASSERT_TRUE(TestTools::Parse(parser,
{ optionShortPrefix + cov::ProgramOptions::VerboseShortOption })->IsVerboseModeEnabled());
ASSERT_TRUE(TestTools::Parse(parser,
{ TestTools::OptionPrefix + cov::ProgramOptions::VerboseOption })->IsVerboseModeEnabled());
ASSERT_EQ(cov::LogLevel::Verbose, TestTools::Parse(parser,
{ optionShortPrefix + cov::ProgramOptions::VerboseShortOption })->GetLogLevel());
ASSERT_EQ(cov::LogLevel::Verbose, TestTools::Parse(parser,
{ TestTools::OptionPrefix + cov::ProgramOptions::VerboseOption })->GetLogLevel());
}

//-------------------------------------------------------------------------
TEST(OptionsParserTest, Quiet)
{
cov::OptionsParser parser;

ASSERT_EQ(cov::LogLevel::Quiet, TestTools::Parse(parser,
{ optionShortPrefix + cov::ProgramOptions::QuietShortOption })->GetLogLevel());
ASSERT_EQ(cov::LogLevel::Quiet, TestTools::Parse(parser,
{ TestTools::OptionPrefix + cov::ProgramOptions::QuietOption })->GetLogLevel());
}

//-------------------------------------------------------------------------
TEST(OptionsParserTest, Plugin)
{
Expand Down
9 changes: 8 additions & 1 deletion OpenCppCoverage/OpenCppCoverage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,14 @@ namespace OpenCppCoverage
//-----------------------------------------------------------------------------
void InitLogger(const cov::Options& options)
{
auto logLevel = (options.IsVerboseModeEnabled()) ? logging::trivial::debug : logging::trivial::info;
auto logLevel = logging::trivial::info;

switch (options.GetLogLevel())
{
case cov::LogLevel::Verbose: logLevel = logging::trivial::debug; break;
case cov::LogLevel::Quiet: logLevel = logging::trivial::error; break;
}

Tools::InitConsoleAndFileLog(L"LastCoverageResults.log");
Tools::SetLoggerMinSeverity(logLevel);
}
Expand Down

0 comments on commit cf8d54b

Please sign in to comment.