Click here to Skip to main content
15,896,269 members
Articles / Web Development / HTML

TraceTool 12.7: The Swiss-Army Knife of Trace

Rate me:
Please Sign up or sign in to vote.
4.97/5 (234 votes)
20 Nov 2016CPL19 min read 2M   39K   1K  
A C#, C++, Delphi, ActiveX , Javascript , NodeJs and Java trace framework and a trace viewer: Tail, OutputDebugString, event log, and with Log4J, Log4Net, and Microsoft Enterprise Instrumentation Framework (EIF) support. This also comes with full support for Pocket PC, Silverlight, and Android.
package service.tracetool;

import java.util.List;
import android.tracetool.*;

import android.app.Activity;
import android.app.ActivityManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.RemoteException;
import android.os.SystemClock;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class ServiceMain extends Activity {
   
   private Button butStart ;
   private Button butStop ;
   private Button ButTest ;
   private TextView tbServiceStatus ;
   private TextView tbConnectionStatus ;
   private TextView tbNbMessSent ;
   private TextView tbNbMessToSend ;
   private TextView tbTotal;
   
   private ITracetoolService tracetoolService ;   
   private ServiceConnection tracetoolConnection ;   
   private ServiceMain me ;
   private Intent intent  ;
   private Handler mHandler = new Handler();

   public int counter = 0 ;
   
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        me = this ;

        tbServiceStatus = (TextView) findViewById(R.id.tbServiceStatus); 
        tbConnectionStatus = (TextView) findViewById(R.id.tbConnectionStatus); 
        tbNbMessSent = (TextView) findViewById(R.id.tbNbMessSent); 
        tbNbMessToSend = (TextView) findViewById(R.id.tbNbMessToSend);        
        tbTotal = (TextView) findViewById(R.id.tbTotal); 
        
        tracetoolConnection = new ServiceConnection() 
        {
           public void onServiceConnected(ComponentName className, IBinder service) 
           {
              // This is called when the connection with the service has been established, giving us the service object we can use to
              // interact with the service.  We are communicating with our service through an IDL interface, so get a client-side
              // representation of that from the raw service object.
              //Log.i("ServiceMain","Service Attached") ;    
              tracetoolService = ITracetoolService.Stub.asInterface(service); 
           }

           public void onServiceDisconnected(ComponentName className) {
              // This is called when the connection with the service has been
              // unexpectedly disconnected -- that is, its process crashed.
              //Log.i("ServiceMain","Service Disconnected.");
              tracetoolService = null;      
           }
        };         

        //------------------------
        // Start service and bind interface
        butStart = (Button) findViewById(R.id.butStart);
        butStart.setOnClickListener(new View.OnClickListener() 
        {
           public void onClick(View v) 
           {
              ComponentName res ;
              
              // start service
              me.intent = new Intent( "service.tracetool.SocketService.ACTION" ) ;
              me.intent.putExtra("ACTION", "ServiceMain");

              res = me.startService(me.intent);
              if (res == null) {
                 Log.e("ServiceMain","Service not started.") ;
              } else {
                 //Log.i("ServiceMain","Service started " + res.getClass().toString()) ;
              }
              
              // bind service
              boolean isOk = me.bindService(intent,  tracetoolConnection, Context.BIND_AUTO_CREATE);
              if (isOk == false) {
                 Log.e("ServiceMain","Service not started. check ActivityManager log");
              } else {
                 //Log.i("ServiceMain","bindService ok") ;
              }
           }
        });

        //------------------------
        // stop service
        butStop = (Button) findViewById(R.id.butStop);
        butStop.setOnClickListener(new View.OnClickListener() 
        {
           public void onClick(View v) {
              // unbind
              if (tracetoolService != null) 
              {
                 unbindService(tracetoolConnection);
                 tracetoolService = null ;
              }
              // stop service
              if (me.intent != null)
                 me.stopService(me.intent);
           }
        });
        
        //------------------------

        // test service
        ButTest = (Button) findViewById(R.id.ButTest);
        ButTest.setOnClickListener(new View.OnClickListener() 
        {
           public void onClick(View v) 
           {
              TTrace.context = v.getContext() ;   // return : instanceof class main (extends ListActivity)
              TTrace.options.sendMode = SendMode.Service ;
              counter++ ;
              TTrace.debug().send("Test from service user interface" + Integer.toString(counter));
           }
        });
        
        //------------------------
        
        // start timer in 3 sec.
        mHandler.postDelayed(mUpdateTimeTask, 3000);

    }  // onCreate
    
    //--------------------------------------------------------------------------------

    @Override
    protected void onDestroy() 
    {
        super.onDestroy();
        
        //unbind the service and null it out.
        if (tracetoolService != null) 
        {
           Log.i("ServiceMain", "Focus onDestroy() attempted to unbind service");
           unbindService(tracetoolConnection);
           tracetoolService = null ;
        }
        tracetoolConnection = null;
        
        // don't stop service

    }  //end onDestroy()
    
    //--------------------------------------------------------------------------------
    // TIMER
    private Runnable mUpdateTimeTask = new Runnable() {
       public void run() {
          tbServiceStatus.setText("Service is not running") ;
          ActivityManager am = (ActivityManager)me.getSystemService(ACTIVITY_SERVICE);
          List<ActivityManager.RunningServiceInfo> rs = am.getRunningServices(100);

          for (int i=0; i<rs.size(); i++) {              
             ActivityManager.RunningServiceInfo rsi = rs.get(i);
             if (rsi.process.compareTo("service.tracetool") == 0) 
             {             
                tbServiceStatus.setText("Service is running.");
                break ;
             }
          }  // next service  
          
          if (tracetoolService == null) 
          {
             tbConnectionStatus.setText("...") ;
             tbNbMessSent.setText("...") ;
             tbNbMessToSend.setText("...") ;
             tbTotal.setText("...") ;
          } else {

             String res ;
             try {
                tbConnectionStatus.setText(tracetoolService.getStatus()) ;
             } catch (RemoteException e) {
                tbConnectionStatus.setText("getStatus exception : " + e.getMessage());
             }      
             
             try 
             {
                res = Integer.toString( tracetoolService.nbMessSent()) ;
                tbNbMessSent.setText(res) ;
             } catch (Exception e) {
                tbNbMessSent.setText("nbMessSend exception : " + e.getMessage());
             }          

             try 
             {
                res = Integer.toString( tracetoolService.nbMessToSend()) ;
                tbNbMessToSend.setText(res) ;
             } catch (Exception e) {
                tbNbMessToSend.setText("nbMessToSend exception : " + e.getMessage());
             }                  

             try 
             {
                res = Integer.toString( tracetoolService.nbMessReceived()) ;
                tbTotal.setText(res) ;
             } catch (Exception e) {
                tbTotal.setText("nbMessReceived exception : " + e.getMessage());
             }          
          }
          
          mHandler.postAtTime(this, SystemClock.uptimeMillis () + 2000);
       }
    };

}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Common Public License Version 1.0 (CPL)


Written By
Architect
Belgium Belgium
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions