Skip to content

Commit

Permalink
updated disk_log_h and logger
Browse files Browse the repository at this point in the history
  • Loading branch information
mbj committed Aug 15, 2007
1 parent 326fddf commit 6611471
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 29 deletions.
80 changes: 62 additions & 18 deletions lib/msc/src/disk_log_h.erl
Expand Up @@ -9,20 +9,30 @@
%%% disk. In this case, the disk_log process is avoided.
%%%
%%% This module is intended to replace log_mf_h.erl.
%%%
%%% It now works with OTP R10B-9 without the need to patch
%%% disk_log.erl. However, disk_log in OTP uses the process
%%% dictionary, which means that only one disk_log_h can be
%%% installed in a single gen_event process.
%%%
%%% Created : 1 Dec 2000 by Martin Bjorklund <mbj@bluetail.com>
%%% Modified: 4 Jan 2006 by Martin Bjorklund <mbj@tail-f.com>
%%% o Added option {force_size, true} (which really should be
%%% in disk_log instead)
%%% o Added function change_size/3
%%%----------------------------------------------------------------------
-module(disk_log_h).
-author('mbj@bluetail.com').

-behaviour(gen_event).

%% External exports
-export([init/2, info/2]).
-export([init/2, info/2, change_size/3]).

%% gen_event callbacks
-export([init/1, handle_event/2, handle_call/2, handle_info/2, terminate/2]).

-record(state, {log, cnt, func}).
-record(state, {cnt, func}).

-include_lib("kernel/src/disk_log.hrl").

Expand All @@ -33,7 +43,7 @@
%%-----------------------------------------------------------------
%% This module is intended to be used as a gen_event handler. Instead
%% of duplicating the functions to gen_event (add_handler etc), it
%% described here hoe to use these function with this module.
%% described here how to use these function with this module.
%%
%% The init function expects a list [Func, Opts], where:
%% Func = fun(Event) -> false | binary() | [binary()]
Expand Down Expand Up @@ -61,10 +71,11 @@
init(Func, DiskLogOpts) ->
[Func, DiskLogOpts].


info(EventMgr, Handler) ->
gen_event:call(EventMgr, Handler, info).


change_size(EventMgr, Handler, NewSize) ->
gen_event:call(EventMgr, Handler, {change_size, NewSize}).

%%%----------------------------------------------------------------------
%%% Callback functions from gen_event
Expand All @@ -76,9 +87,31 @@ info(EventMgr, Handler) ->
%% Other
%%----------------------------------------------------------------------
init([Func, Opts]) ->
case disk_log:ll_open(Opts) of
Opts1 = lists:keydelete(force_size, 1, Opts),
case disk_log:ll_open(Opts1) of
{ok, _, Log, Cnt} ->
{ok, #state{log = Log, cnt = Cnt, func = Func}};
put(log, Log),
{ok, #state{cnt = Cnt, func = Func}};
{error, {size_mismatch, _, NewSize}} = Error ->
case lists:keysearch(force_size, 1, Opts) of
{value, {force_size, true}} ->
%% open w/o size
Opts2 = lists:keydelete(size, 1, Opts1),
case disk_log:ll_open(Opts2) of
{ok, _, Log, Cnt} ->
%% we should really call check_size as well...
case catch do_change_size(Log, NewSize) of
ok ->
{ok, #state{cnt = Cnt, func = Func}};
Else ->
{error, Else}
end;
NError ->
NError
end;
_ ->
Error
end;
Error ->
Error
end.
Expand All @@ -94,14 +127,14 @@ handle_event(Event, S) ->
false ->
{ok, S};
Bin ->
case disk_log:do_log(S#state.log, [Bin]) of
{N, L1} when integer(N) ->
{_, L2} = disk_log:do_sync(L1),
{ok, S#state{cnt = S#state.cnt+N, log = L2}};
{error, {error, {full, _Name}}, L1, N} ->
{_, L2} = disk_log:do_sync(L1),
{ok, S#state{cnt = S#state.cnt+N, log = L2}};
{error, Error, L1, N} ->
case disk_log:do_log(get(log), [Bin]) of
N when integer(N) ->
disk_log:do_sync(get(log)),
{ok, S#state{cnt = S#state.cnt+N}};
{error, {error, {full, _Name}}, N} ->
disk_log:do_sync(get(log)),
{ok, S#state{cnt = S#state.cnt+N}};
{error, Error, N} ->
Error;
Error ->
Error
Expand All @@ -115,8 +148,10 @@ handle_event(Event, S) ->
%% {remove_handler, Reply}
%%----------------------------------------------------------------------
handle_call(info, S) ->
Reply = disk_log:do_info(S#state.log, S#state.cnt),
{ok, Reply, S}.
Reply = disk_log:do_info(get(log), S#state.cnt),
{ok, Reply, S};
handle_call({change_size, NewSize}, S) ->
{ok, catch do_change_size(get(log), NewSize), S}.

%%----------------------------------------------------------------------
%% Func: handle_info/2
Expand All @@ -136,8 +171,17 @@ handle_info(Info, S) ->
%% Returns: any
%%----------------------------------------------------------------------
terminate(Arg, S) ->
disk_log:ll_close(S#state.log).
disk_log:ll_close(get(log)).

%%%----------------------------------------------------------------------
%%% Internal functions
%%%----------------------------------------------------------------------

%% hack so that disk_log doesn't have to be patched - code copied
%% from disk_log:do_change_size/2
do_change_size(L, NewSize) ->
#log{extra = Extra, version = Version} = L,
{ok, Handle} = disk_log_1:change_size_wrap(Extra, NewSize, Version),
erase(is_full),
put(log, L#log{extra = Handle}),
ok.
38 changes: 27 additions & 11 deletions lib/msc/src/logger.erl
Expand Up @@ -9,14 +9,32 @@
%%% {logger, {logger, start_link, []},
%%% permanent, 2000, worker, [logger]},
%%%
%%% start_errlog() ->
%%% Opts = [{name, logger},
%%% {file, "./elog"},
%%% {type, wrap},
%%% {format, external},
%%% {force_size, true},
%%% {size, {1024*1024, 5}}], % 5 files
%%% gen_event:add_sup_handler(
%%% error_logger,
%%% {disk_log_h, logger},
%%% disk_log_h:init(fun logger:form_no_progress/1, Opts)).
%%%
%%% test() ->
%%% error_logger:error_msg("testing ~p\n", [self()]).
%%%
%%% Initiate/deactivate system logging.
%%% Owns the error log.
%%% Created : 13 Apr 1999 by Magnus Fr|berg <magnus@bluetail.com>
%%% Modified: 26 May 1999 by Martin Bjorklund <mbj@bluetail.com>
%%% Modified: 04 Dec 2000 by Martin Bjorklund <mbj@bluetail.com>
%%% Modified: 13 Nov 2003 by Martin Bjorklund <mbj@bluetail.com>
%%% Cleanup for jungerl.
%%% Modified: 15 Aug 2007 by Martin Bjorklund <mbj@tail-f.com>
%%% Added example, minor cleanups.
%%%----------------------------------------------------------------------

-module(logger).
-vsn("$Revision$ ").
-author('magnus@bluetail.com').
Expand Down Expand Up @@ -53,32 +71,24 @@ init([]) ->
set_system_error_logging(),
Type = get_error_logger_mf_type(),
Mf = get_error_logger_mf(),
add_error_logger_mf(Mf, Type),
ok = add_error_logger_mf(Mf, Type),
{Name, Vsn} = init:script_id(),
?LOG("Starting system [~s-~s]\n", [Name, Vsn]),
start_tell_started(),
{ok, []}.

%%----------------------------------------------------------------------
%%----------------------------------------------------------------------
handle_call(_Req, _, S) ->
{reply, unknown_request, S}.

%%----------------------------------------------------------------------
%%----------------------------------------------------------------------
handle_cast(_, S) ->
{noreply, S}.

%%----------------------------------------------------------------------
%%----------------------------------------------------------------------
handle_info({gen_event_EXIT, logger, Reason}, S) ->
{stop, Reason, S};

handle_info(_, S) ->
{noreply, S}.

%%----------------------------------------------------------------------
%%----------------------------------------------------------------------
terminate(_Reason, _S) ->
delete_error_logger_mf(),
ok.
Expand Down Expand Up @@ -107,7 +117,7 @@ tell_started() ->
{started, started} ->
?LOG("System started.\n", []);
_ ->
timer:sleep(1000),
timer:sleep(100),
tell_started()
end.

Expand Down Expand Up @@ -164,6 +174,12 @@ form_all(Event) ->
{error_report, _GL, {Pid, Type, Report}} ->
[mk_hdr("ERROR REPORT", Type, Pid),
io_lib:format("~p\n", [nobin(Report)])];
%% tail-f specific debug messages
{info_report, _GL, {_, debug, {Pid, Now, Level, Tag, MsgStr}}} ->
["*dbg* ", t2s(calendar:now_to_local_time(Now)), " ",
pid_to_list(Pid), " ",
integer_to_list(Level), "/", atom_to_list(Tag),
"\n ", MsgStr];
{info_report, _GL, {Pid, Type, Report}} ->
[mk_hdr("INFO REPORT", Type, Pid),
io_lib:format("~p\n", [nobin(Report)])];
Expand Down Expand Up @@ -199,7 +215,7 @@ pstr(undefined) -> "";
pstr(T) -> io_lib:format("~p", [T]).


nobin(B) when binary(B), size(B) > 32 ->
nobin(B) when binary(B), size(B) > 1024 ->
<<ShortBin:32/binary, _/binary>> = B,
lists:flatten(io_lib:format("~p(~w)", [ShortBin, size(B)]));
nobin(L) when list(L) ->
Expand Down

0 comments on commit 6611471

Please sign in to comment.