core: simplify unprefixed LOG service

The special "platform" label is now "unlabeled".
This commit is contained in:
Ehmry - 2020-01-29 12:17:40 +01:00
parent 5d4e72c874
commit 200d82b4f5
2 changed files with 29 additions and 53 deletions

View File

@ -30,11 +30,7 @@ namespace Genode {
*/
Log_session_component *_create_session(const char *args) override
{
Session_label const label = label_from_args(args);
if (label == "init -> platform")
return new (md_alloc()) Platform_log_session_component();
return new (md_alloc()) Labeled_log_session_component(label);
return new (md_alloc()) Log_session_component(label_from_args(args));
}
public:

View File

@ -24,10 +24,32 @@ namespace Genode {
class Log_session_component : public Rpc_object<Log_session>
{
protected:
private:
template <typename PROC>
size_t split_lines(String const &string_buf, PROC proc)
Session_label const _label;
static Session_label _expand_label(Session_label const &label)
{
if (label == "init -> unlabeled")
return "";
else
return Session_label("[", label, "] ");
}
public:
/**
* Constructor
*/
Log_session_component(Session_label const &label)
: _label(_expand_label(label)) { }
/*****************
** Log session **
*****************/
size_t write(String const &string_buf) override
{
if (!(string_buf.valid_string())) {
error("corrupted string");
@ -38,63 +60,21 @@ namespace Genode {
size_t len = strlen(string);
unsigned from_i = 0;
for (unsigned i = 0; i < len; i++) {
if (string[i] == '\n') {
proc(Cstring(string + from_i, i - from_i));
log(_label, Cstring(string + from_i, i - from_i));
from_i = i + 1;
}
}
/* if last character of string was not a line break, add one */
if (from_i < len)
proc(Cstring(string + from_i));
log(_label, Cstring(string + from_i));
return len;
}
};
class Labeled_log_session_component final : public Log_session_component
{
private:
Session_label const _label;
public:
/**
* Constructor
*/
Labeled_log_session_component(Session_label const &label) : _label(label) { }
/*****************
** Log session **
*****************/
size_t write(String const &string_buf) override
{
return split_lines(string_buf, [&] (Cstring const &line) {
log("[", _label, "] ", line); });
}
};
class Platform_log_session_component final : public Log_session_component
{
public:
/*****************
** Log session **
*****************/
size_t write(String const &string_buf) override
{
return split_lines(string_buf, [&] (Cstring const &line) {
log(line);
});
}
};
}
#endif /* _CORE__INCLUDE__LOG_SESSION_COMPONENT_H_ */