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