Add a service to log into a file

Original version by Alexander Tarasikov, refined by Norman Feske.

Fixes #736
This commit is contained in:
Alexander Tarasikov 2013-05-07 20:10:28 +04:00 committed by Norman Feske
parent 33bb8662a0
commit d8f0392c9f
4 changed files with 194 additions and 0 deletions

View File

@ -314,6 +314,9 @@ Separate components:
:'os/src/server/terminal_log':
Adapter for forwarding LOG messages to a terminal session.
:'libports/src/server/fs_log':
Adapter that writes LOG messages to files on a file system.
:'demo/src/server/nitlog':
Provides a LOG session, printing log output on screen via a nitpicker
session.

View File

@ -0,0 +1,18 @@
LOG server that writes log messages onto a file system.
Using this component, log messages of different processes can be redirected
to files on a file-system service. The assignment of processes to files can
be expressed in the configuration as follows:
! <start name="fs_log">
! <resource name="RAM" quantum="2M"/>
! <provides><service name="LOG"/></provides>
! <config>
! <policy label="noux" file="/noux.log" />
! <policy label="noux ->" file="/noux_process.log" />
! </config>
! </start>
In this example, all messages originating from the noux process are directed
to the file '/noux.log'. All messages originating from children of the noux
process end up in the file '/noux_process.log'.

View File

@ -0,0 +1,170 @@
/*
* \brief LOG service that writes to a file
* \author Alexander Tarasikov <tarasikov@ksyslabs.org>
* \author Norman Feske <norman.feske@genode-labs.com>
* \date 2013-05-07
*/
/*
* Copyright (C) 2013 Ksys Labs LLC
* Copyright (C) 2012-2013 Genode Labs GmbH
*
* This file is part of the Genode OS framework, which is distributed
* under the terms of the GNU General Public License version 2.
*/
/* Genode includes */
#include <base/env.h>
#include <base/rpc_server.h>
#include <base/sleep.h>
#include <root/component.h>
#include <util/string.h>
#include <os/session_policy.h>
#include <cap_session/connection.h>
#include <log_session/log_session.h>
/* libc includes */
#include <fcntl.h>
#include <unistd.h>
class Log_component : public Genode::Rpc_object<Genode::Log_session>
{
public:
enum { LABEL_LEN = 64 };
private:
typedef Genode::size_t size_t;
char _label[LABEL_LEN];
size_t _label_len;
int _fd;
public:
/**
* Constructor
*/
Log_component(const char *label, char const *filename)
:
_fd(::open(filename, O_CREAT | O_WRONLY | O_APPEND))
{
using namespace Genode;
if (_fd < 0) {
PERR("unable to open \"%s\"", filename);
throw Root::Unavailable();
}
snprintf(_label, LABEL_LEN, "[%s] ", label);
_label_len = strlen(_label);
PINF("log client \"%s\" to file \"%s\")", label, filename);
}
/**
* Destructor
*/
~Log_component() { ::close(_fd); }
/*****************
** Log session **
*****************/
/**
* Write a log-message to the file.
*/
size_t write(String const &string_buf)
{
if (!(string_buf.is_valid_string())) {
PERR("corrupted string");
return 0;
}
char const *string = string_buf.string();
Genode::size_t const len = Genode::strlen(string);
::write(_fd, _label, _label_len);
::write(_fd, string, len);
return len;
}
};
class Log_root : public Genode::Root_component<Log_component>
{
private:
int _fd;
protected:
/**
* Root component interface
*/
Log_component *_create_session(const char *args)
{
using namespace Genode;
char label_buf[Log_component::LABEL_LEN];
Arg label_arg = Arg_string::find_arg(args, "label");
label_arg.string(label_buf, sizeof(label_buf), "");
/* obtain file name from configured policy */
enum { FILENAME_MAX_LEN = 256 };
char filename[FILENAME_MAX_LEN];
try {
Session_policy policy(args);
policy.attribute("file").value(filename, sizeof(filename));
} catch (...) {
PERR("Invalid session request, no matching policy");
throw Root::Unavailable();
}
return new (md_alloc()) Log_component(label_buf, filename);
}
public:
/**
* Constructor
*
* \param session_ep entry point for managing cpu session objects
* \param md_alloc meta-data allocator to be used by root component
*/
Log_root(Genode::Rpc_entrypoint *session_ep, Genode::Allocator *md_alloc)
: Genode::Root_component<Log_component>(session_ep, md_alloc) { }
};
int main(int argc, char **argv)
{
using namespace Genode;
/*
* Initialize server entry point
*
* Use a large stack because we are calling libc functions from the
* context of the entrypoint.
*/
enum { STACK_SIZE = sizeof(addr_t)*16*1024 };
static Cap_connection cap;
static Rpc_entrypoint ep(&cap, STACK_SIZE, "fs_log_ep");
static Log_root log_root(&ep, env()->heap());
/*
* Announce services
*/
env()->parent()->announce(ep.manage(&log_root));
/**
* Got to sleep forever
*/
sleep_forever();
return 0;
}

View File

@ -0,0 +1,3 @@
TARGET = fs_log
SRC_CC = main.cc
LIBS = base libc_fs libc