예제 #1
0
extern "C" struct dnet_node *dnet_parse_config(const char *file, int mon)
{
	dnet_node *node = NULL;
	config_data *data = NULL;

	try {
		data = static_cast<config_data *>(dnet_config_data_create());
		if (!data)
			throw std::bad_alloc();

		data->config_path = file;

		auto parser = data->parse_config();
		const config root = parser->root();
		const config logger = root.at("logger");
		const config options = root.at("options");
		const config backends = root.at("backends");

		parse_logger(data, logger);
		parse_options(data, options);
		parse_backends(data, backends);

		if (data->daemon_mode && !mon)
			dnet_background();

		if (!data->cfg_addr_num)
			throw config_error("no local address specified, exiting");

		node = dnet_server_node_create(data);
		if (!node)
			throw config_error("failed to create node");

		static_assert(sizeof(dnet_addr) == sizeof(address), "Size of dnet_addr and size of address must be equal");
		if (data->remotes.size() != 0) {
			int err = dnet_add_state(node, reinterpret_cast<const dnet_addr *>(data->remotes.data()), data->remotes.size(), 0);
			if (err < 0)
				BH_LOG(*node->log, DNET_LOG_WARNING, "Failed to connect to remote nodes: %d", err);
		}

	} catch (std::exception &exc) {
		if (data && data->cfg_state.log) {
			dnet_backend_log(data->cfg_state.log, DNET_LOG_ERROR,
				"cnf: failed to read config file '%s': %s", file, exc.what());
		} else {
			fprintf(stderr, "cnf: %s\n", exc.what());
			fflush(stderr);
		}

		if (node)
			dnet_server_node_destroy(node);
		else if (data)
			dnet_config_data_destroy(data);

		return NULL;
	}

	return node;
}
예제 #2
0
static gboolean
do_configure(Log4gConfigurator *base, const char *uri,
        G_GNUC_UNUSED Log4gLoggerRepository *repository, GError **error)
{
	struct Private *priv = GET_PRIVATE(base);
	gboolean status = TRUE;
	GString *string = g_string_sized_new(128);
	if (!string) {
		g_set_error(error, LOG4G_ERROR, LOG4G_ERROR_FAILURE,
				Q_("g_string_new() returned NULL"));
		return FALSE;
	}
	xmlSetGenericErrorFunc(string, error_handler);
	gint options =
		XML_PARSE_NOWARNING | XML_PARSE_NOERROR | XML_PARSE_NOBLANKS;
	if (g_getenv("LOG4G_PARSE_DTDVALID")) {
		options |= XML_PARSE_DTDVALID;
	}
	/* read XML file */
	xmlDocPtr doc = xmlCtxtReadFile(priv->ctx, uri, NULL, options);
	if (!doc) {
		g_set_error(error, LOG4G_ERROR, LOG4G_ERROR_FAILURE,
				Q_("failed to parse configuration"));
		status = FALSE;
		goto exit;
	}
	/* check root element */
	xmlNodePtr node = xmlDocGetRootElement(doc);
	if (!node) {
		g_set_error(error, LOG4G_ERROR, LOG4G_ERROR_FAILURE,
				Q_("invalid document: document is empty"));
		status = FALSE;
		goto exit;
	}
	if (xmlStrcmp(node->name, (const xmlChar *)"configuration")) {
		g_set_error(error, LOG4G_ERROR, LOG4G_ERROR_FAILURE,
				Q_("%s: invalid root element (expected "
					"log4g:configuration)"),
				node->name);
		status = FALSE;
		goto exit;
	}
	/* parse root attributes */
	xmlChar *att = xmlGetProp(node, (const xmlChar *)"debug");
	if (att) {
		if (!xmlStrcmp(att, (const xmlChar *)"true")) {
			log4g_set_internal_debugging(TRUE);
		} else if (!xmlStrcmp(att, (const xmlChar *)"false")) {
			log4g_set_internal_debugging(FALSE);
		} else if (!xmlStrcmp(att, (const xmlChar *)"null")) {
			log4g_log_warn(Q_("%s: ignoring `debug' attribute"),
					att);
		} else {
			log4g_log_error(Q_("%s: invalid value for attribute "
						"`debug'"), att);
		}
		xmlFree(att);
	}
	att = xmlGetProp(node, (const xmlChar *)"reset");
	if (att) {
		if (!xmlStrcmp(att, (const xmlChar *)"true")) {
			log4g_log_manager_reset_configuration();
		}
		xmlFree(att);
	}
	att = xmlGetProp(node, (const xmlChar *)"threshold");
	if (att) {
		Log4gLevel *level;
		Log4gLoggerRepository *repository =
			log4g_log_manager_get_logger_repository();
		if (!xmlStrcmp(att, (const xmlChar *)"all")) {
			level = log4g_level_ALL();
		} else if (!xmlStrcmp(att, (const xmlChar *)"trace")) {
			level = log4g_level_TRACE();
		} else if (!xmlStrcmp(att, (const xmlChar *)"debug")) {
			level = log4g_level_DEBUG();
		} else if (!xmlStrcmp(att, (const xmlChar *)"info")) {
			level = log4g_level_INFO();
		} else if (!xmlStrcmp(att, (const xmlChar *)"warn")) {
			level = log4g_level_WARN();
		} else if (!xmlStrcmp(att, (const xmlChar *)"error")) {
			level = log4g_level_ERROR();
		} else if (!xmlStrcmp(att, (const xmlChar *)"fatal")) {
			level = log4g_level_FATAL();
		} else if (!xmlStrcmp(att, (const xmlChar *)"off")) {
			level = log4g_level_OFF();
		} else if (!xmlStrcmp(att, (const xmlChar *)"null")) {
			level = NULL;
		} else {
			log4g_log_error(Q_("%s: invalid repository threshold"), att);
			level = NULL;
		}
		if (level) {
			log4g_logger_repository_set_threshold(repository,
					level);
		}
		xmlFree(att);
	}
	/* parse document */
	node = node->xmlChildrenNode;
	while (node) {
		if (!xmlStrcmp(node->name, (const xmlChar *)"appender")) {
			Log4gAppender *appender = parse_appender(base, node);
			if (appender) {
				g_object_unref(appender);
			}
		} else if (!xmlStrcmp(node->name, (const xmlChar *)"logger")) {
			parse_logger(base, node);
		} else if (!xmlStrcmp(node->name, (const xmlChar *)"root")) {
			parse_root(base, node);
		} else if (!xmlStrcmp(node->name, (const xmlChar *)"object")) {
			parse_object(base, node);
		} else if (!xmlStrcmp(node->name, (const xmlChar *)"text")) {
			log4g_log_warn(Q_("invalid text element"));
		} else if (!xmlStrcmp(node->name, (const xmlChar *)"comment")) {
			/* do nothing */
		} else {
			log4g_log_warn(Q_("%s: invalid element"), node->name);
		}
		node = node->next;
	}
exit:
	if (string) {
		g_string_free(string, TRUE);
	}
	if (doc) {
		xmlFreeDoc(doc);
	}
	return status;
}