How to find out which thread called my log method?

I have a log class that has several static methods that help write information about my program.

My problem is that I have 2 threads and both send requests to my log class to record information.

I would like my log class to show which threads are logging strings.

What to do to achieve this functionality?

My code is basically like this:

public class Log { public static void log ( String tag , Object message ) { String lineToPrint = ""; //Builds the string taking in time data and other information //... //This is where I want to see which thread called this log function //... System.out.println( lineToPrint ); } } 
+4
source share
3 answers

Add this to your registrar:

 Thread t = Thread.currentThread(); String name = t.getName(); 

and upload it to the log file.

+8
source
 public class Temp{ static Thread t = null; } 

temp.t = Thread.currentThread ();

  public static void log ( String tag , Object message ) { temp.t.getName();//get it } 
0
source

In addition, with respect to pthread s, a function for obtaining information about the calling thread: pthread_t pthread_self (void);

Link: http://pubs.opengroup.org/onlinepubs/007908799/xsh/pthread_self.html

0
source

Source: https://habr.com/ru/post/1388566/


All Articles