aboutsummaryrefslogtreecommitdiff
path: root/vendor/voclient/libsamp/sampLog.c
blob: 851c85e130fe0ff39672a545930b4fc568b56e92 (plain) (blame)
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
/**
 *  SAMPLOG.C -- SAMP trace and logging interface.
 *
 *  @file  	sampLog.c
 *  @author  	Mike Fitzpatrick
 *  @date  	6/10/09
 *
 *  @brief  SAMP trace and logging interface.  
 *
 */
/*****************************************************************************/


#include <stdio.h>
#include <fcntl.h>
#include <signal.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>

#include "samp.h"


#define	SZ_FMTSPEC	25		/* max size single format spec	*/
#define EOS		0

#define T_INT		0		/* the only types we support	*/
#define T_DOUBLE	1
#define T_CHAR		2



/* Private methods.
*/
static void    samp_encodeString (char *buf, char *format, va_list *argp);
static char   *samp_doarg (va_list **argp, int dtype);
static char   *logtime ();



/**
 *  SAMPLOG -- SAMP message logger.
 *  
 *  @brief   SAMP message logger.
 *  @fn	     sampLog (handle_t handle, char *format, ...)
 *
 *  @param   handle     SAMP handle
 *  @param   format   	message format string  
 *  @return  nothing
 */
void
sampLog (handle_t handle, char *format, ...)
{
    Samp    *sampP = samp_H2P (handle);     /* get struct pointer   */
    char    *buf;
    int      len;
    va_list  argp;


    va_start (argp, format);		/* encode as a single string	*/
    buf = calloc (1, (4 * SZ_LINE) );
    (void) samp_encodeString (buf, format, &argp);
    va_end (argp);

    len = strlen (buf);			/* ensure a newline		*/
    if (strcmp ("\n", &buf[len-1]))
	strcat (buf, "\n");
    
    if (sampP->logfd)
        fprintf (sampP->logfd, "[%s] %s", logtime(), buf);

    if (sampP->debug)
        fprintf (stderr, "[%s] %s", logtime(), buf);

    if (buf) free ((void *) buf);
}


/**
 *  SAMPTRACE -- SAMP tracer.
 *  
 *  @brief   SAMP tracer.
 *  @fn	     sampTrace (handle_t handle, char *format, ...)
 *
 *  @param   handle     SAMP handle
 *  @param   format   	message format string  
 *  @return  nothing
 */
void
sampTrace (handle_t handle, char *format, ...)
{
    Samp    *sampP = samp_H2P (handle);     /* get struct pointer   */
    char    *buf;
    int      len;
    va_list  argp;


    if (!sampP->trace)
	return;

    va_start (argp, format);		/* encode as a single string	*/
    buf = calloc (1, (4 * SZ_LINE) );
    (void) samp_encodeString (buf, format, &argp);
    va_end (argp);

    len = strlen (buf);			/* ensure a newline		*/
    if (strcmp ("\n", &buf[len-1]))
	strcat (buf, "\n");
    
    fprintf (stderr, "[%s] %s", logtime(), buf);

    if (buf) free ((void *) buf);
}



/**************************************************************************
 *  Private Methods
 *************************************************************************/

/**
 *  SAMP_ENCODESTRING -- Process the format to the output file, taking arguments
 *  from the list pointed to by argp as % format specs are encountered in 
 *  the input.
 *
 *  @fn	     samp_encodeString (char *buf, char *format, va_list *argp)
 *
 *  @param   buf     formatted output buffer
 *  @param   format  format string
 *  @param   argp    variable-length arguments
 *  @return
 */
static void
samp_encodeString (char *buf, char *format, va_list *argp)
{
    register int ch;			/* next format char reference	*/
    char	formspec[SZ_FMTSPEC];	/* copy of single format spec	*/
    char	*fsp;			/* pointer into formspec	*/
    char  	cbuf[10];
    int	        done;			/* one when at end of a format	*/
    int	        nch = SZ_LINE;		/* one when at end of a format	*/


    while ((ch = *format++) && nch > 0) {
      if (ch == '%') {
    	fsp = formspec;
    	*fsp++ = ch;
    	done = 0;

    	while (!done) {
    	    ch = *fsp++ = *format++;

    	    switch (ch) {
    	    case EOS:
    		--format;
    		done++;
    		break;

    	    case 'l':
    		fsp--; 			/* arg size modifier; ignored for now */
    		break;

    	    case 'b':			/* nonstandard UNIX	*/
    	    case 'c':
    	    case 'd':
    	    case 'o':
    	    case 'x':
    	    case 'u':
    		*fsp = EOS;
    		strcat (buf, samp_doarg (&argp, T_INT));
    		done++;
    		break;

    	    case 'E':			/* ANSI emulation	*/
    		*(fsp-1) = 'e';
    		goto rval;
    	    case 'G':			/* ANSI emulation	*/
    		*(fsp-1) = 'g';
    		goto rval;

    	    case 'e':
    	    case 'f':
    	    case 'g':
rval:
    		*fsp = EOS;
    		strcat (buf, samp_doarg (&argp, T_DOUBLE));
    		done++;
    		break;

    	    case 's':
    		*fsp = EOS;
    		strcat (buf, samp_doarg (&argp, T_CHAR));
    		done++;
    		break;
    	    }
    	}

      } else {
	  memset (cbuf, 0, 10);
	  sprintf (cbuf, "%c", ch);
    	  strcat (buf, cbuf);
      }

      nch = SZ_LINE - strlen (buf);	/* prevent overflow		*/
    }
}


/**
 *  SAMP_DOARG -- Encode a single argument acording to the data type.
 *
 *  @fn     static char *samp_doarg (va_list **argp, int dtype)
 *
 *  @param  argp	argument list
 *  @param  dtype	data type
 *
 */
static char * 
samp_doarg (va_list **argp, int dtype)
{
    int		ival;
    double	dval;
    char	*cptr;
    static char val[128];


    /* Pass the data value to be encoded, bump argument pointer by the
    ** size of the data object.  If there is no data value the case
    ** is a no-op.
    */
    memset (val, 0, 128);

    switch (dtype) {
    case T_INT:
        ival = va_arg ((**argp), int);
        sprintf (val, "%d", ival);
        break;
    case T_DOUBLE:
        dval = va_arg ((**argp), double);
        sprintf (val, "%g", dval);
        break;
    case T_CHAR:
        cptr = va_arg ((**argp), char *);
        sprintf (val, "%s", cptr);
        break;
    }

    return (val);
}



/**
 *  LOGTIME - Generate a time string for the log.
 *
 *  @fn	static char *logtime()
 */
static char * 
logtime ()
{
    time_t      t  = time (NULL);
    struct tm  *tm = gmtime (&t);
    static char tstr[128];

    memset (tstr, 0, 128);
    strftime (tstr, 128, "%m%d %T", tm);

    return (tstr);
}