diff --git a/lib/msc/src/disk_log_h.erl b/lib/msc/src/disk_log_h.erl index 540eeee..09357af 100644 --- a/lib/msc/src/disk_log_h.erl +++ b/lib/msc/src/disk_log_h.erl @@ -9,7 +9,17 @@ %%% 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 +%%% Modified: 4 Jan 2006 by Martin Bjorklund +%%% 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'). @@ -17,12 +27,12 @@ -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"). @@ -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()] @@ -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 @@ -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. @@ -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 @@ -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 @@ -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. diff --git a/lib/msc/src/logger.erl b/lib/msc/src/logger.erl index 1814be0..04d25a2 100644 --- a/lib/msc/src/logger.erl +++ b/lib/msc/src/logger.erl @@ -9,6 +9,21 @@ %%% {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 @@ -16,7 +31,10 @@ %%% Modified: 04 Dec 2000 by Martin Bjorklund %%% Modified: 13 Nov 2003 by Martin Bjorklund %%% Cleanup for jungerl. +%%% Modified: 15 Aug 2007 by Martin Bjorklund +%%% Added example, minor cleanups. %%%---------------------------------------------------------------------- + -module(logger). -vsn("$Revision$ "). -author('magnus@bluetail.com'). @@ -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. @@ -107,7 +117,7 @@ tell_started() -> {started, started} -> ?LOG("System started.\n", []); _ -> - timer:sleep(1000), + timer:sleep(100), tell_started() end. @@ -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)])]; @@ -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 -> <> = B, lists:flatten(io_lib:format("~p(~w)", [ShortBin, size(B)])); nobin(L) when list(L) ->