| 1 | /********************************************************************** |
| 2 | * logPushMsg |
| 3 | * |
| 4 | * |
| 5 | * This code was created by Peter Harvey (mostly during Christmas 98/99). |
| 6 | * This code is LGPL. Please ensure that this message remains in future |
| 7 | * distributions and uses of this code (thats about all I get out of it). |
| 8 | * - Peter Harvey pharvey@codebydesign.com |
| 9 | * |
| 10 | **********************************************************************/ |
| 11 | |
| 12 | #include <config.h> |
| 13 | #include "log.h" |
| 14 | #include "ini.h" |
| 15 | |
| 16 | /* Define this as a fall through, HAVE_STDARG_H is probably already set */ |
| 17 | |
| 18 | /*#define HAVE_VARARGS_H*/ |
| 19 | |
| 20 | /* varargs declarations: */ |
| 21 | |
| 22 | #if defined(HAVE_STDARG_H) |
| 23 | # include <stdarg.h> |
| 24 | #define VA_LOCAL_DECL va_list ap |
| 25 | #define VA_START(f) va_start(ap, f) |
| 26 | #define VA_SHIFT(v,t) ; /* no-op for ANSI */ |
| 27 | #define VA_END va_end(ap) |
| 28 | #else |
| 29 | #if defined(HAVE_VARARGS_H) |
| 30 | #define VA_LOCAL_DECL va_list ap |
| 31 | #define VA_START(f) va_start(ap) /* f is ignored! */ |
| 32 | #define VA_SHIFT(v,t) v = va_arg(ap,t) |
| 33 | #define VA_END va_end(ap) |
| 34 | #else |
| 35 | #error No variable argument support |
| 36 | #endif |
| 37 | #endif |
| 38 | |
| 39 | #ifndef HAVE_VSNPRINTF |
| 40 | int uodbc_vsnprintf (char *str, size_t count, const char *fmt, va_list args); |
| 41 | #endif |
| 42 | |
| 43 | |
| 44 | int logPushMsg( HLOG hLog, char *pszModule, char *pszFunctionName, int nLine, int nSeverity, int nCode, char *pszMessage ) |
| 45 | { |
| 46 | HLOGMSG hMsg; |
| 47 | FILE *hFile; |
| 48 | |
| 49 | if ( !hLog ) return LOG_ERROR; |
| 50 | if ( !hLog->hMessages ) return LOG_ERROR; |
| 51 | if ( !hLog->bOn ) return LOG_SUCCESS; |
| 52 | |
| 53 | if ( !pszModule ) return LOG_ERROR; |
| 54 | if ( !pszFunctionName ) return LOG_ERROR; |
| 55 | if ( !pszMessage ) return LOG_ERROR; |
| 56 | |
| 57 | |
| 58 | /* check for, and handle, max msg */ |
| 59 | if ( hLog->nMaxMsgs && hLog->hMessages->nItems >= hLog->nMaxMsgs ) |
| 60 | logPopMsg( hLog ); |
| 61 | |
| 62 | hMsg = malloc( sizeof(LOGMSG) ); |
| 63 | if (!hMsg) goto error_abort0; |
| 64 | |
| 65 | hMsg->pszModuleName = (char *)strdup( pszModule ); |
| 66 | if (!hMsg->pszModuleName) goto error_abort1; |
| 67 | |
| 68 | hMsg->pszFunctionName = (char *)strdup( pszFunctionName ); |
| 69 | if (!hMsg->pszFunctionName) goto error_abort2; |
| 70 | |
| 71 | hMsg->pszMessage = (char *)strdup( pszMessage ); |
| 72 | if (!hMsg->pszMessage) goto error_abort3; |
| 73 | |
| 74 | hMsg->nLine = nLine; |
| 75 | hMsg->nSeverity = nSeverity; |
| 76 | hMsg->nCode = nCode; |
| 77 | |
| 78 | /* append to list */ |
| 79 | lstAppend( hLog->hMessages, hMsg ); |
| 80 | |
| 81 | /* append to file */ |
| 82 | if ( hLog->pszLogFile ) |
| 83 | { |
| 84 | hFile = uo_fopen( hLog->pszLogFile, "a" ); |
| 85 | if ( !hFile ) return LOG_ERROR; |
| 86 | |
| 87 | uo_fprintf( hFile, "[%s][%s][%s][%d]%s\n" , hLog->pszProgramName, pszModule, pszFunctionName, nLine, pszMessage ); |
| 88 | |
| 89 | uo_fclose( hFile ); |
| 90 | } |
| 91 | |
| 92 | return LOG_SUCCESS; |
| 93 | |
| 94 | error_abort3: |
| 95 | free(hMsg->pszFunctionName); |
| 96 | error_abort2: |
| 97 | free(hMsg->pszModuleName); |
| 98 | error_abort1: |
| 99 | free(hMsg); |
| 100 | error_abort0: |
| 101 | return LOG_ERROR; |
| 102 | } |
| 103 | |
| 104 | int logvPushMsgf( HLOG hLog, char *pszModule, char *pszFunctionName, int nLine, int nSeverity, int nCode, char *pszMessageFormat, va_list args ) |
| 105 | { |
| 106 | HLOGMSG hMsg=NULL; |
| 107 | FILE *hFile; |
| 108 | int mlen=0; |
| 109 | |
| 110 | if ( !hLog ) return LOG_ERROR; |
| 111 | if ( !hLog->hMessages ) return LOG_ERROR; |
| 112 | if ( !hLog->bOn ) return LOG_SUCCESS; |
| 113 | |
| 114 | if ( !pszModule ) return LOG_ERROR; |
| 115 | if ( !pszFunctionName ) return LOG_ERROR; |
| 116 | if ( !pszMessageFormat ) return LOG_ERROR; |
| 117 | |
| 118 | /* check for, and handle, max msg */ |
| 119 | if ( hLog->nMaxMsgs && hLog->hMessages->nItems == hLog->nMaxMsgs ) |
| 120 | logPopMsg( hLog ); |
| 121 | |
| 122 | hMsg = malloc( sizeof(LOGMSG) ); |
| 123 | if (!hMsg) goto error_abort0; |
| 124 | |
| 125 | hMsg->pszModuleName = (char *)strdup( pszModule ); |
| 126 | if (!hMsg->pszModuleName) goto error_abort1; |
| 127 | |
| 128 | hMsg->pszFunctionName = (char *)strdup( pszFunctionName ); |
| 129 | if (!hMsg->pszFunctionName) goto error_abort2; |
| 130 | |
| 131 | #if defined( HAVE_VSNPRINTF ) |
| 132 | mlen=vsnprintf(NULL,0,pszMessageFormat,args); |
| 133 | #else |
| 134 | mlen=uodbc_vsnprintf(NULL,0,pszMessageFormat,args); |
| 135 | #endif |
| 136 | mlen++; |
| 137 | hMsg->pszMessage = malloc(mlen); |
| 138 | if (!hMsg->pszMessage) goto error_abort3; |
| 139 | |
| 140 | #if defined( HAVE_VSNPRINTF ) |
| 141 | vsnprintf(hMsg->pszMessage,mlen,pszMessageFormat,args); |
| 142 | #else |
| 143 | uodbc_vsnprintf(hMsg->pszMessage,mlen,pszMessageFormat,args); |
| 144 | #endif |
| 145 | |
| 146 | hMsg->nLine = nLine; |
| 147 | hMsg->nSeverity = nSeverity; |
| 148 | hMsg->nCode = nCode; |
| 149 | |
| 150 | /* append to list */ |
| 151 | lstAppend( hLog->hMessages, hMsg ); |
| 152 | |
| 153 | /* append to file */ |
| 154 | if ( hLog->pszLogFile ) |
| 155 | { |
| 156 | hFile = uo_fopen( hLog->pszLogFile, "a" ); |
| 157 | if ( !hFile ) return LOG_ERROR; |
| 158 | |
| 159 | if (hMsg) |
| 160 | { |
| 161 | uo_fprintf( hFile, "[%s][%s][%s][%d]%s\n" , hLog->pszProgramName, pszModule, pszFunctionName, nLine, hMsg->pszMessage ); |
| 162 | } |
| 163 | else |
| 164 | { |
| 165 | uo_fprintf( hFile, "[%s][%s][%s][%d]" , hLog->pszProgramName, pszModule, pszFunctionName, nLine ); |
| 166 | uo_vfprintf( hFile, pszMessageFormat, args ); |
| 167 | uo_fprintf( hFile, "\n" ); |
| 168 | } |
| 169 | |
| 170 | uo_fclose( hFile ); |
| 171 | } |
| 172 | |
| 173 | return LOG_SUCCESS; |
| 174 | |
| 175 | error_abort3: |
| 176 | free(hMsg->pszFunctionName); |
| 177 | error_abort2: |
| 178 | free(hMsg->pszModuleName); |
| 179 | error_abort1: |
| 180 | free(hMsg); |
| 181 | error_abort0: |
| 182 | return LOG_ERROR; |
| 183 | } |
| 184 | |
| 185 | #ifdef HAVE_STDARGS |
| 186 | int logPushMsgf( HLOG hLog, char *pszModule, char *pszFunctionName, int nLine, int nSeverity, int nCode, char *pszMessageFormat, ... ) |
| 187 | #else |
| 188 | int logPushMsgf( va_alist ) va_dcl |
| 189 | #endif |
| 190 | { |
| 191 | int err; |
| 192 | #ifndef HAVE_STDARGS |
| 193 | HLOG hLog; |
| 194 | char *pszModule; |
| 195 | char *pszFunctionName; |
| 196 | int nLine; |
| 197 | int nSeverity; |
| 198 | int nCode; |
| 199 | char *pszMessageFormat; |
| 200 | #endif |
| 201 | VA_LOCAL_DECL; |
| 202 | |
| 203 | VA_START (pszMessageFormat); |
| 204 | VA_SHIFT (hLog, HLOG); |
| 205 | VA_SHIFT (pszModule, char *); |
| 206 | VA_SHIFT (pszFunctionName, char *); |
| 207 | VA_SHIFT (nLine, int ); |
| 208 | VA_SHIFT (nSeverity, int ); |
| 209 | VA_SHIFT (nCode, int ); |
| 210 | VA_SHIFT (pszMessageFormat, char *); |
| 211 | err=logvPushMsgf(hLog,pszModule,pszFunctionName,nLine,nSeverity,nCode,pszMessageFormat,ap); |
| 212 | VA_END; |
| 213 | return err; |
| 214 | |
| 215 | } |
| 216 | |