18fcc8413580317e13eb8e777b7c781712e024e7
[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  * Licensed under the Apache License, Version 2.0 (the "License");\r
10  * you may not use this file except in compliance with the License.\r
11  * You may obtain a copy of the License at\r
12  *\r
13  *      http://www.apache.org/licenses/LICENSE-2.0\r
14  *\r
15  * Unless required by applicable law or agreed to in writing, software\r
16  * distributed under the License is distributed on an "AS IS" BASIS,\r
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
18  * See the License for the specific language governing permissions and\r
19  * limitations under the License.\r
20  *\r
21  * ============LICENSE_END=========================================================\r
22  */\r
23 \r
24 package org.onap.appc.ccadaptor;\r
25 \r
26 import java.io.BufferedWriter;\r
27 import java.io.File;\r
28 import java.io.FileWriter;\r
29 import java.io.IOException;\r
30 import java.io.PrintWriter;\r
31 import java.io.StringWriter;\r
32 import java.text.DateFormat;\r
33 import java.text.SimpleDateFormat;\r
34 import java.util.Date;\r
35 \r
36 \r
37 public class DebugLog {\r
38 \r
39     private static String fileName = "/tmp/rt.log";\r
40 \r
41     public static void main(String args[]) {\r
42         DebugLog debugLog = new DebugLog();\r
43         debugLog.printAriDebug("DebugLog", "The Message");\r
44     }\r
45 \r
46     public static void printAriDebug(String fn, String messg) {\r
47         String logMessg = getDateTime() + " " + fn + " " + messg;\r
48         appendToFile(logMessg + "\n");\r
49 \r
50     }\r
51 \r
52     public static void printRTAriDebug(String fn, String messg) {\r
53         // System.out.println (getDateTime() +" " +fn +" " + messg);\r
54         String logMessg = getDateTime() + " " + fn + " " + messg;\r
55         appendToFile(logMessg + "\n");\r
56     }\r
57 \r
58     public static String getDateTime() {\r
59         DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");\r
60         // DateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");\r
61         Date date = new Date();\r
62         return dateFormat.format(date);\r
63     }\r
64 \r
65     public static void appendToFile(String dataToWrite) {\r
66         String fn = "DebugLog.appendToFile";\r
67         try {\r
68             // First check to see if a file 'fileName' exist, if it does\r
69             // write to it. If it does not exist, don't write to it.\r
70             File tmpFile = new File(fileName);\r
71             if (tmpFile.exists()) {\r
72                 try(BufferedWriter out = new BufferedWriter(new FileWriter(fileName, true))) {\r
73                     out.write(dataToWrite);\r
74                 }\r
75             }\r
76         } catch (IOException e) {\r
77             DebugLog.printRTAriDebug(fn, "writeToFile() exception: " + e);\r
78             //System.err.println("writeToFile() exception: " + e);\r
79             e.printStackTrace();\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