Exemple #1
0
	void printProperties(const std::string& base)
	{
		AbstractConfiguration::Keys keys;
		config().keys(base, keys);
		if (keys.empty())
		{
			if (config().hasProperty(base))
			{
				std::string msg;
				msg.append(base);
				msg.append(" = ");
				msg.append(config().getString(base));
				logger().information(msg);
			}
		}
		else
		{
			for (AbstractConfiguration::Keys::const_iterator it = keys.begin(); it != keys.end(); ++it)
			{
				std::string fullKey = base;
				if (!fullKey.empty()) fullKey += '.';
				fullKey.append(*it);
				printProperties(fullKey);
			}
		}
	}
void LayeredConfigurationTest::testEmpty()
{
	AutoPtr<LayeredConfiguration> pLC = new LayeredConfiguration;
	
	AbstractConfiguration::Keys keys;
	pLC->keys(keys);
	assert (keys.empty());
	
	assert (!pLC->hasProperty("foo"));
	try
	{
		pLC->setString("foo", "bar");
		fail("empty LayeredConfiguration - must throw");
	}
	catch (RuntimeException&)
	{
	}
	
	try
	{
		std::string s = pLC->getString("foo");
		fail("empty LayeredConfiguration - must throw");
	}
	catch (NotFoundException&)
	{
	}
}
void ConfigurationViewTest::testView()
{
	AutoPtr<AbstractConfiguration> pConf = createConfiguration();
	AutoPtr<AbstractConfiguration> pView = pConf->createView("");
	assert (pView->hasProperty("prop1"));
	assert (pView->hasProperty("prop2"));

	AbstractConfiguration::Keys keys;
	pView->keys(keys);
	assert (keys.size() == 4);
	assert (std::find(keys.begin(), keys.end(), "prop1") != keys.end());
	assert (std::find(keys.begin(), keys.end(), "prop2") != keys.end());
	assert (std::find(keys.begin(), keys.end(), "prop3") != keys.end());
	assert (std::find(keys.begin(), keys.end(), "prop4") != keys.end());
	
	assert (pView->getString("prop1") == "foo");
	assert (pView->getString("prop3.string1") == "foo");
	
	pView->setString("prop5", "foobar");
	assert (pConf->getString("prop5") == "foobar");
	
	pView = pConf->createView("prop1");
	pView->keys(keys);
	assert (keys.empty());
	assert (pView->hasProperty("prop1"));

	pView->setString("prop11", "foobar");
	assert (pConf->getString("prop1.prop11") == "foobar");

	pView = pConf->createView("prop3");
	pView->keys(keys);
	assert (keys.size() == 2);
	assert (std::find(keys.begin(), keys.end(), "string1") != keys.end());
	assert (std::find(keys.begin(), keys.end(), "string2") != keys.end());
	
	assert (pView->getString("string1") == "foo");
	assert (pView->getString("string2") == "bar");

	pView->setString("string3", "foobar");
	assert (pConf->getString("prop3.string3") == "foobar");

	pView = pConf->createView("prop4");
	pView->keys(keys);
	assert (keys.size() == 2);
	assert (std::find(keys.begin(), keys.end(), "prop41") != keys.end());
	assert (std::find(keys.begin(), keys.end(), "prop42") != keys.end());
	
	assert (pView->getString("prop41.string1") == "FOO");
	assert (pView->getString("prop42.string2") == "Bar");

	pView = pConf->createView("prop4.prop41");
	pView->keys(keys);
	assert (keys.size() == 2);
	assert (std::find(keys.begin(), keys.end(), "string1") != keys.end());
	assert (std::find(keys.begin(), keys.end(), "string2") != keys.end());
	
	assert (pView->getString("string1") == "FOO");
	assert (pView->getString("string2") == "BAR");
	
	pView->setString("string3", "foobar");
	assert (pConf->getString("prop4.prop41.string3") == "foobar");
}
Exemple #4
0
/// Строит необходимые логгеры
void BaseDaemon::buildLoggers()
{
	/// Сменим директорию для лога
	if (config().hasProperty("logger.log") && !log_to_console)
	{
		std::string path = createDirectory(config().getString("logger.log"));
		if (config().getBool("application.runAsDaemon", false)
			&& chdir(path.c_str()) != 0)
			throw Poco::Exception("Cannot change directory to " + path);
	}
	else
	{
		if (config().getBool("application.runAsDaemon", false)
			&& chdir("/tmp") != 0)
			throw Poco::Exception("Cannot change directory to /tmp");
	}

	if (config().hasProperty("logger.errorlog") && !log_to_console)
		createDirectory(config().getString("logger.errorlog"));

	if (config().hasProperty("logger.log") && !log_to_console)
	{
		std::cerr << "Should logs to " << config().getString("logger.log") << std::endl;

		// splitter
		Poco::AutoPtr<SplitterChannel> split = new SplitterChannel;

		// set up two channel chains
		Poco::AutoPtr<OwnPatternFormatter> pf = new OwnPatternFormatter(this);
		pf->setProperty("times", "local");
		Poco::AutoPtr<FormattingChannel> log = new FormattingChannel(pf);
		log_file = new FileChannel;
		log_file->setProperty("path", Poco::Path(config().getString("logger.log")).absolute().toString());
		log_file->setProperty("rotation", config().getRawString("logger.size", "100M"));
		log_file->setProperty("archive", "number");
		log_file->setProperty("compress", config().getRawString("logger.compress", "true"));
		log_file->setProperty("purgeCount", config().getRawString("logger.count", "1"));
		log->setChannel(log_file);
		split->addChannel(log);
		log_file->open();

		if (config().hasProperty("logger.errorlog"))
		{
			std::cerr << "Should error logs to " << config().getString("logger.errorlog") << std::endl;
			Poco::AutoPtr<Poco::LevelFilterChannel> level = new Poco::LevelFilterChannel;
			level->setLevel(Message::PRIO_NOTICE);
			Poco::AutoPtr<OwnPatternFormatter> pf = new OwnPatternFormatter(this);
			pf->setProperty("times", "local");
			Poco::AutoPtr<FormattingChannel> errorlog = new FormattingChannel(pf);
			error_log_file = new FileChannel;
			error_log_file->setProperty("path", Poco::Path(config().getString("logger.errorlog")).absolute().toString());
			error_log_file->setProperty("rotation", config().getRawString("logger.size", "100M"));
			error_log_file->setProperty("archive", "number");
			error_log_file->setProperty("compress", config().getRawString("logger.compress", "true"));
			error_log_file->setProperty("purgeCount", config().getRawString("logger.count", "1"));
			errorlog->setChannel(error_log_file);
			level->setChannel(errorlog);
			split->addChannel(level);
			errorlog->open();
		}

		if (config().getBool("logger.use_syslog", false) || config().getBool("dynamic_layer_selection", false))
		{
			Poco::AutoPtr<OwnPatternFormatter> pf = new OwnPatternFormatter(this, OwnPatternFormatter::ADD_LAYER_TAG);
			pf->setProperty("times", "local");
			Poco::AutoPtr<FormattingChannel> log = new FormattingChannel(pf);
			syslog_channel = new Poco::SyslogChannel(commandName(), Poco::SyslogChannel::SYSLOG_CONS | Poco::SyslogChannel::SYSLOG_PID, Poco::SyslogChannel::SYSLOG_DAEMON);
			log->setChannel(syslog_channel);
			split->addChannel(log);
			syslog_channel->open();
		}

		split->open();
		logger().close();
		logger().setChannel(split);
	}
	else
	{
		// Выводим на консоль
		Poco::AutoPtr<ConsoleChannel> file = new ConsoleChannel;
		Poco::AutoPtr<OwnPatternFormatter> pf = new OwnPatternFormatter(this);
		pf->setProperty("times", "local");
		Poco::AutoPtr<FormattingChannel> log = new FormattingChannel(pf);
		log->setChannel(file);

		logger().close();
		logger().setChannel(log);
		logger().warning("Logging to console");
	}

	// Уровни для всех
	logger().setLevel(config().getString("logger.level", "trace"));

	// Прикрутим к корневому логгеру
	Logger::root().setLevel(logger().getLevel());
	Logger::root().setChannel(logger().getChannel());

	// Уровни для явно указанных логгеров
	AbstractConfiguration::Keys levels;
	config().keys("logger.levels", levels);

	if(!levels.empty())
		for(AbstractConfiguration::Keys::iterator it = levels.begin(); it != levels.end(); ++it)
			Logger::get(*it).setLevel(config().getString("logger.levels." + *it, "trace"));
}