Skip to content

Commit

Permalink
changed to output error messages to stderr
Browse files Browse the repository at this point in the history
the qWarning() function should not be used (can be disabled)
modified CommandLine::cout() to open stdout or stderr
added '-?' or '-h' for help option
changed Tester table errors from using qWarning() to qCritical()
added CommandLine::coutClose() to close stream (code from destructor)
modified CommandLine::~CommandLine() to call coutClose()
  • Loading branch information
thunder422 committed Dec 1, 2012
1 parent 164fb19 commit e64eade
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 17 deletions.
43 changes: 29 additions & 14 deletions commandline.cpp
Expand Up @@ -57,7 +57,7 @@ CommandLine::CommandLine(const QStringList &args)
Tester tester(args);
if (tester.hasError())
{
qWarning("%s", qPrintable(tester.errorMessage()));
cout(stderr) << tester.errorMessage() << endl;
m_returnCode = 1;
return;
}
Expand All @@ -69,46 +69,54 @@ CommandLine::CommandLine(const QStringList &args)
}
else
{
qWarning("%s", qPrintable(tester.errorMessage()));
coutClose(); // close stdout
cout(stderr) << tester.errorMessage() << endl;
m_returnCode = 1;
}
return;
}

// unsupported option (NOTE: other options get checked before this)
QStringList options = tester.options();
options.prepend("-v");
// append any other options here
qWarning("%s: %s %s", qPrintable(tr("usage")), qPrintable(m_programName),
qPrintable(options.join("|")));
m_returnCode = 1;
options.prepend("-h|-?|-v");
m_returnCode = isHelpOption(args) ? 0 : 1; // error if not help option
cout(m_returnCode == 0 ? stdout : stderr) << tr("usage: ") << m_programName
<< options.join("|") << endl;
}


CommandLine::~CommandLine()
{
if (m_cout.device())
{
QIODevice *output = m_cout.device();
m_cout.setDevice(0);
delete output;
}
coutClose();
}


QTextStream &CommandLine::cout(void)
QTextStream &CommandLine::cout(FILE *stream)
{
if (!m_cout.device())
{
// setup standard output stream first time
QFile *output = new QFile;
output->open(stdout, QIODevice::WriteOnly | QIODevice::Unbuffered);
output->open(stream, QIODevice::WriteOnly | QIODevice::Unbuffered);
m_cout.setDevice(output);
}
return m_cout;
}



void CommandLine::coutClose(void)
{
if (m_cout.device())
{
QIODevice *output = m_cout.device();
m_cout.setDevice(0);
delete output;
}
}


// function to print version number
bool CommandLine::version(const QStringList &args)
{
Expand All @@ -122,4 +130,11 @@ bool CommandLine::version(const QStringList &args)
}


// function to check for help options
bool CommandLine::isHelpOption(const QStringList &args) const
{
return args.count() == 2 && (args.at(1) == "-?" || args.at(1) == "-h");
}


// end: commandline.cpp
4 changes: 3 additions & 1 deletion commandline.h
Expand Up @@ -34,7 +34,9 @@ class CommandLine
Q_DECLARE_TR_FUNCTIONS(CommandLine)

bool version(const QStringList &args);
QTextStream &cout(void);
QTextStream &cout(FILE *stream = stdout);
void cclose(void);
bool isHelpOption(const QStringList &args) const;

QString m_programName;
QStringList m_gplStatement;
Expand Down
2 changes: 1 addition & 1 deletion mainwindow.cpp
Expand Up @@ -35,7 +35,7 @@ MainWindow::MainWindow(QWidget *parent) :
CommandLine commandLine(qApp->arguments());
if (commandLine.processed())
{
// force quit once event processing loop is started
// don't start GUI and retrieve the return code
m_guiActive = false;
m_returnCode = commandLine.returnCode();
return;
Expand Down
3 changes: 2 additions & 1 deletion test_ibcp.cpp
Expand Up @@ -151,7 +151,8 @@ bool Tester::run(QTextStream &cout, const QStringList &gplStatement)
int n = 0;
foreach (QString error, errors)
{
qWarning("%s", qPrintable(tr("Error #%1: %2").arg(++n).arg(error)));
qCritical("%s", qPrintable(tr("Error #%1: %2").arg(++n)
.arg(error)));
}
qFatal("%s", qPrintable(tr("Program aborting!")));
}
Expand Down

0 comments on commit e64eade

Please sign in to comment.