source: mainline/uspace/lib/c/generic/io/log.c@ 9b415c9

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 9b415c9 was 9b415c9, checked in by Jiri Svoboda <jiri@…>, 14 years ago

Add a simple logging module to C library and use it in devman. Only set
devman to display error messages by default. Based on devman_logging patch
by Vojtech Horky. Message levels inspired by USB team's logging facility.

  • Property mode set to 100644
File size: 3.1 KB
Line 
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>
37#include <stdlib.h>
38#include <stdio.h>
39
40#include <io/log.h>
41
42/** Serialization mutex for logging functions. */
43static FIBRIL_MUTEX_INITIALIZE(log_serializer);
44
45/** Current log level. */
46static log_level_t log_level;
47
48static FILE *log_stream;
49
50static const char *log_prog_name;
51
52/** Prefixes for individual logging levels. */
53static const char *log_level_names[] = {
54 [LVL_FATAL] = "Fatal error",
55 [LVL_ERROR] = "Error",
56 [LVL_WARN] = "Warning",
57 [LVL_NOTE] = "Note",
58 [LVL_DEBUG] = "Debug",
59 [LVL_DEBUG2] = "Debug2"
60};
61
62/** Initialize the logging system.
63 *
64 * @param prog_name Program name, will be printed as part of message
65 * @param level Minimum message level to print
66 */
67int log_init(const char *prog_name, log_level_t level)
68{
69 assert(level < LVL_LIMIT);
70 log_level = level;
71
72 log_stream = stdout;
73 log_prog_name = str_dup(prog_name);
74 if (log_prog_name == NULL)
75 return ENOMEM;
76
77 return EOK;
78}
79
80/** Write an entry to the log.
81 *
82 * @param level Message verbosity level. Message is only printed
83 * if verbosity is less than or equal to current
84 * reporting level.
85 * @param fmt Format string
86 */
87void log_msg(log_level_t level, const char *fmt, ...)
88{
89 va_list args;
90
91 assert(level < LVL_LIMIT);
92
93 /* Higher number means higher verbosity. */
94 if (level <= log_level) {
95 va_start(args, fmt);
96 fibril_mutex_lock(&log_serializer);
97
98 fprintf(log_stream, "%s: %s", log_prog_name,
99 log_level_names[level]);
100 vfprintf(log_stream, fmt, args);
101 fflush(log_stream);
102
103 fibril_mutex_unlock(&log_serializer);
104 va_end(args);
105 }
106}
107
108/** @}
109 */
Note: See TracBrowser for help on using the repository browser.