genode/repos/base/src/lib/base/default_log.cc
Norman Feske aa66b5d62f base: remove dependency from deprecated APIs
This patch adjusts the implementation of the base library and core such
that the code no longer relies on deprecated APIs except for very few
cases, mainly to keep those deprecated APIs in tact for now.

The most prominent changes are:

- Removing the use of base/printf.h

- Removing of the log backend for printf. The 'Console' with the
  format-string parser is still there along with 'snprintf.h' because
  the latter is still used at a few places, most prominently the
  'Connection' classes.

- Removing the notion of a RAM session, which does not exist in
  Genode anymore. Still the types were preserved (by typedefs to
  PD session) to keep up compatibility. But this transition should
  come to an end now.

- Slight rennovation of core's tracing service, e.g., the use of an
  Attached_dataspace as the Argument_buffer.

- Reducing the reliance on global accessors like deprecated_env() or
  core_env(). Still there is a longish way to go to eliminate all such
  calls. A useful pattern (or at least a stop-gap solution) is to
  pass the 'Env' to the individual compilation units via init functions.

- Avoiding the use of the old 'Child_policy::resolve_session_request'
  interface that returned a 'Service' instead of a 'Route'.

Issue #1987
2019-02-19 11:08:17 +01:00

100 lines
2.2 KiB
C++

/*
* \brief Access to the component's LOG session
* \author Norman Feske
* \date 2016-05-03
*/
/*
* Copyright (C) 2016-2017 Genode Labs GmbH
*
* This file is part of the Genode OS framework, which is distributed
* under the terms of the GNU Affero General Public License version 3.
*/
/* Genode includes */
#include <util/construct_at.h>
#include <base/log.h>
#include <base/buffered_output.h>
#include <base/sleep.h>
#include <log_session/client.h>
/* base-internal includes */
#include <base/internal/globals.h>
#include <base/internal/unmanaged_singleton.h>
using namespace Genode;
namespace {
/**
* Singleton back end for writing messages to the component's log session
*/
struct Back_end : Noncopyable
{
Log_session_client _client;
static Session_capability _cap(Parent &parent) {
return parent.session_cap(Parent::Env::log()); }
Back_end(Parent &parent)
: _client(reinterpret_cap_cast<Log_session>(_cap(parent))) { }
void write(char const *string) { (void)_client.write(string); }
};
}
static Back_end *back_end_ptr;
/**
* Singleton instance of the 'Log' interface
*/
static Log *log_ptr;
Log &Log::log()
{
if (log_ptr)
return *log_ptr;
raw("Error: Missing call of init_log");
sleep_forever();
}
/**
* Hook for support the 'fork' implementation of the noux libc backend
*/
extern "C" void stdout_reconnect(Parent &parent)
{
/*
* We cannot use a 'Reconstructible' because we have to skip
* the object destruction inside a freshly forked process.
* Otherwise, the attempt to destruct the capability contained
* in the 'Log' object would result in an inconsistent ref counter
* of the respective capability-space element.
*/
construct_at<Back_end>(back_end_ptr, parent);
}
void Genode::init_log(Parent &parent)
{
/* ignore subsequent calls */
if (log_ptr)
return;
back_end_ptr = unmanaged_singleton<Back_end>(parent);
struct Write_fn { void operator () (char const *s) { back_end_ptr->write(s); } };
typedef Buffered_output<Log_session::MAX_STRING_LEN, Write_fn>
Buffered_log_output;
static Buffered_log_output *buffered_log_output =
unmanaged_singleton<Buffered_log_output>(Write_fn());
log_ptr = unmanaged_singleton<Log>(*buffered_log_output);
}