base: add 'trace' function to base/log.h

The 'Genode::trace' convenience function prints messages to Genode's
trace buffer (if tracing is enabled).

issue #3294
This commit is contained in:
Sebastian Sumpf 2019-06-12 17:47:11 +02:00 committed by Christian Helmuth
parent 18b3253cac
commit 24eea0b653
4 changed files with 86 additions and 1 deletions

View File

@ -16,11 +16,13 @@
#include <base/output.h> #include <base/output.h>
#include <base/lock.h> #include <base/lock.h>
#include <trace/timestamp.h>
namespace Genode { namespace Genode {
class Log; class Log;
class Raw; class Raw;
class Trace_output;
} }
@ -101,6 +103,36 @@ class Genode::Raw
}; };
class Genode::Trace_output
{
private:
Lock _lock { };
Output &_output;
void _acquire();
void _release();
public:
Trace_output(Output &output) : _output(output) { }
template <typename... ARGS>
void output(ARGS &&... args)
{
_acquire();
Output::out_args(_output, args...);
_release();
}
/**
* Return component-global singleton instance of the 'Trace_output'
*/
static Trace_output &trace_output();
};
namespace Genode { namespace Genode {
/** /**
@ -140,6 +172,16 @@ namespace Genode {
*/ */
template <typename... ARGS> template <typename... ARGS>
void raw(ARGS &&... args) { Raw::output(args...); } void raw(ARGS &&... args) { Raw::output(args...); }
/**
* Write 'args' to the trace buffer if tracing is enabled
*
* The message is prefixed with a timestamp value
*/
template <typename... ARGS>
void trace(ARGS && ... args) {
Trace_output::trace_output().output(Trace::timestamp(), ": ", args...); }
} }
#endif /* _INCLUDE__BASE__LOG_H_ */ #endif /* _INCLUDE__BASE__LOG_H_ */

View File

@ -80,6 +80,9 @@ _ZN6Genode11Sliced_heapD1Ev T
_ZN6Genode11Sliced_heapD2Ev T _ZN6Genode11Sliced_heapD2Ev T
_ZN6Genode12Address_infoC1Em T _ZN6Genode12Address_infoC1Em T
_ZN6Genode12Address_infoC2Em T _ZN6Genode12Address_infoC2Em T
_ZN6Genode12Trace_output12trace_outputEv T
_ZN6Genode12Trace_output8_acquireEv T
_ZN6Genode12Trace_output8_releaseEv T
_ZN6Genode13Avl_node_base15_rotate_subtreeEPS0_bRNS0_6PolicyE T _ZN6Genode13Avl_node_base15_rotate_subtreeEPS0_bRNS0_6PolicyE T
_ZN6Genode13Avl_node_base18_rebalance_subtreeEPS0_RNS0_6PolicyE T _ZN6Genode13Avl_node_base18_rebalance_subtreeEPS0_RNS0_6PolicyE T
_ZN6Genode13Avl_node_base6_adoptEPS0_bRNS0_6PolicyE T _ZN6Genode13Avl_node_base6_adoptEPS0_bRNS0_6PolicyE T
@ -587,11 +590,11 @@ _ZThn8_N6Genode23Alarm_timeout_scheduler14handle_timeoutENS_8DurationE T
_ZThn8_N6Genode23Alarm_timeout_schedulerD0Ev T _ZThn8_N6Genode23Alarm_timeout_schedulerD0Ev T
_ZThn8_N6Genode23Alarm_timeout_schedulerD1Ev T _ZThn8_N6Genode23Alarm_timeout_schedulerD1Ev T
_ZdlPv W _ZdlPv W
_ZdlPvm W
_ZdlPvPN6Genode11DeallocatorE T _ZdlPvPN6Genode11DeallocatorE T
_ZdlPvPN6Genode9AllocatorE W _ZdlPvPN6Genode9AllocatorE W
_ZdlPvRN6Genode11DeallocatorE T _ZdlPvRN6Genode11DeallocatorE T
_ZdlPvRN6Genode9AllocatorE W _ZdlPvRN6Genode9AllocatorE W
_ZdlPvm W
_ZnajPN6Genode9AllocatorE T _ZnajPN6Genode9AllocatorE T
_ZnajRN6Genode9AllocatorE T _ZnajRN6Genode9AllocatorE T
_ZnamPN6Genode9AllocatorE T _ZnamPN6Genode9AllocatorE T

View File

@ -62,6 +62,18 @@ Log &Log::log()
} }
static Trace_output *trace_ptr;
Trace_output &Trace_output::trace_output()
{
if (trace_ptr)
return *trace_ptr;
raw("Error: Missing call of init_log");
sleep_forever();
}
/** /**
* Hook for support the 'fork' implementation of the noux libc backend * Hook for support the 'fork' implementation of the noux libc backend
*/ */
@ -95,5 +107,16 @@ void Genode::init_log(Parent &parent)
unmanaged_singleton<Buffered_log_output>(Write_fn()); unmanaged_singleton<Buffered_log_output>(Write_fn());
log_ptr = unmanaged_singleton<Log>(*buffered_log_output); log_ptr = unmanaged_singleton<Log>(*buffered_log_output);
/* enable trace back end */
struct Write_trace_fn { void operator () (char const *s) { Thread::trace(s); } };
typedef Buffered_output<Log_session::MAX_STRING_LEN, Write_trace_fn>
Buffered_trace_output;
static Buffered_trace_output *buffered_trace_output =
unmanaged_singleton<Buffered_trace_output>(Write_trace_fn());
trace_ptr = unmanaged_singleton<Trace_output>(*buffered_trace_output);
} }

View File

@ -59,3 +59,20 @@ void Raw::_release()
*/ */
_output().out_string("\033[0m\n"); _output().out_string("\033[0m\n");
} }
void Trace_output::_acquire()
{
_lock.lock();
}
void Trace_output::_release()
{
/*
* Add newline
*/
_output.out_string("\n");
_lock.unlock();
}