changeset 120:4cb417fd4e18

Irccd: add brand new format section, #410
author David Demelier <markand@malikania.fr>
date Tue, 03 May 2016 13:51:57 +0200
parents b39573fc066e
children 12d8f2d20c77
files doc/html/guide/04-irccd/01-config.md doc/man/irccd.conf.5.in doc/procs/20.options.md lib/irccd/config.cpp lib/irccd/config.hpp
diffstat 5 files changed, 114 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/doc/html/guide/04-irccd/01-config.md	Mon May 02 13:28:28 2016 +0200
+++ b/doc/html/guide/04-irccd/01-config.md	Tue May 03 13:51:57 2016 +0200
@@ -73,6 +73,31 @@
 path-errors = "/var/log/irccd/errors.txt"
 ````
 
+### The format section
+
+The format section let you change the irccd's output. It uses the [common patterns](#common-patterns-and-formatting).
+
+Only one keyword is defined, `message` which contains the message that irccd wants to output.
+
+<div class="alert alert-info" role="alert">
+**Note:** the colors and attributes are not supported.
+</div>
+
+The available options:
+
+  - **debug**: (string) template to use to format debug messages (Optional, default: none),
+  - **info**: (string) template to use to format information messages (Optional, default: none),
+  - **warning**: (string) template to use to format warnings (Optional, default: none).
+
+**Example**
+
+````ini
+[format]
+debug = "%H:%M debug: #{message}"
+info = "%H:%M info: #{message}"
+warning = "%H:%M warning: #{message}"
+````
+
 ### The identity section
 
 This section is completely optional, if you don't provide one, irccd will use a default identity with **irccd** as
--- a/doc/man/irccd.conf.5.in	Mon May 02 13:28:28 2016 +0200
+++ b/doc/man/irccd.conf.5.in	Tue May 03 13:51:57 2016 +0200
@@ -80,6 +80,22 @@
 .It path-errors
 (string) path to the error messages.
 .El
+.\" FORMAT
+.Ss format
+The format section let you change the irccd's output. It uses the common patterns.
+.Pp
+Only one keyword is defined, message which contains the message that irccd wants to output.
+.Pp
+Note: the colors and attributes are not supported.
+.Pp
+.Bl -tag -width XXXXXXXXXXXXXXXXXXX -compact
+.It debug
+(string) template to use to format debug messages (Optional, default: none),
+.It info
+(string) template to use to format information messages (Optional, default: none),
+.It warning
+(string) template to use to format warnings (Optional, default: none).
+.El
 .\" IDENTITY
 .Ss identity
 This section is completely optional, if you don't provide one, irccd will use
--- a/doc/procs/20.options.md	Mon May 02 13:28:28 2016 +0200
+++ b/doc/procs/20.options.md	Tue May 03 13:51:57 2016 +0200
@@ -12,6 +12,12 @@
   - **foreground**: (bool) set to true to not daemonize (Optional, default: false),
   - **pidfile**: (string) path to a file where to store the irccd pid (Optional, default: none).
 
+## format
+
+  - **debug**: (string) template to use to format debug messages (Optional, default: none),
+  - **info**: (string) template to use to format information messages (Optional, default: none),
+  - **warning**: (string) template to use to format warnings (Optional, default: none).
+
 ## logs
 
   - **verbose**: (bool) be verbose (Optional, default: false),
--- a/lib/irccd/config.cpp	Mon May 02 13:28:28 2016 +0200
+++ b/lib/irccd/config.cpp	Tue May 03 13:51:57 2016 +0200
@@ -44,6 +44,47 @@
 
 namespace irccd {
 
+namespace {
+
+class IrccdLogFilter : public log::Filter {
+private:
+	std::string convert(const std::string &tmpl, std::string input) const
+	{
+		if (tmpl.empty()) {
+			return input;
+		}
+
+		util::Substitution params;
+
+		params.flags &= ~(util::Substitution::IrcAttrs);
+		params.keywords.emplace("message", std::move(input));
+
+		return util::format(tmpl, params);
+	}
+
+public:
+	std::string m_debug;
+	std::string m_info;
+	std::string m_warning;
+
+	std::string preDebug(std::string input) const override
+	{
+		return convert(m_debug, std::move(input));
+	}
+
+	std::string preInfo(std::string input) const override
+	{
+		return convert(m_info, std::move(input));
+	}
+
+	std::string preWarning(std::string input) const override
+	{
+		return convert(m_warning, std::move(input));
+	}
+};
+
+} // !namespace
+
 void Config::loadGeneral(const ini::Document &config) const
 {
 	ini::Document::const_iterator sc = config.find("general");
@@ -102,6 +143,30 @@
 	}
 }
 
+void Config::loadFormats(const ini::Document &config) const
+{
+	ini::Document::const_iterator sc = config.find("format");
+
+	if (sc == config.end()) {
+		return;
+	}
+
+	ini::Section::const_iterator it;
+	std::unique_ptr<IrccdLogFilter> filter = std::make_unique<IrccdLogFilter>();
+
+	if ((it = sc->find("debug")) != sc->cend()) {
+		filter->m_debug = it->value();
+	}
+	if ((it = sc->find("info")) != sc->cend()) {
+		filter->m_info = it->value();
+	}
+	if ((it = sc->find("warning")) != sc->cend()) {
+		filter->m_warning = it->value();
+	}
+
+	log::setFilter(std::move(filter));
+}
+
 void Config::loadLogFile(const ini::Section &sc) const
 {
 	/*
@@ -563,6 +628,7 @@
 
 		loadGeneral(config);
 		loadLogs(config);
+		loadFormats(config);
 		loadIdentities(irccd, config);
 		loadServers(irccd, config);
 		loadRules(irccd, config);
--- a/lib/irccd/config.hpp	Mon May 02 13:28:28 2016 +0200
+++ b/lib/irccd/config.hpp	Tue May 03 13:51:57 2016 +0200
@@ -46,6 +46,7 @@
 	parser::Result m_options;
 
 	void loadGeneral(const ini::Document &config) const;
+	void loadFormats(const ini::Document &config) const;
 	void loadLogFile(const ini::Section &sc) const;
 	void loadLogSyslog() const;
 	void loadLogs(const ini::Document &config) const;