Bläddra i källkod

ft: 时间格式函数修改

master
SisMaker 4 år sedan
förälder
incheckning
e875778ebb
4 ändrade filer med 131 tillägg och 149 borttagningar
  1. +10
    -29
      README.md
  2. +1
    -0
      rebar.config
  3. +2
    -2
      src/backend/rumBackendFile.erl
  4. +118
    -118
      src/utils/rumUtil.erl

+ 10
- 29
README.md Visa fil

@ -312,42 +312,23 @@ info - info and higher (>= is implicit)
该$D0语法来自newsyslog.conf中newsyslog使用的语法。相关摘录如下:
```
Day, week and month time format: The lead-in character
for day, week and month specification is a `$'-sign.
The particular format of day, week and month
specification is: [Dhh], [Ww[Dhh]] and [Mdd[Dhh]],
respectively. Optional time fields default to
midnight. The ranges for day and hour specifications
are:
Hour, Day, week and month 时间格式:
以`$'符号作为时间格式的前缀
hour、day、week、month规范的具体格式分别为:[Hmm] [Dhh]、[WwDhh]和[MddDhhHmm]
可选时间字段默认为midnightthe start of the day)
The ranges for hour, day and hour specifications are:
mm minutes, range 0 ... 59
hh hours, range 0 ... 23
w day of week, range 0 ... 6, 0 = Sunday
dd day of month, range 1 ... 31, or the
letter L or l to specify the last day of
the month.
w day of week, range 1 ... 7
dd day of month, range 1 ... 31, or the letter L or l to specify the last day of the month.
Some examples:
$D0 rotate every night at midnight
$D23 rotate every day at 23:00 hr
$W0D23 rotate every week on Sunday at 23:00 hr
$W5D16 rotate every week on Friday at 16:00 hr
$M1D0 rotate on the first day of every month at
midnight (i.e., the start of the day)
$M5D6 rotate on every 5th day of the month at
6:00 hr
```
On top of the day, week and month time format from newsyslog, hour specification is added from
PR [#420](https://github.com/erlang-lager/lager/pull/420)
```
Format of hour specification is : [Hmm]
The range for minute specification is:
mm minutes, range 0 ... 59
Some examples:
$M1D0 rotate on the first day of every month at midnight (i.e., the start of the day)
$M5D6 rotate on every 5th day of the month at 6:00 hr
$H00 rotate every hour at HH:00
$D12H30 rotate every day at 12:30
$W0D0H0 rotate every week on Sunday at 00:00

+ 1
- 0
rebar.config Visa fil

@ -30,6 +30,7 @@
{deps, [
{eGbh, ".*", {git, "http://47.108.26.175:53000/SisMaker/eGbh.git", {branch, "master"}}},
{eFmt, ".*", {git, "http://47.108.26.175:53000/SisMaker/eFmt.git", {branch, "master"}}},
{eSync, ".*", {git, "http://47.108.26.175:53000/SisMaker/eSync.git", {branch, "master"}}},
{goldrush, "0.1.9"}
]}.

+ 2
- 2
src/backend/rumBackendFile.erl Visa fil

@ -315,7 +315,7 @@ validate_logfile_proplist(List) ->
false;
_File ->
%% merge with the default options
{ok, DefaultRotationDate} = rumUtil:parseRotateDateSpec(?DEFAULT_ROTATION_DATE),
{ok, DefaultRotationDate} = rumUtil:parseRotateSpec(?DEFAULT_ROTATION_DATE),
lists:keymerge(1, lists:sort(Res), lists:sort([
{level, validate_loglevel(?DEFAULT_LOG_LEVEL)}, {date, DefaultRotationDate},
{size, ?DEFAULT_ROTATION_SIZE}, {count, ?DEFAULT_ROTATION_COUNT},
@ -373,7 +373,7 @@ validate_logfile_proplist([{high_water_mark, HighWaterMark} | Tail], Acc) ->
throw({bad_config, "Invalid high water mark", HighWaterMark})
end;
validate_logfile_proplist([{date, Date} | Tail], Acc) ->
case rumUtil:parseRotateDateSpec(Date) of
case rumUtil:parseRotateSpec(Date) of
{ok, Spec} ->
validate_logfile_proplist(Tail, [{date, Spec} | Acc]);
{error, _} when Date == "" ->

+ 118
- 118
src/utils/rumUtil.erl Visa fil

@ -14,8 +14,10 @@
, nowMs/0
, msToBinStr/0
, msToBinStr/1
, parseRotateDateSpec/1
, parseRotateSpec/1
, calcNextRotate/1
, calcNextRotate/2
, calcNextRotateT/2
, validate_trace/1
, check_traces/4
, is_loggable/3
@ -204,159 +206,157 @@ i3b(Num) ->
integer_to_binary(Num)
end.
parseRotateHourSpec([], Res) ->
{ok, Res};
parseRotateHourSpec([$H, M1, M2], Res) ->
case list_to_integer([M1, M2]) of
X when X >= 0, X =< 59 ->
{ok, Res ++ [{minute, X}]};
_ ->
{error, invalid_date_spec}
end;
parseRotateHourSpec([$H, M], Res) when M >= $0, M =< $9 ->
{ok, Res ++ [{minute, M - $0}]};
parseRotateHourSpec(_, _) ->
%% last parse hour
parseRotateHourSpec([], DayOrMonth, Hour, Minute) ->
{DayOrMonth, Hour, Minute};
parseRotateHourSpec([$H, M1, M2], DayOrMonth, Hour, _Minute) when M1 >= $0, M1 =< $9, M2 >= $0, M2 =< $9 ->
Min = list_to_integer([M1, M2]),
?IIF(Hour >= 0 andalso Hour =< 59, {DayOrMonth, Hour, Min}, {error, invalid_date_spec});
parseRotateHourSpec([$H, M], DayOrMonth, Hour, _Minute) when M >= $0, M =< $9 ->
{DayOrMonth, Hour, M - $0};
parseRotateHourSpec(_, _DayOrMonth, _Hour, _Minute) ->
{error, invalid_date_spec}.
%% Default to 00:00:00 rotation
parseRotateDaySpec([], Res) ->
{ok, Res ++ [{hour, 0}]};
parseRotateDaySpec([$D, D1, D2 | T], Res) ->
case list_to_integer([D1, D2]) of
X when X >= 0, X =< 23 ->
parseRotateHourSpec(T, Res ++ [{hour, X}]);
_ ->
{error, invalid_date_spec}
end;
parseRotateDaySpec([$D, D | T], Res) when D >= $0, D =< $9 ->
parseRotateHourSpec(T, Res ++ [{hour, D - $0}]);
parseRotateDaySpec(X, Res) ->
parseRotateHourSpec(X, Res).
parseRotateDateSpec([$$, $W, W | T]) when W >= $0, W =< $6 ->
Week = W - $0,
parseRotateDaySpec(T, [{day, Week}]);
parseRotateDateSpec([$$, $M, L | T]) when L == $L; L == $l ->
%% last day in month.
parseRotateDaySpec(T, [{date, last}]);
parseRotateDateSpec([$$, $M, M1, M2 | [$D | _] = T]) ->
case list_to_integer([M1, M2]) of
X when X >= 1, X =< 31 ->
parseRotateDaySpec(T, [{date, X}]);
_ ->
{error, invalid_date_spec}
end;
parseRotateDateSpec([$$, $M, M | [$D | _] = T]) ->
parseRotateDaySpec(T, [{date, M - $0}]);
parseRotateDateSpec([$$, $M, M1, M2]) ->
case list_to_integer([M1, M2]) of
X when X >= 1, X =< 31 ->
{ok, [{date, X}, {hour, 0}]};
_ ->
{error, invalid_date_spec}
end;
parseRotateDateSpec([$$, $M, M]) ->
{ok, [{date, M - $0}, {hour, 0}]};
parseRotateDateSpec([$$ | X]) when X /= [] ->
parseRotateDaySpec(X, []);
parseRotateDateSpec(_) ->
%% second parse day Default to 00:00:00 rotation
parseRotateDaySpec([], DayOrMonth, Hour, Minute) ->
{DayOrMonth, Hour, Minute};
parseRotateDaySpec([$D, D1, D2 | T], DayOrMonth, _Hour, _Minute) when D1 > $0, D1 < $9, D2 > $0, D2 < $9 ->
Day = list_to_integer([D1, D1]),
?IIF(Day >= 0 andalso Day =< 23, parseRotateHourSpec(T, DayOrMonth, Day, 0), {error, invalid_date_spec});
parseRotateDaySpec([$D, D | T], DayOrMonth, _Hour, _Minute) when D >= $0, D =< $9 ->
parseRotateHourSpec(T, DayOrMonth, D - $0, 0);
parseRotateDaySpec(T, DayOrMonth, Hour, Minute) ->
parseRotateHourSpec(T, DayOrMonth, Hour, Minute).
%% first parse date or week
parseRotateDateSpec([$$, $W, W | T], _DayOrMonth, _Hour, _Minute) when W >= $1, W =< $7 ->
parseRotateDaySpec(T, {day, W - $0}, 0, 0);
parseRotateDateSpec([$$, $M, L | T], _DayOrMonth, _Hour, _Minute) when L == $L; L == $l ->
parseRotateDaySpec(T, {date, last}, 0, 0);
parseRotateDateSpec([$$, $M, M1, M2 | T], _DayOrMonth, _Hour, _Minute) when M1 >= $0, M1 =< $9, M2 >= $0, M2 =< $9 ->
Date = list_to_integer([M1, M2]),
?IIF(Date >= 1 andalso Date =< 31, parseRotateDaySpec(T, {date, Date}, 0, 0), {error, invalid_date_spec});
parseRotateDateSpec([$$, $M, M | T], _DayOrMonth, _Hour, _Minute) when M >= $1, M =< $9 ->
parseRotateDaySpec(T, {date, M - $0}, 0, 0);
parseRotateDateSpec([$$ | T], DayOrMonth, Hour, Minute) ->
parseRotateDaySpec(T, DayOrMonth, Hour, Minute);
parseRotateDateSpec(_, _DayOrMonth, _Hour, _Minute) ->
{error, invalid_date_spec}.
parseRotateSpec(Spec) ->
case parseRotateDateSpec(Spec, undefined, undefined, undefined) of
{error, _} = ErrRet ->
ErrRet;
{undefined, undefined, undefined} ->
{error, undefined};
STuple ->
{ok, STuple}
end.
calcNextRotate(Spec) ->
{Date, Time} = NowDataTime = erlang:localtime(),
calcNextRotate(Spec, Date, Time, NowDataTime).
calcNextRotate([], Data, Time, NowDataTime) ->
Later = {Data, Time},
calendar:datetime_to_gregorian_seconds(Later) - calendar:datetime_to_gregorian_seconds(NowDataTime);
calcNextRotate([OneSpec | Spec], NDate, NTime, NowDataTime) ->
{NYear, NMonth, NDay} = NDate, {NHour, NMinute, _} = NTime,
case OneSpec of
{minute, SMinute} ->
case NMinute < SMinute of
NextTime = calcNextRotate(Spec, Date, Time),
(rumTime:lDateTimeToSec(NextTime) - rumTime:lDateTimeToSec(NowDataTime)) * 1000.
calcNextRotate(Spec, NowDataTime) ->
{Date, Time} = NowDataTime,
NextTime = calcNextRotate(Spec, Date, Time),
(rumTime:lDateTimeToSec(NextTime) - rumTime:lDateTimeToSec(NowDataTime)) * 1000.
calcNextRotateT(Spec, NowDataTime) ->
{Date, Time} = NowDataTime,
calcNextRotate(Spec, Date, Time).
calcNextRotate(Spec, CurDate, CurTime) ->
{CurYear, CurMonth, CurDay} = CurDate, {CurHour, CurMinute, _} = CurTime,
case Spec of
{undefined, undefined, SMinute} ->
case CurMinute < SMinute of
true ->
%% rotation is this hour
calcNextRotate(Spec, NDate, {NHour, SMinute, 0}, NowDataTime);
{CurDate, {CurHour, SMinute, 0}};
_ ->
%% rotation is next hour
NexSec = rumTime:lDateTimeToSec({NDate, NTime}) + 3600,
{NNDate, NNTime} = rumTime:secToLDateTime(NexSec),
{NewHour, _, _} = NNTime,
calcNextRotate(Spec, NNDate, {NewHour, SMinute, 0}, NowDataTime)
NexSec = rumTime:lDateTimeToSec({CurDate, {CurHour, SMinute, 0}}) + 3600,
rumTime:secToLDateTime(NexSec)
end;
{hour, SHour} ->
case NHour < SHour of
{undefined, SHour, SMinute} ->
case CurTime < {SHour, SMinute, 0} of
true ->
%% rotation is today, sometime
calcNextRotate(Spec, NDate, {SHour, NMinute, 0}, NowDataTime);
%% rotation is this day
{CurDate, {SHour, SMinute, 0}};
_ ->
%% rotation is not today
NexSec = rumTime:lDateTimeToSec({NDate, NTime}) + 86400,
{NNDate, _NNTime} = rumTime:secToLDateTime(NexSec),
calcNextRotate(Spec, NNDate, {SHour, NMinute, 0}, NowDataTime)
%% rotation is next day
NexSec = rumTime:lDateTimeToSec({CurDate, {SHour, SMinute, 0}}) + 86400,
rumTime:secToLDateTime(NexSec)
end;
{day, SDay} ->
CurDay = rumTime:weekDay(NDate),
AdjustedDay = ?IIF(SDay == 0, 7, SDay),
{{day, SDay}, SHour, SMinute} ->
CurDay = rumTime:weekDay(CurDate),
if
AdjustedDay > CurDay ->
PlusDays = AdjustedDay - CurDay,
NexSec = rumTime:lDateTimeToSec({NDate, NTime}) + (86400 * PlusDays),
{NewNDate, NewNTime} = rumTime:secToLDateTime(NexSec),
calcNextRotate(Spec, NewNDate, NewNTime, NowDataTime);
AdjustedDay < CurDay ->
PlusDays = ((7 - CurDay) + AdjustedDay),
NexSec = rumTime:lDateTimeToSec({NDate, NTime}) + (86400 * PlusDays),
{NewNDate, NewNTime} = rumTime:secToLDateTime(NexSec),
calcNextRotate(Spec, NewNDate, NewNTime, NowDataTime);
CurDay < SDay ->
%% rotation is this week
DiffDays = SDay - CurDay,
NexSec = rumTime:lDateTimeToSec({CurDate, {SHour, SMinute, 0}}) + (86400 * DiffDays),
rumTime:secToLDateTime(NexSec);
CurDay > SDay ->
%% rotation is next week
DiffDays = ((7 - CurDay) + SDay),
NexSec = rumTime:lDateTimeToSec({CurDate, {SHour, SMinute, 0}}) + (86400 * DiffDays),
rumTime:secToLDateTime(NexSec);
true ->
case {NDate, NTime} > NowDataTime of
case CurTime < {SHour, SMinute, 0} of
true ->
calcNextRotate(Spec, NDate, NTime, NowDataTime);
%% rotation is this week
{CurDate, {SHour, SMinute, 0}};
_ ->
NexSec = rumTime:lDateTimeToSec({NDate, NTime}) + (86400 * 7),
{NewNDate, NewNTime} = rumTime:secToLDateTime(NexSec),
calcNextRotate(Spec, NewNDate, NewNTime, NowDataTime)
%% rotation is next week
NexSec = rumTime:lDateTimeToSec({CurDate, {SHour, SMinute, 0}}) + (86400 * 7),
rumTime:secToLDateTime(NexSec)
end
end;
{date, last} ->
LastDay = rumTime:monthDay(NYear, NMonth),
case LastDay == NDay of
{{date, last}, SHour, SMinute} ->
CurMonthDay = rumTime:monthDay(CurYear, CurMonth),
case CurMonthDay == CurDay of
true ->
case {NDate, NTime} > NowDataTime of
case CurTime < {SHour, SMinute, 0} of
true ->
calcNextRotate(Spec, NDate, NTime, NowDataTime);
%% rotation is this last month day
{CurDate, {SHour, SMinute, 0}};
_ ->
NexSec = rumTime:lDateTimeToSec({NDate, {23, 59, 59}}) + 1, %% 1
%% rotation is next last month day
NexSec = rumTime:lDateTimeToSec({CurDate, {23, 59, 59}}) + 1, %% 1
{NewNDate, _NewNTime} = rumTime:secToLDateTime(NexSec),
{NewNYear, NewNMonth, _} = NewNDate,
NewNDay = rumTime:monthDay(NewNYear, NewNMonth),
calcNextRotate(Spec, {NewNYear, NewNMonth, NewNDay}, NTime, NowDataTime)
NewMonthDay = rumTime:monthDay(NewNYear, NewNMonth),
{{NewNYear, NewNMonth, NewMonthDay}, {SHour, SMinute, 0}}
end;
_ ->
calcNextRotate(Spec, {NYear, NMonth, LastDay}, NTime, NowDataTime)
%% rotation is this last month day
{{CurYear, CurMonth, CurMonthDay}, {SHour, SMinute, 0}}
end;
{date, SDate} ->
{{date, SDate}, SHour, SMinute} ->
if
SDate < NDay ->
LastDay = rumTime:monthDay(NYear, NMonth),
NexSec = calendar:lDateTimeToSec({{NYear, NMonth, LastDay}, {23, 59, 59}}) + 1,
CurDay < SDate ->
%% rotation is this month day
{{CurYear, CurMonth, SDate}, {SHour, SMinute, 0}};
CurDay > SDate ->
%% rotation is next month day
LastDay = rumTime:monthDay(CurYear, CurMonth),
NexSec = rumTime:lDateTimeToSec({{CurYear, CurMonth, LastDay}, {23, 59, 59}}) + 1,
{NewNDate, _NewNTime} = rumTime:secToLDateTime(NexSec),
{NewNYear, NewNMonth, _} = NewNDate,
calcNextRotate(Spec, {NewNYear, NewNMonth, SDate}, NTime, NowDataTime);
SDate > NDay ->
calcNextRotate(Spec, {NYear, NMonth, SDate}, NTime, NowDataTime);
{{NewNYear, NewNMonth, SDate}, {SHour, SMinute, 0}};
true ->
case {NDate, NTime} > NowDataTime of
case CurTime < {SHour, SMinute, 0} of
true ->
calcNextRotate(Spec, NDate, NTime, NowDataTime);
%% rotation is this month day
{CurDate, {SHour, SMinute, 0}};
_ ->
LastDay = rumTime:monthDay(NYear, NMonth),
NexSec = calendar:lDateTimeToSec({{NYear, NMonth, LastDay}, {23, 59, 59}}) + 1,
%% rotation is next month day
LastDay = rumTime:monthDay(CurYear, CurMonth),
NexSec = rumTime:lDateTimeToSec({{CurYear, CurMonth, LastDay}, {23, 59, 59}}) + 1,
{NewNDate, _NewNTime} = rumTime:secToLDateTime(NexSec),
{NewNYear, NewNMonth, _} = NewNDate,
calcNextRotate(Spec, {NewNYear, NewNMonth, SDate}, NTime, NowDataTime)
{{NewNYear, NewNMonth, SDate}, {SHour, SMinute, 0}}
end
end
end.

Laddar…
Avbryt
Spara