Also log date and time
[notion/jeffpc.git] / ioncore / log.c
blobe808b747f40d885d3cb5fc6dc7f75e3048cbeb2e
1 /*
2 * notion/ioncore/log.c
4 * Copyright (c) the Notion team 2013
6 * See the included file LICENSE for details.
7 */
9 #include <stdarg.h>
10 #include <stddef.h>
11 #include <stdio.h>
12 #include <time.h>
13 #include "log.h"
15 const char* loglevel_names[] = {
16 "DEBUG",
17 "INFO",
18 "WARN",
19 "ERROR",
22 /** For this category, show log messages at this loglevel and above */
23 LogLevel minimumLevel(LogCategory category)
25 switch(category){
26 case FONT:
27 /** For https://sourceforge.net/p/notion/bugs/63/ */
28 return DEBUG;
29 default:
30 return INFO;
34 #define TIME_BUFFER_MAXSIZE 100
36 void printtime(FILE *stream, const char *format, time_t time)
38 char buf[TIME_BUFFER_MAXSIZE];
39 strftime(buf, TIME_BUFFER_MAXSIZE, format, localtime(&time));
40 fprintf(stream, "%s", buf);
43 void vlog_message(LogLevel level, LogCategory category, const char *file, int line, const char *function, const char *message, va_list argp)
45 if(level >= minimumLevel(category)){
46 printtime(stderr, "%F %T ", time(NULL));
47 fprintf(stderr, "%-6s", loglevel_names[level]);
48 if(file==NULL)
49 fprintf(stderr, "Notion: ");
50 else
51 fprintf(stderr, "/notion/../%s:%d: %s: ", file, line, function);
53 vfprintf(stderr, message, argp);
54 fprintf(stderr, "\n");
58 void log_message(LogLevel level, LogCategory category, const char *file, int line, const char* function, const char* message, ...)
60 va_list argp;
61 va_start(argp, message);
62 vlog_message(level, category, file, line, function, message, argp);
63 va_end(argp);
66 #if __STDC_VERSION__ < 199901L
67 extern void LOG(LogLevel level, LogCategory category, const char* message, ...)
69 va_list argp;
70 va_start(argp, message);
71 vlog_message(level, category, NULL, -1, NULL, message, argp);
72 va_end(argp);
74 #endif