File: log.c

package info (click to toggle)
notion 4.0.2%2Bdfsg-6
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 4,676 kB
  • sloc: ansic: 47,508; sh: 2,096; makefile: 603; perl: 270
file content (74 lines) | stat: -rw-r--r-- 1,849 bytes parent folder | download | duplicates (5)
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
/*
 * notion/ioncore/log.c
 *
 * Copyright (c) the Notion team 2013
 *
 * See the included file LICENSE for details.
 */

#include <stdarg.h>
#include <stddef.h>
#include <stdio.h>
#include <time.h>
#include "log.h"

const char* loglevel_names[] = {
    "DEBUG",
    "INFO",
    "WARN",
    "ERROR",
};

/** For this category, show log messages at this loglevel and above */
static LogLevel minimumLevel(LogCategory category)
{
    switch(category){
        case VALGRIND:
            return DEBUG;
        default:
            return INFO;
    }
}

#define TIME_BUFFER_MAXSIZE 100

static void printtime(FILE *stream, const char *format, time_t time)
{
    char buf[TIME_BUFFER_MAXSIZE];
    strftime(buf, TIME_BUFFER_MAXSIZE, format, localtime(&time));
    fprintf(stream, "%s", buf);
}

static void vlog_message(LogLevel level, LogCategory category, const char *file, int line, const char *function, const char *message, va_list argp)
{
    if(level >= minimumLevel(category)){
        printtime(stderr, "%F %T ", time(NULL));
        fprintf(stderr, "%-6s", loglevel_names[level]);
        if(file==NULL)
            fprintf(stderr, "Notion: ");
        else
            fprintf(stderr, "/notion/../%s:%d: %s: ", file, line, function);

        vfprintf(stderr, message, argp);
        fprintf(stderr, "\n");
    }
}

void log_message(LogLevel level, LogCategory category, const char *file, int line, const char* function, const char* message, ...)
{
    va_list argp;
    va_start(argp, message);
    vlog_message(level, category, file, line, function, message, argp);
    va_end(argp);
}

#if __STDC_VERSION__ < 199901L
extern void LOG(LogLevel level, LogCategory category, const char* message, ...)
{
    va_list argp;
    va_start(argp, message);
    vlog_message(level, category, NULL, -1, NULL, message, argp);
    va_end(argp);
}
#endif