File: logPushMsg.c

package info (click to toggle)
unixodbc 2.3.1-3
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 11,524 kB
  • ctags: 7,656
  • sloc: ansic: 89,405; sh: 15,975; makefile: 1,574; yacc: 969; sql: 1
file content (215 lines) | stat: -rw-r--r-- 6,090 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
/**********************************************************************
 * logPushMsg
 *
 *
 * This code was created by Peter Harvey (mostly during Christmas 98/99).
 * This code is LGPL. Please ensure that this message remains in future
 * distributions and uses of this code (thats about all I get out of it).
 * - Peter Harvey pharvey@codebydesign.com
 *
 **********************************************************************/

#include <config.h>
#include "log.h"
#include "ini.h"

/* Define this as a fall through, HAVE_STDARG_H is probably already set */

/*#define HAVE_VARARGS_H*/

/* varargs declarations: */

#if defined(HAVE_STDARG_H)
# include <stdarg.h>
    #define VA_LOCAL_DECL   va_list ap
    #define VA_START(f)     va_start(ap, f)
    #define VA_SHIFT(v,t)  ;   /* no-op for ANSI */
    #define VA_END          va_end(ap)
#else
    #if defined(HAVE_VARARGS_H)
        #define VA_LOCAL_DECL   va_list ap
        #define VA_START(f)     va_start(ap)      /* f is ignored! */
        #define VA_SHIFT(v,t) v = va_arg(ap,t)
        #define VA_END        va_end(ap)
    #else
        #error No variable argument support
    #endif
#endif

#ifndef HAVE_VSNPRINTF 
int uodbc_vsnprintf (char *str, size_t count, const char *fmt, va_list args);
#endif


int logPushMsg( HLOG hLog, char *pszModule, char *pszFunctionName, int nLine, int nSeverity, int nCode, char *pszMessage )
{
    HLOGMSG     hMsg;
    FILE        *hFile;

    if ( !hLog ) return LOG_ERROR;
    if ( !hLog->hMessages ) return LOG_ERROR;
    if ( !hLog->bOn ) return LOG_SUCCESS;

    if ( !pszModule ) return LOG_ERROR;
    if ( !pszFunctionName ) return LOG_ERROR;
    if ( !pszMessage ) return LOG_ERROR;


    /* check for, and handle, max msg */
    if ( hLog->nMaxMsgs && hLog->hMessages->nItems >= hLog->nMaxMsgs )
        logPopMsg( hLog );

    hMsg                    = malloc( sizeof(LOGMSG) );
    if (!hMsg) goto error_abort0;

    hMsg->pszModuleName     = (char *)strdup( pszModule );
    if (!hMsg->pszModuleName) goto error_abort1;

    hMsg->pszFunctionName   = (char *)strdup( pszFunctionName );
    if (!hMsg->pszFunctionName) goto error_abort2;

    hMsg->pszMessage        = (char *)strdup( pszMessage );
    if (!hMsg->pszMessage) goto error_abort3;

    hMsg->nLine             = nLine;
    hMsg->nSeverity         = nSeverity;
    hMsg->nCode             = nCode;

    /* append to  list */
    lstAppend( hLog->hMessages, hMsg );

    /* append to file */
    if ( hLog->pszLogFile )
    {
        hFile = uo_fopen( hLog->pszLogFile, "a" );
        if ( !hFile ) return LOG_ERROR;

        uo_fprintf( hFile, "[%s][%s][%s][%d]%s\n", hLog->pszProgramName, pszModule, pszFunctionName, nLine, pszMessage );

        uo_fclose( hFile );
    }

    return LOG_SUCCESS;

    error_abort3:
    free(hMsg->pszFunctionName);
    error_abort2:
    free(hMsg->pszModuleName);
    error_abort1:
    free(hMsg);
    error_abort0:
    return LOG_ERROR;
}

int logvPushMsgf( HLOG hLog, char *pszModule, char *pszFunctionName, int nLine, int nSeverity, int nCode, char *pszMessageFormat, va_list args )
{
    HLOGMSG     hMsg=NULL;
    FILE        *hFile;
    int             mlen=0;

    if ( !hLog ) return LOG_ERROR;
    if ( !hLog->hMessages ) return LOG_ERROR;
    if ( !hLog->bOn ) return LOG_SUCCESS;

    if ( !pszModule ) return LOG_ERROR;
    if ( !pszFunctionName ) return LOG_ERROR;
    if ( !pszMessageFormat ) return LOG_ERROR;

    /* check for, and handle, max msg */
    if ( hLog->nMaxMsgs && hLog->hMessages->nItems == hLog->nMaxMsgs )
        logPopMsg( hLog );

    hMsg                    = malloc( sizeof(LOGMSG) );
    if (!hMsg) goto error_abort0;

    hMsg->pszModuleName     = (char *)strdup( pszModule );
    if (!hMsg->pszModuleName) goto error_abort1;

    hMsg->pszFunctionName   = (char *)strdup( pszFunctionName );
    if (!hMsg->pszFunctionName) goto error_abort2;

#if defined( HAVE_VSNPRINTF )
    mlen=vsnprintf(NULL,0,pszMessageFormat,args);
#else
    mlen=uodbc_vsnprintf(NULL,0,pszMessageFormat,args);
#endif
    mlen++;
    hMsg->pszMessage        = malloc(mlen);
    if (!hMsg->pszMessage) goto error_abort3;

#if defined( HAVE_VSNPRINTF )
    vsnprintf(hMsg->pszMessage,mlen,pszMessageFormat,args);
#else
    uodbc_vsnprintf(hMsg->pszMessage,mlen,pszMessageFormat,args);
#endif

    hMsg->nLine             = nLine;
    hMsg->nSeverity         = nSeverity;
    hMsg->nCode             = nCode;

    /* append to  list */
    lstAppend( hLog->hMessages, hMsg );

    /* append to file */
    if ( hLog->pszLogFile )
    {
        hFile = uo_fopen( hLog->pszLogFile, "a" );
        if ( !hFile ) return LOG_ERROR;

        if (hMsg)
        {
            uo_fprintf( hFile, "[%s][%s][%s][%d]%s\n", hLog->pszProgramName, pszModule, pszFunctionName, nLine, hMsg->pszMessage );
        }
        else
        {
            uo_fprintf( hFile, "[%s][%s][%s][%d]", hLog->pszProgramName, pszModule, pszFunctionName, nLine );
            uo_vfprintf( hFile, pszMessageFormat, args );
            uo_fprintf( hFile, "\n" );
        }

        uo_fclose( hFile );
    }

    return LOG_SUCCESS;

    error_abort3:
    free(hMsg->pszFunctionName);
    error_abort2:
    free(hMsg->pszModuleName);
    error_abort1:
    free(hMsg);
    error_abort0:
    return LOG_ERROR;
}

#ifdef HAVE_STDARGS
int logPushMsgf( HLOG hLog, char *pszModule, char *pszFunctionName, int nLine, int nSeverity, int nCode, char *pszMessageFormat, ... )
#else
int logPushMsgf( va_alist ) va_dcl
#endif
{
    int err;
#ifndef HAVE_STDARGS
    HLOG hLog;
    char *pszModule;
    char *pszFunctionName;
    int nLine;
    int nSeverity;
    int nCode;
    char *pszMessageFormat;
#endif
    VA_LOCAL_DECL;

    VA_START (pszMessageFormat);
    VA_SHIFT (hLog, HLOG);
    VA_SHIFT (pszModule, char *);
    VA_SHIFT (pszFunctionName, char *);
    VA_SHIFT (nLine, int );
    VA_SHIFT (nSeverity, int );
    VA_SHIFT (nCode, int );
    VA_SHIFT (pszMessageFormat, char *);
    err=logvPushMsgf(hLog,pszModule,pszFunctionName,nLine,nSeverity,nCode,pszMessageFormat,ap);
    VA_END;
    return err;

}