1
0
mirror of https://github.com/gryf/wmaker.git synced 2025-12-28 09:22:27 +01:00

Fold many functions into one

The result is not much more convoluted than the original was, but much shorter.
Several vararg macros were added -- no idea what !gcc compilers make of this.

The messages sent to these functions are inconsistent across the source tree.
I have now decided that the logging function will add the final newline -
messages will need to be modified accordingly.

I have no idea why the original implementation was as complicated and ugly as
it was. My guess is that it was anticipated that these are be called from
sighandlers, but why no snprintf when they are all stuffed up with vsnprintf...

Still, the result is not worse in this regard either.
This commit is contained in:
Tamas TEVESZ
2010-03-17 04:04:18 +01:00
committed by Carlos R. Mafra
parent 63e4338284
commit 109e504262
2 changed files with 54 additions and 124 deletions

View File

@@ -219,11 +219,22 @@ waborthandler* wsetabort(waborthandler* handler);
/* don't free the returned string */ /* don't free the returned string */
char* wstrerror(int errnum); char* wstrerror(int errnum);
void wmessage(const char *msg, ...) __attribute__((__format__(printf,1,2))); enum {
void wwarning(const char *msg, ...) __attribute__((__format__(printf,1,2))); WMESSAGE_TYPE_MESSAGE,
void wfatal(const char *msg, ...) __attribute__((__format__(printf,1,2))); WMESSAGE_TYPE_WARNING,
void wsyserror(const char *msg, ...) __attribute__((__format__(printf,1,2))); WMESSAGE_TYPE_FATAL,
void wsyserrorwithcode(int error, const char *msg, ...) __attribute__((__format__(printf,2,3))); WMESSAGE_TYPE_WSYSERROR,
WMESSAGE_TYPE_WSYSERRORWITHCODE
};
#define wmessage(fmt, args...) __wmessage( WMESSAGE_TYPE_MESSAGE, NULL, fmt, ## args)
#define wwarning(fmt, args...) __wmessage( WMESSAGE_TYPE_WARNING, NULL, fmt, ## args)
#define wfatal(fmt, args...) __wmessage( WMESSAGE_TYPE_FATAL, NULL, fmt, ## args)
#define wsyserror(fmt, args...) __wmessage( WMESSAGE_TYPE_WSYSERROR, NULL, fmt, ## args)
#define wsyserrorwithcode(errno, fmt, args...) \
__wmessage( WMESSAGE_TYPE_WSYSERRORWITHCODE, &errno, fmt, ## args)
void __wmessage(int type, void *extra, const char *msg, ...) __attribute__((__format__(printf,3,4)));
char* wfindfile(char *paths, char *file); char* wfindfile(char *paths, char *file);

View File

@@ -26,6 +26,8 @@
#include <string.h> #include <string.h>
#include <errno.h> #include <errno.h>
#include <WUtil.h>
extern char *_WINGS_progname; extern char *_WINGS_progname;
#define MAXLINE 1024 #define MAXLINE 1024
@@ -57,130 +59,47 @@ char *wstrerror(int errnum)
#endif #endif
} }
/********************************************************************* void __wmessage(int type, void *extra, const char *msg, ...)
* Prints a message with variable arguments
*
* msg - message to print with optional formatting
* ... - arguments to use on formatting
*********************************************************************/
void wmessage(const char *msg, ...)
{ {
va_list args; va_list args;
char buf[MAXLINE]; char buf[MAXLINE];
fflush(stdout);
/* message format: <wings_progname>: <qualifier>: <message>[: <extra info>]"\n" */
snprintf(buf, sizeof(buf), "%s: ", _WINGS_progname ? _WINGS_progname : "WINGs");
va_start(args, msg); va_start(args, msg);
switch (type) {
vsnprintf(buf, MAXLINE - 3, msg, args); case WMESSAGE_TYPE_WARNING:
strcat(buf, "\n"); snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
fflush(stdout); _("warning: "));
fputs(_WINGS_progname ? _WINGS_progname : "WINGs", stderr); vsnprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
fputs(": ", stderr); msg, args);
fputs(buf, stderr); break;
fflush(stdout); case WMESSAGE_TYPE_FATAL:
fflush(stderr); snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
_("fatal error: "));
vsnprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
msg, args);
break;
case WMESSAGE_TYPE_WSYSERROR:
case WMESSAGE_TYPE_WSYSERRORWITHCODE:
snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
_("error: "));
vsnprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
msg, args);
snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
": %s", type == WMESSAGE_TYPE_WSYSERROR ?
wstrerror(errno) : wstrerror(*(int *)extra));
break;
case WMESSAGE_TYPE_MESSAGE:
/* FALLTHROUGH */
default:
snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), ": ");
vsnprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), msg, args);
break;
}
va_end(args); va_end(args);
strncat(buf + strlen(buf), "\n", sizeof(buf) - strlen(buf) - 1);
fputs(buf, stderr);
} }
/*********************************************************************
* Prints a warning message with variable arguments
*
* msg - message to print with optional formatting
* ... - arguments to use on formatting
*********************************************************************/
void wwarning(const char *msg, ...)
{
va_list args;
char buf[MAXLINE];
va_start(args, msg);
vsnprintf(buf, MAXLINE - 3, msg, args);
strcat(buf, "\n");
fflush(stdout);
fputs(_WINGS_progname ? _WINGS_progname : "WINGs", stderr);
fputs(_(" warning: "), stderr);
fputs(buf, stderr);
fflush(stdout);
fflush(stderr);
va_end(args);
}
/**************************************************************************
* Prints a fatal error message with variable arguments and terminates
*
* msg - message to print with optional formatting
* ... - arguments to use on formatting
**************************************************************************/
void wfatal(const char *msg, ...)
{
va_list args;
char buf[MAXLINE];
va_start(args, msg);
vsnprintf(buf, MAXLINE - 3, msg, args);
strcat(buf, "\n");
fflush(stdout);
fputs(_WINGS_progname ? _WINGS_progname : "WINGs", stderr);
fputs(_(" fatal error: "), stderr);
fputs(buf, stderr);
fflush(stdout);
fflush(stderr);
va_end(args);
}
/*********************************************************************
* Prints a system error message with variable arguments
*
* msg - message to print with optional formatting
* ... - arguments to use on formatting
*********************************************************************/
void wsyserror(const char *msg, ...)
{
va_list args;
char buf[MAXLINE];
int error = errno;
va_start(args, msg);
vsnprintf(buf, MAXLINE - 3, msg, args);
fflush(stdout);
fputs(_WINGS_progname ? _WINGS_progname : "WINGs", stderr);
fputs(_(" error: "), stderr);
fputs(buf, stderr);
fputs(": ", stderr);
fputs(wstrerror(error), stderr);
fputs("\n", stderr);
fflush(stderr);
fflush(stdout);
va_end(args);
}
/*********************************************************************
* Prints a system error message with variable arguments, being given
* the error code.
*
* error - the error code foe which to print the message
* msg - message to print with optional formatting
* ... - arguments to use on formatting
*********************************************************************/
void wsyserrorwithcode(int error, const char *msg, ...)
{
va_list args;
char buf[MAXLINE];
va_start(args, msg);
vsnprintf(buf, MAXLINE - 3, msg, args);
fflush(stdout);
fputs(_WINGS_progname ? _WINGS_progname : "WINGs", stderr);
fputs(_(" error: "), stderr);
fputs(buf, stderr);
fputs(": ", stderr);
fputs(wstrerror(error), stderr);
fputs("\n", stderr);
fflush(stderr);
fflush(stdout);
va_end(args);
}