Add support for redirecting or disabling messages from stdout to elsewhere.

The newly introduced procedure wiiuse_set_output can be used to set the FILE*
used for each of the loglevels LOGLEVEL_ERROR, LOGLEVEL_WARNING, LOGLEVEL_INFO,
and LOGLEVEL_DEBUG. Setting the logfile to 0 disables log output for a
loglevel. This adds the macro definition WIIUSE_HAS_OUTPUT_REDIRECTION, so
programs can test for the feature.

The internal log macros in definitions.h all honor this output redirection mechanism.
This commit is contained in:
Johannes Zarl
2011-04-18 17:01:58 +02:00
committed by Ryan Pavlik
parent ca4a38c31c
commit 4829592601
3 changed files with 53 additions and 9 deletions

View File

@@ -41,25 +41,34 @@
//#define WITH_WIIUSE_DEBUG
extern FILE* logtarget[];
#define OUTF_ERROR logtarget[0]
#define OUTF_WARNING logtarget[1]
#define OUTF_INFO logtarget[2]
#define OUTF_DEBUG logtarget[3]
/* Error output macros */
#define WIIUSE_ERROR(fmt, ...) fprintf(stderr, "[ERROR] " fmt "\n", ##__VA_ARGS__)
#define WIIUSE_ERROR(fmt, ...) do { if (OUTF_ERROR) fprintf(OUTF_ERROR, "[ERROR] " fmt "\n", ##__VA_ARGS__); } while(0)
/* Warning output macros */
#define WIIUSE_WARNING(fmt, ...) fprintf(stderr, "[WARNING] " fmt "\n", ##__VA_ARGS__)
#define WIIUSE_WARNING(fmt, ...) do { if (OUTF_WARNING) fprintf(OUTF_WARNING, "[WARNING] " fmt "\n", ##__VA_ARGS__); } while(0)
/* Information output macros */
#define WIIUSE_INFO(fmt, ...) fprintf(stderr, "[INFO] " fmt "\n", ##__VA_ARGS__)
#define WIIUSE_INFO(fmt, ...) do { if (OUTF_INFO) fprintf(OUTF_INFO, "[INFO] " fmt "\n", ##__VA_ARGS__); } while(0)
#ifdef WITH_WIIUSE_DEBUG
#ifdef WIN32
#define WIIUSE_DEBUG(fmt, ...) do { \
char* file = __FILE__; \
int i = strlen(file) - 1; \
for (; i && (file[i] != '\\'); --i); \
fprintf(stderr, "[DEBUG] %s:%i: " fmt "\n", file+i+1, __LINE__, ##__VA_ARGS__); \
#define WIIUSE_DEBUG(fmt, ...) do { \
if (OUTF_DEBUG) { \
char* file = __FILE__; \
int i = strlen(file) - 1; \
for (; i && (file[i] != '\\'); --i); \
fprintf(OUTF_DEBUG, "[DEBUG] %s:%i: " fmt "\n", file+i+1, __LINE__, ##__VA_ARGS__); \
} \
} while (0)
#else
#define WIIUSE_DEBUG(fmt, ...) fprintf(stderr, "[DEBUG] " __FILE__ ":%i: " fmt "\n", __LINE__, ##__VA_ARGS__)
#define WIIUSE_DEBUG(fmt, ...) do { if (OUTF_DEBUG) fprintf(OUTF_DEBUG, "[DEBUG] " __FILE__ ":%i: " fmt "\n", __LINE__, ##__VA_ARGS__); } while (0)
#endif
#else
#define WIIUSE_DEBUG(fmt, ...)

View File

@@ -58,6 +58,24 @@ const char* wiiuse_version() {
return WIIUSE_VERSION;
}
/**
* @brief Output FILE stream for each wiiuse_loglevel.
*/
FILE* logtarget[4];
/**
* @brief Initialize an array of wiimote structures.
*
* @param loglevel The loglevel, for which the output should be set.
*
* @param logfile A valid, writeable <code>FILE*</code>, or 0, if output should be disabled.
*
* The default <code>FILE*</code> for all loglevels is <code>stderr</code>
*/
void wiiuse_set_output(enum wiiuse_loglevel loglevel, FILE *logfile)
{
logtarget[(int)loglevel] = logfile;
}
/**
* @brief Clean up wiimote_t array created by wiiuse_init()
@@ -112,6 +130,11 @@ struct wiimote_t** wiiuse_init(int wiimotes) {
g_banner = 1;
}
logtarget[0] = stderr;
logtarget[1] = stderr;
logtarget[2] = stderr;
logtarget[3] = stderr;
if (!wiimotes)
return NULL;

View File

@@ -612,6 +612,16 @@ typedef struct wiimote_t {
WCONST byte event_buf[MAX_PAYLOAD]; /**< event buffer */
} wiimote;
/**
* @enum WIIUSE_LOGLEVEL
* @brief Loglevels supported by wiiuse.
*/
typedef enum wiiuse_loglevel {
LOGLEVEL_ERROR = 0,
LOGLEVEL_WARNING = 1,
LOGLEVEL_INFO = 2,
LOGLEVEL_DEBUG = 3
} wiiuse_loglevel;
/*****************************************
*
@@ -639,6 +649,8 @@ extern "C" {
/* wiiuse.c */
WIIUSE_EXPORT extern const char* wiiuse_version();
#define WIIUSE_HAS_OUTPUT_REDIRECTION
WIIUSE_EXPORT extern void wiiuse_set_output(enum wiiuse_loglevel loglevel, FILE *logtarget);
WIIUSE_EXPORT extern struct wiimote_t** wiiuse_init(int wiimotes);
WIIUSE_EXPORT extern void wiiuse_disconnected(struct wiimote_t* wm);