2012-05-21 15:39:57 +02:00
|
|
|
/*
|
|
|
|
* \brief LOG service that prints to a terminal
|
|
|
|
* \author Stefan Kalkowski
|
|
|
|
* \date 2012-05-21
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
2017-02-20 13:23:52 +01:00
|
|
|
* Copyright (C) 2012-2017 Genode Labs GmbH
|
2012-05-21 15:39:57 +02:00
|
|
|
*
|
|
|
|
* This file is part of the Genode OS framework, which is distributed
|
2017-02-20 13:23:52 +01:00
|
|
|
* under the terms of the GNU Affero General Public License version 3.
|
2012-05-21 15:39:57 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <root/component.h>
|
2017-01-16 12:02:32 +01:00
|
|
|
#include <base/component.h>
|
|
|
|
#include <base/heap.h>
|
2012-05-21 15:39:57 +02:00
|
|
|
#include <util/string.h>
|
|
|
|
|
|
|
|
#include <terminal_session/connection.h>
|
|
|
|
#include <log_session/log_session.h>
|
|
|
|
|
|
|
|
|
|
|
|
namespace Genode {
|
|
|
|
|
|
|
|
class Termlog_component : public Rpc_object<Log_session>
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
|
|
|
enum { LABEL_LEN = 64 };
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
|
|
char _label[LABEL_LEN];
|
2017-01-16 12:02:32 +01:00
|
|
|
Terminal::Connection &_terminal;
|
2012-05-21 15:39:57 +02:00
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor
|
|
|
|
*/
|
2017-01-16 12:02:32 +01:00
|
|
|
Termlog_component(const char *label, Terminal::Connection &terminal)
|
2012-05-21 15:39:57 +02:00
|
|
|
: _terminal(terminal) { snprintf(_label, LABEL_LEN, "[%s] ", label); }
|
|
|
|
|
|
|
|
|
|
|
|
/*****************
|
|
|
|
** Log session **
|
|
|
|
*****************/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Write a log-message to the terminal.
|
|
|
|
*
|
|
|
|
* The following function's code is a modified variant of the one in:
|
|
|
|
* 'base/src/core/include/log_session_component.h'
|
|
|
|
*/
|
2020-05-05 13:51:05 +02:00
|
|
|
void write(String const &string_buf) override
|
2012-05-21 15:39:57 +02:00
|
|
|
{
|
2016-05-11 18:21:47 +02:00
|
|
|
if (!(string_buf.valid_string())) {
|
base: avoid use of deprecated base/printf.h
Besides adapting the components to the use of base/log.h, the patch
cleans up a few base headers, i.e., it removes unused includes from
root/component.h, specifically base/heap.h and
ram_session/ram_session.h. Hence, components that relied on the implicit
inclusion of those headers have to manually include those headers now.
While adjusting the log messages, I repeatedly stumbled over the problem
that printing char * arguments is ambiguous. It is unclear whether to
print the argument as pointer or null-terminated string. To overcome
this problem, the patch introduces a new type 'Cstring' that allows the
caller to express that the argument should be handled as null-terminated
string. As a nice side effect, with this type in place, the optional len
argument of the 'String' class could be removed. Instead of supplying a
pair of (char const *, size_t), the constructor accepts a 'Cstring'.
This, in turn, clears the way let the 'String' constructor use the new
output mechanism to assemble a string from multiple arguments (and
thereby getting rid of snprintf within Genode in the near future).
To enforce the explicit resolution of the char * ambiguity, the 'char *'
overload of the 'print' function is marked as deleted.
Issue #1987
2016-07-13 19:07:09 +02:00
|
|
|
Genode::error("corrupted string");
|
2020-05-05 13:51:05 +02:00
|
|
|
return;
|
2012-05-21 15:39:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
char const *string = string_buf.string();
|
2021-12-02 11:23:38 +01:00
|
|
|
size_t len = strlen(string);
|
2012-05-21 15:39:57 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Heuristic: The Log console implementation flushes
|
|
|
|
* the output preferably in front of escape
|
|
|
|
* sequences. If the line contains only
|
|
|
|
* the escape sequence, we skip the printing
|
|
|
|
* of the label and cut the line break (last
|
|
|
|
* character).
|
|
|
|
*/
|
|
|
|
enum { ESC = 27 };
|
|
|
|
if ((string[0] == ESC) && (len == 5) && (string[4] == '\n')) {
|
2017-01-16 12:02:32 +01:00
|
|
|
_terminal.write(string, len - 1);
|
2020-05-05 13:51:05 +02:00
|
|
|
return;
|
2012-05-21 15:39:57 +02:00
|
|
|
}
|
|
|
|
|
2017-01-16 12:02:32 +01:00
|
|
|
_terminal.write(_label, strlen(_label));
|
|
|
|
_terminal.write(string, len);
|
2012-05-21 15:39:57 +02:00
|
|
|
|
|
|
|
/* if last character of string was not a line break, add one */
|
|
|
|
if ((len > 0) && (string[len - 1] != '\n'))
|
2017-01-16 12:02:32 +01:00
|
|
|
_terminal.write("\n", 1);
|
2012-05-21 15:39:57 +02:00
|
|
|
|
2017-07-07 12:02:53 +02:00
|
|
|
/* carriage-return as expected by hardware terminals on newline */
|
|
|
|
_terminal.write("\r", 1);
|
2012-05-21 15:39:57 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
class Termlog_root : public Root_component<Termlog_component>
|
|
|
|
{
|
|
|
|
private:
|
|
|
|
|
2017-01-16 12:02:32 +01:00
|
|
|
Terminal::Connection _terminal;
|
2012-05-21 15:39:57 +02:00
|
|
|
|
|
|
|
protected:
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Root component interface
|
|
|
|
*/
|
2019-02-14 22:39:08 +01:00
|
|
|
Termlog_component *_create_session(const char *args) override
|
2012-05-21 15:39:57 +02:00
|
|
|
{
|
|
|
|
char label_buf[Termlog_component::LABEL_LEN];
|
|
|
|
|
|
|
|
Arg label_arg = Arg_string::find_arg(args, "label");
|
|
|
|
label_arg.string(label_buf, sizeof(label_buf), "");
|
|
|
|
|
|
|
|
return new (md_alloc()) Termlog_component(label_buf, _terminal);
|
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor
|
|
|
|
*
|
|
|
|
* \param session_ep entry point for managing cpu session objects
|
|
|
|
* \param md_alloc meta-data allocator to be used by root component
|
|
|
|
*/
|
2017-01-16 12:02:32 +01:00
|
|
|
Termlog_root(Genode::Env &env, Allocator &md_alloc)
|
|
|
|
: Root_component<Termlog_component>(env.ep(), md_alloc),
|
|
|
|
_terminal(env, "log") { }
|
2012-05-21 15:39:57 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-01-16 12:02:32 +01:00
|
|
|
void Component::construct(Genode::Env &env)
|
2012-05-21 15:39:57 +02:00
|
|
|
{
|
|
|
|
using namespace Genode;
|
|
|
|
|
2017-01-16 12:02:32 +01:00
|
|
|
static Sliced_heap session_alloc(env.ram(), env.rm());
|
|
|
|
static Genode::Termlog_root termlog_root(env, session_alloc);
|
|
|
|
|
|
|
|
env.parent().announce(env.ep().manage(termlog_root));
|
2012-05-21 15:39:57 +02:00
|
|
|
}
|