21 #include "sol-common-buildopts.h"
29 #include <sys/types.h>
33 template <
typename T>
inline const char *sol_int_format(T) {
return NULL; }
34 template <>
inline const char *sol_int_format<int>(int) {
return "%d"; }
35 template <>
inline const char *sol_int_format<long>(long) {
return "%ld"; }
36 template <>
inline const char *sol_int_format<long long>(
long long) {
return "%lld"; }
37 template <>
inline const char *sol_int_format<short>(short) {
return "%hd"; }
38 template <>
inline const char *sol_int_format<signed char>(
signed char) {
return "%hhd"; }
39 template <>
inline const char *sol_int_format<unsigned>(unsigned) {
return "%u"; }
40 template <>
inline const char *sol_int_format<unsigned long>(
unsigned long) {
return "%lu"; }
41 template <>
inline const char *sol_int_format<unsigned long long>(
unsigned long long) {
return "%llu"; }
42 template <>
inline const char *sol_int_format<unsigned short>(
unsigned short) {
return "%hu"; }
43 template <>
inline const char *sol_int_format<unsigned char>(
unsigned char) {
return "%hhu"; }
45 #define SOL_INT_CHECK_IMPL(var, exp, ...) \
47 if (SOL_UNLIKELY((var)exp)) { \
48 char *str = (char *)alloca(snprintf(NULL, 0, "%s (%s) %s", #var, sol_int_format(var), #exp) + 1); \
49 sprintf(str, "%s (%s) %s", #var, sol_int_format(var), #exp); \
55 #define SOL_INT_CHECK_GOTO_IMPL(var, exp, label) \
57 if (SOL_UNLIKELY((var)exp)) { \
58 char *str = (char *)alloca(snprintf(NULL, 0, "%s (%s) %s", #var, sol_int_format(var), #exp) + 1); \
59 sprintf(str, "%s (%s) %s", #var, sol_int_format(var), #exp); \
65 #define SOL_INT_CHECK_GOTO_IMPL_ERRNO(var, exp, err, label) \
67 if (SOL_UNLIKELY((var)exp)) { \
68 char *str = (char *)alloca(snprintf(NULL, 0, "%s (%s) %s", \
69 # var, sol_int_format(var), # exp) + 1); \
70 sprintf(str, "%s (%s) %s", # var, sol_int_format(var), # exp); \
77 #define SOL_INT_CHECK_IMPL_ERRNO(var, exp, err, ...) \
79 if (SOL_UNLIKELY((var)exp)) { \
80 char *str = (char *)alloca(snprintf(NULL, 0, "%s (%s) %s", \
81 # var, sol_int_format(var), # exp) + 1); \
82 sprintf(str, "%s (%s) %s", # var, sol_int_format(var), # exp); \
96 #define _SOL_INT_CHECK_FMT(var) \
97 __builtin_choose_expr( \
98 __builtin_types_compatible_p(__typeof__(var), int), \
99 "" # var " (%d) %s", \
100 __builtin_choose_expr( \
101 __builtin_types_compatible_p(__typeof__(var), long), \
102 "" # var " (%ld) %s", \
103 __builtin_choose_expr( \
104 __builtin_types_compatible_p(__typeof__(var), size_t), \
105 "" # var " (%zu) %s", \
106 __builtin_choose_expr( \
107 __builtin_types_compatible_p(__typeof__(var), unsigned), \
108 "" # var " (%u) %s", \
109 __builtin_choose_expr( \
110 __builtin_types_compatible_p(__typeof__(var), uint64_t), \
111 "" # var " (%" PRIu64 ") %s", \
112 __builtin_choose_expr( \
113 __builtin_types_compatible_p(__typeof__(var), uint32_t), \
114 "" # var " (%" PRIu32 ") %s", \
115 __builtin_choose_expr( \
116 __builtin_types_compatible_p(__typeof__(var), uint16_t), \
117 "" # var " (%" PRIu16 ") %s", \
118 __builtin_choose_expr( \
119 __builtin_types_compatible_p(__typeof__(var), uint8_t), \
120 "" # var " (%" PRIu8 ") %s", \
121 __builtin_choose_expr( \
122 __builtin_types_compatible_p(__typeof__(var), int64_t), \
123 "" # var " (%" PRId64 ") %s", \
124 __builtin_choose_expr( \
125 __builtin_types_compatible_p(__typeof__(var), int32_t), \
126 "" # var " (%" PRId32 ") %s", \
127 __builtin_choose_expr( \
128 __builtin_types_compatible_p(__typeof__(var), int16_t), \
129 "" # var " (%" PRId16 ") %s", \
130 __builtin_choose_expr( \
131 __builtin_types_compatible_p(__typeof__(var), int8_t), \
132 "" # var " (%" PRId8 ") %s", \
133 __builtin_choose_expr( \
134 __builtin_types_compatible_p(__typeof__(var), ssize_t), \
135 "" # var " (%zd) %s", \
138 #define SOL_INT_CHECK_IMPL(var, exp, ...) \
140 if (SOL_UNLIKELY((var)exp)) { \
141 SOL_WRN(_SOL_INT_CHECK_FMT(var), var, # exp); \
142 return __VA_ARGS__; \
146 #define SOL_INT_CHECK_GOTO_IMPL(var, exp, label) \
148 if (SOL_UNLIKELY((var)exp)) { \
149 SOL_WRN(_SOL_INT_CHECK_FMT(var), var, # exp); \
154 #define SOL_INT_CHECK_GOTO_IMPL_ERRNO(var, exp, err, label) \
156 if (SOL_UNLIKELY((var)exp)) { \
157 SOL_WRN(_SOL_INT_CHECK_FMT(var), var, # exp); \
163 #define SOL_INT_CHECK_IMPL_ERRNO(var, exp, err, ...) \
165 if (SOL_UNLIKELY((var)exp)) { \
166 SOL_WRN(_SOL_INT_CHECK_FMT(var), var, # exp); \
168 return __VA_ARGS__; \
196 #define SOL_LOG_COLOR_LIGHTRED "\033[31;1m"
197 #define SOL_LOG_COLOR_RED "\033[31m"
198 #define SOL_LOG_COLOR_LIGHTBLUE "\033[34;1m"
199 #define SOL_LOG_COLOR_BLUE "\033[34m"
200 #define SOL_LOG_COLOR_GREEN "\033[32;1m"
201 #define SOL_LOG_COLOR_YELLOW "\033[33;1m"
202 #define SOL_LOG_COLOR_ORANGE "\033[0;33m"
203 #define SOL_LOG_COLOR_WHITE "\033[37;1m"
204 #define SOL_LOG_COLOR_LIGHTMAGENTA "\033[35;1m"
205 #define SOL_LOG_COLOR_MAGENTA "\033[35m"
206 #define SOL_LOG_COLOR_LIGHTCYAN "\033[36;1m"
207 #define SOL_LOG_COLOR_CYAN "\033[36m"
208 #define SOL_LOG_COLOR_RESET "\033[0m"
209 #define SOL_LOG_COLOR_HIGH "\033[1m"
223 #define SOL_NULL_CHECK(ptr, ...) \
225 if (SOL_UNLIKELY(!(ptr))) { \
226 SOL_WRN("%s == NULL", # ptr); \
227 return __VA_ARGS__; \
243 #define SOL_NULL_CHECK_ERRNO(ptr, err, ...) \
245 if (SOL_UNLIKELY(!(ptr))) { \
246 SOL_WRN("%s == NULL", # ptr); \
248 return __VA_ARGS__; \
262 #define SOL_NULL_CHECK_GOTO(ptr, label) \
264 if (SOL_UNLIKELY(!(ptr))) { \
265 SOL_WRN("%s == NULL", # ptr); \
280 #define SOL_NULL_CHECK_MSG(ptr, ret, fmt, ...) \
282 if (SOL_UNLIKELY(!(ptr))) { \
283 SOL_WRN(fmt, ## __VA_ARGS__); \
298 #define SOL_NULL_CHECK_MSG_GOTO(ptr, label, fmt, ...) \
300 if (SOL_UNLIKELY(!(ptr))) { \
301 SOL_WRN(fmt, ## __VA_ARGS__); \
316 #define SOL_INT_CHECK(var, exp, ...) \
317 SOL_INT_CHECK_IMPL(var, exp, __VA_ARGS__)
332 #define SOL_INT_CHECK_ERRNO(var, exp, err, ...) \
333 SOL_INT_CHECK_IMPL_ERRNO(var, exp, err, __VA_ARGS__)
345 #define SOL_INT_CHECK_GOTO(var, exp, label) \
346 SOL_INT_CHECK_GOTO_IMPL(var, exp, label)
361 #define SOL_INT_CHECK_GOTO_ERRNO(var, exp, err, label) \
362 SOL_INT_CHECK_GOTO_IMPL_ERRNO(var, exp, label)
373 #define SOL_EXP_CHECK(exp, ...) \
375 if (SOL_UNLIKELY((exp))) { \
376 SOL_WRN("(%s) is true", # exp); \
377 return __VA_ARGS__; \
390 #define SOL_EXP_CHECK_GOTO(exp, label) \
392 if (SOL_UNLIKELY((exp))) { \
393 SOL_WRN("(%s) is true", # exp); \
463 #ifdef SOL_LOG_ENABLED
465 void sol_log_init_level_global(
const char *str,
size_t length);
466 void sol_log_init_levels(
const char *str,
size_t length);
469 #define SOL_LOG_LEVEL_INIT() \
470 sol_log_init_level_global(SOL_LOG_LEVEL, sizeof(SOL_LOG_LEVEL) - 1)
472 #define SOL_LOG_LEVEL_INIT()
475 #ifdef SOL_LOG_LEVELS
476 #define SOL_LOG_LEVELS_INIT() \
477 sol_log_init_levels(SOL_LOG_LEVELS, sizeof(SOL_LOG_LEVELS) - 1)
479 #define SOL_LOG_LEVELS_INIT()
488 #define SOL_LOG_LEVEL_INIT()
489 #define SOL_LOG_LEVELS_INIT()
493 #ifndef SOL_LOG_DOMAIN
505 #define SOL_LOG_DOMAIN sol_log_global_domain
530 #define SOL_LOG_LEVEL_MAXIMUM SOL_LOG_LEVEL_WARNING
541 #ifdef SOL_LOG_ENABLED
542 #ifdef SOL_LOG_LEVEL_MAXIMUM
543 #define SOL_LOG_LEVEL_POSSIBLE(level) (level <= SOL_LOG_LEVEL_MAXIMUM)
545 #define SOL_LOG_LEVEL_POSSIBLE(level) (1)
548 #ifdef SOL_LOG_LEVEL_MAXIMUM
549 #undef SOL_LOG_LEVEL_MAXIMUM
551 #define SOL_LOG_LEVEL_MAXIMUM -1
552 #define SOL_LOG_LEVEL_POSSIBLE(level) (0)
567 #define SOL_LOG_FILE __FILE__
569 #define SOL_LOG_FILE ""
583 #ifdef SOL_LOG_FUNCTIONS
584 #define SOL_LOG_FUNCTION __PRETTY_FUNCTION__
586 #define SOL_LOG_FUNCTION ""
599 #define SOL_LOG(level, fmt, ...) \
601 if (SOL_LOG_LEVEL_POSSIBLE(level)) { \
602 sol_log_print(SOL_LOG_DOMAIN, level, \
603 SOL_LOG_FILE, SOL_LOG_FUNCTION, __LINE__, \
604 fmt, ## __VA_ARGS__); \
619 #define SOL_CRI(fmt, ...) SOL_LOG(SOL_LOG_LEVEL_CRITICAL, fmt, ## __VA_ARGS__)
632 #define SOL_ERR(fmt, ...) SOL_LOG(SOL_LOG_LEVEL_ERROR, fmt, ## __VA_ARGS__)
645 #define SOL_WRN(fmt, ...) SOL_LOG(SOL_LOG_LEVEL_WARNING, fmt, ## __VA_ARGS__)
658 #define SOL_INF(fmt, ...) SOL_LOG(SOL_LOG_LEVEL_INFO, fmt, ## __VA_ARGS__)
671 #define SOL_DBG(fmt, ...) SOL_LOG(SOL_LOG_LEVEL_DEBUG, fmt, ## __VA_ARGS__)
732 #ifdef SOL_LOG_ENABLED
738 sol_log_print(
const struct sol_log_domain *domain, uint8_t message_level,
const char *file,
const char *
function,
int line,
const char *format, ...)
742 sol_log_vprint(
const struct sol_log_domain *domain, uint8_t message_level,
const char *file,
const char *
function,
int line,
const char *format, va_list args)
764 #ifdef SOL_LOG_ENABLED
765 void sol_log_set_print_function(
void (*print)(
void *data,
const struct sol_log_domain *domain, uint8_t message_level,
const char *file,
const char *
function,
int line,
const char *format, va_list args),
const void *data);
768 sol_log_set_print_function(
void (*print)(
void *data,
const struct sol_log_domain *domain, uint8_t message_level,
const char *file,
const char *
function,
int line,
const char *format, va_list args),
const void *data)
781 #ifdef SOL_LOG_ENABLED
790 #ifdef SOL_PLATFORM_LINUX
804 #ifdef SOL_LOG_ENABLED
805 void sol_log_print_function_file(
void *data,
const struct sol_log_domain *domain, uint8_t message_level,
const char *file,
const char *
function,
int line,
const char *format, va_list args)
SOL_ATTR_PRINTF(7, 0);
808 sol_log_print_function_file(
void *data,
const struct sol_log_domain *domain, uint8_t message_level,
const char *file,
const char *
function,
int line,
const char *format, va_list args)
814 #ifdef SOL_PLATFORM_LINUX
831 #ifdef SOL_LOG_ENABLED
832 void sol_log_print_function_syslog(
void *data,
const struct sol_log_domain *domain, uint8_t message_level,
const char *syslog,
const char *
function,
int line,
const char *format, va_list args)
SOL_ATTR_PRINTF(7, 0);
835 sol_log_print_function_syslog(
void *data,
const struct sol_log_domain *domain, uint8_t message_level,
const char *syslog,
const char *
function,
int line,
const char *format, va_list args)
841 #ifdef SOL_PLATFORM_LINUX
863 #ifdef SOL_LOG_ENABLED
864 void sol_log_print_function_journal(
void *data,
const struct sol_log_domain *domain, uint8_t message_level,
const char *journal,
const char *
function,
int line,
const char *format, va_list args)
SOL_ATTR_PRINTF(7, 0);
867 sol_log_print_function_journal(
void *data,
const struct sol_log_domain *domain, uint8_t message_level,
const char *journal,
const char *
function,
int line,
const char *format, va_list args)
989 #ifdef SOL_LOG_ENABLED
1017 static inline const char *
1022 static inline uint8_t
1027 static inline uint8_t
static void sol_log_set_print_function(void(*print)(void *data, const struct sol_log_domain *domain, uint8_t message_level, const char *file, const char *function, int line, const char *format, va_list args), const void *data)
Set the function to print out log messages.
Definition: sol-log.h:768
#define SOL_ATTR_NO_INSTRUMENT
Used to tell that this functions shouldn't be instrumented.
Definition: sol-macros.h:192
static void sol_log_set_show_function(bool enabled)
Enable/Disables the output of function's name containing the logging messages.
Definition: sol-log.h:1069
#define SOL_ATTR_PRINTF(fmt, arg)
Specifies that a function takes printf style arguments which should be type-checked against a format ...
Definition: sol-macros.h:189
static void sol_log_print(const struct sol_log_domain *domain, uint8_t message_level, const char *file, const char *function, int line, const char *format,...)
Print out a message in a given domain and level.
Definition: sol-log.h:738
Debug.
Definition: sol-log.h:408
static void sol_log_level_to_str(uint8_t level, char *buf, size_t buflen)
Convenience function to convert the logging level to string.
Definition: sol-log.h:1014
static const char * sol_log_get_level_color(uint8_t level)
Get the color code used for the given logging level level.
Definition: sol-log.h:1018
static bool sol_log_get_show_colors(void)
Get if color output is enabled or not.
Definition: sol-log.h:1033
static uint8_t sol_log_get_abort_level(void)
Get the logging level that triggers the program to abort.
Definition: sol-log.h:1023
static void sol_log_vprint(const struct sol_log_domain *domain, uint8_t message_level, const char *file, const char *function, int line, const char *format, va_list args)
Similar to sol_log_print, but called with va_list instead of a variable number of arguments...
Definition: sol-log.h:742
static void enabled(void *data, bool powered)
Definition: browse.c:155
static bool sol_log_get_show_function(void)
Get if showing function's name is enabled or not.
Definition: sol-log.h:1043
These are common Soletta macros.
Structure containing the attributes of the domain used for logging.
Definition: sol-log.h:414
struct sol_log_domain * sol_log_global_domain
Global logging domain.
static void sol_log_print_function_stderr(void *data, const struct sol_log_domain *domain, uint8_t message_level, const char *file, const char *function, int line, const char *format, va_list args)
Standard logging function that send to standard error output.
Definition: sol-log.h:785
static void sol_log_domain_init_level(struct sol_log_domain *domain)
Initialize domain log level based on system configuration.
Definition: sol-log.h:484
static void sol_log_set_level(uint8_t level)
Set the global domain maximum level to level.
Definition: sol-log.h:1057
struct sol_log_domain sol_log_domain
Structure containing the attributes of the domain used for logging.
static void sol_log_set_abort_level(uint8_t level)
Set the logging level that should trigger the program to abort.
Definition: sol-log.h:1053
const char * color
Color to be used.
Definition: sol-log.h:415
sol_log_level
Available logging levels.
Definition: sol-log.h:403
static bool sol_log_get_show_file(void)
Get if showing source file's name is enabled or not.
Definition: sol-log.h:1038
Informational.
Definition: sol-log.h:407
static void sol_log_set_show_colors(bool enabled)
Enable/Disables the use of colors in logging messages.
Definition: sol-log.h:1061
static bool sol_log_get_show_line(void)
Get if showing the line number is enabled or not.
Definition: sol-log.h:1048
const char * name
Domain name.
Definition: sol-log.h:416
static uint8_t sol_log_get_level(void)
Get the maximum log level allowed.
Definition: sol-log.h:1028
Warning.
Definition: sol-log.h:406
Error.
Definition: sol-log.h:405
Critical.
Definition: sol-log.h:404
static void sol_log_set_show_line(bool enabled)
Enable/Disables the output of the line number in logging messages.
Definition: sol-log.h:1073
uint8_t level
Maximum level to log for this domain.
Definition: sol-log.h:417
static void sol_log_set_show_file(bool enabled)
Enable/Disables the output of source file's name in logging messages.
Definition: sol-log.h:1065