X-Git-Url: http://git.efficios.com/?a=blobdiff_plain;f=src%2Fcommon%2Fcommon.h;h=5849101b578aaf6d59cccb83de0746df04ab8913;hb=b189a968258bcf286f022208b71849f8194828bb;hp=13dbaf49b1bef3e38cc9cb39597b80a9a2f3f9f8;hpb=578e048b5debf169e286e5b5cc747b5d6c16886d;p=babeltrace.git diff --git a/src/common/common.h b/src/common/common.h index 13dbaf49..5849101b 100644 --- a/src/common/common.h +++ b/src/common/common.h @@ -24,19 +24,24 @@ * SOFTWARE. */ -#include -#include "common/assert.h" -#include "common/babeltrace.h" -#include -#include -#include -#include -#include +#include +#include #include #include +#include #include +#include #include -#include +#include + +#include + +#define __BT_IN_BABELTRACE_H +#include + +#include "common/assert.h" +#include "common/macros.h" +#include "common/safe.h" #define BT_COMMON_COLOR_RESET "\033[0m" #define BT_COMMON_COLOR_BOLD "\033[1m" @@ -87,7 +92,7 @@ const char *bt_common_get_system_plugin_path(void); * return value. */ BT_HIDDEN -char *bt_common_get_home_plugin_path(void); +char *bt_common_get_home_plugin_path(int log_level); /* * Appends the list of directories in `paths` to the array `dirs`. @@ -304,7 +309,47 @@ void bt_common_custom_snprintf(char *buf, size_t buf_size, * Returns the system page size. */ BT_HIDDEN -size_t bt_common_get_page_size(void); +size_t bt_common_get_page_size(int log_level); + +/* + * Adds the digit separator `sep` as many times as needed to form groups + * of `digits_per_group` digits within `str`. `str` must have enough + * room to accomodate the new separators, that is: + * + * strlen(str) + (strlen(str) / digits_per_group) + 1 + * + * Example: with `str` `1983198398213`, `digits_per_group` 3, and `sep` + * `,`, `str` becomes `1,983,198,398,213`. + * + * `strlen(str)` must not be 0. `digits_per_group` must not be 0. `sep` + * must not be `\0`. + */ +BT_HIDDEN +void bt_common_sep_digits(char *str, unsigned int digits_per_group, char sep); + +/* + * This is similar to what the command `fold --spaces` does: it wraps + * the input lines of `str`, breaking at spaces, and indenting each line + * with `indent` spaces so that each line fits the total length + * `total_length`. + * + * If an original line in `str` contains a word which is >= the content + * length (`total_length - indent`), then the corresponding folded line + * is also larger than the content length. In other words, breaking at + * spaces is a best effort, but it might not be possible. + * + * The returned string, on success, is owned by the caller. + */ +BT_HIDDEN +GString *bt_common_fold(const char *str, unsigned int total_length, + unsigned int indent); + +/* + * Writes the terminal's width to `*width`, its height to `*height`, + * and returns 0 on success, or returns -1 on error. + */ +BT_HIDDEN +int bt_common_get_term_size(unsigned int *width, unsigned int *height); /* * Wraps read() function to handle EINTR and partial reads. @@ -312,7 +357,7 @@ size_t bt_common_get_page_size(void); * value smaller than the requested `count`. */ static inline -ssize_t bt_common_read(int fd, void *buf, size_t count) +ssize_t bt_common_read(int fd, void *buf, size_t count, int log_level) { size_t i = 0; ssize_t ret; @@ -326,15 +371,18 @@ ssize_t bt_common_read(int fd, void *buf, size_t count) ret = read(fd, buf + i, count - i); if (ret < 0) { if (errno == EINTR) { -#ifdef BT_LOGD_STR - BT_LOGD_STR("read() call interrupted. Retrying..."); +#ifdef BT_LOG_WRITE_CUR_LVL + BT_LOG_WRITE_CUR_LVL(BT_LOG_DEBUG, log_level, + BT_LOG_TAG, + "read() call interrupted; retrying..."); #endif /* retry operation */ continue; } else { -#ifdef BT_LOGE_ERRNO - BT_LOGE_ERRNO("Error while reading", ": fd=%d", - fd); +#ifdef BT_LOG_WRITE_ERRNO_CUR_LVL + BT_LOG_WRITE_ERRNO_CUR_LVL(BT_LOG_ERROR, + log_level, BT_LOG_TAG, + "Error while reading", ": fd=%d", fd); #endif goto end; } @@ -377,8 +425,12 @@ const char *bt_common_field_class_type_string(enum bt_field_class_type class_typ return "BT_FIELD_CLASS_TYPE_STATIC_ARRAY"; case BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY: return "BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY"; - case BT_FIELD_CLASS_TYPE_VARIANT: - return "BT_FIELD_CLASS_TYPE_VARIANT"; + case BT_FIELD_CLASS_TYPE_VARIANT_WITHOUT_SELECTOR: + return "BT_FIELD_CLASS_TYPE_VARIANT_WITHOUT_SELECTOR"; + case BT_FIELD_CLASS_TYPE_VARIANT_WITH_UNSIGNED_SELECTOR: + return "BT_FIELD_CLASS_TYPE_VARIANT_WITH_UNSIGNED_SELECTOR"; + case BT_FIELD_CLASS_TYPE_VARIANT_WITH_SIGNED_SELECTOR: + return "BT_FIELD_CLASS_TYPE_VARIANT_WITH_SIGNED_SELECTOR"; default: return "(unknown)"; } @@ -522,25 +574,60 @@ end: } static inline -const char *bt_common_self_message_iterator_status_string( - enum bt_self_message_iterator_status status) +const char *bt_common_logging_level_string( + enum bt_logging_level level) { - switch (status) { - case BT_SELF_MESSAGE_ITERATOR_STATUS_AGAIN: - return "BT_SELF_MESSAGE_ITERATOR_STATUS_AGAIN"; - case BT_SELF_MESSAGE_ITERATOR_STATUS_END: - return "BT_SELF_MESSAGE_ITERATOR_STATUS_END"; - case BT_SELF_MESSAGE_ITERATOR_STATUS_OK: - return "BT_SELF_MESSAGE_ITERATOR_STATUS_OK"; - case BT_SELF_MESSAGE_ITERATOR_STATUS_ERROR: - return "BT_SELF_MESSAGE_ITERATOR_STATUS_ERROR"; - case BT_SELF_MESSAGE_ITERATOR_STATUS_NOMEM: - return "BT_SELF_MESSAGE_ITERATOR_STATUS_NOMEM"; + switch (level) { + case BT_LOGGING_LEVEL_TRACE: + return "BT_LOGGING_LEVEL_TRACE"; + case BT_LOGGING_LEVEL_DEBUG: + return "BT_LOGGING_LEVEL_DEBUG"; + case BT_LOGGING_LEVEL_INFO: + return "BT_LOGGING_LEVEL_INFO"; + case BT_LOGGING_LEVEL_WARNING: + return "BT_LOGGING_LEVEL_WARNING"; + case BT_LOGGING_LEVEL_ERROR: + return "BT_LOGGING_LEVEL_ERROR"; + case BT_LOGGING_LEVEL_FATAL: + return "BT_LOGGING_LEVEL_FATAL"; + case BT_LOGGING_LEVEL_NONE: + return "BT_LOGGING_LEVEL_NONE"; default: return "(unknown)"; } }; +static inline +const char *bt_common_func_status_string(int status) +{ + switch (status) { + case __BT_FUNC_STATUS_OVERFLOW: + return "OVERFLOW"; + case __BT_FUNC_STATUS_INVALID_PARAMS: + return "INVALID_PARAMS"; + case __BT_FUNC_STATUS_INVALID_OBJECT: + return "INVALID_OBJECT"; + case __BT_FUNC_STATUS_MEMORY_ERROR: + return "MEMORY_ERROR"; + case __BT_FUNC_STATUS_ERROR: + return "ERROR"; + case __BT_FUNC_STATUS_OK: + return "OK"; + case __BT_FUNC_STATUS_END: + return "END"; + case __BT_FUNC_STATUS_NOT_FOUND: + return "NOT_FOUND"; + case __BT_FUNC_STATUS_AGAIN: + return "AGAIN"; + case __BT_FUNC_STATUS_UNSUPPORTED: + return "UNSUPPORTED"; + case __BT_FUNC_STATUS_CANCELED: + return "CANCELED"; + default: + return "(unknown)"; + } +} + #define NS_PER_S_I INT64_C(1000000000) #define NS_PER_S_U UINT64_C(1000000000) @@ -632,10 +719,50 @@ end: return ret; } +/* + * bt_g_string_append_printf cannot be inlined because it expects a + * variadic argument list. + */ +BT_HIDDEN +int bt_common_g_string_append_printf(GString *str, const char *fmt, ...); + +static inline +void bt_common_g_string_append(GString *str, const char *s) +{ + gsize len, allocated_len, s_len; + + /* str->len excludes \0. */ + len = str->len; + /* Exclude \0. */ + allocated_len = str->allocated_len - 1; + s_len = strlen(s); + if (G_UNLIKELY(allocated_len < len + s_len)) { + /* Resize. */ + g_string_set_size(str, len + s_len); + } else { + str->len = len + s_len; + } + memcpy(str->str + len, s, s_len + 1); +} + static inline -enum bt_self_message_iterator_status bt_common_message_iterator_status_to_self( - enum bt_message_iterator_status status) +void bt_common_g_string_append_c(GString *str, char c) { - return (int) status; + gsize len, allocated_len, s_len; + + /* str->len excludes \0. */ + len = str->len; + /* Exclude \0. */ + allocated_len = str->allocated_len - 1; + s_len = 1; + if (G_UNLIKELY(allocated_len < len + s_len)) { + /* Resize. */ + g_string_set_size(str, len + s_len); + } else { + str->len = len + s_len; + } + str->str[len] = c; + str->str[len + 1] = '\0'; } + #endif /* BABELTRACE_COMMON_INTERNAL_H */