Created
July 23, 2022 17:00
-
-
Save psxdev/fb3fc1c9c329758c7aebaf4b4858afdc to your computer and use it in GitHub Desktop.
udp logger for bdj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//add this class to your bdj project change host for your host ip | |
//To use initialize with: | |
//Logger log=Logger.getInstance(); | |
//send info logs to host with log.info("......."); | |
//use this listener on host for example with(remember wsl2 has not direct map for udp ports): | |
//socat udp-recv:18194 stdout | |
package org.homebrew; | |
import java.net.*; | |
import java.io.*; | |
import java.util.*; | |
import java.lang.Boolean; | |
public final class Logger | |
{ | |
public int loggerPort; | |
public String loggerServer; | |
public boolean loggerInitialized; | |
public DatagramSocket loggerSocket; | |
private byte[] loggerSendBytes; | |
private String loggerErrorMessage; | |
private Socket loggerFileSocket; | |
private String host="192.168.1.12"; | |
private int port=18194; | |
private static Logger instance=null; | |
private Logger() | |
{ | |
this.init(); | |
} | |
private void init() | |
{ | |
try | |
{ | |
loggerSocket=new DatagramSocket(); | |
loggerPort=port; | |
loggerServer=host; | |
loggerSendBytes=new byte[4096]; | |
loggerInitialized=true; | |
loggerErrorMessage="No error"; | |
} | |
catch(Exception e) | |
{ | |
loggerInitialized=false; | |
loggerErrorMessage=e.getMessage(); | |
} | |
} | |
public static synchronized Logger getInstance() | |
{ | |
if(instance==null) | |
{ | |
instance=new Logger(); | |
} | |
return instance; | |
} | |
public boolean openFileSocket() | |
{ | |
try | |
{ | |
this.info("[+] Creating File Socket use socat -u TCP-LISTEN:18194,reuseaddr OPEN:app0.zip,creat,trunc"); | |
loggerFileSocket=new Socket(loggerServer,loggerPort); | |
if(loggerFileSocket==null) | |
{ | |
this.error("[+] loggerFileSocket is null something is wrong"); | |
} | |
this.info("[+] File Socket created"); | |
return true; | |
} | |
catch(Exception e) | |
{ | |
this.error(e.getMessage()); | |
return false; | |
} | |
} | |
public void sendBuffer(byte[] b,int size) | |
{ | |
try | |
{ | |
if(loggerFileSocket==null) | |
{ | |
this.error("[+] File Socket is not initialized"); | |
return; | |
} | |
this.info("[+] Sending buffer chunk"); | |
loggerFileSocket.getOutputStream().write(b,0,size); | |
} | |
catch(Exception e) | |
{ | |
this.error(e.getMessage()); | |
} | |
} | |
public boolean isInitialized() | |
{ | |
return loggerInitialized; | |
} | |
public String getLastError() | |
{ | |
return loggerErrorMessage; | |
} | |
private void sendUdpMessage(String message) | |
{ | |
try | |
{ | |
loggerSendBytes=message.getBytes(); | |
DatagramPacket sendPacket=new DatagramPacket(loggerSendBytes, loggerSendBytes.length, InetAddress.getByName(loggerServer), loggerPort); | |
loggerSocket.send(sendPacket); | |
} | |
catch(Exception e) | |
{ | |
loggerErrorMessage=e.getMessage(); | |
} | |
} | |
public void debug(String msg) | |
{ | |
//tested on powerdvd only | |
//String fullClassName = Thread.currentThread().getStackTrace()[2].getClassName(); | |
//String className = fullClassName.substring(fullClassName.lastIndexOf(".") + 1); | |
//String methodName = Thread.currentThread().getStackTrace()[2].getMethodName(); | |
//int lineNumber = Thread.currentThread().getStackTrace()[2].getLineNumber(); | |
//String message="[PROSPERO][DEBUG]["+className+"]["+methodName+"]["+lineNumber+"] "+msg+"\n"; | |
//sendUdpMessage(message); | |
} | |
public void error(String msg) | |
{ | |
String message="[PROSPERO][ERROR] "+msg+"\n"; | |
sendUdpMessage(message); | |
} | |
public void info(String msg) | |
{ | |
String message="[PROSPERO][INFO] "+msg+"\n"; | |
sendUdpMessage(message); | |
} | |
/*public static void main(String[] args) throws Exception | |
{ | |
UdpLogger log=new UdpLogger("192.168.1.12",18194); | |
System.out.println(log.getLastError()); | |
log.info("jander jar"); | |
log.error("jander jur"); | |
log.debug("jander jir"); | |
}*/ | |
public void printStackTrace(Throwable e) | |
{ | |
StringWriter sw = new StringWriter(); | |
PrintWriter pw = new PrintWriter(sw); | |
e.printStackTrace(pw); | |
String trace = sw.toString(); | |
if(trace.charAt(trace.length()-1) != '\n') | |
trace += '\n'; | |
String line = ""; | |
for(int i = 0; i < trace.length(); i++) | |
{ | |
char x = trace.charAt(i); | |
if(x == '\n') | |
{ | |
this.error(line); | |
line = ""; | |
} | |
else | |
line += x; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment