Index: uspace/srv/logger/Makefile
===================================================================
--- uspace/srv/logger/Makefile	(revision e1eda1e95323c34f73a28bbd88bfeb5abb1e79cb)
+++ uspace/srv/logger/Makefile	(revision e1eda1e95323c34f73a28bbd88bfeb5abb1e79cb)
@@ -0,0 +1,37 @@
+#
+# Copyright (c) 2012 Vojtech Horky
+# All rights reserved.
+#
+# Redistribution and use in source and binary forms, with or without
+# modification, are permitted provided that the following conditions
+# are met:
+#
+# - Redistributions of source code must retain the above copyright
+#   notice, this list of conditions and the following disclaimer.
+# - Redistributions in binary form must reproduce the above copyright
+#   notice, this list of conditions and the following disclaimer in the
+#   documentation and/or other materials provided with the distribution.
+# - The name of the author may not be used to endorse or promote products
+#   derived from this software without specific prior written permission.
+#
+# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+# IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+#
+
+USPACE_PREFIX = ../..
+BINARY = logger
+STATIC_NEEDED = y
+
+SOURCES = \
+	main.c \
+	namespace.c
+
+include $(USPACE_PREFIX)/Makefile.common
Index: uspace/srv/logger/logger.h
===================================================================
--- uspace/srv/logger/logger.h	(revision e1eda1e95323c34f73a28bbd88bfeb5abb1e79cb)
+++ uspace/srv/logger/logger.h	(revision e1eda1e95323c34f73a28bbd88bfeb5abb1e79cb)
@@ -0,0 +1,73 @@
+/*
+ * Copyright (c) 2012 Vojtech Horky
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * - Redistributions of source code must retain the above copyright
+ *   notice, this list of conditions and the following disclaimer.
+ * - Redistributions in binary form must reproduce the above copyright
+ *   notice, this list of conditions and the following disclaimer in the
+ *   documentation and/or other materials provided with the distribution.
+ * - The name of the author may not be used to endorse or promote products
+ *   derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+/** @addtogroup logger
+ * @{
+ */
+/** @file Common logger service definitions.
+ */
+
+#ifndef LOGGER_H_
+#define LOGGER_H_
+
+#include <adt/list.h>
+#include <adt/prodcons.h>
+#include <io/log.h>
+#include <bool.h>
+#include <fibril_synch.h>
+
+#define DEFAULT_LOGGING_LEVEL LVL_WARN
+
+#define NAME "logger"
+#define MAX_NAMESPACE_LENGTH 256
+
+typedef struct {
+	link_t link;
+	log_level_t level;
+	const char *message;
+} log_message_t;
+
+typedef struct logging_namespace logging_namespace_t;
+
+log_message_t *message_create(const char *, log_level_t);
+void message_destroy(log_message_t *);
+
+logging_namespace_t *namespace_create(const char *);
+const char *namespace_get_name(logging_namespace_t *);
+void namespace_destroy(logging_namespace_t *);
+logging_namespace_t *namespace_reader_attach(const char *);
+void namespace_reader_detach(logging_namespace_t *);
+void namespace_writer_detach(logging_namespace_t *);
+
+void namespace_add_message(logging_namespace_t *, const char *, log_level_t);
+log_message_t *namespace_get_next_message(logging_namespace_t *);
+
+#endif
+
+/** @}
+ */
Index: uspace/srv/logger/main.c
===================================================================
--- uspace/srv/logger/main.c	(revision e1eda1e95323c34f73a28bbd88bfeb5abb1e79cb)
+++ uspace/srv/logger/main.c	(revision e1eda1e95323c34f73a28bbd88bfeb5abb1e79cb)
@@ -0,0 +1,257 @@
+/*
+ * Copyright (c) 2012 Vojtech Horky
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * - Redistributions of source code must retain the above copyright
+ *   notice, this list of conditions and the following disclaimer.
+ * - Redistributions in binary form must reproduce the above copyright
+ *   notice, this list of conditions and the following disclaimer in the
+ *   documentation and/or other materials provided with the distribution.
+ * - The name of the author may not be used to endorse or promote products
+ *   derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+/**
+ * @defgroup logger Logging service.
+ * @brief HelenOS logging service.
+ * @{
+ */
+
+/** @file
+ */
+
+#include <ipc/services.h>
+#include <ipc/logger.h>
+#include <io/log.h>
+#include <ns.h>
+#include <async.h>
+#include <stdio.h>
+#include <errno.h>
+#include <str_error.h>
+#include <malloc.h>
+#include "logger.h"
+
+static logging_namespace_t *register_namespace(void)
+{
+	ipc_call_t call;
+	ipc_callid_t callid = async_get_call(&call);
+
+	if (IPC_GET_IMETHOD(call) != LOGGER_REGISTER) {
+		async_answer_0(callid, EINVAL);
+		return NULL;
+	}
+
+	void *name;
+	int rc = async_data_write_accept(&name, true, 1, MAX_NAMESPACE_LENGTH, 0, NULL);
+	async_answer_0(callid, rc);
+
+	if (rc != EOK) {
+		return NULL;
+	}
+
+	logging_namespace_t *result = namespace_create((const char *) name);
+
+	free(name);
+
+	return result;
+}
+
+static int handle_receive_message(logging_namespace_t *namespace, int level)
+{
+	void *message;
+	int rc = async_data_write_accept(&message, true, 0, 0, 0, NULL);
+	if (rc != EOK) {
+		return rc;
+	}
+
+	namespace_add_message(namespace, message, level);
+
+	free(message);
+
+	return EOK;
+}
+
+static void connection_handler_sink(logging_namespace_t *namespace)
+{
+	printf(NAME "/sink: new client %s.\n", namespace_get_name(namespace));
+
+	while (true) {
+		ipc_call_t call;
+		ipc_callid_t callid = async_get_call(&call);
+
+		if (!IPC_GET_IMETHOD(call))
+			break;
+
+		int rc;
+
+		switch (IPC_GET_IMETHOD(call)) {
+		case LOGGER_MESSAGE:
+			rc = handle_receive_message(namespace, IPC_GET_ARG1(call));
+			async_answer_0(callid, rc);
+			break;
+		default:
+			async_answer_0(callid, EINVAL);
+			break;
+		}
+	}
+
+	printf(NAME "/sink: client %s terminated.\n", namespace_get_name(namespace));
+	namespace_writer_detach(namespace);
+}
+
+
+static logging_namespace_t *find_namespace_and_attach_reader(void)
+{
+	ipc_call_t call;
+	ipc_callid_t callid = async_get_call(&call);
+
+	if (IPC_GET_IMETHOD(call) != LOGGER_CONNECT) {
+		async_answer_0(callid, EINVAL);
+		return NULL;
+	}
+
+	void *name;
+	int rc = async_data_write_accept(&name, true, 1, MAX_NAMESPACE_LENGTH, 0, NULL);
+
+	if (rc != EOK) {
+		async_answer_0(callid, rc);
+		return NULL;
+	}
+
+	logging_namespace_t *result = namespace_reader_attach((const char *) name);
+	if (result == NULL) {
+		rc = ENOENT;
+	}
+
+	async_answer_0(callid, rc);
+
+	free(name);
+
+	return result;
+}
+
+
+static int handle_send_message(logging_namespace_t *namespace, int *level)
+{
+	ipc_callid_t callid;
+	size_t size;
+	bool expects_data_read = async_data_read_receive(&callid, &size);
+	if (!expects_data_read) {
+		return EINVAL;
+	}
+
+	log_message_t *message = namespace_get_next_message(namespace);
+	size_t message_len = str_size(message->message) + 1;
+
+	if (size > message_len) {
+		size = message_len;
+	}
+
+	async_data_read_finalize(callid, message->message, size);
+
+	*level = (int) message->level;
+	message_destroy(message);
+
+	return EOK;
+}
+
+
+static void connection_handler_source(logging_namespace_t *namespace)
+{
+	printf(NAME "/source: new client for %s.\n", namespace_get_name(namespace));
+
+	while (true) {
+		ipc_call_t call;
+		ipc_callid_t callid = async_get_call(&call);
+
+		if (!IPC_GET_IMETHOD(call))
+			break;
+
+		int rc;
+		int message_level = 0;
+
+		switch (IPC_GET_IMETHOD(call)) {
+		case LOGGER_GET_MESSAGE:
+			rc = handle_send_message(namespace, &message_level);
+			async_answer_1(callid, rc, message_level);
+			break;
+		default:
+			async_answer_0(callid, EINVAL);
+			break;
+		}
+	}
+
+	printf(NAME "/source: client %s terminated.\n", namespace_get_name(namespace));
+	namespace_reader_detach(namespace);
+}
+
+static void connection_handler(ipc_callid_t iid, ipc_call_t *icall, void *arg)
+{
+	logger_interface_t iface = IPC_GET_ARG1(*icall);
+
+	logging_namespace_t *namespace = NULL;
+
+	switch (iface) {
+	case LOGGER_INTERFACE_SINK:
+		/* First call has to be the registration. */
+		async_answer_0(iid, EOK);
+		namespace = register_namespace();
+		if (namespace == NULL) {
+			fprintf(stderr, NAME ": failed to register namespace.\n");
+			break;
+		}
+		connection_handler_sink(namespace);
+		break;
+	case LOGGER_INTERFACE_SOURCE:
+		async_answer_0(iid, EOK);
+		/* First call has to find existing namespace. */
+		namespace = find_namespace_and_attach_reader();
+		if (namespace == NULL) {
+			fprintf(stderr, NAME ": failed to attach client.\n");
+			break;
+		}
+		connection_handler_source(namespace);
+		break;
+	default:
+		async_answer_0(iid, EINVAL);
+		break;
+	}
+}
+
+int main(int argc, char *argv[])
+{
+	printf(NAME ": HelenOS Logging Service\n");
+	
+	async_set_client_connection(connection_handler);
+	
+	int rc = service_register(SERVICE_LOGGER);
+	if (rc != EOK) {
+		printf(NAME ": failed to register: %s.\n", str_error(rc));
+		return -1;
+	}
+	
+	printf(NAME ": Accepting connections\n");
+	async_manager();
+	
+	/* Never reached */
+	return 0;
+}
+
+/**
+ * @}
+ */
Index: uspace/srv/logger/namespace.c
===================================================================
--- uspace/srv/logger/namespace.c	(revision e1eda1e95323c34f73a28bbd88bfeb5abb1e79cb)
+++ uspace/srv/logger/namespace.c	(revision e1eda1e95323c34f73a28bbd88bfeb5abb1e79cb)
@@ -0,0 +1,223 @@
+/*
+ * Copyright (c) 2012 Vojtech Horky
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * - Redistributions of source code must retain the above copyright
+ *   notice, this list of conditions and the following disclaimer.
+ * - Redistributions in binary form must reproduce the above copyright
+ *   notice, this list of conditions and the following disclaimer in the
+ *   documentation and/or other materials provided with the distribution.
+ * - The name of the author may not be used to endorse or promote products
+ *   derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+/** @addtogroup logger
+ * @{
+ */
+#include <assert.h>
+#include <malloc.h>
+#include <str.h>
+#include <stdio.h>
+#include "logger.h"
+
+/** @file
+ * Logging namespaces.
+ */
+
+struct logging_namespace {
+	fibril_mutex_t guard;
+	bool has_writer;
+	bool has_reader;
+	const char *name;
+	link_t link;
+	prodcons_t messages;
+};
+
+static FIBRIL_MUTEX_INITIALIZE(namespace_list_guard);
+static LIST_INITIALIZE(namespace_list);
+
+log_message_t *message_create(const char *name, log_level_t level)
+{
+	log_message_t *message = malloc(sizeof(log_message_t));
+	if (message == NULL)
+		return NULL;
+
+	message->message = str_dup(name);
+	if (message->message == NULL) {
+		free(message);
+		return NULL;
+	}
+
+	message->level = level;
+	link_initialize(&message->link);
+
+	return message;
+}
+
+void message_destroy(log_message_t *message)
+{
+	assert(message);
+	free(message->message);
+	free(message);
+}
+
+static logging_namespace_t *namespace_find_no_lock(const char *name)
+{
+	list_foreach(namespace_list, it) {
+		logging_namespace_t *namespace = list_get_instance(it, logging_namespace_t, link);
+		if (str_cmp(namespace->name, name) == 0) {
+			return namespace;
+		}
+	}
+
+	return NULL;
+}
+
+logging_namespace_t *namespace_create(const char *name)
+{
+	fibril_mutex_lock(&namespace_list_guard);
+	logging_namespace_t *existing = namespace_find_no_lock(name);
+	if (existing != NULL) {
+		fibril_mutex_unlock(&namespace_list_guard);
+		return NULL;
+	}
+
+	logging_namespace_t *namespace = malloc(sizeof(logging_namespace_t));
+	if (namespace == NULL) {
+		fibril_mutex_unlock(&namespace_list_guard);
+		return NULL;
+	}
+
+	namespace->name = str_dup(name);
+	if (namespace->name == NULL) {
+		fibril_mutex_unlock(&namespace_list_guard);
+		free(namespace);
+		return NULL;
+	}
+
+	fibril_mutex_initialize(&namespace->guard);
+	prodcons_initialize(&namespace->messages);
+	namespace->has_reader = false;
+	namespace->has_writer = true;
+	link_initialize(&namespace->link);
+
+	list_append(&namespace->link, &namespace_list);
+	fibril_mutex_unlock(&namespace_list_guard);
+
+	return namespace;
+}
+
+const char *namespace_get_name(logging_namespace_t *namespace)
+{
+	assert(namespace);
+	return namespace->name;
+}
+
+static void namespace_destroy_careful(logging_namespace_t *namespace)
+{
+	assert(namespace);
+	fibril_mutex_lock(&namespace_list_guard);
+
+	fibril_mutex_lock(&namespace->guard);
+	if (namespace->has_reader || namespace->has_writer) {
+		fibril_mutex_unlock(&namespace->guard);
+		fibril_mutex_unlock(&namespace_list_guard);
+		return;
+	}
+
+	list_remove(&namespace->link);
+
+	fibril_mutex_unlock(&namespace->guard);
+	fibril_mutex_unlock(&namespace_list_guard);
+
+	// TODO - destroy pending messages
+	free(namespace->name);
+	free(namespace);
+}
+
+void namespace_destroy(logging_namespace_t *namespace)
+{
+	namespace_destroy_careful(namespace);
+}
+
+logging_namespace_t *namespace_reader_attach(const char *name)
+{
+	logging_namespace_t *namespace = NULL;
+
+	fibril_mutex_lock(&namespace_list_guard);
+
+	namespace = namespace_find_no_lock(name);
+
+	if (namespace != NULL) {
+		fibril_mutex_lock(&namespace->guard);
+		namespace->has_reader = true;
+		fibril_mutex_unlock(&namespace->guard);
+	}
+
+	fibril_mutex_unlock(&namespace_list_guard);
+
+	return namespace;
+}
+
+void namespace_reader_detach(logging_namespace_t *namespace)
+{
+	fibril_mutex_lock(&namespace->guard);
+	namespace->has_reader = false;
+	fibril_mutex_unlock(&namespace->guard);
+
+	namespace_destroy_careful(namespace);
+}
+
+void namespace_writer_detach(logging_namespace_t *namespace)
+{
+	fibril_mutex_lock(&namespace->guard);
+	namespace->has_writer = false;
+	fibril_mutex_unlock(&namespace->guard);
+
+	namespace_destroy_careful(namespace);
+}
+
+
+
+void namespace_add_message(logging_namespace_t *namespace, const char *message, log_level_t level)
+{
+	if (level <= DEFAULT_LOGGING_LEVEL) {
+		printf("[%s %d]: %s\n", namespace->name, level, message);
+	}
+
+	fibril_mutex_lock(&namespace->guard);
+	if (namespace->has_reader) {
+		log_message_t *msg = message_create(message, level);
+		if (msg != NULL) {
+			prodcons_produce(&namespace->messages, &msg->link);
+		}
+	}
+	fibril_mutex_unlock(&namespace->guard);
+}
+
+log_message_t *namespace_get_next_message(logging_namespace_t *namespace)
+{
+	link_t *message = prodcons_consume(&namespace->messages);
+
+	return list_get_instance(message, log_message_t, link);
+}
+
+
+/**
+ * @}
+ */
