diff options
| author | Sven Eisenhauer <sven@sven-eisenhauer.net> | 2023-11-10 15:11:48 +0100 |
|---|---|---|
| committer | Sven Eisenhauer <sven@sven-eisenhauer.net> | 2023-11-10 15:11:48 +0100 |
| commit | 33613a85afc4b1481367fbe92a17ee59c240250b (patch) | |
| tree | 670b842326116b376b505ec2263878912fca97e2 /Bachelor/Verteilte Systeme/Praktikum1/error.cpp | |
| download | Studium-master.tar.gz Studium-master.tar.bz2 | |
Diffstat (limited to 'Bachelor/Verteilte Systeme/Praktikum1/error.cpp')
| -rw-r--r-- | Bachelor/Verteilte Systeme/Praktikum1/error.cpp | 331 |
1 files changed, 331 insertions, 0 deletions
diff --git a/Bachelor/Verteilte Systeme/Praktikum1/error.cpp b/Bachelor/Verteilte Systeme/Praktikum1/error.cpp new file mode 100644 index 0000000..5bf172d --- /dev/null +++ b/Bachelor/Verteilte Systeme/Praktikum1/error.cpp @@ -0,0 +1,331 @@ +/************************************************************************
+ * 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 <varargs.h>
+#include <string.h>
+#include <stdarg.h>
+#include <errno.h>
+
+#include <iostream>
+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 <syslog.h>
+
+#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 ------------------- */
|
