Address more sonars in apex-pdp
[policy/apex-pdp.git] / tools / tools-common / src / main / java / org / onap / policy / apex / tools / common / Console.java
index 5713331..59c3c0e 100644 (file)
@@ -1,6 +1,8 @@
 /*-
  * ============LICENSE_START=======================================================
  *  Copyright (C) 2016-2018 Ericsson. All rights reserved.
+ *  Modifications Copyright (C) 2020 Nordix Foundation.
+ *  Modifications Copyright (C) 2021 AT&T Intellectual Property. All rights reserved.
  * ================================================================================
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
 
 package org.onap.policy.apex.tools.common;
 
+import java.io.PrintStream;
 import java.util.ArrayList;
 import java.util.List;
-
 import org.apache.commons.lang3.StringUtils;
-import org.apache.commons.text.StrBuilder;
+import org.slf4j.ext.XLoggerFactory;
 import org.slf4j.helpers.MessageFormatter;
 
 /**
@@ -35,7 +37,6 @@ import org.slf4j.helpers.MessageFormatter;
  * @author Sven van der Meer (sven.van.der.meer@ericsson.com)
  */
 public final class Console {
-
     /** The console as static object. */
     public static final Console CONSOLE = new Console();
 
@@ -72,6 +73,9 @@ public final class Console {
     /** Configuration for a collecting warning messages. */
     public static final int CONFIG_COLLECT_WARNINGS = 0b0010;
 
+    // Input and output streams
+    private static final PrintStream ERR_STREAM = System.err;
+
     /** The setting for message types, set using type flags. */
     private int types;
 
@@ -149,16 +153,6 @@ public final class Console {
         types = type;
     }
 
-    /**
-     * Configures the console. Use the configuration flags in combination for the required configuration. For instance,
-     * to collect errors and warnings use <code>CONFIG_COLLECT_ERRORS | CONFIG_COLLECT_WARNINGS</code>.
-     *
-     * @param config the new configuration, overwrites the current configuration, 0 deactivates all settings
-     */
-    public void configure(final int config) {
-        this.configuration = config;
-    }
-
     /**
      * Sets the type to the given types, effectively deactivating all other types.
      *
@@ -171,6 +165,16 @@ public final class Console {
         }
     }
 
+    /**
+     * Configures the console. Use the configuration flags in combination for the required configuration. For instance,
+     * to collect errors and warnings use <code>CONFIG_COLLECT_ERRORS | CONFIG_COLLECT_WARNINGS</code>.
+     *
+     * @param config the new configuration, overwrites the current configuration, 0 deactivates all settings
+     */
+    public void configure(final int config) {
+        this.configuration = config;
+    }
+
     /**
      * Prints an error message with message and objects if {@link #TYPE_ERROR} is set; and increases the error count.
      * Errors are collected (if configuration is set) and the error counter is increased regardless of the console error
@@ -185,7 +189,7 @@ public final class Console {
             return;
         }
 
-        final StrBuilder err = new StrBuilder();
+        final var err = new StringBuilder();
         if (appName != null) {
             err.append(this.getAppName()).append(": ");
         }
@@ -193,10 +197,10 @@ public final class Console {
         err.append(MessageFormatter.arrayFormat(message, objects).getMessage());
 
         if ((types & TYPE_ERROR) == TYPE_ERROR) {
-            System.err.println(err.build());
+            ERR_STREAM.println(err.toString());
         }
         if ((configuration & CONFIG_COLLECT_ERRORS) == CONFIG_COLLECT_ERRORS) {
-            errors.add(err.build());
+            errors.add(err.toString());
         }
     }
 
@@ -214,7 +218,7 @@ public final class Console {
             return;
         }
 
-        final StrBuilder warn = new StrBuilder();
+        final var warn = new StringBuilder();
         if (appName != null) {
             warn.append(this.getAppName()).append(": ");
         }
@@ -222,10 +226,10 @@ public final class Console {
         warn.append(MessageFormatter.arrayFormat(message, objects).getMessage());
 
         if ((types & TYPE_WARNING) == TYPE_WARNING) {
-            System.err.println(warn.build());
+            ERR_STREAM.println(warn.toString());
         }
         if ((configuration & CONFIG_COLLECT_WARNINGS) == CONFIG_COLLECT_WARNINGS) {
-            warnings.add(warn.build());
+            warnings.add(warn.toString());
         }
     }
 
@@ -243,9 +247,9 @@ public final class Console {
 
         if ((types & TYPE_INFO) == TYPE_INFO) {
             if (appName != null) {
-                System.err.print(appName + ": ");
+                ERR_STREAM.print(appName + ": ");
             }
-            System.err.println(MessageFormatter.arrayFormat(message, objects).getMessage());
+            ERR_STREAM.println(MessageFormatter.arrayFormat(message, objects).getMessage());
         }
     }
 
@@ -263,10 +267,10 @@ public final class Console {
 
         if ((types & TYPE_PROGRESS) == TYPE_PROGRESS) {
             if (appName != null) {
-                System.err.print(appName + ": ");
+                ERR_STREAM.print(appName + ": ");
             }
-            System.err.print("progress: ");
-            System.err.println(MessageFormatter.arrayFormat(message, objects).getMessage());
+            ERR_STREAM.print("progress: ");
+            ERR_STREAM.println(MessageFormatter.arrayFormat(message, objects).getMessage());
         }
     }
 
@@ -284,10 +288,10 @@ public final class Console {
 
         if ((types & TYPE_DEBUG) == TYPE_DEBUG) {
             if (appName != null) {
-                System.err.print(appName + ": ");
+                ERR_STREAM.print(appName + ": ");
             }
-            System.err.print("debug: ");
-            System.err.println(MessageFormatter.arrayFormat(message, objects).getMessage());
+            ERR_STREAM.print("debug: ");
+            ERR_STREAM.println(MessageFormatter.arrayFormat(message, objects).getMessage());
         }
     }
 
@@ -305,10 +309,10 @@ public final class Console {
 
         if ((types & TYPE_TRACE) == TYPE_TRACE) {
             if (appName != null) {
-                System.err.print(appName + ": ");
+                ERR_STREAM.print(appName + ": ");
             }
-            System.err.print("trace: ");
-            System.err.println(MessageFormatter.arrayFormat(message, objects).getMessage());
+            ERR_STREAM.print("trace: ");
+            ERR_STREAM.println(MessageFormatter.arrayFormat(message, objects).getMessage());
         }
     }
 
@@ -324,14 +328,14 @@ public final class Console {
 
         if ((types & TYPE_STACKTRACE) == TYPE_STACKTRACE) {
             if (appName != null) {
-                System.err.print(appName + ": ");
+                ERR_STREAM.print(appName + ": ");
             }
-            System.err.println("exception stack trace: ");
-            System.err.println(" - message: " + exception.getMessage());
+            ERR_STREAM.println(" exception message: " + exception.getMessage());
             if (exception.getCause() != null) {
-                System.err.println(" - cause: " + exception.getCause());
+                ERR_STREAM.println(" exception cause: " + exception.getCause());
             }
-            exception.printStackTrace();
+            ERR_STREAM.println("for exception stack trace, please refer logs.");
+            XLoggerFactory.getXLogger(Console.class).error("stacktrace", exception);
         }
     }