From bf5803f7d897c4b11ffd062202390c86b99e056c Mon Sep 17 00:00:00 2001 From: Emery Hemingway Date: Wed, 29 Jan 2020 11:16:53 +0100 Subject: [PATCH] Simplify implementation of unlabeled logging --- repos/base/src/core/include/log_root.h | 6 +- .../src/core/include/log_session_component.h | 79 ++++++++----------- 2 files changed, 32 insertions(+), 53 deletions(-) diff --git a/repos/base/src/core/include/log_root.h b/repos/base/src/core/include/log_root.h index 7865c60f3..085c6053f 100644 --- a/repos/base/src/core/include/log_root.h +++ b/repos/base/src/core/include/log_root.h @@ -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: diff --git a/repos/base/src/core/include/log_session_component.h b/repos/base/src/core/include/log_session_component.h index 1381ee5bb..c34400951 100644 --- a/repos/base/src/core/include/log_session_component.h +++ b/repos/base/src/core/include/log_session_component.h @@ -24,10 +24,32 @@ namespace Genode { class Log_session_component : public Rpc_object { - protected: + private: - template - size_t split_lines(String const &string_buf, PROC proc) + Session_label const _label; + + 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"); @@ -37,64 +59,25 @@ namespace Genode { char const *string = string_buf.string(); size_t len = strlen(string); + char buf[string_buf.MAX_SIZE]; unsigned from_i = 0; + for (unsigned i = 0; i < len; i++) { if (string[i] == '\n') { - proc(Cstring(string + from_i, i - from_i)); + memcpy(buf, string + from_i, i - from_i); + buf[i - from_i] = 0; + log(_label, Cstring(buf)); 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_ */