00001 #include "errmanager.h"
00002 #include "dbug_mem.h"
00003 #include "errno.h"
00004
00005
00006 emData emd;
00007
00008
00009 int set_errno(int err)
00010 {
00011 errno = err;
00012 return -1;
00013 }
00014
00015
00016 void *set_errno_null(int err)
00017 {
00018 errno = err;
00019 return NULL;
00020 }
00021
00022
00023 void emClose(void)
00024 {
00025 closelog();
00026 }
00027
00028
00029 int emInit(int logLevel,
00030 int logType,
00031 void (*cb)(void *ctxt, char *pFile, char *pFunc, int iLine, int level, char *fmt),
00032 void *ctxt,
00033 char *filename,
00034 const char *name)
00035 {
00036 memset(&emd, 0, sizeof(emData));
00037
00038 switch(logType) {
00039 case EM_TYPE_SYSLOG:
00040 emd.logLevel = logLevel;
00041 emd.logType = logType;
00042 emd.errCallBack = NULL;
00043 emd.ctxt = NULL;
00044 emd.filename = NULL;
00045 openlog(name, LOG_CONS|LOG_PID, LOG_USER);
00046 break;
00047 case EM_TYPE_STDERR:
00048 emd.logLevel = logLevel;
00049 emd.logType = logType;
00050 emd.errCallBack = NULL;
00051 emd.ctxt = NULL;
00052 emd.filename = NULL;
00053 openlog(name, LOG_CONS|LOG_PERROR, LOG_USER);
00054 break;
00055 case EM_TYPE_CALLBACK:
00056 emd.logLevel = logLevel;
00057 emd.logType = logType;
00058 if(cb == NULL) {
00059 printf("Need a callback funtion by EM_TYPE_CALLBACK");
00060 return -1;
00061 } else {
00062 emd.errCallBack = cb;
00063 emd.ctxt = ctxt;
00064 }
00065 emd.filename = NULL;
00066 break;
00067 default:
00068 printf("Not Supported");
00069 return -1;
00070 break;
00071 }
00072
00073 return 0;
00074 }
00075
00076 int reentrant=0;
00077 void emManager(char* pFile, char* pFunc, int iLine, int level, char* fmt, ...)
00078 {
00079 va_list arglist;
00080 char msgbuf[MAX_STRLEN];
00081 char debuginfos[LOG_DEBUG+1][20] = {
00082 "EMERG:",
00083 "ALERT:",
00084 "CRIT:",
00085 "ERROR:",
00086 "WARNING:",
00087 "NOTICE:",
00088 "INFO:",
00089 "DEBUG:"
00090 };
00091
00092 if (reentrant > 0){
00093 return;
00094 }
00095 reentrant++;
00096
00097 va_start(arglist, fmt);
00098 vsnprintf(msgbuf, MAX_STRLEN, fmt, arglist);
00099 va_end(arglist);
00100
00101 if(emd.logLevel >= level) {
00102 if(emd.logLevel == LOG_DEBUG && emd.logType != EM_TYPE_CALLBACK) {
00103 syslog(level, "%-9s%s:%d: %s", debuginfos[level], pFile, iLine, msgbuf);
00104
00105
00106
00107 } else if(emd.logType != EM_TYPE_CALLBACK) {
00108 syslog(level, "%-9s%s", debuginfos[level], msgbuf);
00109
00110 } else {
00111 emd.errCallBack(emd.ctxt, pFile, pFunc, iLine, level, msgbuf);
00112 }
00113 }
00114 reentrant--;
00115 }