File: log.c

package info (click to toggle)
reprozip 1.3-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 552 kB
  • sloc: ansic: 2,848; python: 2,734; sh: 20; makefile: 12
file content (56 lines) | stat: -rw-r--r-- 1,215 bytes parent folder | download
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
#include <assert.h>
#include <errno.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

#include "log.h"


int logging_level = 0;


int log_setup()
{
    return 0;
}

void log_real_(pid_t tid, int lvl, const char *format, ...)
{
    va_list args;
    char datestr[13]; /* HH:MM:SS.mmm */
    static char *buffer = NULL;
    static size_t bufsize = 4096;
    size_t length;

    if(lvl < logging_level)
        return;

    if(buffer == NULL)
        buffer = malloc(bufsize);
    {
        struct timeval tv;
        gettimeofday(&tv, NULL);
        strftime(datestr, 13, "%H:%M:%S", localtime(&tv.tv_sec));
        sprintf(datestr+8, ".%03u", (unsigned int)(tv.tv_usec / 1000));
    }
    va_start(args, format);
    length = (size_t)vsnprintf(buffer, bufsize, format, args);
    va_end(args);
    if(length + 1 >= bufsize)
    {
        while(length + 1 >= bufsize)
            bufsize *= 2;
        free(buffer);
        buffer = malloc(bufsize);
        va_start(args, format);
        length = vsnprintf(buffer, bufsize, format, args);
        va_end(args);
    }

    if(tid > 0)
        fprintf(stderr, "[%d] %s", tid, buffer);
    else
        fprintf(stderr, "%s", buffer);
}