00001
00013 #include <assert.h>
00014 #include <stdarg.h>
00015 #include <stdio.h>
00016 #include <string.h>
00017 #include "boris.h"
00018 #include "plugin.h"
00019
00020 #define LOGBASIC_LENGTH_MAX 1024
00021
00022 struct plugin_basiclog_class {
00023 struct plugin_basic_class base_class;
00024 struct plugin_logging_interface log_interface;
00025 };
00026
00027 extern const struct plugin_basiclog_class plugin_class;
00028
00029 static int log_level=B_LOG_INFO;
00030
00031 static char *prio_names[] = {
00032 "ASSERT", "CRITIAL", "ERROR", "WARNING",
00033 "INFO", "TODO", "DEBUG", "TRACE"
00034 };
00035
00036 static void do_log(int priority, const char *domain, const char *fmt, ...) {
00037 char buf[LOGBASIC_LENGTH_MAX];
00038 int i;
00039 va_list ap;
00040
00041 assert(priority >= 0 && priority <= 7);
00042 assert(fmt != NULL);
00043
00044
00045 i=snprintf(buf, sizeof buf-1, "%s:",
00046 priority>=0 && priority<=7 ? prio_names[priority] : "UNKNOWN"
00047 );
00048
00049
00050 if(domain)
00051 i+=snprintf(buf+i, sizeof buf-i-1, "%s:", domain);
00052
00053
00054 va_start(ap, fmt);
00055 i+=vsnprintf(buf+i, sizeof buf-i-1, fmt, ap);
00056 va_end(ap);
00057
00058
00059 if(i && buf[i-1]!='\n') strcpy(buf+i, "\n");
00060
00061 fputs(buf, stderr);
00062 }
00063
00064 static int initialize(void) {
00065 fprintf(stderr, "loaded %s\n", plugin_class.base_class.class_name);
00066 service_attach_log(do_log);
00067 b_log(B_LOG_INFO, "logging", "Logging system loaded (" __FILE__ " compiled " __TIME__ " " __DATE__ ")");
00068 return 1;
00069 }
00070
00071 static int shutdown(void) {
00072 service_detach_log(do_log);
00073 return 1;
00074 }
00075
00079 static void set_level(int level) {
00080 if(level>7) level=7;
00081 if(level<0) level=0;
00082 log_level=level;
00083 }
00084
00088 const struct plugin_basiclog_class plugin_class = {
00089 .base_class = { PLUGIN_API, "logging", initialize, shutdown },
00090 .log_interface = { do_log, set_level }
00091 };