root / lab4 / .minix-src / include / minix / log.h @ 14
History | View | Annotate | Download (2.84 KB)
1 | 13 | up20180614 | #ifndef __LOG_H__
|
---|---|---|---|
2 | #define __LOG_H__
|
||
3 | /*
|
||
4 | * Simple logging functions
|
||
5 | */
|
||
6 | |||
7 | #include <stdarg.h> |
||
8 | |||
9 | /*
|
||
10 | * LEVEL_NONE do not log anything.
|
||
11 | * LEVEL_WARN Information that needs to be known.
|
||
12 | * LEVEL_INFO Basic information like startup messages and occasional events.
|
||
13 | * LEVEL_DEBUG debug statements about things happening that are less expected.
|
||
14 | * LEVEL_TRACE Way to much information for anybody.
|
||
15 | */
|
||
16 | |||
17 | #define LEVEL_NONE 0 |
||
18 | #define LEVEL_WARN 1 |
||
19 | #define LEVEL_INFO 2 |
||
20 | #define LEVEL_DEBUG 3 |
||
21 | #define LEVEL_TRACE 4 |
||
22 | |||
23 | static const char *level_string[5] = { |
||
24 | "none",
|
||
25 | "warn",
|
||
26 | "info",
|
||
27 | "debug",
|
||
28 | "trace"
|
||
29 | }; |
||
30 | |||
31 | /*
|
||
32 | * struct to be initialized by the user of the logging system.
|
||
33 | *
|
||
34 | * name: The name attribute is used in logging statements do differentiate
|
||
35 | * drivers
|
||
36 | *
|
||
37 | * log_level The level attribute describes the requested logging level. a level
|
||
38 | * of 1 will only print warnings while a level of 4 will print all the trace
|
||
39 | * information.
|
||
40 | *
|
||
41 | * log_func The logging function to use to log, log.h provides default_log
|
||
42 | * to display information on the kernel output buffer. As a bonus if the
|
||
43 | * requested log level is debug or trace the method , file and line number will
|
||
44 | * be printed to the steam.
|
||
45 | */
|
||
46 | struct log
|
||
47 | { |
||
48 | const char *name; |
||
49 | int log_level;
|
||
50 | |||
51 | /* the logging function itself */
|
||
52 | void (*log_func) (struct log * driver, |
||
53 | int level,
|
||
54 | const char *file, |
||
55 | const char *function, int line, const char *fmt, ...); |
||
56 | |||
57 | }; |
||
58 | |||
59 | #define __log(driver,log_level, fmt, args...) \
|
||
60 | ((driver)->log_func(driver,log_level, \ |
||
61 | __FILE__, __FUNCTION__, __LINE__,\ |
||
62 | fmt, ## args)) |
||
63 | |||
64 | /* Log a warning */
|
||
65 | #define log_warn(driver, fmt, args...) \
|
||
66 | __log(driver, LEVEL_WARN, fmt, ## args) |
||
67 | |||
68 | /* Log an information message */
|
||
69 | #define log_info(driver, fmt, args...) \
|
||
70 | __log(driver, LEVEL_INFO, fmt, ## args) |
||
71 | |||
72 | /* log debugging output */
|
||
73 | #define log_debug(driver, fmt, args...) \
|
||
74 | __log(driver, LEVEL_DEBUG, fmt, ## args) |
||
75 | |||
76 | /* log trace output */
|
||
77 | #define log_trace(driver, fmt, args...) \
|
||
78 | __log(driver, LEVEL_TRACE, fmt, ## args) |
||
79 | |||
80 | #endif /* __LOG_H__ */ |
||
81 | |||
82 | static void |
||
83 | default_log(struct log *driver,
|
||
84 | int level,
|
||
85 | const char *file, const char *function, int line, const char *fmt, ...) |
||
86 | { |
||
87 | va_list args; |
||
88 | |||
89 | if (level > driver->log_level) {
|
||
90 | return;
|
||
91 | } |
||
92 | /* If the wanted level is debug also display line/method information */
|
||
93 | if (driver->log_level >= LEVEL_DEBUG) {
|
||
94 | printf("%s(%s):%s+%d(%s):", driver->name,
|
||
95 | level_string[level], file, line, function); |
||
96 | } else {
|
||
97 | printf("%s(%s)", driver->name, level_string[level]);
|
||
98 | } |
||
99 | |||
100 | va_start(args, fmt); |
||
101 | vprintf(fmt, args); |
||
102 | va_end(args); |
||
103 | } |
||
104 | |||
105 | #ifdef hacks
|
||
106 | static void |
||
107 | hexdump(unsigned char *d, unsigned int size) |
||
108 | { |
||
109 | int s;
|
||
110 | for (s = 0; s < size; s += 4) { |
||
111 | fprintf(stdout, "0x%04x 0x%02X%02X%02X%02X %c%c%c%c\n", s,
|
||
112 | (unsigned int) d[s], (unsigned int) d[s + 1], |
||
113 | (unsigned int) d[s + 2], (unsigned int) d[s + 3], d[s], |
||
114 | d[s + 1], d[s + 2], d[s + 3]); |
||
115 | } |
||
116 | } |
||
117 | #endif |