1
%% ``The contents of this file are subject to the Erlang Public License,
2
%% Version 1.1, (the "License"); you may not use this file except in
3
%% compliance with the License. You should have received a copy of the
4
%% Erlang Public License along with this software. If not, it can be
5
%% retrieved via the world wide web at http://www.erlang.org/.
7
%% Software distributed under the License is distributed on an "AS IS"
8
%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
9
%% the License for the specific language governing rights and limitations
12
%% The Initial Developer of the Original Code is Ericsson Utvecklings AB.
13
%% Portions created by Ericsson are Copyright 1999, Ericsson Utvecklings
14
%% AB. All Rights Reserved.''
18
-module(snmp_verbosity).
20
-include_lib("stdlib/include/erl_compile.hrl").
22
-export([print/4,print/5,printc/4,validate/1]).
24
-export([process_args/2]).
26
print(silence,_Severity,_Format,_Arguments) ->
28
print(Verbosity,Severity,Format,Arguments) ->
29
print1(printable(Verbosity,Severity),Format,Arguments).
32
print(silence,_Severity,_Module,_Format,_Arguments) ->
34
print(Verbosity,Severity,Module,Format,Arguments) ->
35
print1(printable(Verbosity,Severity),Module,Format,Arguments).
38
printc(silence,_Severity,_Format,_Arguments) ->
40
printc(Verbosity,Severity,Format,Arguments) ->
41
print2(printable(Verbosity,Severity),Format,Arguments).
44
print1(false,_Format,_Arguments) -> ok;
45
print1(Verbosity,Format,Arguments) ->
46
V = image_of_verbosity(Verbosity),
47
S = image_of_sname(get(sname)),
48
A = process_args(Arguments, []),
49
(catch io:format("*** [~s] SNMP ~s ~s *** ~n"
50
" " ++ Format ++ "~n",
51
[timestamp(), S, V | A])).
53
print1(false,_Module,_Format,_Arguments) -> ok;
54
print1(Verbosity,Module,Format,Arguments) ->
55
V = image_of_verbosity(Verbosity),
56
S = image_of_sname(get(sname)),
57
A = process_args(Arguments, []),
58
(catch io:format("*** [~s] SNMP ~s ~s ~s *** ~n"
59
" " ++ Format ++ "~n",
60
[timestamp(), S, Module, V | A])).
63
print2(false,_Format,_Arguments) -> ok;
64
print2(_Verbosity,Format,Arguments) ->
65
A = process_args(Arguments, []),
66
(catch io:format(Format ++ "~n",A)).
70
format_timestamp(now()).
72
format_timestamp({_N1, _N2, N3} = Now) ->
73
{Date, Time} = calendar:now_to_datetime(Now),
75
{Hour,Min,Sec} = Time,
77
io_lib:format("~.4w:~.2.0w:~.2.0w ~.2.0w:~.2.0w:~.2.0w 4~w",
78
[YYYY,MM,DD,Hour,Min,Sec,round(N3/1000)]),
79
lists:flatten(FormatDate).
81
process_args([], Acc) ->
83
process_args([{vapply, {M,F,A}}|T], Acc) when atom(M), atom(F), list(A) ->
84
process_args(T, [(catch apply(M,F,A))|Acc]);
85
process_args([H|T], Acc) ->
86
process_args(T, [H|Acc]).
89
%% printable(Verbosity,Severity)
90
printable(info,info) -> info;
91
printable(log,info) -> info;
92
printable(log,log) -> log;
93
printable(debug,info) -> info;
94
printable(debug,log) -> log;
95
printable(debug,debug) -> debug;
96
printable(trace,V) -> V;
97
printable(_Verb,_Sev) -> false.
100
image_of_verbosity(info) -> "INFO";
101
image_of_verbosity(log) -> "LOG";
102
image_of_verbosity(debug) -> "DEBUG";
103
image_of_verbosity(trace) -> "TRACE";
104
image_of_verbosity(_) -> "".
107
image_of_sname(ma) -> "MASTER-AGENT";
108
image_of_sname(maw) -> io_lib:format("MASTER-AGENT-worker(~p)",[self()]);
109
image_of_sname(mais) -> io_lib:format("MASTER-AGENT-inform_sender(~p)",
111
image_of_sname(mats) -> io_lib:format("MASTER-AGENT-trap_sender(~p)",
113
image_of_sname(maph) -> io_lib:format("MASTER-AGENT-pdu_handler(~p)",
115
image_of_sname(sa) -> "SUB-AGENT";
116
image_of_sname(saw) -> io_lib:format("SUB-AGENT-worker(~p)",[self()]);
117
image_of_sname(sais) -> io_lib:format("SUB-AGENT-inform_sender(~p)",
119
image_of_sname(sats) -> io_lib:format("SUB-AGENT-trap_sender(~p)",
121
image_of_sname(saph) -> io_lib:format("SUB-AGENT-pdu_handler(~p)",
123
image_of_sname(nif) -> "A-NET-IF";
124
image_of_sname(ldb) -> "A-LOCAL-DB";
125
image_of_sname(ns) -> "A-NOTE-STORE";
126
image_of_sname(ss) -> "A-SYMBOLIC-STORE";
127
image_of_sname(asup) -> "A-SUPERVISOR";
128
image_of_sname(ms) -> "A-MIB-SERVER";
129
image_of_sname(conf) -> "A-CONF";
131
image_of_sname(abs) -> "A-BKP";
132
image_of_sname(albs) -> "A-LDB-BKP";
133
image_of_sname(ambs) -> "A-MS-BKP";
134
image_of_sname(asbs) -> "A-SS-BKP";
135
image_of_sname(mcbs) -> "M-C-BKP";
137
image_of_sname(mse) -> "M-SERVER";
138
image_of_sname(msew) -> io_lib:format("M-SERVER-worker(~p)", [self()]);
139
image_of_sname(mns) -> "M-NOTE-STORE";
140
image_of_sname(mnif) -> "M-NET-IF";
141
image_of_sname(mconf) -> "M-CONF";
143
image_of_sname(mgr) -> "MGR";
144
image_of_sname(mgr_misc) -> "MGR_MISC";
146
image_of_sname(undefined) -> "";
147
image_of_sname(V) -> lists:flatten(io_lib:format("~p",[V])).
150
validate(info) -> info;
151
validate(log) -> log;
152
validate(debug) -> debug;
153
validate(trace) -> trace;
154
validate(_) -> silence.