[9b415c9] | 1 | /*
|
---|
| 2 | * Copyright (c) 2011 Vojtech Horky
|
---|
| 3 | * Copyright (c) 2011 Jiri Svoboda
|
---|
| 4 | * All rights reserved.
|
---|
| 5 | *
|
---|
| 6 | * Redistribution and use in source and binary forms, with or without
|
---|
| 7 | * modification, are permitted provided that the following conditions
|
---|
| 8 | * are met:
|
---|
| 9 | *
|
---|
| 10 | * - Redistributions of source code must retain the above copyright
|
---|
| 11 | * notice, this list of conditions and the following disclaimer.
|
---|
| 12 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 13 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 14 | * documentation and/or other materials provided with the distribution.
|
---|
| 15 | * - The name of the author may not be used to endorse or promote products
|
---|
| 16 | * derived from this software without specific prior written permission.
|
---|
| 17 | *
|
---|
| 18 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 19 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 20 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 21 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 22 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 23 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 24 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 25 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 27 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 28 | */
|
---|
| 29 |
|
---|
| 30 | /** @addtogroup libc
|
---|
| 31 | * @{
|
---|
| 32 | */
|
---|
| 33 |
|
---|
| 34 | #include <assert.h>
|
---|
| 35 | #include <errno.h>
|
---|
| 36 | #include <fibril_synch.h>
|
---|
[fc51296] | 37 | #include <stdarg.h>
|
---|
[9b415c9] | 38 | #include <stdlib.h>
|
---|
| 39 | #include <stdio.h>
|
---|
| 40 |
|
---|
| 41 | #include <io/log.h>
|
---|
| 42 |
|
---|
| 43 | /** Serialization mutex for logging functions. */
|
---|
| 44 | static FIBRIL_MUTEX_INITIALIZE(log_serializer);
|
---|
| 45 |
|
---|
| 46 | /** Current log level. */
|
---|
| 47 | static log_level_t log_level;
|
---|
| 48 |
|
---|
| 49 | static FILE *log_stream;
|
---|
| 50 |
|
---|
| 51 | static const char *log_prog_name;
|
---|
| 52 |
|
---|
| 53 | /** Prefixes for individual logging levels. */
|
---|
| 54 | static const char *log_level_names[] = {
|
---|
| 55 | [LVL_FATAL] = "Fatal error",
|
---|
| 56 | [LVL_ERROR] = "Error",
|
---|
| 57 | [LVL_WARN] = "Warning",
|
---|
| 58 | [LVL_NOTE] = "Note",
|
---|
| 59 | [LVL_DEBUG] = "Debug",
|
---|
| 60 | [LVL_DEBUG2] = "Debug2"
|
---|
| 61 | };
|
---|
| 62 |
|
---|
| 63 | /** Initialize the logging system.
|
---|
| 64 | *
|
---|
| 65 | * @param prog_name Program name, will be printed as part of message
|
---|
| 66 | * @param level Minimum message level to print
|
---|
| 67 | */
|
---|
| 68 | int log_init(const char *prog_name, log_level_t level)
|
---|
| 69 | {
|
---|
| 70 | assert(level < LVL_LIMIT);
|
---|
| 71 | log_level = level;
|
---|
| 72 |
|
---|
| 73 | log_stream = stdout;
|
---|
| 74 | log_prog_name = str_dup(prog_name);
|
---|
| 75 | if (log_prog_name == NULL)
|
---|
| 76 | return ENOMEM;
|
---|
| 77 |
|
---|
| 78 | return EOK;
|
---|
| 79 | }
|
---|
| 80 |
|
---|
| 81 | /** Write an entry to the log.
|
---|
| 82 | *
|
---|
| 83 | * @param level Message verbosity level. Message is only printed
|
---|
| 84 | * if verbosity is less than or equal to current
|
---|
| 85 | * reporting level.
|
---|
[ebcb05a] | 86 | * @param fmt Format string (no traling newline).
|
---|
[9b415c9] | 87 | */
|
---|
| 88 | void log_msg(log_level_t level, const char *fmt, ...)
|
---|
| 89 | {
|
---|
| 90 | va_list args;
|
---|
| 91 |
|
---|
[fc51296] | 92 | va_start(args, fmt);
|
---|
| 93 | log_msgv(level, fmt, args);
|
---|
| 94 | va_end(args);
|
---|
| 95 | }
|
---|
| 96 |
|
---|
| 97 | /** Write an entry to the log (va_list variant).
|
---|
| 98 | *
|
---|
| 99 | * @param level Message verbosity level. Message is only printed
|
---|
| 100 | * if verbosity is less than or equal to current
|
---|
| 101 | * reporting level.
|
---|
[ebcb05a] | 102 | * @param fmt Format string (no trailing newline)
|
---|
[fc51296] | 103 | */
|
---|
| 104 | void log_msgv(log_level_t level, const char *fmt, va_list args)
|
---|
| 105 | {
|
---|
[9b415c9] | 106 | assert(level < LVL_LIMIT);
|
---|
| 107 |
|
---|
| 108 | /* Higher number means higher verbosity. */
|
---|
| 109 | if (level <= log_level) {
|
---|
| 110 | fibril_mutex_lock(&log_serializer);
|
---|
| 111 |
|
---|
[fc51296] | 112 | fprintf(log_stream, "%s: %s: ", log_prog_name,
|
---|
[9b415c9] | 113 | log_level_names[level]);
|
---|
| 114 | vfprintf(log_stream, fmt, args);
|
---|
[ebcb05a] | 115 | fputc('\n', log_stream);
|
---|
[9b415c9] | 116 | fflush(log_stream);
|
---|
| 117 |
|
---|
| 118 | fibril_mutex_unlock(&log_serializer);
|
---|
| 119 | }
|
---|
| 120 | }
|
---|
| 121 |
|
---|
| 122 | /** @}
|
---|
| 123 | */
|
---|