aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAvatar Ciaran McCreesh <ciaran.mccreesh@googlemail.com> 2009-02-14 14:42:00 +0000
committerAvatar Ciaran McCreesh <ciaran.mccreesh@googlemail.com> 2009-02-14 14:42:00 +0000
commite0f6f1ab615872a8fc75b6cc81bbf5712848cd86 (patch)
tree9666629f3ae431b78ffd522005d74f2ffd823725
parent0d1264a7740c448c6e50ce074e03ed294ae8be2b (diff)
downloadpaludis-e0f6f1ab615872a8fc75b6cc81bbf5712848cd86.tar.gz
paludis-e0f6f1ab615872a8fc75b6cc81bbf5712848cd86.tar.xz
Message output formatting
-rw-r--r--paludis/environments/paludis/output_managers.cc78
-rw-r--r--paludis/environments/paludis/output_managers.hh9
-rw-r--r--paludis/environments/paludis/output_managers/keep_messages.conf5
-rw-r--r--paludis/file_output_manager.cc9
-rw-r--r--paludis/messages_to_stdout_output_handler.cc88
-rw-r--r--paludis/messages_to_stdout_output_handler.hh10
-rw-r--r--paludis/output_manager_factory.hh5
7 files changed, 147 insertions, 57 deletions
diff --git a/paludis/environments/paludis/output_managers.cc b/paludis/environments/paludis/output_managers.cc
index 56563d5..b448da9 100644
--- a/paludis/environments/paludis/output_managers.cc
+++ b/paludis/environments/paludis/output_managers.cc
@@ -95,10 +95,11 @@ OutputManagers::create_named_output_manager(const std::string & s, const CreateO
if (i == _imp->store.end())
throw PaludisConfigError("No output manager named '" + s + "' exists");
+ std::tr1::shared_ptr<Map<std::string, std::string> > vars(vars_from_create_output_manager_info(n));
return OutputManagerFactory::get_instance()->create(
std::tr1::bind(&from_kv, i->second, std::tr1::placeholders::_1),
std::tr1::bind(&OutputManagers::create_named_output_manager, this, std::tr1::placeholders::_1, std::tr1::cref(n)),
- std::tr1::bind(&OutputManagers::replace_vars, this, std::tr1::placeholders::_1, std::tr1::cref(n))
+ std::tr1::bind(replace_percent_vars, std::tr1::placeholders::_1, vars, std::tr1::placeholders::_2)
);
}
@@ -114,59 +115,58 @@ namespace
struct CreateVarsFromInfo
{
- std::tr1::unordered_map<std::string, std::string> & m;
+ std::tr1::shared_ptr<Map<std::string, std::string> > m;
- CreateVarsFromInfo(std::tr1::unordered_map<std::string, std::string> & mm) :
+ CreateVarsFromInfo(std::tr1::shared_ptr<Map<std::string, std::string> > & mm) :
m(mm)
{
}
void visit(const CreateOutputManagerForRepositorySyncInfo & i)
{
- m["type"] = "repository";
- m["action"] = "sync";
- m["name"] = stringify(i.repository().name());
- m["pid"] = stringify(getpid());
- m["time"] = stringify(time(0));
+ m->insert("type", "repository");
+ m->insert("action", "sync");
+ m->insert("name", stringify(i.repository().name()));
+ m->insert("pid", stringify(getpid()));
+ m->insert("time", stringify(time(0)));
}
void visit(const CreateOutputManagerForPackageIDActionInfo & i)
{
- m["type"] = "package";
- m["action"] = action_to_string(i.action());
- m["name"] = stringify(i.package_id()->name());
- m["id"] = escape(stringify(*i.package_id()));
+ m->insert("type", "package");
+ m->insert("action", action_to_string(i.action()));
+ m->insert("name", stringify(i.package_id()->name()));
+ m->insert("id", escape(stringify(*i.package_id())));
if (i.package_id()->slot_key())
- m["slot"] = stringify(i.package_id()->slot_key()->value());
- m["version"] = stringify(i.package_id()->version());
- m["repository"] = stringify(i.package_id()->repository()->name());
- m["category"] = stringify(i.package_id()->name().category());
- m["package"] = stringify(i.package_id()->name().package());
- m["pid"] = stringify(getpid());
- m["time"] = stringify(time(0));
+ m->insert("slot", stringify(i.package_id()->slot_key()->value()));
+ m->insert("version", stringify(i.package_id()->version()));
+ m->insert("repository", stringify(i.package_id()->repository()->name()));
+ m->insert("category", stringify(i.package_id()->name().category()));
+ m->insert("package", stringify(i.package_id()->name().package()));
+ m->insert("pid", stringify(getpid()));
+ m->insert("time", stringify(time(0)));
}
};
-
- void create_vars_from_info(const CreateOutputManagerInfo & i,
- std::tr1::unordered_map<std::string, std::string> & m)
- {
- CreateVarsFromInfo v(m);
- i.accept(v);
- }
}
-std::string
-OutputManagers::replace_vars(
- const std::string & s,
+const std::tr1::shared_ptr<Map<std::string, std::string> >
+OutputManagers::vars_from_create_output_manager_info(
const CreateOutputManagerInfo & i) const
{
- Context context("When expanding variables in '" + s + "':");
+ std::tr1::shared_ptr<Map<std::string, std::string> > result(new Map<std::string, std::string>);
+ CreateVarsFromInfo v(result);
+ i.accept(v);
+ return result;
+}
- SimpleParser parser(s);
+const std::string
+paludis::paludis_environment::replace_percent_vars(
+ const std::string & s,
+ const std::tr1::shared_ptr<const Map<std::string, std::string> > & vars,
+ const std::tr1::shared_ptr<const Map<std::string, std::string> > & override_vars)
+{
std::string result, token;
- std::tr1::unordered_map<std::string, std::string> m;
- create_vars_from_info(i, m);
-
+ SimpleParser parser(s);
while (! parser.eof())
{
if (parser.consume((+simple_parser::any_except("%")) >> token))
@@ -177,11 +177,13 @@ OutputManagers::replace_vars(
((+simple_parser::any_except("} \t\r\n%")) >> token) &
simple_parser::exact("}")))
{
- std::tr1::unordered_map<std::string, std::string>::const_iterator j(m.find(token));
- if (j == m.end())
+ Map<std::string, std::string>::ConstIterator v(override_vars->find(token));
+ if (v == override_vars->end())
+ v = vars->find(token);
+ if (v == vars->end())
throw PaludisConfigError("No variable named '" + token + "' in var string '" + s + "'");
- else
- result.append(j->second);
+
+ result.append(v->second);
}
else
throw PaludisConfigError("Invalid var string '" + s + "'");
diff --git a/paludis/environments/paludis/output_managers.hh b/paludis/environments/paludis/output_managers.hh
index 1061665..80ad723 100644
--- a/paludis/environments/paludis/output_managers.hh
+++ b/paludis/environments/paludis/output_managers.hh
@@ -24,6 +24,7 @@
#include <paludis/util/private_implementation_pattern.hh>
#include <paludis/util/instantiation_policy.hh>
#include <paludis/util/fs_entry-fwd.hh>
+#include <paludis/util/map-fwd.hh>
#include <paludis/output_manager-fwd.hh>
#include <paludis/create_output_manager_info-fwd.hh>
#include <tr1/memory>
@@ -34,6 +35,11 @@ namespace paludis
namespace paludis_environment
{
+ const std::string replace_percent_vars(
+ const std::string &,
+ const std::tr1::shared_ptr<const Map<std::string, std::string> > & vars,
+ const std::tr1::shared_ptr<const Map<std::string, std::string> > & override_vars);
+
class OutputManagers :
private PrivateImplementationPattern<OutputManagers>,
private InstantiationPolicy<OutputManagers, instantiation_method::NonCopyableTag>
@@ -56,8 +62,7 @@ namespace paludis
const std::string &,
const CreateOutputManagerInfo &) const;
- std::string replace_vars(
- const std::string &,
+ const std::tr1::shared_ptr<Map<std::string, std::string> > vars_from_create_output_manager_info(
const CreateOutputManagerInfo &) const;
};
}
diff --git a/paludis/environments/paludis/output_managers/keep_messages.conf b/paludis/environments/paludis/output_managers/keep_messages.conf
index 9271243..38f7505 100644
--- a/paludis/environments/paludis/output_managers/keep_messages.conf
+++ b/paludis/environments/paludis/output_managers/keep_messages.conf
@@ -2,4 +2,9 @@
handler = messages_to_stdout
child = keep_stdout_as_messages
+format_debug =
+format_info = --- %{message}
+format_warn = *** %{message}
+format_error = !!! %{message}
+format_log = +++ %{message}
diff --git a/paludis/file_output_manager.cc b/paludis/file_output_manager.cc
index 8e9feca..ff797a5 100644
--- a/paludis/file_output_manager.cc
+++ b/paludis/file_output_manager.cc
@@ -21,6 +21,7 @@
#include <paludis/util/private_implementation_pattern-impl.hh>
#include <paludis/util/safe_ofstream.hh>
#include <paludis/util/set.hh>
+#include <paludis/util/map.hh>
#include <paludis/util/wrapped_forward_iterator.hh>
#include <paludis/util/make_shared_ptr.hh>
#include <paludis/util/destringify.hh>
@@ -176,11 +177,11 @@ FileOutputManager::factory_create(
if (stdout_s.empty())
throw ConfigurationError("Key 'stdout' not specified when creating a file output manager");
- stdout_s = replace_vars_func(stdout_s);
+ stdout_s = replace_vars_func(stdout_s, make_shared_ptr(new Map<std::string, std::string>));
if (stderr_s.empty())
throw ConfigurationError("Key 'stderr' not specified when creating a file output manager");
- stderr_s = replace_vars_func(stderr_s);
+ stderr_s = replace_vars_func(stderr_s, make_shared_ptr(new Map<std::string, std::string>));
if (keep_on_success_s.empty())
keep_on_success_s = "true";
@@ -192,8 +193,8 @@ FileOutputManager::factory_create(
if (! summary_output_manager_s.empty())
summary_output_manager = create_child_function(summary_output_manager_s);
- summary_output_stdout_message_s = replace_vars_func(summary_output_stdout_message_s);
- summary_output_stderr_message_s = replace_vars_func(summary_output_stderr_message_s);
+ summary_output_stdout_message_s = replace_vars_func(summary_output_stdout_message_s, make_shared_ptr(new Map<std::string, std::string>));
+ summary_output_stderr_message_s = replace_vars_func(summary_output_stderr_message_s, make_shared_ptr(new Map<std::string, std::string>));
return make_shared_ptr(new FileOutputManager(FSEntry(stdout_s), FSEntry(stderr_s),
destringify<bool>(keep_on_success_s), destringify<bool>(keep_on_empty_s),
diff --git a/paludis/messages_to_stdout_output_handler.cc b/paludis/messages_to_stdout_output_handler.cc
index 0a83359..0492fe4 100644
--- a/paludis/messages_to_stdout_output_handler.cc
+++ b/paludis/messages_to_stdout_output_handler.cc
@@ -25,7 +25,8 @@
#include <paludis/util/wrapped_forward_iterator.hh>
#include <paludis/util/make_shared_ptr.hh>
#include <paludis/util/exception.hh>
-#include <sstream>
+#include <paludis/util/map.hh>
+#include <paludis/util/stringify.hh>
using namespace paludis;
@@ -36,17 +37,43 @@ namespace paludis
{
DiscardOutputStream output_stream;
const std::tr1::shared_ptr<OutputManager> child;
-
- Implementation(const std::tr1::shared_ptr<OutputManager> & c) :
- child(c)
+ const OutputManagerFactory::ReplaceVarsFunc replace_vars_func;
+ const std::string f_debug;
+ const std::string f_info;
+ const std::string f_warn;
+ const std::string f_error;
+ const std::string f_log;
+
+ Implementation(
+ const std::tr1::shared_ptr<OutputManager> & c,
+ const OutputManagerFactory::ReplaceVarsFunc & r,
+ const std::string & f_d,
+ const std::string & f_i,
+ const std::string & f_w,
+ const std::string & f_e,
+ const std::string & f_l) :
+ child(c),
+ replace_vars_func(r),
+ f_debug(f_d),
+ f_info(f_i),
+ f_warn(f_w),
+ f_error(f_e),
+ f_log(f_l)
{
}
};
}
MessagesToStdoutOutputManager::MessagesToStdoutOutputManager(
- const std::tr1::shared_ptr<OutputManager> & s) :
- PrivateImplementationPattern<MessagesToStdoutOutputManager>(new Implementation<MessagesToStdoutOutputManager>(s))
+ const std::tr1::shared_ptr<OutputManager> & s,
+ const OutputManagerFactory::ReplaceVarsFunc & replace_vars_func,
+ const std::string & f_debug,
+ const std::string & f_info,
+ const std::string & f_warn,
+ const std::string & f_error,
+ const std::string & f_log) :
+ PrivateImplementationPattern<MessagesToStdoutOutputManager>(new Implementation<MessagesToStdoutOutputManager>(s, replace_vars_func,
+ f_debug, f_info, f_warn, f_error, f_log))
{
}
@@ -75,9 +102,44 @@ MessagesToStdoutOutputManager::succeeded()
void
MessagesToStdoutOutputManager::message(const MessageType t, const std::string & s)
{
- std::ostringstream m;
- m << t << " " << s << std::endl;
- _imp->child->stdout_stream() << m.str();
+ std::string msg;
+ std::tr1::shared_ptr<Map<std::string, std::string> > x(new Map<std::string, std::string>);
+ x->insert("message", s);
+
+ do
+ {
+ switch (t)
+ {
+ case mt_debug:
+ msg = _imp->replace_vars_func(_imp->f_debug, x);
+ continue;
+
+ case mt_info:
+ msg = _imp->replace_vars_func(_imp->f_info, x);
+ continue;
+
+ case mt_warn:
+ msg = _imp->replace_vars_func(_imp->f_warn, x);
+ continue;
+
+ case mt_error:
+ msg = _imp->replace_vars_func(_imp->f_error, x);
+ continue;
+
+ case mt_log:
+ msg = _imp->replace_vars_func(_imp->f_log, x);
+ continue;
+
+ case last_mt:
+ break;
+ }
+
+ throw InternalError(PALUDIS_HERE, "bad MessageType");
+ }
+ while (false);
+
+ if (! msg.empty())
+ _imp->child->stdout_stream() << msg << std::endl;
}
const std::tr1::shared_ptr<const Set<std::string> >
@@ -92,13 +154,17 @@ const std::tr1::shared_ptr<OutputManager>
MessagesToStdoutOutputManager::factory_create(
const OutputManagerFactory::KeyFunction & key_func,
const OutputManagerFactory::CreateChildFunction & create_child,
- const OutputManagerFactory::ReplaceVarsFunc &)
+ const OutputManagerFactory::ReplaceVarsFunc & replace_vars_func)
{
std::string child(key_func("child"));
if (child.empty())
throw ConfigurationError("No child specified for MessagesToStdoutOutputManager");
- return make_shared_ptr(new MessagesToStdoutOutputManager(create_child(child)));
+ std::string f_d(key_func("format_debug")), f_i(key_func("format_info")), f_w(key_func("format_warn")),
+ f_e(key_func("format_error")), f_l(key_func("format_log"));
+
+ return make_shared_ptr(new MessagesToStdoutOutputManager(create_child(child), replace_vars_func,
+ f_d, f_i, f_w, f_e, f_l));
}
template class PrivateImplementationPattern<MessagesToStdoutOutputManager>;
diff --git a/paludis/messages_to_stdout_output_handler.hh b/paludis/messages_to_stdout_output_handler.hh
index 3766f9e..5591931 100644
--- a/paludis/messages_to_stdout_output_handler.hh
+++ b/paludis/messages_to_stdout_output_handler.hh
@@ -36,7 +36,15 @@ namespace paludis
public OutputManager
{
public:
- MessagesToStdoutOutputManager(const std::tr1::shared_ptr<OutputManager> &);
+ MessagesToStdoutOutputManager(
+ const std::tr1::shared_ptr<OutputManager> &,
+ const OutputManagerFactory::ReplaceVarsFunc &,
+ const std::string & f_debug,
+ const std::string & f_info,
+ const std::string & f_warn,
+ const std::string & f_error,
+ const std::string & f_log);
+
~MessagesToStdoutOutputManager();
virtual std::ostream & stdout_stream() PALUDIS_ATTRIBUTE((warn_unused_result));
diff --git a/paludis/output_manager_factory.hh b/paludis/output_manager_factory.hh
index eb653d7..be627d9 100644
--- a/paludis/output_manager_factory.hh
+++ b/paludis/output_manager_factory.hh
@@ -25,6 +25,7 @@
#include <paludis/util/private_implementation_pattern.hh>
#include <paludis/util/instantiation_policy.hh>
#include <paludis/util/wrapped_forward_iterator-fwd.hh>
+#include <paludis/util/map-fwd.hh>
#include <paludis/output_manager-fwd.hh>
#include <paludis/util/set-fwd.hh>
#include <tr1/functional>
@@ -48,7 +49,9 @@ namespace paludis
typedef std::tr1::function<const std::tr1::shared_ptr<OutputManager> (
const std::string &)> CreateChildFunction;
- typedef std::tr1::function<std::string (const std::string &)> ReplaceVarsFunc;
+ typedef std::tr1::function<std::string (
+ const std::string & pattern,
+ const std::tr1::shared_ptr<const Map<std::string, std::string> > & extra_vars)> ReplaceVarsFunc;
typedef std::tr1::function<const std::tr1::shared_ptr<OutputManager>(
const KeyFunction &,