int main( ) { execute( Seq( If( True( ), Print( String( "hello " ) ), Print( True( ) ) ), If( Or( False( ), Neg( True( ) ) ), Print( Unit( ) ), Print( String( "world!\n" ) ) ) ) ); execute( Seq( When( True( ), Print( String( "hello " ) ) ), Unless( False( ), Print( String( "world!\n" ) ) ) ) ); execute( Print( Print( Seq( Print( True( ) ), Print( False( ) ) ) ) ) ); execute( Print( Concat( String( "4" ), Show( True( ) ) ) ) ); assert( value_to_bool( execute( IsDefined( String( "not defined" ) ) ) ) == false ); execute( Seq( Set( String( "SomeVar" ), String( "12" ) ), Seq( Print( Get( String( "SomeVar" ) ) ), Seq( Set( Concat( String( "S" ), String( "omeVar" ) ), String( "345\n" ) ), Print( Get( Concat( String( "Some" ), String( "Var" ) ) ) ) ) ) ) ); execute( Seq( If( True( ), Set( String( "hello " ), String( "hellos \n" ) ), Print( True( ) ) ), Print( Get( String( "hello " ) ) ) ) ); execute( Seq( Scope( Set( String( "hi" ), True( ) ) ), Print( IsDefined( String( "hi" ) ) ) ) ); assert( value_to_bool( execute( Seq( Seq( Set( String( "var" ), False( ) ), Scope( Set( String( "var" ), True( ) ) ) ), Get( String( "var" ) ) ) ) ) ); execute( Seq( Seq( Set( String( "b1" ), True( ) ), Set( String( "b2" ), True( ) ) ), While( Or( Get( String( "b1" ) ), Get( String( "b2" ) ) ), If( Get( String( "b1" ) ), Seq( Set( String( "b1" ), False( ) ), Print( String( "Hello" ) ) ), Seq( Set( String( "b2" ), False( ) ), Print( String( "World" ) ) ) ) ) ) ); }
/** * Callback function to use with INI-H. * @arg user Opaque user value. We use the statsite_config pointer * @arg section The INI seciton * @arg name The config name * @arg value The config value * @return 1 on success. */ static int config_callback(void* user, const char* section, const char* name, const char* value) { // Specially handle histogram sections if (strncasecmp("histogram", section, 9) == 0) { return histogram_callback(user, section, name, value); } // Ignore any non-statsite sections if (strcasecmp("statsite", section) != 0) { return 0; } // Cast the user handle statsite_config *config = (statsite_config*)user; // Switch on the config #define NAME_MATCH(param) (strcasecmp(param, name) == 0) // Handle the int cases if (NAME_MATCH("port")) { return value_to_int(value, &config->tcp_port); } else if (NAME_MATCH("tcp_port")) { return value_to_int(value, &config->tcp_port); } else if (NAME_MATCH("udp_port")) { return value_to_int(value, &config->udp_port); } else if (NAME_MATCH("flush_interval")) { return value_to_int(value, &config->flush_interval); } else if (NAME_MATCH("daemonize")) { return value_to_bool(value, &config->daemonize); } else if (NAME_MATCH("binary_stream")) { return value_to_bool(value, &config->binary_stream); // Handle the double cases } else if (NAME_MATCH("timer_eps")) { return value_to_double(value, &config->timer_eps); } else if (NAME_MATCH("set_eps")) { return value_to_double(value, &config->set_eps); // Copy the string values } else if (NAME_MATCH("log_level")) { config->log_level = strdup(value); } else if (NAME_MATCH("stream_cmd")) { config->stream_cmd = strdup(value); } else if (NAME_MATCH("pid_file")) { config->pid_file = strdup(value); } else if (NAME_MATCH("input_counter")) { config->input_counter = strdup(value); } else if (NAME_MATCH("bind_address")) { config->bind_address = strdup(value); // Unknown parameter? } else { // Log it, but ignore syslog(LOG_NOTICE, "Unrecognized config parameter: %s", value); } // Success return 1; }
bool get_bool(const std::string &key, bool defval) const { for (auto const &t : *this) if (t.key == key) { return value_to_bool(t.value.c_str(), defval); } return defval; }
/** * Callback function to use with INI-H. * @arg user Opaque user value. We use the statsite_config pointer * @arg section The INI seciton * @arg name The config name * @arg value The config value * @return 1 on success. */ static int config_callback(void* user, const char* section, const char* name, const char* value) { // Specially handle histogram sections if (strncasecmp("histogram", section, 9) == 0) { return histogram_callback(user, section, name, value); } if (strncasecmp("sink", section, 4) == 0) { return sink_callback(user, section, name, value); } // Ignore any non-statsite sections if (strcasecmp("statsite", section) != 0) { syslog(LOG_NOTICE, "Unknown values in section ignored: %s", section); return 0; } // Cast the user handle statsite_config *config = (statsite_config*)user; // Handle the int cases if (NAME_MATCH("port")) { return value_to_int(value, &config->tcp_port); } else if (NAME_MATCH("tcp_port")) { return value_to_int(value, &config->tcp_port); } else if (NAME_MATCH("udp_port")) { return value_to_int(value, &config->udp_port); } else if (NAME_MATCH("flush_interval")) { return value_to_int(value, &config->flush_interval); } else if (NAME_MATCH("parse_stdin")) { return value_to_bool(value, &config->parse_stdin); } else if (NAME_MATCH("daemonize")) { return value_to_bool(value, &config->daemonize); } else if (NAME_MATCH("use_type_prefix")) { return value_to_bool(value, &config->use_type_prefix); } else if (NAME_MATCH("extended_counters")) { return value_to_bool(value, &config->extended_counters); } else if (NAME_MATCH("prefix_binary_stream")) { return value_to_bool(value, &config->prefix_binary_stream); // Handle the double cases } else if (NAME_MATCH("timer_eps")) { return value_to_double(value, &config->timer_eps); } else if (NAME_MATCH("set_eps")) { return value_to_double(value, &config->set_eps); // Handle quantiles as a comma-separated list of doubles } else if (NAME_MATCH("quantiles")) { return value_to_list_of_doubles(value, &config->quantiles, &config->num_quantiles); // Copy the string values } else if (NAME_MATCH("log_level")) { config->log_level = strdup(value); } else if (NAME_MATCH("log_facility")) { config->log_facility = strdup(value); } else if (NAME_MATCH("pid_file")) { config->pid_file = strdup(value); } else if (NAME_MATCH("input_counter")) { config->input_counter = strdup(value); } else if (NAME_MATCH("bind_address")) { config->bind_address = strdup(value); } else if (NAME_MATCH("global_prefix")) { config->global_prefix = strdup(value); } else if (NAME_MATCH("counts_prefix")) { config->prefixes[COUNTER] = strdup(value); } else if (NAME_MATCH("gauges_prefix")) { config->prefixes[GAUGE] = strdup(value); } else if (NAME_MATCH("timers_prefix")) { config->prefixes[TIMER] = strdup(value); } else if (NAME_MATCH("sets_prefix")) { config->prefixes[SET] = strdup(value); } else if (NAME_MATCH("kv_prefix")) { config->prefixes[KEY_VAL] = strdup(value); // Copy the multi-case variables } else if (NAME_MATCH("log_facility")) { return name_to_facility(value, &config->syslog_log_facility); // Unknown parameter? } else { // Log it, but ignore syslog(LOG_NOTICE, "Unrecognized config parameter: %s", name); } // Success return 1; }
/** * Callback function to use with INIH for parsing sink configurations. * The sink type is currently encoded into the section name to * simplify instantiating the correct configuration type. */ static int sink_callback(void* user, const char* section, const char* name, const char* value) { statsite_config *all_config = (statsite_config*)user; /* The sink section does not match - lets commit since we're on to a new one now */ if (sink_in_progress && strcasecmp(sink_section, section)) { sink_commit(all_config); } /* Nothing in progress? Create it! */ if (!sink_in_progress) { if (!sink_section) sink_section = strdup(section); char* section_to_tokenize = strdup(section); char* tok = NULL; char* header = strtok_r(section_to_tokenize, "_", &tok); char* type = strtok_r(NULL, "_", &tok); char* name = strtok_r(NULL, "_", &tok); if (header == NULL || type == NULL || name == NULL) { free(section_to_tokenize); syslog(LOG_WARNING, "Sink section %s is not of the form \"sink_[type]_[name]\"", section); return 0; } /* Match various sink types to find their type */ if (strcasecmp(type, "stream") == 0) { sink_config_stream* config = calloc(1, sizeof(sink_config_stream)); sink_in_progress = (sink_config*)config; config->super.type = SINK_TYPE_STREAM; config->super.name = strdup(name); config->stream_cmd = DEFAULT_SINK.stream_cmd; } else if (strcasecmp(type, "http") == 0) { sink_config_http* config = malloc(sizeof(sink_config_http)); memcpy(config, &DEFAULT_HTTP_SINK, sizeof(sink_config_http)); sink_in_progress = (sink_config*)config; config->super.name = strdup(name); } else { free(section_to_tokenize); /* Unknown sink type - abort! */ syslog(LOG_WARNING, "Unknown sink type: %s for sink: %s", type, name); return 0; } free(section_to_tokenize); } switch(sink_in_progress->type) { case SINK_TYPE_STREAM: { sink_config_stream* config = (sink_config_stream*)sink_in_progress; if (NAME_MATCH("binary")) { return value_to_bool(value, &config->binary_stream); } else if (NAME_MATCH("command")) { config->stream_cmd = strdup(value); } else { syslog(LOG_NOTICE, "Unrecognized stream sink parameter: %s", name); return 0; } break; } case SINK_TYPE_HTTP: { sink_config_http* config = (sink_config_http*)sink_in_progress; if (NAME_MATCH("url")) { config->post_url = strdup(value); } else if (NAME_MATCH("metrics_name")) { config->metrics_name = strdup(value); } else if (NAME_MATCH("timestamp_name")) { config->timestamp_name = strdup(value); } else if (NAME_MATCH("timestamp_format")) { config->timestamp_format = strdup(value); } else if (NAME_MATCH("ciphers")) { config->ciphers = strdup(value); } else if (NAME_MATCH("oauth_key")) { config->oauth_key = strdup(value); } else if (NAME_MATCH("oauth_secret")) { config->oauth_secret = strdup(value); } else if (NAME_MATCH("oauth_token_url")) { config->oauth_token_url = strdup(value); } else if (NAME_MATCH("max_buffer_size")) { value_to_int(value, &config->max_buffer_size); } else if (NAME_MATCH("send_backoff_ms")) { value_to_int(value, &config->send_backoff_ms); } else { /* Attempt to locate keys * of the form param_PNAME */ char* param_tok = strdup(name); char* tok = NULL; char* header = strtok_r(param_tok, "_", &tok); char* param = strtok_r(NULL, "_", &tok); if (header == NULL || param == NULL || strcasecmp("param", header) != 0) { syslog(LOG_NOTICE, "Unrecognized http sink parameters: %s: %s", name, header); free(param_tok); return 0; } kv_config* last_kv = config->params; config->params = calloc(1, sizeof(kv_config)); config->params->k = strdup(param); config->params->v = strdup(value); config->params->next = last_kv; free(param_tok); } break; } default: syslog(LOG_WARNING, "Grevious state problem"); return 0; } return 1; }
TEST(double) value_t x = value_from_double(42.101010); PT_ASSERT_EQ(value_to_double(x), 42.101010); END(double) TEST(fixnum) value_t x = value_from_int(322); PT_ASSERT_EQ(value_to_int(x), 322); END(fixnum) TEST(bool) value_t x = value_true(); PT_ASSERT(value_to_bool(x)); x = value_false(); PT_ASSERT(!value_to_bool(x)); END(bool) TEST(ptr) struct arena_handle *a = arena_new((yu_allocator *)&mctx); struct boxed_value *v = arena_alloc_val(a); value_t x = value_from_ptr(&v); PT_ASSERT_EQ(value_get_ptr(x), v); PT_ASSERT_EQ(boxed_value_owner(v), a); END(ptr) TEST(value_type) struct arena_handle *a = arena_new((yu_allocator *)&mctx); struct boxed_value *v1 = arena_alloc_val(a), *v2 = arena_alloc_val(a); value_t w = value_from_int(655), x = value_true(), y = value_from_ptr(&v1),