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