aboutsummaryrefslogtreecommitdiff
path: root/3rdparty/log/log.c
blob: a1cdf2c6068ffe4b0afdbde201f8d83769a5af01 (plain)
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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
/* ******************************************** */
/* File Name: log.c                             */
/* Author: (C) 2007-2009 - Juan Jos� Ponteprino */
/* Description: Loggin API                      */
/* License: This code is licenced as LGPL       */
/* ******************************************** */

#include <stdlib.h>
#include <stdio.h>
#if WIN32
#define vsnprintf _vsnprintf
#endif
#include <stdarg.h>
#include <string.h>

#include <log.h>

/* ---------------------------------------------------- */

static char * log_str_priority[] =
{
    "EMERG"     ,
    "ALERT"     ,
    "CRIT"      ,
    "ERR"       ,
    "WARN"      ,
    "NOTICE"    ,
    "INFO"      ,
    "DEBUG"     ,
    "N/D"
};

static int log_level = -1;

/* ---------------------------------------------------- */
/* ---------------------------------------------------- */
/* ---------------------------------------------------- */
/* Default Backend                                      */
/* ---------------------------------------------------- */
/* ---------------------------------------------------- */
/* ---------------------------------------------------- */

static FILE * __log_file_roll( log_be_file_t * _privData )
{
    time_t  Moment = time( NULL );
    char    MomentStr[ 18 ];
    struct tm Now ;

    FILE  * filelog;
    char * nfilename = malloc( strlen( _privData->fullpathname ) + 16 );

#if WIN32
    struct tm * _Now = localtime( &Moment );
    Now = * _Now;
#else
    localtime_r( &Moment, &Now );
#endif

    fclose( _privData->handle );

    strftime( MomentStr, sizeof( MomentStr ), "%y%m%d%H%M%S", &Now );

    sprintf( nfilename, "%s.%s", _privData->fullpathname, MomentStr );

    rename( _privData->fullpathname, nfilename );
    free( nfilename );

    filelog = _privData->handle = fopen( _privData->fullpathname, "a" );
    if ( !filelog )
    {
        filelog = stdout;
    }

    return filelog;
}

/* ---------------------------------------------------- */

log_t * log_file_be_create()
{
    log_t * lt = malloc( sizeof( log_t ) );

    if ( !lt )
    {
        return NULL;
    }

    lt->log_open  = log_file_open;
    lt->log_close = log_file_close;
    lt->log_write = log_file_write;
    lt->log_control = log_file_control;

    return lt;
}

/* ---------------------------------------------------- */

void * log_file_open( char * name, int flags )
{
    char * p;

    log_be_file_t * privData = ( log_be_file_t * ) malloc( sizeof( log_be_file_t ) );
    if ( !privData )
    {
        return NULL;
    }

    privData->flags = flags;

    privData->fullpathname = strdup( name );
    if ( !privData->fullpathname )
    {
        free( privData );
        return NULL;
    }

    privData->filename = strrchr( privData->fullpathname, '/' );
    if (!privData->filename)
    {
        privData->filename = strrchr( privData->fullpathname, '\\' );
    }
    if ( !privData->filename )
        privData->filename = privData->fullpathname;
    else
        privData->filename++;

    privData->handle = fopen( name, "a" );
    if ( !privData->handle )
    {
        free( privData->fullpathname );
        free( privData );
        return NULL;
    }

    return privData;
}

/* ---------------------------------------------------- */

void log_file_close( void * privData )
{
    if ( privData && (( log_be_file_t * ) privData )->handle )
    {
        free((( log_be_file_t * ) privData )->fullpathname );
        fclose((( log_be_file_t * ) privData )->handle );
        free( privData );
    }
}

/* ---------------------------------------------------- */

void log_file_write( void * privData, int priority, char * message )
{
    int     doroll = 0;

    time_t  Moment = time( NULL );
    char    MomentStr[ 18 ];
    struct tm Now ;

    log_be_file_t  * _privData = privData;
    FILE  * filelog = _privData->handle;

#if WIN32
    struct tm * _Now = localtime( &Moment );
    Now = * _Now;
#else
    localtime_r( &Moment, &Now );
#endif

    if ( !filelog )
    {
        filelog = stdout;
    }

    if ( _privData->flags & LOG_FILE_ROLL_SIZE_MASK )
    {
        long pos = ftell( filelog );

        if ( pos / 1024 >= ( _privData->flags & LOG_FILE_ROLL_SIZE_MASK ) )
        {
            doroll = 1;
        }
    }

    if ( _privData->flags & LOG_FILE_ROLL_DAY )
    {
        /* If day of year is different of last log, then roll it
           (tm_mday is used only for check valid date in internal structs)
         */
        if ( Now.tm_yday != _privData->lastTime.tm_yday && _privData->lastTime.tm_mday != 0 )
        {
            doroll = 1;
        }
    }

    if ( doroll )
    {
        filelog = __log_file_roll( _privData );
    }

    if ( priority >= LOG_PRIO_MAX )
    {
        priority = LOG_PRIO_NODEF ;
    }

    if ( message && * message != '\0' )
    {
        strftime( MomentStr, sizeof( MomentStr ), "%y/%m/%d-%H:%M:%S", &Now );

        if ( _privData->flags & LOG_FILE_DISPLAY_FNAME )
            fprintf( filelog, "%s:%s %7.4f %s %s\n", _privData->filename, MomentStr, clock() / 1000.00, log_str_priority[ priority ], message );
        else
            fprintf( filelog, "%s %7.4f %s %s\n", MomentStr, clock() / 1000.00, log_str_priority[ priority ], message );

        if ( !( _privData->flags & LOG_FILE_DONT_FLUSH ) ) fflush( filelog );
    }

    _privData->lastTime = Now;
}

/* ---------------------------------------------------- */

void log_file_control( void * privData, long kid, char * args )
{
    switch ( kid )
    {
        case    LOG_FILE_ROLL_NOW:
            __log_file_roll(( log_be_file_t * ) privData );
            break;

        case    LOG_FILE_GET_FD:
            ( ( FILE ** ) args )[0] = ( FILE * )( ( ( log_be_file_t * ) privData )->handle );
            break;
    }
}

/* ---------------------------------------------------- */
/* ---------------------------------------------------- */
/* ---------------------------------------------------- */
/* Log core functions                                   */
/* ---------------------------------------------------- */
/* ---------------------------------------------------- */
/* ---------------------------------------------------- */

int log_open( log_t * backend, char * name, int flags )
{
    if ( !backend || !backend->log_open )
    {
        return 0;
    }

    backend->internalBuffer = malloc( LOG_INIT_INTERNAL_BUFFER_SIZE );
    if ( !backend->internalBuffer )
    {
        return 0;
    }

    backend->privData = backend->log_open( name, flags );
    if ( !backend->privData )
    {
        free( backend->internalBuffer ) ;
        return 0;
    }

    return 1; /* OK */
}

/* ---------------------------------------------------- */

void log_close( log_t * backend )
{
    if ( backend && backend->privData && backend->log_close )
    {
        backend->log_close( backend->privData );
        if ( backend->internalBuffer )
        {
            free( backend->internalBuffer ) ;
        }
    }
}

/* ---------------------------------------------------- */

void log_write( log_t * backend, int priority, char * format, ... )
{
    /* Suponemos que no necesitamos m�s de 100 bytes. */
    int n;
    va_list ap;

    if ( !backend || !backend->privData || !backend->internalBuffer || !backend->log_write )
    {
        return;
    }

    if ( log_level != -1 && priority > log_level ) return;

    /* Intenta imprimir en el espacio reservado. */
    va_start( ap, format );
    n = vsnprintf( backend->internalBuffer, LOG_INIT_INTERNAL_BUFFER_SIZE, format, ap );
    va_end( ap );

    if ( n <= -1 || n > LOG_INIT_INTERNAL_BUFFER_SIZE )
    {
        int size = LOG_INIT_INTERNAL_BUFFER_SIZE * 2;
        char * p, * p1;
        if (( p = malloc( size ) ) == NULL )
        {
            return;
        }

        while ( 1 )
        {
            /* Intenta imprimir en el espacio reservado. */
            va_start( ap, format );
            n = vsnprintf( p, size, format, ap );
            va_end( ap );

            /* Si ha funcionado, devuelve la cadena. */
            if ( n > -1 && n < size )
            {
                break ;
            }

            /* Si no, int�ntalo de nuevo con m�s espacio. */
            if ( n > -1 )  /* glibc 2.1 */
            {
                size = n + 1; /* exactamente lo que se necesita */
            }
            else           /* glibc 2.0 */
            {
                size *= 2;  /* el doble del tama�o anterior*/
            }

            if (( p1 = realloc( p, size ) ) == NULL )
            {
                free( p );
                return;
            }
            p = p1;
        }

        backend->log_write( backend->privData, priority, p );
        free( p );
    }
    else
    {
        backend->log_write( backend->privData, priority, backend->internalBuffer );
    }
}

/* ---------------------------------------------------- */

void log_control( log_t * backend, long kid, char * args )
{
    if ( !backend || !backend->privData || !backend->log_control )
    {
        return;
    }

    if ( ( kid & LOG_SYSTEM_CTRL ) == LOG_SYSTEM_CTRL )
    {
        switch ( kid )
        {
            case    LOG_LEVEL:
                log_level = ( int ) args;
                break;
        }
        return;
    }

    backend->log_control(( void * )( backend->privData ), kid, args );
}

/* ---------------------------------------------------- */

FILE * log_get_handle( log_t * backend )
{
    log_be_file_t * _privData;
    _privData = ( log_be_file_t * ) backend->privData;
    return ( FILE *) _privData->handle;
}

/* ---------------------------------------------------- */