/************************************************************************ * Error handling routines. * * The functions in this file are independent of any application * variables, and may be used with any C program. * Either of the names CLIENT or SERVER may be defined when compiling * this function. If neither are defined, we assume CLIENT. */ //#include #include #include #include #include using namespace std; #ifdef CLIENT #ifdef SERVER cant define both CLIENT and SERVER #endif #endif #ifndef CLIENT #ifndef SERVER #define CLIENT 1 /* default to client */ #endif #endif #ifndef NULL #define NULL ((void *) 0) #endif char *pname = NULL; #ifdef CLIENT /* these all output to stderr */ /* ------------------- BEGIN CLIENT ------------------- */ /* * Print the UNIX errno value. */ void my_perror() { //char *sys_err_str(); char *str = strerror(errno); cerr << str << endl; //fprintf(stderr, " %s\n", sys_err_str()); } /* * Print the UNIX errno value. * We just append it to the end of the emesgstr[] array. */ /* * Fatal error. Print a message and terminate. * Don't dump core and don't print the system's errno value. * * err_quit(str, arg1, arg2, ...) * * The string "str" must specify the conversion specification for any args. */ /*VARARGS1*/ void err_quit(char *fmt, ...) { va_list args; va_start(args, fmt); if (pname != NULL) cerr << pname; fmt = va_arg(args, char *); vfprintf(stderr, fmt, args); fputc('\n', stderr); va_end(args); exit(1); } /* * Fatal error related to a system call. Print a message and terminate. * Don't dump core, but do print the system's errno value and its * associated message. * * err_sys(str, arg1, arg2, ...) * * The string "str" must specify the conversion specification for any args. */ /*VARARGS1*/ void err_sys(char *fmt, ...) { va_list args; va_start(args, fmt); if (pname != NULL) { cerr << "if (pname != NULL) {" << endl; fprintf(stderr, "%s: ", pname); } fmt = va_arg(args, char *); vfprintf(stderr, fmt, args); va_end(args); my_perror(); exit(1); } /* * Recoverable error. Print a message, and return to caller. * * err_ret(str, arg1, arg2, ...) * * The string "str" must specify the conversion specification for any args. */ /*VARARGS1*/ void err_ret(char *fmt, ...) { va_list args; va_start(args, fmt); if (pname != NULL) fprintf(stderr, "%s: ", pname); fmt = va_arg(args, char *); vfprintf(stderr, fmt, args); va_end(args); my_perror(); fflush(stdout); fflush(stderr); return; } /* * Fatal error. Print a message, dump core (for debugging) and terminate. * * err_dump(str, arg1, arg2, ...) * * The string "str" must specify the conversion specification for any args. */ /*VARARGS1*/ void err_dump(char *fmt, ...) { va_list args; va_start(args, fmt); if (pname != NULL) fprintf(stderr, "%s: ", pname); fmt = va_arg(args, char *); vfprintf(stderr, fmt, args); va_end(args); my_perror(); fflush(stdout); /* abort doesn't flush stdio buffers */ fflush(stderr); abort(); /* dump core and terminate */ exit(1); /* shouldn't get here */ } #endif /* ------------------- END CLIENT ------------------- */ /* ------------------- BEGIN SERVER ------------------- */ #ifdef SERVER #ifdef BSD /* * Under BSD, these server routines use the syslog(3) facility. * They don't append a newline, for example. */ #include #else /* not BSD */ /* * There really ought to be a better way to handle server logging * under System V. */ #define syslog(a,b) cerr << "syslog: "<< (b) #define openlog(a,b,c) cerr << (a) #endif /* BSD */ /* * Print the UNIX errno value. * We just append it to the end of the emesgstr[] array. */ char emesgstr[255] = {"test"}; /* used by all server routines */ void my_perror() { // register int len; //char *sys_err_str(); // len = strlen(emesgstr); cerr << emesgstr << strerror(errno) << endl; //sprintf(emesgstr + len, " %s", sys_err_str()); } /* * Identify ourself, for syslog() messages. * * LOG_PID is an option that says prepend each message with our pid. * LOG_CONS is an option that says write to console if unable to send * the message to syslogd. * LOG_DAEMON is our facility. */ void err_init(char *ident) { openlog(ident, (LOG_PID | LOG_CONS), LOG_DAEMON); } /* * Fatal error. Print a message and terminate. * Don't print the system's errno value. * * err_quit(str, arg1, arg2, ...) * * The string "str" must specify the conversion specification for any args. */ /*VARARGS1*/ void err_quit(char *fmt, ...) { va_list args; va_start(args, fmt); fmt = va_arg(args, char *); vsprintf(emesgstr, fmt, args); va_end(args); syslog(LOG_ERR, emesgstr); exit(1); } /* * Fatal error related to a system call. Print a message and terminate. * Don't dump core, but do print the system's errno value and its * associated message. * * err_sys(str, arg1, arg2, ...) * * The string "str" must specify the conversion specification for any args. */ /*VARARGS1*/ void err_sys(char *fmt, ...) { cerr << "err_sys: " << fmt << endl; va_list args; va_start(args, fmt); fmt = va_arg(args, char *); vsprintf(emesgstr, fmt, args); va_end(args); my_perror(); syslog(LOG_ERR, emesgstr); exit(1); } /* * Recoverable error. Print a message, and return to caller. * * err_ret(str, arg1, arg2, ...) * * The string "str" must specify the conversion specification for any args. */ /*VARARGS1*/ void err_ret(char *fmt, ...) { va_list args; va_start(args, fmt); fmt = va_arg(args, char *); vsprintf(emesgstr, fmt, args); va_end(args); my_perror(); syslog(LOG_ERR, emesgstr); return; } /* * Fatal error. Print a message, dump core (for debugging) and terminate. * * err_dump(str, arg1, arg2, ...) * * The string "str" must specify the conversion specification for any args. */ /*VARARGS1*/ void err_dump(char *fmt, ...) { va_list args; va_start(args,fmt); fmt = va_arg(args, char *); vsprintf(emesgstr, fmt, args); va_end(args); my_perror(); syslog(LOG_ERR, emesgstr); abort(); /* dump core and terminate */ exit(1); /* shouldn't get here */ } #endif /* ------------------- SERVER ------------------- */