Implementing API and process for routing BDB log messages into the OTP logger

This commit is contained in:
Dave Smith 2009-04-16 07:08:50 -06:00
parent 22f33e0b0d
commit 1281a0c07f
3 changed files with 88 additions and 2 deletions

View file

@ -1,7 +1,7 @@
{application, bdberl,
[{description, "Berkeley DB Erlang Driver"},
{vsn, "6"},
{modules, [ bdberl ]},
{modules, [ bdberl, bdberl_logger ]},
{registered, []},
{applications, [kernel,
stdlib]},

View file

@ -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.

57
src/bdberl_logger.erl Normal file
View file

@ -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}.