Line data Source code
1 : /**
2 : * @file gensvm_print.c
3 : * @author G.J.J. van den Burg
4 : * @date 2014-01-07
5 : * @brief Various print functions for printing to output streams
6 : *
7 : * @details
8 : * This file contains several utility functions for coordinating input and
9 : * output of data and model files. It also contains string functions.
10 : *
11 : * @copyright
12 : Copyright 2016, G.J.J. van den Burg.
13 :
14 : This file is part of GenSVM.
15 :
16 : GenSVM is free software: you can redistribute it and/or modify
17 : it under the terms of the GNU General Public License as published by
18 : the Free Software Foundation, either version 3 of the License, or
19 : (at your option) any later version.
20 :
21 : GenSVM is distributed in the hope that it will be useful,
22 : but WITHOUT ANY WARRANTY; without even the implied warranty of
23 : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 : GNU General Public License for more details.
25 :
26 : You should have received a copy of the GNU General Public License
27 : along with GenSVM. If not, see <http://www.gnu.org/licenses/>.
28 :
29 : */
30 :
31 : #include "gensvm_print.h"
32 :
33 : FILE *GENSVM_OUTPUT_FILE = NULL; ///< The #GENSVM_OUTPUT_FILE specifies the
34 : ///< output stream to which all output is
35 : ///< written. This is done through the
36 : ///< function note(). The
37 : ///< advantage of using a global output
38 : ///< stream variable is that the output can
39 : ///< temporarily be suppressed by importing
40 : ///< this variable through @c extern and
41 : ///< (temporarily) setting it to NULL.
42 :
43 : FILE *GENSVM_ERROR_FILE = NULL; ///< The #GENSVM_ERROR_FILE specifies the
44 : ///< output stream to use when writing an
45 : ///< error. Typically this is stderr, but
46 : ///< when unit testing we can temporarily
47 : ///< redirect this to check if the correct
48 : ///< output is written.
49 :
50 : /**
51 : * @brief Parse a formatted string and write to the output stream
52 : *
53 : * @details
54 : * This function is a replacement of fprintf(), such that the output stream
55 : * does not have to be specified at each function call. The functionality is
56 : * exactly the same however.
57 : *
58 : * @param[in] fmt String format
59 : * @param[in] ... variable argument list for the string format
60 : *
61 : */
62 104 : void note(const char *fmt,...)
63 : {
64 : char buf[BUFSIZ];
65 : va_list ap;
66 104 : va_start(ap,fmt);
67 104 : vsprintf(buf,fmt,ap);
68 104 : va_end(ap);
69 104 : if (GENSVM_OUTPUT_FILE != NULL) {
70 54 : fputs(buf, GENSVM_OUTPUT_FILE);
71 54 : fflush(GENSVM_OUTPUT_FILE);
72 : }
73 104 : }
74 :
75 : /**
76 : * @brief Parse a formatted string and write it to standard error
77 : *
78 : * @details
79 : * Shorthand for fprintf(GENSVM_ERROR_FILE, ...)
80 : *
81 : * @param[in] fmt string format
82 : * @param[in] ... variable argument list for the string format
83 : */
84 7 : void err(const char *fmt, ...)
85 : {
86 : char buf[BUFSIZ];
87 : va_list ap;
88 7 : va_start(ap, fmt);
89 7 : vsprintf(buf, fmt, ap);
90 7 : va_end(ap);
91 7 : if (GENSVM_ERROR_FILE != NULL) {
92 2 : fputs(buf, GENSVM_ERROR_FILE);
93 2 : fflush(GENSVM_ERROR_FILE);
94 : }
95 7 : }
|