END_TEST #ifdef HAVE_PARSE_PRINTF_FORMAT /** * Test parsing additional format strings, that are only supported if * we're under glibc. */ START_TEST (test_positional_params) { char *msg; struct json_object *jo; ul_openlog ("umberlog/test_positional_params", 0, LOG_LOCAL0); #define COMPLEX_FORMAT \ "%3$*5$.*2$hhd , %1$Lf , %4$.3s , %4$s", 1.0L, 5, (char)100, "prefix", -8 #define COMPLEX_RESULT "00100 , 1.000000 , pre , prefix" msg = ul_format (LOG_DEBUG, COMPLEX_FORMAT, "simple1", "value1", "complex", COMPLEX_FORMAT, "simple2", "value2", NULL); jo = parse_msg (msg); free (msg); verify_value (jo, "msg", COMPLEX_RESULT); verify_value (jo, "simple1", "value1"); verify_value (jo, "complex", COMPLEX_RESULT); verify_value (jo, "simple2", "value2"); json_object_put (jo); ul_closelog (); }
END_TEST /** * Test that using ul_openlog() does work, and sets up the program * identity appropriately. */ START_TEST (test_ul_openlog) { char *msg; struct json_object *jo; char host[_POSIX_HOST_NAME_MAX + 1]; ul_openlog ("umberlog/test_ul_openlog", 0, LOG_LOCAL0); msg = ul_format (LOG_DEBUG, "hello, I'm %s!", __FUNCTION__, NULL); jo = parse_msg (msg); free (msg); gethostname (host, _POSIX_HOST_NAME_MAX); verify_value (jo, "msg", "hello, I'm test_ul_openlog!"); verify_value (jo, "facility", "local0"); verify_value (jo, "priority", "debug"); verify_value (jo, "program", "umberlog/test_ul_openlog"); verify_value_exists (jo, "pid"); verify_value_exists (jo, "uid"); verify_value_exists (jo, "gid"); verify_value_exists (jo, "timestamp"); verify_value (jo, "host", host); json_object_put (jo); ul_closelog (); }
END_TEST /** * Test that discovering priorities work, and the implicit pid * overrides the explicit one. */ START_TEST (test_discover_priority) { char *msg, *pid; struct json_object *jo; ul_openlog ("umberlog/test_discover_priority", 0, LOG_LOCAL0); ul_set_log_flags (LOG_UL_ALL); msg = ul_format (LOG_DEBUG, "testing 1, 2, 3...", "pid", "%d", getpid () + 42, NULL); jo = parse_msg (msg); free (msg); verify_value (jo, "msg", "testing 1, 2, 3..."); if (asprintf (&pid, "%d", getpid ()) == -1) abort (); verify_value (jo, "pid", pid); free (pid); json_object_put (jo); ul_closelog (); }
END_TEST /** * Test adding additional fields. */ START_TEST (test_additional_fields) { char *msg; struct json_object *jo; ul_openlog ("umberlog/test_additional_fields", 0, LOG_LOCAL0); msg = ul_format (LOG_DEBUG, "testing 1, 2, 3...", "random_number", "%d", 42, "random_string", "fourty-two", NULL); jo = parse_msg (msg); free (msg); verify_value (jo, "msg", "testing 1, 2, 3..."); verify_value (jo, "random_number", "42"); verify_value (jo, "random_string", "fourty-two"); json_object_put (jo); ul_closelog (); }
END_TEST /** * Test turning off the timestamp. */ START_TEST (test_no_timestamp) { char *msg; struct json_object *jo; ul_openlog ("umberlog/test_no_timestamp", 0, LOG_LOCAL0); ul_set_log_flags (LOG_UL_NOTIME); msg = ul_format (LOG_DEBUG, "hello, I'm %s!", __FUNCTION__, NULL); jo = parse_msg (msg); free (msg); verify_value (jo, "msg", "hello, I'm test_no_timestamp!"); verify_value (jo, "facility", "local0"); verify_value (jo, "priority", "debug"); verify_value (jo, "program", "umberlog/test_no_timestamp"); verify_value_exists (jo, "pid"); verify_value_exists (jo, "uid"); verify_value_exists (jo, "gid"); verify_value_missing (jo, "timestamp"); verify_value_exists (jo, "host"); json_object_put (jo); ul_closelog (); }
END_TEST /** * Test that setting LOG_UL_NOIMPLICIT will, indeed, turn off * automatically discovered fields. */ START_TEST (test_no_implicit) { char *msg; struct json_object *jo; ul_openlog ("umberlog/test_no_implicit", 0, LOG_LOCAL0); ul_set_log_flags (LOG_UL_NOIMPLICIT); msg = ul_format (LOG_DEBUG, "hello, I'm %s!", __FUNCTION__, NULL); jo = parse_msg (msg); free (msg); verify_value (jo, "msg", "hello, I'm test_no_implicit!"); verify_value_missing (jo, "facility"); verify_value_missing (jo, "priority"); verify_value_missing (jo, "program"); verify_value_missing (jo, "pid"); verify_value_missing (jo, "uid"); verify_value_missing (jo, "gid"); verify_value_missing (jo, "host"); verify_value_missing (jo, "timestamp"); json_object_put (jo); ul_closelog (); }
END_TEST /** * Test that using a FACILITY | PRIORITY combo with ul_format has the * desired result. */ START_TEST (test_facprio) { char *msg; struct json_object *jo; msg = ul_format (LOG_LOCAL1 | LOG_DEBUG, "%s", __FUNCTION__, NULL); jo = parse_msg (msg); free (msg); verify_value (jo, "facility", "local1"); verify_value (jo, "priority", "debug"); json_object_put (jo); }
END_TEST /** * Test for correct JSON escaping. */ START_TEST (test_json_escape) { static const char control_chars[] = "\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f" "\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f" "\x20"; char *msg; struct json_object *jo; ul_openlog ("umberlog/test_json_escape", 0, LOG_LOCAL0); msg = ul_format (LOG_DEBUG, "%s", __FUNCTION__, "quotes", "Hi, \"quoted value\" speaking!", "\"really\"", "yeah", "control", "foo\nbar", "utf8", "Árvíztűrő tükörfúrógép", "junk", "\013foo", "all_control", control_chars, NULL); jo = parse_msg (msg); free (msg); verify_value (jo, "quotes", "Hi, \"quoted value\" speaking!"); verify_value (jo, "\"really\"", "yeah"); verify_value (jo, "control", "foo\nbar"); verify_value (jo, "utf8", "Árvíztűrő tükörfúrógép"); verify_value (jo, "junk", "\013foo"); verify_value (jo, "all_control", control_chars); json_object_put (jo); ul_closelog (); }
/* * capft * * test ca_put() over a range of data types * */ static int capft( char *pname, char *pvalue ) { dbr_short_t shortvalue; dbr_long_t longvalue; dbr_float_t floatvalue; dbr_char_t charvalue; dbr_double_t doublevalue; unsigned long ntries = 10ul; int status; chid chan_id; if (((*pname < ' ') || (*pname > 'z')) || ((*pvalue < ' ') || (*pvalue > 'z'))){ printf("\nusage \"pv name\",\"value\"\n"); return -1; } /* * convert name to chan id */ status = ca_search(pname, &chan_id); SEVCHK(status,NULL); status = ca_pend_io(5.0); if(status != ECA_NORMAL){ SEVCHK(ca_clear_channel(chan_id),NULL); printf("Not Found %s\n", pname); return -1; } printf("name:\t%s\n", ca_name(chan_id)); printf("native type:\t%d\n", ca_field_type(chan_id)); printf("native count:\t%lu\n", ca_element_count(chan_id)); /* * string value ca_put */ status = ca_put( DBR_STRING, chan_id, pvalue); SEVCHK(status, NULL); verify_value(chan_id, DBR_STRING); if(ca_field_type(chan_id)==0)goto skip_rest; if(sscanf(pvalue,"%hd",&shortvalue)==1) { /* * short integer ca_put */ status = ca_put( DBR_SHORT, chan_id, &shortvalue); SEVCHK(status, NULL); verify_value(chan_id, DBR_SHORT); status = ca_put( DBR_ENUM, chan_id, &shortvalue); SEVCHK(status, NULL); verify_value(chan_id, DBR_ENUM); charvalue=(dbr_char_t)shortvalue; status = ca_put( DBR_CHAR, chan_id, &charvalue); SEVCHK(status, NULL); verify_value(chan_id, DBR_CHAR); } if(sscanf(pvalue,"%d",&longvalue)==1) { /* * long integer ca_put */ status = ca_put( DBR_LONG, chan_id, &longvalue); SEVCHK(status, NULL); verify_value(chan_id, DBR_LONG); } if(epicsScanFloat(pvalue, &floatvalue)==1) { /* * single precision float ca_put */ status = ca_put( DBR_FLOAT, chan_id, &floatvalue); SEVCHK(status, NULL); verify_value(chan_id, DBR_FLOAT); } if(epicsScanDouble(pvalue, &doublevalue)==1) { /* * double precision float ca_put */ status = ca_put( DBR_DOUBLE, chan_id, &doublevalue); SEVCHK(status, NULL); verify_value(chan_id, DBR_DOUBLE); } skip_rest: /* * wait for the operation to complete * (outstabnding decrements to zero) */ while(ntries){ ca_pend_event(1.0); if(!outstanding){ SEVCHK(ca_clear_channel(chan_id),NULL); printf("\n\n"); return 0; } ntries--; } SEVCHK(ca_clear_channel(chan_id),NULL); return -1; }