1%%
2%% %CopyrightBegin%
3%%
4%% Copyright Ericsson AB 2008-2018. All Rights Reserved.
5%%
6%% Licensed under the Apache License, Version 2.0 (the "License");
7%% you may not use this file except in compliance with the License.
8%% You may obtain a copy of the License at
9%%
10%%     http://www.apache.org/licenses/LICENSE-2.0
11%%
12%% Unless required by applicable law or agreed to in writing, software
13%% distributed under the License is distributed on an "AS IS" BASIS,
14%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15%% See the License for the specific language governing permissions and
16%% limitations under the License.
17%%
18%% %CopyrightEnd%
19%%
20%%
21-module(tftp_logger).
22
23%%-------------------------------------------------------------------
24%% Interface
25%%-------------------------------------------------------------------
26
27%% public functions
28-export([
29	 error_msg/2,
30	 warning_msg/2,
31	 info_msg/2
32	]).
33
34-export([behaviour_info/1]).
35
36behaviour_info(callbacks) ->
37    [{error_msg, 2}, {warning_msg, 2}, {info_msg, 2}];
38behaviour_info(_) ->
39    undefined.
40
41%%-------------------------------------------------------------------
42%% error_msg(Format, Data) -> ok | exit(Reason)
43%%
44%% Format = string()
45%% Data = [term()]
46%% Reason = term()
47%%
48%% Log an error message
49%%-------------------------------------------------------------------
50
51error_msg(Format, Data) ->
52    {Format2, Data2} = add_timestamp(Format, Data),
53    error_logger:error_msg(Format2, Data2).
54
55%%-------------------------------------------------------------------
56%% warning_msg(Format, Data) -> ok | exit(Reason)
57%%
58%% Format = string()
59%% Data = [term()]
60%% Reason = term()
61%%
62%% Log a warning message
63%%-------------------------------------------------------------------
64
65warning_msg(Format, Data) ->
66    {Format2, Data2} = add_timestamp(Format, Data),
67    error_logger:warning_msg(Format2, Data2).
68
69%%-------------------------------------------------------------------
70%% info_msg(Format, Data) -> ok | exit(Reason)
71%%
72%% Format = string()
73%% Data = [term()]
74%% Reason = term()
75%%
76%% Log an info message
77%%-------------------------------------------------------------------
78
79info_msg(Format, Data) ->
80    {Format2, Data2} = add_timestamp(Format, Data),
81    io:format(Format2, Data2).
82
83%%-------------------------------------------------------------------
84%% Add timestamp to log message
85%%-------------------------------------------------------------------
86
87add_timestamp(Format, Data) ->
88    Time = erlang:timestamp(),
89    {{_Y, _Mo, _D}, {H, Mi, S}} = calendar:now_to_universal_time(Time),
90    %% {"~p-~s-~sT~s:~s:~sZ,~6.6.0w tftp: " ++ Format ++ "\n",
91    %%  [Y, t(Mo), t(D), t(H), t(Mi), t(S), MicroSecs | Data]}.
92    {"~s:~s:~s tftp: " ++ Format, [t(H), t(Mi), t(S) | Data]}.
93
94%% Convert 9 to "09".
95t(Int) ->
96    case integer_to_list(Int) of
97	[Single] -> [$0, Single];
98        Multi    -> Multi
99    end.
100