First part of onap rename
[appc.git] / appc-config / appc-config-adaptor / provider / src / main / java / org / openecomp / 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.*;
24 import java.text.DateFormat;
25 import java.text.SimpleDateFormat;
26 import java.util.Date;
27
28
29 public class DebugLog
30 {
31   private static String fileName = "/tmp/rt.log";
32
33   public static void main (String args[])
34   {
35     DebugLog debugLog = new DebugLog();
36     debugLog.printAriDebug("DebugLog", "The Message");
37   }
38
39   public static void printAriDebug(String fn, String messg)
40   {
41     String logMessg = getDateTime() +" " +fn +" " + messg;
42     appendToFile(logMessg +"\n");
43
44   }
45
46   public static void printRTAriDebug(String fn, String messg)
47   {
48     // System.out.println (getDateTime() +" " +fn +" " + messg);
49     String logMessg = getDateTime() +" " +fn +" " + messg;
50     appendToFile(logMessg +"\n");
51   }
52   public static String getDateTime()
53   {
54     DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
55     // DateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");
56     Date date = new Date();
57     return dateFormat.format(date);
58   }
59
60   public static void appendToFile (String dataToWrite)
61   {
62     String fn = "DebugLog.appendToFile";
63     try
64     {
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       {
70         BufferedWriter out = new BufferedWriter(new FileWriter(fileName, true));
71         out.write(dataToWrite);
72         out.close();
73       }
74     }
75     catch (IOException e)
76     {
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   {
85     String fn = "DebugLog.outputStackTrace";
86     StringWriter sw = new StringWriter();
87     e.printStackTrace(new PrintWriter(sw));
88     String stackTrace = sw.toString();
89     DebugLog.printRTAriDebug(fn, "Stack trace::: "+stackTrace);
90   }
91
92   public static String getStackTraceString(Exception e)
93   {
94     String fn = "DebugLog.outputStackTrace";
95     StringWriter sw = new StringWriter();
96     e.printStackTrace(new PrintWriter(sw));
97     String stackTrace = sw.toString();
98     return(stackTrace);
99   }
100
101 }
102