Index: boot/Makefile.common
===================================================================
--- boot/Makefile.common	(revision 5e4f22b5f9e2114f784c5b534476c7afdc86121e)
+++ boot/Makefile.common	(revision 1f2dd20bb7535f5d8a3876582e186cb29b7d6453)
@@ -74,5 +74,6 @@
 	$(USPACE_PATH)/srv/locsrv/locsrv \
 	$(USPACE_PATH)/srv/bd/rd/rd \
-	$(USPACE_PATH)/srv/vfs/vfs
+	$(USPACE_PATH)/srv/vfs/vfs \
+	$(USPACE_PATH)/srv/logger/logger
 
 ifeq ($(RDFMT),tmpfs)
Index: uspace/Makefile
===================================================================
--- uspace/Makefile	(revision 5e4f22b5f9e2114f784c5b534476c7afdc86121e)
+++ uspace/Makefile	(revision 1f2dd20bb7535f5d8a3876582e186cb29b7d6453)
@@ -74,4 +74,5 @@
 	srv/clipboard \
 	srv/locsrv \
+	srv/logger \
 	srv/devman \
 	srv/loader \
Index: uspace/lib/c/generic/io/log.c
===================================================================
--- uspace/lib/c/generic/io/log.c	(revision 5e4f22b5f9e2114f784c5b534476c7afdc86121e)
+++ uspace/lib/c/generic/io/log.c	(revision 1f2dd20bb7535f5d8a3876582e186cb29b7d6453)
@@ -38,26 +38,59 @@
 #include <stdlib.h>
 #include <stdio.h>
+#include <async.h>
+#include <io/log.h>
+#include <ipc/logger.h>
+#include <ns.h>
 
-#include <io/log.h>
-
-/** Serialization mutex for logging functions. */
-static FIBRIL_MUTEX_INITIALIZE(log_serializer);
-
-/** Current log level. */
-static log_level_t log_level;
-
-static FILE *log_stream;
-
+/** Log messages are printed under this name. */
 static const char *log_prog_name;
 
-/** Prefixes for individual logging levels. */
-static const char *log_level_names[] = {
-	[LVL_FATAL] = "Fatal error",
-	[LVL_ERROR] = "Error",
-	[LVL_WARN] = "Warning",
-	[LVL_NOTE] = "Note",
-	[LVL_DEBUG] = "Debug",
-	[LVL_DEBUG2] = "Debug2"
-};
+/** IPC session with the logger service. */
+static async_sess_t *logger_session;
+
+/** Maximum length of a single log message (in bytes). */
+#define MESSAGE_BUFFER_SIZE 4096
+
+static int logger_register(async_sess_t *session, const char *prog_name)
+{
+	async_exch_t *exchange = async_exchange_begin(session);
+	if (exchange == NULL) {
+		return ENOMEM;
+	}
+
+	aid_t reg_msg = async_send_0(exchange, LOGGER_REGISTER, NULL);
+	int rc = async_data_write_start(exchange, prog_name, str_size(prog_name));
+	sysarg_t reg_msg_rc;
+	async_wait_for(reg_msg, &reg_msg_rc);
+
+	async_exchange_end(exchange);
+
+	if (rc != EOK) {
+		return rc;
+	}
+
+	return reg_msg_rc;
+}
+
+static int logger_message(async_sess_t *session, log_level_t level, const char *message)
+{
+	async_exch_t *exchange = async_exchange_begin(session);
+	if (exchange == NULL) {
+		return ENOMEM;
+	}
+
+	aid_t reg_msg = async_send_1(exchange, LOGGER_MESSAGE, level, NULL);
+	int rc = async_data_write_start(exchange, message, str_size(message));
+	sysarg_t reg_msg_rc;
+	async_wait_for(reg_msg, &reg_msg_rc);
+
+	async_exchange_end(exchange);
+
+	if (rc != EOK) {
+		return rc;
+	}
+
+	return reg_msg_rc;
+}
 
 /** Initialize the logging system.
@@ -69,12 +102,17 @@
 {
 	assert(level < LVL_LIMIT);
-	log_level = level;
 
-	log_stream = stdout;
 	log_prog_name = str_dup(prog_name);
 	if (log_prog_name == NULL)
 		return ENOMEM;
 
-	return EOK;
+	logger_session = service_connect_blocking(EXCHANGE_SERIALIZE, SERVICE_LOGGER, LOGGER_INTERFACE_SINK, 0);
+	if (logger_session == NULL) {
+		return ENOMEM;
+	}
+
+	int rc = logger_register(logger_session, log_prog_name);
+
+	return rc;
 }
 
@@ -106,16 +144,11 @@
 	assert(level < LVL_LIMIT);
 
-	/* Higher number means higher verbosity. */
-	if (level <= log_level) {
-		fibril_mutex_lock(&log_serializer);
+	char *message_buffer = malloc(MESSAGE_BUFFER_SIZE);
+	if (message_buffer == NULL) {
+		return;
+	}
 
-		fprintf(log_stream, "%s: %s: ", log_prog_name,
-		    log_level_names[level]);
-		vfprintf(log_stream, fmt, args);
-		fputc('\n', log_stream);
-		fflush(log_stream);
-
-		fibril_mutex_unlock(&log_serializer);
-	}
+	vsnprintf(message_buffer, MESSAGE_BUFFER_SIZE, fmt, args);
+	logger_message(logger_session, level, message_buffer);
 }
 
Index: uspace/lib/c/include/ipc/logger.h
===================================================================
--- uspace/lib/c/include/ipc/logger.h	(revision 1f2dd20bb7535f5d8a3876582e186cb29b7d6453)
+++ uspace/lib/c/include/ipc/logger.h	(revision 1f2dd20bb7535f5d8a3876582e186cb29b7d6453)
@@ -0,0 +1,58 @@
+/*
+ * 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 libc
+ * @{
+ */
+
+#ifndef LIBC_IPC_LOGGER_H_
+#define LIBC_IPC_LOGGER_H_
+
+#include <ipc/common.h>
+
+typedef enum {
+	LOGGER_REGISTER = IPC_FIRST_USER_METHOD,
+	LOGGER_MESSAGE
+} logger_sink_request_t;
+
+typedef enum {
+	 LOGGER_CONNECT = IPC_FIRST_USER_METHOD,
+	 LOGGER_GET_MESSAGE
+} logger_source_request_t;
+
+typedef enum {
+	/** Interface for servers writing to the log. */
+	LOGGER_INTERFACE_SINK,
+	/** Interface for clients displaying the log. */
+	LOGGER_INTERFACE_SOURCE
+} logger_interface_t;
+
+#endif
+
+/** @}
+ */
Index: uspace/lib/c/include/ipc/services.h
===================================================================
--- uspace/lib/c/include/ipc/services.h	(revision 5e4f22b5f9e2114f784c5b534476c7afdc86121e)
+++ uspace/lib/c/include/ipc/services.h	(revision 1f2dd20bb7535f5d8a3876582e186cb29b7d6453)
@@ -45,4 +45,5 @@
 	SERVICE_VFS        = FOURCC('v', 'f', 's', ' '),
 	SERVICE_LOC        = FOURCC('l', 'o', 'c', ' '),
+	SERVICE_LOGGER     = FOURCC('l', 'o', 'g', 'g'),
 	SERVICE_DEVMAN     = FOURCC('d', 'e', 'v', 'n'),
 	SERVICE_IRC        = FOURCC('i', 'r', 'c', ' '),
Index: uspace/srv/logger/Makefile
===================================================================
--- uspace/srv/logger/Makefile	(revision 1f2dd20bb7535f5d8a3876582e186cb29b7d6453)
+++ uspace/srv/logger/Makefile	(revision 1f2dd20bb7535f5d8a3876582e186cb29b7d6453)
@@ -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 1f2dd20bb7535f5d8a3876582e186cb29b7d6453)
+++ uspace/srv/logger/logger.h	(revision 1f2dd20bb7535f5d8a3876582e186cb29b7d6453)
@@ -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 1f2dd20bb7535f5d8a3876582e186cb29b7d6453)
+++ uspace/srv/logger/main.c	(revision 1f2dd20bb7535f5d8a3876582e186cb29b7d6453)
@@ -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 1f2dd20bb7535f5d8a3876582e186cb29b7d6453)
+++ uspace/srv/logger/namespace.c	(revision 1f2dd20bb7535f5d8a3876582e186cb29b7d6453)
@@ -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);
+}
+
+
+/**
+ * @}
+ */
