简体   繁体   中英

How do I know which Thread called my Log method?

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

My problem is that I have 2 threads running and both of them send requests to my Log class to log information.

I would like to have my Log class show which threads are logging which lines.

What should I 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 );
     }
 }

Add this to your logger:

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

and dump it to log file.

public class Temp{
   static Thread t = null;
}

temp.t = Thread.currentThread();

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

Also, with respect to pthread s, the function for knowing about the calling thread is: pthread_t pthread_self (void);

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM