From 1281a0c07fceb289675a20ea99e1ee480dc966e6 Mon Sep 17 00:00:00 2001 From: Dave Smith Date: Thu, 16 Apr 2009 07:08:50 -0600 Subject: [PATCH] Implementing API and process for routing BDB log messages into the OTP logger --- ebin/bdberl.app | 2 +- src/bdberl.erl | 31 ++++++++++++++++++++++- src/bdberl_logger.erl | 57 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 88 insertions(+), 2 deletions(-) create mode 100644 src/bdberl_logger.erl diff --git a/ebin/bdberl.app b/ebin/bdberl.app index 4f3643f..5a76dd6 100644 --- a/ebin/bdberl.app +++ b/ebin/bdberl.app @@ -1,7 +1,7 @@ {application, bdberl, [{description, "Berkeley DB Erlang Driver"}, {vsn, "6"}, - {modules, [ bdberl ]}, + {modules, [ bdberl, bdberl_logger ]}, {registered, []}, {applications, [kernel, stdlib]}, diff --git a/src/bdberl.erl b/src/bdberl.erl index d51dc93..1323032 100644 --- a/src/bdberl.erl +++ b/src/bdberl.erl @@ -24,7 +24,8 @@ update/3, update/4, update/5, truncate/0, truncate/1, delete_database/1, - cursor_open/1, cursor_next/0, cursor_prev/0, cursor_current/0, cursor_close/0]). + cursor_open/1, cursor_next/0, cursor_prev/0, cursor_current/0, cursor_close/0, + register_logger/0]). -include("bdberl.hrl"). @@ -1280,6 +1281,23 @@ get_txn_timeout() -> end. +%%-------------------------------------------------------------------- +%% @doc +%% Registers the port owner pid to receive any BDB err/msg events. Note +%% that this is global registration -- ALL BDB err/msg events for this +%% VM instance will be routed to the pid. +%% +%% @spec register_logger() -> ok +%% +%% @end +%%-------------------------------------------------------------------- +-spec register_logger() -> ok. + +register_logger() -> + [] = erlang:port_control(get_port(), ?CMD_REGISTER_LOGGER, <<>>), + ok. + + %% ==================================================================== %% Internal functions %% ==================================================================== @@ -1289,6 +1307,17 @@ init() -> ok -> ok; {error, permanent} -> ok % Means that the driver is already active end, + + %% Look for logging process -- make sure it's running and/or registered + case whereis(bdberl_logger) of + undefined -> + C = {bdberl_logger, {bdberl_logger, start_link, []}, permanent, 1000, + worker, [bdberl_logger]}, + supervisor:start_child(kernel_safe_sup, C); + _ -> + ok + end, + Port = open_port({spawn, bdberl_drv}, [binary]), erlang:put(bdb_port, Port), Port. diff --git a/src/bdberl_logger.erl b/src/bdberl_logger.erl new file mode 100644 index 0000000..aae46c2 --- /dev/null +++ b/src/bdberl_logger.erl @@ -0,0 +1,57 @@ +%% ------------------------------------------------------------------- +%% @doc +%% SASL/OTP logger for BDB. Routes BDB errors/messages into SASL logger. +%% +%% @copyright 2008-9 The Hive. All rights reserved. +%% @end +%% ------------------------------------------------------------------- +-module(bdberl_logger). + +-behaviour(gen_server). + +%% API +-export([start_link/0]). + +%% gen_server callbacks +-export([init/1, handle_call/3, handle_cast/2, handle_info/2, + terminate/2, code_change/3]). + +-record(state, {}). + +%% ==================================================================== +%% API +%% ==================================================================== + +start_link() -> + gen_server:start_link({local, ?MODULE}, ?MODULE, [], []). + +%% ==================================================================== +%% gen_server callbacks +%% ==================================================================== + +init([]) -> + %% Start up the logger -- automatically initializes a port for this + %% PID. + bdberl:register_logger(), + {ok, #state{}}. + +handle_call(_Request, _From, State) -> + {stop, unsupportedOperation, State}. + +handle_cast(_Msg, State) -> + {stop, unsupportedOperation, State}. + +handle_info({bdb_error_log, Msg}, State) -> + error_logger:error_msg("BDB Error: ~s\n", [Msg]), + {noreply, State}; + +handle_info({bdb_info_log, Msg}, State) -> + error_logger:info_msg("BDB Info: ~s\n", [Msg]), + {noreply, State}. + +terminate(_Reason, _State) -> + ok. + +code_change(_OldVsn, State, _Extra) -> + {ok, State}. +