00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #ifdef HAVE_CONFIG_H
00025 #include <config.h>
00026 #endif
00027
00028 #ifdef HAVE_STDLIB_H
00029 #include <stdlib.h>
00030 #endif
00031
00032 #ifdef HAVE_STDIO_H
00033 #include <stdio.h>
00034 #endif
00035
00036 #ifdef HAVE_STDARG_H
00037 #include <stdarg.h>
00038 #endif
00039
00040 #ifdef HAVE_STRING_H
00041 #include <string.h>
00042 #endif
00043
00044 #ifdef HAVE_ERRNO_H
00045 #include <errno.h>
00046 #endif
00047
00048 #include "nanohttp-logging.h"
00049 #include "nanohttp-error.h"
00050
00051 typedef struct _herror_impl_t
00052 {
00053 int errcode;
00054 char message[250];
00055 char *func;
00056 } herror_impl_t;
00057
00058 herror_t
00059 herror_new(const char *func, int errcode, const char *format, ...)
00060 {
00061 va_list ap;
00062 herror_impl_t *impl;
00063
00064 if (!(impl = (herror_impl_t *) malloc(sizeof(herror_impl_t))))
00065 {
00066 log_error2("malloc failed (%s)", strerror(errno));
00067 return NULL;
00068 }
00069
00070 impl->errcode = errcode;
00071
00072 if (func)
00073 impl->func = strdup(func);
00074 else
00075 func = NULL;
00076
00077 va_start(ap, format);
00078 vsprintf(impl->message, format, ap);
00079 va_end(ap);
00080
00081 return (herror_t) impl;
00082 }
00083
00084 int
00085 herror_code(herror_t err)
00086 {
00087 herror_impl_t *impl = (herror_impl_t *) err;
00088
00089 if (!err)
00090 return H_OK;
00091
00092 return impl->errcode;
00093 }
00094
00095 const char *
00096 herror_func(herror_t err)
00097 {
00098 herror_impl_t *impl = (herror_impl_t *) err;
00099
00100 if (!err)
00101 return "";
00102
00103 return impl->func;
00104 }
00105
00106 const char *
00107 herror_message(herror_t err)
00108 {
00109 herror_impl_t *impl = (herror_impl_t *) err;
00110
00111 if (!err)
00112 return "";
00113
00114 return impl->message;
00115 }
00116
00117 void
00118 herror_release(herror_t err)
00119 {
00120 herror_impl_t *impl = (herror_impl_t *) err;
00121
00122 if (!err)
00123 return;
00124
00125 if (impl->func)
00126 free(impl->func);
00127
00128 free(impl);
00129
00130 return;
00131 }