2 * ============LICENSE_START=======================================================
\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
15 * http://www.apache.org/licenses/LICENSE-2.0
\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
23 * ============LICENSE_END=========================================================
\r
26 package org.onap.appc.ccadaptor;
\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
39 public class DebugLog {
\r
41 private static String fileName = "/tmp/rt.log";
\r
43 public static void main(String args[]) {
\r
44 DebugLog debugLog = new DebugLog();
\r
45 debugLog.printAriDebug("DebugLog", "The Message");
\r
48 public static void printAriDebug(String fn, String messg) {
\r
49 String logMessg = getDateTime() + " " + fn + " " + messg;
\r
50 appendToFile(logMessg + "\n");
\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
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
67 public static void appendToFile(String dataToWrite) {
\r
68 String fn = "DebugLog.appendToFile";
\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
78 } catch (IOException e) {
\r
79 DebugLog.printRTAriDebug(fn, "writeToFile() exception: " + e);
\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
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