Remove Tabs, per Jococo
[aaf/authz.git] / misc / env / src / main / java / org / onap / aaf / misc / env / LogTarget.java
1 /**\r
2  * ============LICENSE_START====================================================\r
3  * org.onap.aaf\r
4  * ===========================================================================\r
5  * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved.\r
6  * ===========================================================================\r
7  * Licensed under the Apache License, Version 2.0 (the "License");\r
8  * you may not use this file except in compliance with the License.\r
9  * You may obtain a copy of the License at\r
10  * \r
11  *      http://www.apache.org/licenses/LICENSE-2.0\r
12  * \r
13  * Unless required by applicable law or agreed to in writing, software\r
14  * distributed under the License is distributed on an "AS IS" BASIS,\r
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
16  * See the License for the specific language governing permissions and\r
17  * limitations under the License.\r
18  * ============LICENSE_END====================================================\r
19  *\r
20  */\r
21 \r
22 package org.onap.aaf.misc.env;\r
23 \r
24 import java.io.PrintStream;\r
25 import java.util.Date;\r
26 \r
27 import org.onap.aaf.misc.env.util.Chrono;\r
28 \r
29 /**\r
30  * LogTarget is the interface with which to assign any kind of Logging Implementations.\r
31  * \r
32  * Implement for any Logging Library of your choice, and for any logging string Format desired.\r
33  * \r
34  * Included are several Static Implementations for various uses:\r
35  *      NULL: Does nothing with Logging Messages\r
36  *   SYSOUT: Writes messages in general form to System Out\r
37  *   SYSERR: Writes messages in general form to System Err\r
38  *   \r
39  * @author Jonathan\r
40  *\r
41  */\r
42 public interface LogTarget {\r
43     public abstract void log(Object... msgs);\r
44     public abstract void log(Throwable e, Object ... msgs);\r
45     public abstract boolean isLoggable();\r
46     public abstract void printf(String fmt, Object ... vars);\r
47 \r
48     // A Convenient LogTarget to insert when a NO-OP is desired.\r
49     public static final LogTarget NULL = new LogTarget() {\r
50         public void log(Object ... msgs) {\r
51         }\r
52 \r
53         public void log(Throwable t, Object ... msgs) {\r
54         }\r
55 \r
56         public boolean isLoggable() {\r
57             return false;\r
58         }\r
59 \r
60         @Override\r
61         public void printf(String fmt, Object ... vars) {\r
62         }\r
63     };\r
64 \r
65     // A Convenient LogTarget to write to the Console\r
66     public static final LogTarget SYSOUT = new StreamLogTarget(System.out,false);\r
67     \r
68     // A Convenient LogTarget to write to the Console\r
69     public static final LogTarget SYSERR = new StreamLogTarget(System.err,false);\r
70     \r
71     public static class StreamLogTarget implements LogTarget {\r
72         private final PrintStream out;\r
73         private final boolean closeMe;\r
74 \r
75         public StreamLogTarget(PrintStream ps) {\r
76             this(ps,true);\r
77         }\r
78 \r
79         /* Do NOT close SYSTEM ERR or OUT*/\r
80         protected StreamLogTarget(PrintStream ps, boolean shouldClose) {\r
81             out = ps;\r
82             closeMe = shouldClose;\r
83         }\r
84         public void log(Object ... msgs) {\r
85             out.print(Chrono.dateFmt.format(new Date()));\r
86             out.print(": ");\r
87             for (Object str : msgs) {\r
88                 if (str!=null) {\r
89                     out.print(str.toString());\r
90                     out.print(' ');\r
91                 } else {\r
92                     out.print("null ");\r
93                 }\r
94             }\r
95             out.println();\r
96         }\r
97 \r
98         public void log(Throwable t, Object ... msgs) {\r
99             out.print(Chrono.dateFmt.format(new Date()));\r
100             out.print(": ");\r
101             for (Object str : msgs) {\r
102                 out.print(str.toString());\r
103                 out.print(' ');\r
104             }\r
105             out.println();\r
106             t.printStackTrace(out);\r
107             out.println();\r
108         }\r
109 \r
110         public boolean isLoggable() {\r
111             return true;\r
112         }\r
113 \r
114         @Override\r
115         public void printf(String fmt, Object ... vars) {\r
116             log(String.format(fmt,vars));\r
117         }\r
118         \r
119         public void close() {\r
120             if(closeMe) {\r
121                 out.close();\r
122             }\r
123         }\r
124 \r
125     }\r
126 \r
127 };