Changed to unmaintained
[appc.git] / appc-config / appc-config-adaptor / provider / src / main / java / org / onap / appc / ccadaptor / DebugLog.java
1 /*-\r
2  * ============LICENSE_START=======================================================\r
3  * ONAP : APPC\r
4  * ================================================================================\r
5  * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.\r
6  * ================================================================================\r
7  * Copyright (C) 2017 Amdocs\r
8  * =============================================================================\r
9  * Modifications Copyright (C) 2019 IBM\r
10  * =============================================================================\r
11  * Licensed under the Apache License, Version 2.0 (the "License");\r
12  * you may not use this file except in compliance with the License.\r
13  * You may obtain a copy of the License at\r
14  *\r
15  *      http://www.apache.org/licenses/LICENSE-2.0\r
16  *\r
17  * Unless required by applicable law or agreed to in writing, software\r
18  * distributed under the License is distributed on an "AS IS" BASIS,\r
19  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
20  * See the License for the specific language governing permissions and\r
21  * limitations under the License.\r
22  *\r
23  * ============LICENSE_END=========================================================\r
24  */\r
25 \r
26 package org.onap.appc.ccadaptor;\r
27 \r
28 import java.io.BufferedWriter;\r
29 import java.io.File;\r
30 import java.io.FileWriter;\r
31 import java.io.IOException;\r
32 import java.io.PrintWriter;\r
33 import java.io.StringWriter;\r
34 import java.text.DateFormat;\r
35 import java.text.SimpleDateFormat;\r
36 import java.util.Date;\r
37 \r
38 \r
39 public class DebugLog {\r
40 \r
41     private static String fileName = "/tmp/rt.log";\r
42 \r
43     public static void main(String args[]) {\r
44         DebugLog debugLog = new DebugLog();\r
45         debugLog.printAriDebug("DebugLog", "The Message");\r
46     }\r
47 \r
48     public static void printAriDebug(String fn, String messg) {\r
49         String logMessg = getDateTime() + " " + fn + " " + messg;\r
50         appendToFile(logMessg + "\n");\r
51 \r
52     }\r
53 \r
54     public static void printRTAriDebug(String fn, String messg) {\r
55         // System.out.println (getDateTime() +" " +fn +" " + messg);\r
56         String logMessg = getDateTime() + " " + fn + " " + messg;\r
57         appendToFile(logMessg + "\n");\r
58     }\r
59 \r
60     public static String getDateTime() {\r
61         DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");\r
62         // DateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");\r
63         Date date = new Date();\r
64         return dateFormat.format(date);\r
65     }\r
66 \r
67     public static void appendToFile(String dataToWrite) {\r
68         String fn = "DebugLog.appendToFile";\r
69         try {\r
70             // First check to see if a file 'fileName' exist, if it does\r
71             // write to it. If it does not exist, don't write to it.\r
72             File tmpFile = new File(fileName);\r
73             if (tmpFile.exists()) {\r
74                 try(BufferedWriter out = new BufferedWriter(new FileWriter(fileName, true))) {\r
75                     out.write(dataToWrite);\r
76                 }\r
77             }\r
78         } catch (IOException e) {\r
79             DebugLog.printRTAriDebug(fn, "writeToFile() exception: " + e);\r
80         }\r
81     }\r
82 \r
83     public void outputStackTrace(Exception e) {\r
84         String fn = "DebugLog.outputStackTrace";\r
85         StringWriter sw = new StringWriter();\r
86         e.printStackTrace(new PrintWriter(sw));\r
87         String stackTrace = sw.toString();\r
88         DebugLog.printRTAriDebug(fn, "Stack trace::: " + stackTrace);\r
89     }\r
90 \r
91     public static String getStackTraceString(Exception e) {\r
92         String fn = "DebugLog.outputStackTrace";\r
93         StringWriter sw = new StringWriter();\r
94         e.printStackTrace(new PrintWriter(sw));\r
95         String stackTrace = sw.toString();\r
96         return (stackTrace);\r
97     }\r
98 \r
99 }\r
100 \r