log.c

00001 /*
00002  * Copyright (c) 2011 Vojtech Horky
00003  * Copyright (c) 2011 Jiri Svoboda
00004  * All rights reserved.
00005  *
00006  * Redistribution and use in source and binary forms, with or without
00007  * modification, are permitted provided that the following conditions
00008  * are met:
00009  *
00010  * - Redistributions of source code must retain the above copyright
00011  *   notice, this list of conditions and the following disclaimer.
00012  * - Redistributions in binary form must reproduce the above copyright
00013  *   notice, this list of conditions and the following disclaimer in the
00014  *   documentation and/or other materials provided with the distribution.
00015  * - The name of the author may not be used to endorse or promote products
00016  *   derived from this software without specific prior written permission.
00017  *
00018  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
00019  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
00020  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
00021  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
00022  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
00023  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00024  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
00025  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00026  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
00027  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
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         /* Higher number means higher verbosity. */
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 

Generated on Thu Jun 2 07:45:46 2011 for HelenOS/USB by  doxygen 1.4.7