aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGertjan van den Burg <burg@ese.eur.nl>2016-09-20 16:41:52 +0200
committerGertjan van den Burg <burg@ese.eur.nl>2016-09-20 16:41:52 +0200
commit014d9b5e6b0de17a1910c32347bf86de48e06af4 (patch)
treeea87fcc0dc21b5fde4723f22e0b007870edbc348
parentFix memory leak (diff)
downloadgensvm-014d9b5e6b0de17a1910c32347bf86de48e06af4.tar.gz
gensvm-014d9b5e6b0de17a1910c32347bf86de48e06af4.zip
Use file stream for errors too
-rw-r--r--src/GenSVMgrid.c5
-rw-r--r--src/GenSVMtraintest.c8
-rw-r--r--src/gensvm_print.c17
3 files changed, 24 insertions, 6 deletions
diff --git a/src/GenSVMgrid.c b/src/GenSVMgrid.c
index e2cb161..2ac9ce3 100644
--- a/src/GenSVMgrid.c
+++ b/src/GenSVMgrid.c
@@ -26,6 +26,7 @@
#define MINARGS 2
extern FILE *GENSVM_OUTPUT_FILE;
+extern FILE *GENSVM_ERROR_FILE;
// function declarations
void exit_with_help();
@@ -41,7 +42,7 @@ void exit_with_help()
printf("Usage: trainGenSVMdataset [options] grid_file\n");
printf("Options:\n");
printf("-h | -help : print this help.\n");
- printf("-q : quiet mode (no output)\n");
+ printf("-q : quiet mode (no output, not even errors!)\n");
exit(EXIT_FAILURE);
}
@@ -128,6 +129,7 @@ void parse_command_line(int argc, char **argv, char *input_filename)
int i;
GENSVM_OUTPUT_FILE = stdout;
+ GENSVM_ERROR_FILE = stderr;
for (i=1; i<argc; i++) {
if (argv[i][0] != '-') break;
@@ -136,6 +138,7 @@ void parse_command_line(int argc, char **argv, char *input_filename)
switch (argv[i-1][1]) {
case 'q':
GENSVM_OUTPUT_FILE = NULL;
+ GENSVM_ERROR_FILE = NULL;
i--;
break;
default:
diff --git a/src/GenSVMtraintest.c b/src/GenSVMtraintest.c
index 4d99c04..34b1677 100644
--- a/src/GenSVMtraintest.c
+++ b/src/GenSVMtraintest.c
@@ -18,6 +18,7 @@
#define MINARGS 2
extern FILE *GENSVM_OUTPUT_FILE;
+extern FILE *GENSVM_ERROR_FILE;
// function declarations
void exit_with_help();
@@ -45,7 +46,7 @@ void exit_with_help()
"file\n");
printf("-p p-value : set the value of p in the lp norm "
"(1.0 <= p <= 2.0)\n");
- printf("-q : quiet mode (no output)\n");
+ printf("-q : quiet mode (no output, not even errors!)\n");
printf("-r rho : choose the weigth specification (1 = unit, 2 = "
"group)\n");
printf("-t type: kerneltype (0=LINEAR, 1=POLY, 2=RBF, 3=SIGMOID)\n");
@@ -153,6 +154,7 @@ void parse_command_line(int argc, char **argv, struct GenModel *model,
coef = 0.0;
GENSVM_OUTPUT_FILE = stdout;
+ GENSVM_ERROR_FILE = stderr;
// parse options
for (i=1; i<argc; i++) {
@@ -205,9 +207,13 @@ void parse_command_line(int argc, char **argv, struct GenModel *model,
break;
case 'q':
GENSVM_OUTPUT_FILE = NULL;
+ GENSVM_ERROR_FILE = NULL;
i--;
break;
default:
+ // this one should always print explicitly to
+ // stderr, even if '-q' is supplied, because
+ // otherwise you can't debug cmdline flags.
fprintf(stderr, "Unknown option: -%c\n",
argv[i-1][1]);
exit_with_help();
diff --git a/src/gensvm_print.c b/src/gensvm_print.c
index 2c00512..9ac210e 100644
--- a/src/gensvm_print.c
+++ b/src/gensvm_print.c
@@ -12,7 +12,7 @@
#include "gensvm_print.h"
-FILE *GENSVM_OUTPUT_FILE; ///< The #GENSVM_OUTPUT_FILE specifies the
+FILE *GENSVM_OUTPUT_FILE = NULL; ///< The #GENSVM_OUTPUT_FILE specifies the
///< output stream to which all output is
///< written. This is done through the
///< internal (!)
@@ -23,6 +23,13 @@ FILE *GENSVM_OUTPUT_FILE; ///< The #GENSVM_OUTPUT_FILE specifies the
///< this variable through @c extern and
///< (temporarily) setting it to NULL.
+FILE *GENSVM_ERROR_FILE = NULL; ///< The #GENSVM_ERROR_FILE specifies the
+ ///< output stream to use when writing an
+ ///< error. Typically this is stderr, but
+ ///< when unit testing we can temporarily
+ ///< redirect this to check if the correct
+ ///< output is written.
+
/**
* @brief Print a given string to the specified output stream
*
@@ -71,7 +78,7 @@ void note(const char *fmt,...)
* @brief Parse a formatted string and write it to standard error
*
* @details
- * Shorthand for fprintf(stderr, ...)
+ * Shorthand for fprintf(GENSVM_ERROR_FILE, ...)
*
* @param[in] fmt string format
* @param[in] ... variable argument list for the string format
@@ -83,6 +90,8 @@ void err(const char *fmt, ...)
va_start(ap, fmt);
vsprintf(buf, fmt, ap);
va_end(ap);
- fputs(buf, stderr);
- fflush(stderr);
+ if (GENSVM_ERROR_FILE != NULL) {
+ fputs(buf, GENSVM_ERROR_FILE);
+ fflush(GENSVM_ERROR_FILE);
+ }
}