00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00034 #include <assert.h>
00035 #include <errno.h>
00036 #include <fibril_synch.h>
00037 #include <stdarg.h>
00038 #include <stdlib.h>
00039 #include <stdio.h>
00040
00041 #include <io/log.h>
00042
00044 static FIBRIL_MUTEX_INITIALIZE(log_serializer);
00045
00047 static log_level_t log_level;
00048
00049 static FILE *log_stream;
00050
00051 static const char *log_prog_name;
00052
00054 static const char *log_level_names[] = {
00055 [LVL_FATAL] = "Fatal error",
00056 [LVL_ERROR] = "Error",
00057 [LVL_WARN] = "Warning",
00058 [LVL_NOTE] = "Note",
00059 [LVL_DEBUG] = "Debug",
00060 [LVL_DEBUG2] = "Debug2"
00061 };
00062
00068 int log_init(const char *prog_name, log_level_t level)
00069 {
00070 assert(level < LVL_LIMIT);
00071 log_level = level;
00072
00073 log_stream = stdout;
00074 log_prog_name = str_dup(prog_name);
00075 if (log_prog_name == NULL)
00076 return ENOMEM;
00077
00078 return EOK;
00079 }
00080
00088 void log_msg(log_level_t level, const char *fmt, ...)
00089 {
00090 va_list args;
00091
00092 va_start(args, fmt);
00093 log_msgv(level, fmt, args);
00094 va_end(args);
00095 }
00096
00104 void log_msgv(log_level_t level, const char *fmt, va_list args)
00105 {
00106 assert(level < LVL_LIMIT);
00107
00108
00109 if (level <= log_level) {
00110 fibril_mutex_lock(&log_serializer);
00111
00112 fprintf(log_stream, "%s: %s: ", log_prog_name,
00113 log_level_names[level]);
00114 vfprintf(log_stream, fmt, args);
00115 fputc('\n', log_stream);
00116 fflush(log_stream);
00117
00118 fibril_mutex_unlock(&log_serializer);
00119 }
00120 }
00121