Browse Source

ft:修改

master
SisMaker 4 years ago
parent
commit
b698760ea3
7 changed files with 40 additions and 126 deletions
  1. +0
    -18
      src/formatter/rumFormat.erl
  2. +1
    -19
      src/formatter/rumFormatter.erl
  3. +0
    -20
      src/misc/rumCrashLog.erl
  4. +0
    -16
      src/misc/rumErrLoggerH.erl
  5. +0
    -19
      src/misc/rumStdlib.erl
  6. +11
    -9
      src/rotator/rumRotatorExm.erl
  7. +28
    -25
      src/rotator/rumRotatorIns.erl

+ 0
- 18
src/formatter/rumFormat.erl View File

@ -1,21 +1,3 @@
%%
%% %CopyrightBegin%
%%
%% Copyright Ericsson AB 1996-2011-2012. All Rights Reserved.
%%
%% The contents of this file are subject to the Erlang Public License,
%% Version 1.1, (the "License"); you may not use this file except in
%% compliance with the License. You should have received a copy of the
%% Erlang Public License along with this software. If not, it can be
%% retrieved online at http://www.erlang.org/.
%%
%% Software distributed under the License is distributed on an "AS IS"
%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
%% the License for the specific language governing rights and limitations
%% under the License.
%%
%% %CopyrightEnd%
%%
-module(rumFormat). -module(rumFormat).
%% fork of io_lib_format that uses trunc_io to protect against large terms %% fork of io_lib_format that uses trunc_io to protect against large terms

+ 1
- 19
src/formatter/rumFormatter.erl View File

@ -1,25 +1,7 @@
%% Copyright (c) 2011-2012 Basho Technologies, Inc. All Rights Reserved.
%%
%% This file is provided to you under the Apache License,
%% Version 2.0 (the "License"); you may not use this file
%% except in compliance with the License. You may obtain
%% a copy of the License at
%%
%% http://www.apache.org/licenses/LICENSE-2.0
%%
%% Unless required by applicable law or agreed to in writing,
%% software distributed under the License is distributed on an
%% "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
%% KIND, either express or implied. See the License for the
%% specific language governing permissions and limitations
%% under the License.
-module(rumFormatter). -module(rumFormatter).
%%
%% Include files
%%
-include("eRum.hrl"). -include("eRum.hrl").
-ifdef(TEST). -ifdef(TEST).
-include_lib("eunit/include/eunit.hrl"). -include_lib("eunit/include/eunit.hrl").
-endif. -endif.

+ 0
- 20
src/misc/rumCrashLog.erl View File

@ -1,23 +1,3 @@
%% -------------------------------------------------------------------
%%
%% Copyright (c) 2011-2017 Basho Technologies, Inc.
%%
%% This file is provided to you under the Apache License,
%% Version 2.0 (the "License"); you may not use this file
%% except in compliance with the License. You may obtain
%% a copy of the License at
%%
%% http://www.apache.org/licenses/LICENSE-2.0
%%
%% Unless required by applicable law or agreed to in writing,
%% software distributed under the License is distributed on an
%% "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
%% KIND, either express or implied. See the License for the
%% specific language governing permissions and limitations
%% under the License.
%%
%% -------------------------------------------------------------------
%% @doc Lager crash log writer. This module implements a gen_server which writes %% @doc Lager crash log writer. This module implements a gen_server which writes
%% error_logger error messages out to a file in their original format. The %% error_logger error messages out to a file in their original format. The
%% location to which it logs is configured by the application var `crash_log'. %% location to which it logs is configured by the application var `crash_log'.

+ 0
- 16
src/misc/rumErrLoggerH.erl View File

@ -1,19 +1,3 @@
%% Copyright (c) 2011-2015 Basho Technologies, Inc. All Rights Reserved.
%%
%% This file is provided to you under the Apache License,
%% Version 2.0 (the "License"); you may not use this file
%% except in compliance with the License. You may obtain
%% a copy of the License at
%%
%% http://www.apache.org/licenses/LICENSE-2.0
%%
%% Unless required by applicable law or agreed to in writing,
%% software distributed under the License is distributed on an
%% "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
%% KIND, either express or implied. See the License for the
%% specific language governing permissions and limitations
%% under the License.
%% @doc A error_logger backend for redirecting events into lager. %% @doc A error_logger backend for redirecting events into lager.
%% Error messages and crash logs are also optionally written to a crash log. %% Error messages and crash logs are also optionally written to a crash log.

+ 0
- 19
src/misc/rumStdlib.erl View File

@ -1,22 +1,3 @@
%%
%% %CopyrightBegin%
%%
%% Copyright Ericsson AB 1996-2009. All Rights Reserved.
%%
%% The contents of this file are subject to the Erlang Public License,
%% Version 1.1, (the "License"); you may not use this file except in
%% compliance with the License. You should have received a copy of the
%% Erlang Public License along with this software. If not, it can be
%% retrieved online at http://www.erlang.org/.
%%
%% Software distributed under the License is distributed on an "AS IS"
%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
%% the License for the specific language governing rights and limitations
%% under the License.
%%
%% %CopyrightEnd%
%%
%% @doc Functions from Erlang OTP distribution that are really useful %% @doc Functions from Erlang OTP distribution that are really useful
%% but aren't exported. %% but aren't exported.
%% %%

+ 11
- 9
src/rotator/rumRotatorExm.erl View File

@ -1,18 +1,20 @@
-module(rumRotatorExm). -module(rumRotatorExm).
%% Create a log file %% Create a log file
-callback(create_logfile(Name :: list(), Buffer :: {integer(), integer()} | any()) ->
{ok, {file:io_device(), integer(), file:date_time(), integer()}} | {error, any()}).
-callback createLogFile(Name :: list(), Buffer :: {integer(), integer()} | any()) ->
{ok, {file:io_device(), integer(), file:date_time(), integer()}} |
{error, any()}.
%% Open a log file %% Open a log file
-callback(open_logfile(Name :: list(), Buffer :: {integer(), integer()} | any()) ->
{ok, {file:io_device(), integer(), file:date_time(), integer()}} | {error, any()}).
-callback openLogFile(Name :: list(), Buffer :: {integer(), integer()} | any()) ->
{ok, {file:io_device(), integer(), file:date_time(), integer()}} |
{error, any()}.
%% Ensure reference to current target, could be rotated %% Ensure reference to current target, could be rotated
-callback(ensure_logfile(Name :: list(), FD :: file:io_device(), Inode :: integer(), Ctime :: file:date_time(),
Buffer :: {integer(), integer()} | any()) ->
{ok, {file:io_device(), integer(), file:date_time(), integer()}} | {error, any()}).
-callback ensureLogFile(Name :: list(), FD :: file:io_device(), Inode :: integer(), CTime :: file:date_time(), Buffer :: {integer(), integer()} | any()) ->
{ok, {file:io_device(), integer(), file:date_time(), integer()}} |
{error, any()}.
%% Rotate the log file %% Rotate the log file
-callback(rotate_logfile(Name :: list(), Count :: integer()) ->
ok).
-callback rotateLogFile(Name :: list(), Count :: integer()) ->
ok.

+ 28
- 25
src/rotator/rumRotatorIns.erl View File

@ -1,21 +1,24 @@
-module(rumRotatorIns). -module(rumRotatorIns).
-include_lib("kernel/include/file.hrl").
-behaviour(rumRotatorExm). -behaviour(rumRotatorExm).
-include_lib("kernel/include/file.hrl").
-export([ -export([
create_logfile/2, open_logfile/2, ensure_logfile/5, rotate_logfile/2
createLogFile/2
, openLogFile/2
, ensureLogFile/5
, rotateLogFile/2
]). ]).
-ifdef(TEST). -ifdef(TEST).
-include_lib("eunit/include/eunit.hrl"). -include_lib("eunit/include/eunit.hrl").
-endif. -endif.
create_logfile(Name, Buffer) ->
open_logfile(Name, Buffer).
createLogFile(Name, Buffer) ->
openLogFile(Name, Buffer).
open_logfile(Name, Buffer) ->
openLogFile(Name, Buffer) ->
case filelib:ensure_dir(Name) of case filelib:ensure_dir(Name) of
ok -> ok ->
Options = [append, raw] ++ Options = [append, raw] ++
@ -29,9 +32,9 @@ open_logfile(Name, Buffer) ->
case file:read_file_info(Name, [raw]) of case file:read_file_info(Name, [raw]) of
{ok, FInfo0} -> {ok, FInfo0} ->
Inode = FInfo0#file_info.inode, Inode = FInfo0#file_info.inode,
{ok, Ctime} = maybe_update_ctime(Name, FInfo0),
{ok, CTime} = maybe_update_ctime(Name, FInfo0),
Size1 = FInfo0#file_info.size, Size1 = FInfo0#file_info.size,
{ok, {FD, Inode, Ctime, Size1}};
{ok, {FD, Inode, CTime, Size1}};
X -> X X -> X
end; end;
Y -> Y Y -> Y
@ -39,14 +42,14 @@ open_logfile(Name, Buffer) ->
Z -> Z Z -> Z
end. end.
ensure_logfile(Name, undefined, _Inode, _Ctime, Buffer) ->
open_logfile(Name, Buffer);
ensure_logfile(Name, FD, Inode0, Ctime0, Buffer) ->
case rumUtil:has_file_changed(Name, Inode0, Ctime0) of
ensureLogFile(Name, undefined, _Inode, _CTime, Buffer) ->
openLogFile(Name, Buffer);
ensureLogFile(Name, FD, Inode0, CTime0, Buffer) ->
case rumUtil:has_file_changed(Name, Inode0, CTime0) of
{true, _FInfo} -> {true, _FInfo} ->
reopen_logfile(Name, FD, Buffer); reopen_logfile(Name, FD, Buffer);
{_, FInfo} -> {_, FInfo} ->
{ok, {FD, Inode0, Ctime0, FInfo#file_info.size}}
{ok, {FD, Inode0, CTime0, FInfo#file_info.size}}
end. end.
reopen_logfile(Name, FD0, Buffer) -> reopen_logfile(Name, FD0, Buffer) ->
@ -55,8 +58,8 @@ reopen_logfile(Name, FD0, Buffer) ->
_ = file:datasync(FD0), _ = file:datasync(FD0),
_ = file:close(FD0), _ = file:close(FD0),
_ = file:close(FD0), _ = file:close(FD0),
case open_logfile(Name, Buffer) of
{ok, {_FD1, _Inode, _Size, _Ctime} = FileInfo} ->
case openLogFile(Name, Buffer) of
{ok, {_FD1, _Inode, _Size, _CTime} = FileInfo} ->
%% inode changed, file was probably moved and %% inode changed, file was probably moved and
%% recreated %% recreated
{ok, FileInfo}; {ok, FileInfo};
@ -65,26 +68,26 @@ reopen_logfile(Name, FD0, Buffer) ->
end. end.
%% renames failing are OK %% renames failing are OK
rotate_logfile(File, 0) ->
rotateLogFile(File, 0) ->
%% open the file in write-only mode to truncate/create it %% open the file in write-only mode to truncate/create it
case file:open(File, [write]) of case file:open(File, [write]) of
{ok, FD} -> {ok, FD} ->
_ = file:close(FD), _ = file:close(FD),
_ = file:close(FD), _ = file:close(FD),
{ok, _Ctime} = maybe_update_ctime(File),
{ok, _CTime} = maybe_update_ctime(File),
ok; ok;
Error -> Error ->
Error Error
end; end;
rotate_logfile(File0, 1) ->
rotateLogFile(File0, 1) ->
File1 = File0 ++ ".0", File1 = File0 ++ ".0",
_ = file:rename(File0, File1), _ = file:rename(File0, File1),
rotate_logfile(File0, 0);
rotate_logfile(File0, Count) ->
rotateLogFile(File0, 0);
rotateLogFile(File0, Count) ->
File1 = File0 ++ "." ++ integer_to_list(Count - 2), File1 = File0 ++ "." ++ integer_to_list(Count - 2),
File2 = File0 ++ "." ++ integer_to_list(Count - 1), File2 = File0 ++ "." ++ integer_to_list(Count - 1),
_ = file:rename(File1, File2), _ = file:rename(File1, File2),
rotate_logfile(File0, Count - 1).
rotateLogFile(File0, Count - 1).
maybe_update_ctime(Name) -> maybe_update_ctime(Name) ->
case file:read_file_info(Name, [raw]) of case file:read_file_info(Name, [raw]) of
@ -130,7 +133,7 @@ rotate_file_test() ->
% creates the file that Inner looks for. % creates the file that Inner looks for.
% Don't shoot the messenger, it was worse before this refactoring. % Don't shoot the messenger, it was worse before this refactoring.
lists:foreach(Inner, lists:seq(0, Count - 1)), lists:foreach(Inner, lists:seq(0, Count - 1)),
rotate_logfile(TestLog, RotCount)
rotateLogFile(TestLog, RotCount)
end, end,
lists:foreach(Outer, lists:seq(0, (RotCount * 2))), lists:foreach(Outer, lists:seq(0, (RotCount * 2))),
rumUtil:delete_test_dir(TestDir). rumUtil:delete_test_dir(TestDir).
@ -139,7 +142,7 @@ rotate_file_zero_count_test() ->
%% Test that a rotation count of 0 simply truncates the file %% Test that a rotation count of 0 simply truncates the file
{ok, TestDir} = rumUtil:create_test_dir(), {ok, TestDir} = rumUtil:create_test_dir(),
TestLog = filename:join(TestDir, "rotation.log"), TestLog = filename:join(TestDir, "rotation.log"),
?assertMatch(ok, rotate_logfile(TestLog, 0)),
?assertMatch(ok, rotateLogFile(TestLog, 0)),
?assertNot(filelib:is_regular(TestLog ++ ".0")), ?assertNot(filelib:is_regular(TestLog ++ ".0")),
?assertEqual(true, filelib:is_regular(TestLog)), ?assertEqual(true, filelib:is_regular(TestLog)),
?assertEqual(1, length(filelib:wildcard(TestLog ++ "*"))), ?assertEqual(1, length(filelib:wildcard(TestLog ++ "*"))),
@ -167,7 +170,7 @@ rotate_file_fail_test() ->
_ -> _ ->
%% hose up the permissions %% hose up the permissions
ok = rumUtil:set_dir_permissions("u-w", TestDir), ok = rumUtil:set_dir_permissions("u-w", TestDir),
?assertMatch({error, _}, rotate_logfile(TestLog, 10))
?assertMatch({error, _}, rotateLogFile(TestLog, 10))
end, end,
%% check we still only have one file, rotation.log %% check we still only have one file, rotation.log
@ -177,7 +180,7 @@ rotate_file_fail_test() ->
%% fix the permissions %% fix the permissions
ok = rumUtil:set_dir_permissions("u+w", TestDir), ok = rumUtil:set_dir_permissions("u+w", TestDir),
?assertMatch(ok, rotate_logfile(TestLog, 10)),
?assertMatch(ok, rotateLogFile(TestLog, 10)),
?assert(filelib:is_regular(TestLog ++ ".0")), ?assert(filelib:is_regular(TestLog ++ ".0")),
?assertEqual(true, filelib:is_regular(TestLog)), ?assertEqual(true, filelib:is_regular(TestLog)),
?assertEqual(2, length(filelib:wildcard(TestLog ++ "*"))), ?assertEqual(2, length(filelib:wildcard(TestLog ++ "*"))),

Loading…
Cancel
Save