ph_result_t ph_sockaddr_print(ph_sockaddr_t *sa, ph_string_t *str, bool want_port) { int ret; char buf[128]; char pbuf[32]; switch (sa->family) { case AF_UNIX: return ph_string_append_cstr(str, sa->sa.nix.sun_path); case AF_INET: case AF_INET6: ret = getnameinfo(&sa->sa.sa, ph_sockaddr_socklen(sa), buf, sizeof(buf), pbuf, sizeof(pbuf), NI_NUMERICHOST|NI_NUMERICSERV); if (ret) { return PH_ERR; } if (want_port) { ph_string_printf(str, "[%s]:%s", buf, pbuf); return PH_OK; } return ph_string_append_cstr(str, buf); } return PH_ERR; }
void ph_logv(uint8_t level, const char *fmt, va_list ap) { struct timeval now = ph_time_now(); ph_thread_t *me; int len; va_list copy; char *buf; PH_STRING_DECLARE_STACK(mystr, 1024); void *args[] = { &level, &mystr }; static ph_hook_point_t *hook = NULL; char tname[32]; if (level > log_level) { return; } len = strlen(fmt); if (len == 0) { return; } me = ph_thread_self(); get_tname(me, tname, sizeof(tname)); va_copy(copy, ap); ph_string_printf(&mystr, "%" PRIi64 ".%03d %s: %s `Pv%s%p%s", (int64_t)now.tv_sec, (int)(now.tv_usec / 1000), log_labels[level], tname, fmt, ph_vaptr(copy), fmt[len-1] == '\n' ? "" : "\n"); va_end(copy); if (ph_unlikely(hook == NULL)) { hook = ph_hook_point_get_cstr(PH_LOG_HOOK_NAME, true); } ph_hook_invoke_inner(hook, sizeof(args)/sizeof(args[0]), args); if (disable_stderr) { return; } pthread_mutex_lock(&log_lock); buf = mystr.buf; len = mystr.len; while (len) { int x = write(STDERR_FILENO, buf, len); if (x <= 0) { break; } buf += x; len -= x; } pthread_mutex_unlock(&log_lock); }
int main(int argc, char **argv) { ph_string_t *str, *str2; ph_unused_parameter(argc); ph_unused_parameter(argv); ph_library_init(); plan_tests(102); mt_misc = ph_memtype_register(&mt_def); stack_tests(); // Tests reallocation str = ph_string_make_empty(mt_misc, 16); is(ph_string_append_cstr(str, "1234567890"), PH_OK); is(10, ph_string_len(str)); is(ph_string_append_cstr(str, "1234567890"), PH_OK); is(20, ph_string_len(str)); is(memcmp(str->buf, "12345678901234567890", 20), 0); ph_string_delref(str); // Tests reallocation and string formatting str = ph_string_make_empty(mt_misc, 4); is(ph_string_printf(str, "Hello %s", "world"), 11); is(ph_string_len(str), 11); str2 = ph_string_make_empty(mt_misc, 10); is(ph_string_printf(str2, "copy `Ps%p", (void*)str), 16); diag(":%.*s:", str2->len, str2->buf); is(ph_string_len(str2), 16); is(memcmp(str2->buf, "copy Hello world", 16), 0); ph_string_delref(str2); str2 = ph_string_make_empty(mt_misc, 10); is(ph_string_printf(str2, "copy `Ps%d%p", 5, (void*)str), 10); is(memcmp(str2->buf, "copy Hello", 10), 0); ok(!ph_string_equal(str, str2), "not same"); ph_string_delref(str2); str2 = ph_string_make_empty(mt_misc, 10); ph_string_append_buf(str2, str->buf, str->len); ok(ph_string_equal(str, str2), "same"); is(ph_string_compare(str, str2), 0); ph_string_delref(str); ph_string_delref(str2); str = ph_string_make_cstr(mt_misc, "abc"); str2 = ph_string_make_cstr(mt_misc, "bbc"); ok(ph_string_compare(str, str2) < 0, "abc < bbc"); ok(ph_string_compare(str2, str) > 0, "abc < bbc"); ph_string_delref(str2); str2 = ph_string_make_cstr(mt_misc, "abca"); ok(ph_string_compare(str, str2) < 0, "abc < abca"); ok(ph_string_compare(str2, str) > 0, "abc < abca"); ph_string_delref(str2); str2 = ph_string_make_cstr(mt_misc, "ab"); ok(ph_string_compare(str, str2) > 0, "abc > ab"); ok(ph_string_compare(str2, str) < 0, "abc > ab"); ph_string_delref(str2); ph_string_delref(str); str = ph_string_make_printf(mt_misc, 16, "Hello %d", 42); ok(ph_string_equal_cstr(str, "Hello 42"), "same"); ph_string_delref(str); utf16_tests(); string_stream_tests(); return exit_status(); }