2011-12-22 16:19:25 +01:00
|
|
|
/*
|
|
|
|
* \brief Log output service for Core
|
|
|
|
* \author Norman Feske
|
|
|
|
* \date 2006-09-15
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
2017-02-20 13:23:52 +01:00
|
|
|
* Copyright (C) 2006-2017 Genode Labs GmbH
|
2011-12-22 16:19:25 +01: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.
|
2011-12-22 16:19:25 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef _CORE__INCLUDE__LOG_SESSION_COMPONENT_H_
|
|
|
|
#define _CORE__INCLUDE__LOG_SESSION_COMPONENT_H_
|
|
|
|
|
|
|
|
#include <util/string.h>
|
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
|
|
|
#include <base/log.h>
|
2011-12-22 16:19:25 +01:00
|
|
|
#include <base/rpc_server.h>
|
2020-01-02 21:14:52 +01:00
|
|
|
#include <base/session_label.h>
|
2011-12-22 16:19:25 +01:00
|
|
|
#include <log_session/log_session.h>
|
|
|
|
|
|
|
|
namespace Genode {
|
|
|
|
|
|
|
|
class Log_session_component : public Rpc_object<Log_session>
|
2020-01-21 10:07:07 +01:00
|
|
|
{
|
|
|
|
private:
|
|
|
|
|
|
|
|
Session_label const _label;
|
|
|
|
|
2020-01-29 12:17:40 +01:00
|
|
|
static Session_label _expand_label(Session_label const &label)
|
|
|
|
{
|
|
|
|
if (label == "init -> unlabeled")
|
|
|
|
return "";
|
|
|
|
else
|
|
|
|
return Session_label("[", label, "] ");
|
|
|
|
}
|
|
|
|
|
2020-01-21 10:07:07 +01:00
|
|
|
public:
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor
|
|
|
|
*/
|
2020-01-29 12:17:40 +01:00
|
|
|
Log_session_component(Session_label const &label)
|
|
|
|
: _label(_expand_label(label)) { }
|
2020-01-21 10:07:07 +01:00
|
|
|
|
|
|
|
|
|
|
|
/*****************
|
|
|
|
** Log session **
|
|
|
|
*****************/
|
|
|
|
|
|
|
|
size_t write(String const &string_buf) override
|
|
|
|
{
|
2020-01-29 12:17:40 +01:00
|
|
|
if (!(string_buf.valid_string())) {
|
|
|
|
error("corrupted string");
|
|
|
|
return 0;
|
|
|
|
}
|
2020-01-21 10:07:07 +01:00
|
|
|
|
2020-01-29 12:17:40 +01:00
|
|
|
char const *string = string_buf.string();
|
|
|
|
size_t len = strlen(string);
|
2020-01-21 10:07:07 +01:00
|
|
|
|
2020-01-29 12:17:40 +01:00
|
|
|
unsigned from_i = 0;
|
2020-01-21 10:07:07 +01:00
|
|
|
|
2020-01-29 12:17:40 +01:00
|
|
|
for (unsigned i = 0; i < len; i++) {
|
|
|
|
if (string[i] == '\n') {
|
|
|
|
log(_label, Cstring(string + from_i, i - from_i));
|
|
|
|
from_i = i + 1;
|
|
|
|
}
|
|
|
|
}
|
2020-01-21 10:07:07 +01:00
|
|
|
|
2020-01-29 12:17:40 +01:00
|
|
|
/* if last character of string was not a line break, add one */
|
|
|
|
if (from_i < len)
|
|
|
|
log(_label, Cstring(string + from_i));
|
|
|
|
|
|
|
|
return len;
|
2020-01-21 10:07:07 +01:00
|
|
|
}
|
|
|
|
};
|
2011-12-22 16:19:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* _CORE__INCLUDE__LOG_SESSION_COMPONENT_H_ */
|