diff options
Diffstat (limited to 'src/log.h')
| -rw-r--r-- | src/log.h | 148 |
1 files changed, 148 insertions, 0 deletions
diff --git a/src/log.h b/src/log.h new file mode 100644 index 0000000..0ad1b97 --- /dev/null +++ b/src/log.h @@ -0,0 +1,148 @@ +#ifndef _SK_LOG_H +#define _SK_LOG_H + +#include "util/font.h" + + +/* types */ + +enum sk_log_level { + SK_LOG_ERROR = 1, + SK_LOG_WARN, + SK_LOG_INFO, + SK_LOG_DEBUG, + SK_LOG_TRACE, + + SK_LOG_MIN = SK_LOG_ERROR, + SK_LOG_MAX = SK_LOG_TRACE +}; + +typedef enum sk_log_level sk_log_level; +typedef const char * sk_font_map[SK_LOG_MAX+1]; + +/* font maps */ + +extern sk_font_map _SK_LOG_COLOURED; +extern sk_font_map _SK_LOG_MONOCHROME; +#define SK_LOG_COLOURED &_SK_LOG_COLOURED +#define SK_LOG_MONOCHROME &_SK_LOG_MONOCHROME + + +/* type conversion functions */ + +/** + * sk_log_level_to_string: + * converts an sk_log_level to its string representation. + * + * @param level: the log level + * @return the string representation of level. + */ +const char *sk_log_level_to_string(sk_log_level level); + +/** + * sk_log_level_of_string: + * converts a string representation to its sk_log_level. + * + * s is compared case-insensitively. + * In case an invalid string representation is given, + * 0 is returned. + * + * @param s: the string representation of the log level + * @return sk_log_level + */ +sk_log_level sk_log_level_of_string(const char *s); + + +/* getters/setters for global state variables */ + +/** + * sk_log_level_get: + * returns the current minimum log level to be logged. + * + * @return the log level + */ +sk_log_level sk_log_level_get(); + +/** + * sk_log_level_set: + * sets a new minimum log level to be logged. + * + * @param new: the new minimum log level. + */ +void sk_log_level_set(sk_log_level new); + +/** + * sk_log_font_get: + * get the current logging font map. + * + * @return the current logging font map + */ +sk_font_map *sk_log_font_get(); + +/** + * sk_log_font_set: + * sets a new logging font map. + * + * @param font: the new logging font map. + */ +void sk_log_font_set(sk_font_map *restrict font); + + +/* logging functions */ + +/** + * sk_log: + * a generic logging function accepting the log level as a function argument. + * + * The message given using a format and format arguments will be formatted using + * a standard format and a new line will be appended. + * + * @param level: the log level of this message. + * @param format: the message as a format string, cf. printf(3). + * @param varg: format arguments if needed. + * @return the number of bytes logged (0 if no message was logged due to + * "insufficient" log level, and a negative number if an error occurred) + */ +int sk_log(sk_log_level level, const char *restrict format, ...); + +/** + * sk_error: + * log an error message. + * + * cf. sk_log(). + */ +#define sk_error(format, ...) sk_log(SK_LOG_ERROR, format, ## __VA_ARGS__) + +/** + * sk_warn: + * log a warning message. + * + * cf. sk_log(). + */ +#define sk_warn(format, ...) sk_log(SK_LOG_WARN, format, ## __VA_ARGS__) + +/** + * sk_info: + * log an informational message. + * + * cf. sk_log(). + */ +#define sk_info(format, ...) sk_log(SK_LOG_INFO, format, ## __VA_ARGS__) + +/** + * sk_debug: + * log a debug message. + * + * cf. sk_log(). + */ +#define sk_debug(format, ...) sk_log(SK_LOG_DEBUG, format, ## __VA_ARGS__) + +/** + * sk_trace: + * log a trace message. + * + * cf. sk_log(). + */ +#define sk_trace(format, ...) sk_log(SK_LOG_TRACE, format, ## __VA_ARGS__) + +#endif |
