Skip to content

Commit 12c9f01

Browse files
committed
eliminate io_lib:format overhead in yaws_log:fmtnow
1 parent 4d4fad6 commit 12c9f01

File tree

1 file changed

+18
-8
lines changed

1 file changed

+18
-8
lines changed

src/yaws_log.erl

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -383,7 +383,7 @@ handle_info(secs3, State) ->
383383

384384
handle_info({notify,{yaws_hupped,_}}, State) ->
385385
handle_info(minute10,State);
386-
%% once every 10 minute, check log sizes
386+
%% once every 10 minutes, check log sizes
387387
handle_info(minute10, State) ->
388388
L = lists:map(
389389
fun(AL) ->
@@ -437,8 +437,8 @@ wrap(AL, State) ->
437437
false ->
438438
AL;
439439
enoent ->
440-
%% Logfile disapeared,
441-
error_logger:format("Logfile ~p disapeared - we reopen it",
440+
%% Logfile disappeared,
441+
error_logger:format("Logfile ~p disappeared - we reopen it",
442442
[AL#alog.filename]),
443443
file:close(AL#alog.fd),
444444
{ok, Fd2} = file:open(AL#alog.filename, [write, raw]),
@@ -476,9 +476,9 @@ fmt_ip(HostName) ->
476476

477477

478478
fmtnow() ->
479-
{{Year, Month, Date}, {Hour, Min, Sec}} = calendar:local_time(),
480-
io_lib:format("[~2..0w/~s/~4..0w:~2..0w:~2..0w:~2..0w ~s]",
481-
[Date,yaws:month(Month),Year, Hour, Min, Sec, zone()]).
479+
{{Year, Month, Day}, {Hour, Min, Sec}} = calendar:local_time(),
480+
[fill_zero(Day,2),"/",yaws:month(Month),"/",integer_to_list(Year),":",
481+
fill_zero(Hour,2),":",fill_zero(Min,2),":",fill_zero(Sec,2)," ",zone()].
482482

483483
zone() ->
484484
Time = erlang:universaltime(),
@@ -490,6 +490,16 @@ zone() ->
490490
%% Ugly reformatting code to get times like +0000 and -1300
491491

492492
zone(Val) when Val < 0 ->
493-
io_lib:format("-~4..0w", [trunc(abs(Val))]);
493+
[$-|fill_zero(trunc(abs(Val)), 4)];
494494
zone(Val) when Val >= 0 ->
495-
io_lib:format("+~4..0w", [trunc(abs(Val))]).
495+
[$+|fill_zero(trunc(Val), 4)].
496+
497+
fill_zero(N, Width) ->
498+
left_fill(N, Width, $0).
499+
500+
left_fill(N, Width, Fill) when is_integer(N) ->
501+
left_fill(integer_to_list(N), Width, Fill);
502+
left_fill(N, Width, _Fill) when length(N) >= Width ->
503+
N;
504+
left_fill(N, Width, Fill) ->
505+
left_fill([Fill|N], Width, Fill).

0 commit comments

Comments
 (0)