From f1cb4443aede6d4657bdc3396c8914d3a9f4fa93 Mon Sep 17 00:00:00 2001
From: Alejandro Acuña <alejandro.acuna@aluvisagrupo.com>
Date: Mon, 11 Nov 2024 13:09:35 +0000
Subject: [PATCH] bck

---
 libraries/server/src/art/servers/controller/ControllerListenerDebug.java |  174 ++++++++++++++++++++++++++++++++++++++++++++++------------
 1 files changed, 138 insertions(+), 36 deletions(-)

diff --git a/libraries/server/src/art/servers/controller/ControllerListenerDebug.java b/libraries/server/src/art/servers/controller/ControllerListenerDebug.java
index 8c632a3..bbf594b 100644
--- a/libraries/server/src/art/servers/controller/ControllerListenerDebug.java
+++ b/libraries/server/src/art/servers/controller/ControllerListenerDebug.java
@@ -1,11 +1,13 @@
 package art.servers.controller;
 
 import art.library.model.devices.application.ApplicationRealtime;
+import art.library.model.transactions.traces.Note;
 import art.library.utils.synchro.Mutex;
 import art.library.model.transactions.traces.Trace;
 import art.library.model.transactions.traces.TracePersistance;
 import art.servers.Shared;
 import art.servers.configuration.ConfigurationListenerDEBUG;
+import java.io.PrintStream;
 import java.net.ServerSocket;
 import java.net.Socket;
 import java.text.SimpleDateFormat;
@@ -15,25 +17,45 @@
 
 public class ControllerListenerDebug extends Thread
 {
-    public static final int TYPE_INFORMATION = 1;
-    public static final int TYPE_WARNING = 2;
-    public static final int TYPE_ERROR = 3;
+    private static String COLOR_BLACK = "\u001B[0;30;40m";
+    private static String COLOR_RED = "\u001B[0;31;40m";
+    private static String COLOR_GREEN = "\u001B[0;32;40m";
+    private static String COLOR_YELLOW = "\u001B[0;33;40m";
+    private static String COLOR_BLUE = "\u001B[0;34;40m";
+    private static String COLOR_MAGENTA = "\u001B[0;35;40m";
+    private static String COLOR_CYAN = "\u001B[0;36;40m";
+    private static String COLOR_WHITE = "\u001B[0;37;40m";
+    private static String COLOR_LIGHT_GRAY = "\u001B[0;33;37m";
+    private static String COLOR_DARK_GRAY = "\u001B[0;33;90m";
+    private static String COLOR_ORANGE = "\u001B[0;33;93m";
     
     
     private String name = null;
     private Mutex mutexConnection = new Mutex();
     private ServerSocket serverSocket = null;
-    private List<DebugConnection> lconnection = new ArrayList<DebugConnection>();
+    private List<DebugConnection> connections = new ArrayList<DebugConnection>();
     private ConfigurationListenerDEBUG configuration = null;
     
     
     public ControllerListenerDebug(ConfigurationListenerDEBUG configuration)
     {
         this.configuration = configuration;
-        this.name = Shared.getMessage("Listener DEBUG");
-        this.setName(name);
+        initialise();
     }
     
+
+    private void initialise()
+    {
+        this.name = Shared.getMessage("Listener LOGGER");
+        this.setName(name);
+        
+        // Terminal output
+        
+        DebugConnection connection = new DebugConnection(System.out);
+        this.connections.add(connection);
+        connection.start();
+    }
+
     
 
     public void println(Object object, boolean save)
@@ -42,7 +64,7 @@
         
         try
         {
-            for (DebugConnection connection : lconnection) 
+            for (DebugConnection connection : connections) 
             {
                 connection.addMessage(object);
             }
@@ -63,9 +85,9 @@
                 {
                     if (trace.username == null) trace.username = "-";
                     TracePersistance tracePersistance = new TracePersistance(trace);
+                    Shared.controllerDatabase.getHistoricalPersistance().get(0).addObject_asynchronous(tracePersistance);
                     ApplicationRealtime realtime = (ApplicationRealtime)Shared.controllerStatus.getApplication().getDeviceRealtime();
                     realtime.addTrace(trace);
-                    Shared.controllerDatabase.getHistoricalPersistance().get(0).addObject_asynchronous(tracePersistance);
                 }
                 catch (Exception exception)
                 {
@@ -73,17 +95,11 @@
             }
         }        
         
-        SimpleDateFormat formato1 = new SimpleDateFormat(Shared.getMessage("dd/MM/yyyy HH:mm:ss.SSS"));
-        System.out.println(formato1.format(System.currentTimeMillis()) + " : " + object);
         if (Shared.window != null) Shared.window.addTrace(object);
     }
 
 
-    
-        
-    
-    
-    
+
     public void run()
     {
         Shared.traceInformation(name, "Starting");
@@ -118,6 +134,7 @@
     }
     
     
+    
 
     private void connection(Socket clientSocket)
     {
@@ -126,7 +143,7 @@
             
             if (configuration.allowed(clientSocket.getLocalAddress().getHostAddress()))
             {
-                if (lconnection.size() < configuration.maximumConnections)
+                if (connections.size() < configuration.maximumConnections)
                 {
                     DebugConnection connection = new DebugConnection(clientSocket);
                     addConnection(connection);
@@ -134,7 +151,7 @@
                     return;
                 }
                 
-                Shared.traceError(name, "Connecting", Shared.getMessage("Connection rejected due too many connections"), null);
+                Shared.traceError(name, "Connecting", Shared.getMessage("Connection rejected due too many connections"), "");
             }
             else
             {
@@ -158,16 +175,13 @@
     
     
     
-    
-
-    
     private void addConnection(DebugConnection connection) throws Exception
     {
         mutexConnection.lockWrite();
         
         try
         {
-            lconnection.add(connection);
+            connections.add(connection);
         }
         catch (Exception e)
         {
@@ -179,15 +193,13 @@
     
 
     
-    
-    
     private void removeConnection(DebugConnection connection)
     {
         mutexConnection.lockWrite();
         
         try
         {
-            lconnection.remove(connection);
+            connections.remove(connection);
         }
         catch (Exception e)
         {
@@ -195,8 +207,8 @@
         
         mutexConnection.releaseWrite();
     }
-    
-    
+        
+
     
     
     
@@ -204,15 +216,23 @@
     private class DebugConnection extends Thread
     {
         private Socket socket = null;
-        public List<Object> ltrace = new ArrayList<Object>();
+        private PrintStream output = null;
+        public List<Object> messages = new ArrayList<Object>();
         public Mutex mutex = new Mutex();
         
-        public DebugConnection(Socket socket)
+        public DebugConnection(Socket socket) throws Exception
         {
             this.socket = socket;
             String address = socket.getInetAddress().getHostAddress();
             String name = address + ":" + socket.getPort();
-            this.setName("DebugConnection " + name);
+            this.setName("Logger connection " + name);
+            this.output = new PrintStream(socket.getOutputStream());
+        }
+        
+        public DebugConnection(PrintStream output)
+        {
+            this.setName("Logger screen");
+            this.output = output;
         }
         
         
@@ -220,7 +240,7 @@
         {   
             mutex.lockWrite();
             {
-               ltrace.add(object);
+               messages.add(object);
             }
             mutex.releaseWrite();
         }
@@ -233,16 +253,16 @@
             {
                 while (isInterrupted() == false)
                 {
-                    if (ltrace.size() > 0)
+                    if (messages.size() > 0)
                     {
-                        String message = ltrace.get(0).toString() + "\r\n";
+                        Object object = messages.get(0);
                         
-                        if (message != null)
+                        if (object != null)
                         {
-                            socket.getOutputStream().write(message.getBytes());
+                            print(object);
                         }
                         
-                        ltrace.remove(0);
+                        messages.remove(0);
                     }
                     else
                     {
@@ -257,7 +277,85 @@
             close();
         }
         
-        
+
+
+        private void print(Object object)
+        {
+            String color1 = COLOR_BLACK;
+            String color2 = COLOR_BLACK;
+
+            //if (out != System.out)
+            {
+                color1 = COLOR_WHITE;
+                color2 = COLOR_WHITE;
+            }
+
+            if (object instanceof Trace)
+            {
+                Trace trace = (Trace)object;
+
+                switch (trace.type)
+                {
+                    case Trace.TRACE_INFORMATION: color2 = COLOR_CYAN; break;
+                    case Trace.TRACE_WARNING: color2 = COLOR_YELLOW; break;
+                    case Trace.TRACE_ERROR: color2 = COLOR_RED; break;
+                    case Trace.TRACE_SUCCESS: color2 = COLOR_GREEN; break;
+                }
+
+                print(trace, color1, color2);
+            }
+            else if (object instanceof Note)
+            {
+                Note note = (Note)object;
+
+                switch (note.type)
+                {
+                    case Trace.TRACE_INFORMATION: color2 = COLOR_CYAN; break;
+                    case Trace.TRACE_WARNING: color2 = COLOR_YELLOW; break;
+                    case Trace.TRACE_ERROR: color2 = COLOR_RED; break;
+                    case Trace.TRACE_SUCCESS: color2 = COLOR_GREEN; break;
+                }
+
+                print(note, color1, color2);
+            }
+        }
+    
+
+
+
+        private void print(Trace trace, String color1, String color2)
+        {
+            SimpleDateFormat formato1 = new SimpleDateFormat(Shared.getMessage("dd/MM/yyyy HH:mm:ss.SSS"));
+            output.println(color1 + formato1.format(trace.timestamp));
+
+            output.print("{");
+            if (trace.sourceComputer != null) output.print("\r\n\t" + color1 + "Source computer" + " : " + color2 + trace.sourceComputer);
+            if (trace.destinationComputer != null) output.print("\r\n\t" + color1 + "Destination computer" + " : " + color2 + trace.destinationComputer);
+            if (trace.username != null) output.print("\r\n\t" + color1 + "User" + " : " + color2 + trace.username);
+            if (trace.application != null) output.print("\r\n\t" + color1 + "Application" + " : " + color2 + trace.application);
+            if (trace.service != null) output.print("\r\n\t" + color1 + "Service" + " : " + color2 + trace.service);
+            if (trace.action != null) output.print("\r\n\t" + color1 + "Action" + " : " + color2 + trace.action);
+            if (trace.resource != null) output.print("\r\n\t" + color1 + "Resource" + " : " + color2 + trace.resource);
+            if (trace.result != null) output.print("\r\n\t" + color1 + "Result" + " : " + color2 + trace.result);
+            if ((trace.stack != null) && (trace.stack.length() > 0)) output.print("\r\n\t" + color1 + "Stack" + " : " + color2 + trace.stack);
+            output.print(color1 + "\r\n}\r\n\r\n");
+        }
+
+
+        private void print(Note note, String color1, String color2)
+        {
+            SimpleDateFormat formato1 = new SimpleDateFormat(Shared.getMessage("dd/MM/yyyy HH:mm:ss.SSS"));
+            
+            if (note.service != null)
+            {
+                output.println(color1 + formato1.format(note.timestamp) + " : " + COLOR_LIGHT_GRAY + note.service + " | " + color2 + note.message);
+            }
+            else
+            {
+                output.println(color1 + formato1.format(note.timestamp) + " : " + color2 + note.message);
+            }
+        }
+
         
         public void close()
         {
@@ -268,4 +366,8 @@
     }
     
     
+    
+
+    
+    
 }

--
Gitblit v1.10.0