|
30 | 30 |
|
31 | 31 | /* Logging functions: */
|
32 | 32 |
|
33 |
| -/* log only if a bit is set on the current loglevel mask: |
34 |
| - * @mask: bit to check in the mask |
35 |
| - * @fmt: printf-style format string |
36 |
| - * @args: optional arguments for format string |
| 33 | +/* To verbose logging, enable the next line. */ |
| 34 | +//#define LOGGING_ENABLED // to enable logging |
| 35 | + |
| 36 | +#ifdef LOGGING_ENABLED |
| 37 | + |
| 38 | +#include <stdint.h> |
| 39 | +#include <string.h> |
| 40 | +#include <stdlib.h> |
| 41 | +#include <stdarg.h> |
| 42 | +#include <stdio.h> |
| 43 | + |
| 44 | +/** |
| 45 | + * Reads the @p env_name and tries to parse the value into an uint32_t. |
| 46 | + * @param env_name The environment variable name to parse. |
| 47 | + * @return The parsed value. |
| 48 | + ** 0 in case if the value can not be parsed (or is 0). |
| 49 | + ** ULONG_MAX if the value is bigger than an uint32_t. |
| 50 | + */ |
| 51 | +static inline uint32_t read_and_parse_env(const char* env_name) |
| 52 | +{ |
| 53 | + uint32_t data = 0; |
| 54 | + const char* env_data = getenv(env_name); |
| 55 | + |
| 56 | + if (env_data != NULL) { |
| 57 | + char buffer[11] = {0}; // 0xFFFF'FFFF\0 |
| 58 | + strncpy(buffer, env_data, sizeof(buffer) - 1); |
| 59 | + data = (uint32_t)strtoul(buffer, NULL, 0); |
| 60 | + } |
| 61 | + |
| 62 | + return data; |
| 63 | +} |
| 64 | + |
| 65 | +/** |
| 66 | + * Gets the log level by reading it once. |
| 67 | + * @return The log level. |
| 68 | + */ |
| 69 | +static inline uint32_t get_log_level() |
| 70 | +{ |
| 71 | + static uint64_t log_level = UINT64_MAX; |
| 72 | + |
| 73 | + if (log_level == UINT64_MAX) { |
| 74 | + log_level = read_and_parse_env("UNICORN_LOG_LEVEL"); |
| 75 | + } |
| 76 | + |
| 77 | + return (uint32_t)log_level; |
| 78 | +} |
| 79 | + |
| 80 | +/** |
| 81 | + * Gets the log detail level by reading it once. |
| 82 | + * @return The detail log level. |
| 83 | + */ |
| 84 | +static inline uint32_t get_log_detail_level() |
| 85 | +{ |
| 86 | + static uint64_t log_detail_level = UINT64_MAX; |
| 87 | + |
| 88 | + if (log_detail_level == UINT64_MAX) { |
| 89 | + log_detail_level = read_and_parse_env("UNICORN_LOG_DETAIL_LEVEL"); |
| 90 | + } |
| 91 | + |
| 92 | + return (uint32_t)log_detail_level; |
| 93 | +} |
| 94 | + |
| 95 | +/** |
| 96 | + * Checks if the @p log_level is active. |
| 97 | + * @param log_level The log level to be checked. |
| 98 | + * @return True if the log level is active. |
37 | 99 | */
|
38 |
| -#define qemu_log_mask(MASK, FMT, ...) |
| 100 | +static inline bool is_log_level_active(uint32_t log_level) |
| 101 | +{ |
| 102 | + const uint32_t active_log_level = get_log_level(); |
| 103 | + const bool is_active = (active_log_level & log_level) == log_level; |
39 | 104 |
|
40 |
| -/* log only if a bit is set on the current loglevel mask |
41 |
| - * and we are in the address range we care about: |
42 |
| - * @mask: bit to check in the mask |
43 |
| - * @addr: address to check in dfilter |
44 |
| - * @fmt: printf-style format string |
45 |
| - * @args: optional arguments for format string |
| 105 | + return is_active; |
| 106 | +} |
| 107 | + |
| 108 | +/** |
| 109 | + * Checks if the logging is enabled. |
| 110 | + * @return True if enabled, else false. |
46 | 111 | */
|
47 |
| -#define qemu_log_mask_and_addr(MASK, ADDR, FMT, ...) |
| 112 | +static inline bool is_logging_enabled() |
| 113 | +{ |
| 114 | + const bool log_level = get_log_level(); |
| 115 | + |
| 116 | + return log_level != 0; |
| 117 | +} |
48 | 118 |
|
| 119 | +/** |
| 120 | + * Gets the filename of the caller on given @p detail_level. |
| 121 | + * @param filename The filename to process on. |
| 122 | + * @param detail_level The level of detail of the filename. |
| 123 | + ** 0: Returns an empty string. |
| 124 | + ** 1: Returns the full filename including it's path. |
| 125 | + ** 2: Returns just the filename (to shorten the log). |
| 126 | + * @return always an valid null-terminated string. Do NOT free it. |
| 127 | + */ |
| 128 | +static inline const char* const get_detailed_filename(const char* filename, |
| 129 | + int detail_level) |
| 130 | +{ |
| 131 | + filename = (filename != NULL) ? filename : ""; |
| 132 | + const char* resulting_filename = filename; |
| 133 | + |
| 134 | +#if (defined(WIN32) || defined(WIN64) || defined(_WIN32) || defined(_WIN64)) |
| 135 | + const char path_separator = '\\'; |
| 136 | +#else |
| 137 | + const char path_separator = '/'; |
49 | 138 | #endif
|
| 139 | + |
| 140 | + switch (detail_level) { |
| 141 | + default: |
| 142 | + case 0: |
| 143 | + resulting_filename = ""; |
| 144 | + break; |
| 145 | + case 1: |
| 146 | + resulting_filename = filename; |
| 147 | + break; |
| 148 | + case 2: |
| 149 | + resulting_filename = strrchr(filename, path_separator); |
| 150 | + |
| 151 | + if (resulting_filename == NULL) { |
| 152 | + resulting_filename = filename; |
| 153 | + } |
| 154 | + else { |
| 155 | + ++resulting_filename; |
| 156 | + } |
| 157 | + |
| 158 | + break; |
| 159 | + } |
| 160 | + |
| 161 | + return resulting_filename; |
| 162 | +} |
| 163 | + |
| 164 | +/** |
| 165 | + * Prints the formatted log message with details if enabled. |
| 166 | + * @param mask The log mask to log on. |
| 167 | + * @param filename The filename of the caller. |
| 168 | + * @param line The line number of the caller. |
| 169 | + * @param fmt Printf-style format string. |
| 170 | + * @param args optional arguments for the format string. |
| 171 | + */ |
| 172 | +static inline void print_log(uint32_t mask, const char* filename, uint32_t line, const char* fmt, ...) |
| 173 | +{ |
| 174 | + if ((mask & get_log_level()) == 0) { |
| 175 | + return; |
| 176 | + } |
| 177 | + |
| 178 | + const uint32_t log_detail_level = get_log_detail_level(); |
| 179 | + |
| 180 | + if (log_detail_level > 0) { |
| 181 | + const char* detailed_filename = get_detailed_filename(filename, log_detail_level); |
| 182 | + printf("[%s:%u] ", detailed_filename, line); |
| 183 | + } |
| 184 | + |
| 185 | + va_list argptr; |
| 186 | + |
| 187 | + va_start(argptr, fmt); |
| 188 | + vfprintf(stdout, fmt, argptr); |
| 189 | + va_end(argptr); |
| 190 | +} |
| 191 | + |
| 192 | +/** |
| 193 | + * Logs only if the right log level is set. |
| 194 | + * @param mask The log mask to log on. |
| 195 | + * @param fmt Printf-style format string. |
| 196 | + * @param args optional arguments for the format string. |
| 197 | + */ |
| 198 | +#define LOG_MESSAGE(mask, fmt, ...) \ |
| 199 | + do { \ |
| 200 | + print_log(mask, __FILE__, __LINE__, fmt, ## __VA_ARGS__); \ |
| 201 | + } while (0) |
| 202 | +#else |
| 203 | +#define LOG_MESSAGE(mask, fmt, ...) |
| 204 | + |
| 205 | +/** |
| 206 | + * Dummy implementation which returns always false. |
| 207 | + * @return Always false. |
| 208 | + */ |
| 209 | +static inline bool is_logging_enabled() |
| 210 | +{ |
| 211 | + return false; |
| 212 | +} |
| 213 | + |
| 214 | +/** |
| 215 | + * Dummy implementation which returns always false. |
| 216 | + * @param level The log level to be checked. |
| 217 | + * @return Always false. |
| 218 | + */ |
| 219 | +static inline bool is_log_level_active(uint32_t level) |
| 220 | +{ |
| 221 | + (void)level; |
| 222 | + |
| 223 | + return false; |
| 224 | +} |
| 225 | +#endif /* LOGGING_ENABLED */ |
| 226 | + |
| 227 | +/** |
| 228 | + * Logs only if the right log level is set. |
| 229 | + * @param mask The log mask to log on. |
| 230 | + * @param fmt Printf-style format string. |
| 231 | + * @param args Optional arguments for the format string. |
| 232 | + */ |
| 233 | +#define qemu_log_mask(mask, fmt, ...) \ |
| 234 | + LOG_MESSAGE(mask, fmt, ## __VA_ARGS__) |
| 235 | + |
| 236 | +#endif /* QEMU_LOG_H */ |
0 commit comments