/* trace server library */!!

inherit(ProvidedLibs, #TraceLib, #(initialized), 2, nil)!!

setClassVars(TraceLib, #())!!

now(class(TraceLib))!!

/* semiPRIVATE - only called via require(). */
Def provide(self, path)
{ 
  ^initialize(new(self:ancestor),path);
}
!!

now(TraceLib)!!

/* 11/25/1996 19:11 */

Def prim_trcNewestTimestampGet(self | rc value)
{
  value := new(String, 20);
  if pcallLock(self)
    rc := pcall(procs[#TRCNEWESTTIMESTAMPGET], value); 
    pcallUNLock(self);
  else
    rc := ERR_PCALL_RECURSION;
  endif;
  displayError(ErrorTextLibClass$Inst,rc,FORCE_POPUP);
  if (rc <> 0)
    ^"" ;
  else
    ^ removeNulls(value);
  endif ;  
}!!

/* PUBLIC -- gingerbread for pcall */
Def trcBtmGet(self| rc val)
{
  val := new( Struct, 4 ) ;
  if pcallLock(self)
    rc := pcall(procs[#TRCBTMGET], val);
    pcallUNLock(self);
  else
    rc := ERR_PCALL_RECURSION;
  endif;
  displayError(ErrorTextLibClass$Inst,rc,FORCE_POPUP);
  if (rc <> 0)
    ^tuple(rc, 0)
  else
    ^tuple(0, wordAt(val, 0));
  endif ;  
}
!!

/* PUBLIC -- gingerbread for pcall */
Def trcBtmSet(self, val | rc)
{ 
  if pcallLock(self)
    rc := pcall(procs[#TRCBTMSET], val);
    pcallUNLock(self);
  else
    rc := ERR_PCALL_RECURSION;
  endif;
  displayError(ErrorTextLibClass$Inst,rc,FORCE_POPUP);
  ^rc;
}
!!

/* PUBLIC -- gingerbread for pcall ; returns tuple(rc,value)*/
Def prim_trcMaxValidTraceBuffer(self | rc value)
{
  value := new( Struct, 4 ) ;
  if pcallLock(self)
  then
    rc := pcall(procs[#TRCMAXVALIDTRACEBUFFERS], value);
    pcallUNLock(self);
  else 
    rc := ERR_PCALL_RECURSION ;
  endif ;

  if (rc <> 0)
    ^tuple(rc, 0)
  else
    ^tuple(0, wordAt(value, 0));
  endif ;  
}
!!

/* PUBLIC -- gingerbread for pcall */
Def prim_trcCollectingBufferGet(self | rc value)
{ 
  value := new( Struct, 4 ) ;
  if pcallLock(self)
  then
    rc := pcall(procs[#TRCCOLLECTINGBUFFERGET], value);
    pcallUNLock(self);
  else 
    rc := ERR_PCALL_RECURSION ;
  endif ;

  if (rc <> 0)
    ^tuple(rc, 0)
  else
    ^tuple(rc, wordAt(value,0))
  endif ;  
}
!!

/* PUBLIC -- gingerbread for pcall ; returns tuple(rc,value)*/
Def prim_trcTracingGet(self | rc value)
{
  value := new( Struct, 4 ) ;
  if pcallLock(self)
  then
    rc := pcall(procs[#TRCTRACINGGET], value);
    pcallUNLock(self);
  else 
    rc := ERR_PCALL_RECURSION ;
  endif ;

  if (rc <> 0)
    ^tuple(rc, 0)
  else
    ^tuple(0, wordAt(value, 0));
  endif ;  
}
!!

/* PRIVATE */
Def addImportProcs(self)
{
  add( self, #INITCSERVER, 1, #(0, 0) ) ;
  add(self,#TRCTRACEOPEN,1,#(1,1,0,0));
  add(self,#TRCTRACEBUFFERINFOGET,1,#(1,1,1,1));
  add(self,#TRCTRACECLOSE,1,#(1));
  add(self,#TRCBACKWARDREAD,1,#(1,1,1,1,1,1)); 
  add(self,#TRCFORWARDREAD,1,#(1,1,1,1,1,1));  
  add(self,#TRCMAXVALIDTRACEBUFFERS,1,#(1));
  add(self,#TRCTRACETEMPLATESET,1,#(1,0));
  add(self,#TRCFRAMENUMBERAREASIZEGET,1,#(1));
  add(self,#TRCSUBBUFFERSIZEGET,1,#(1));
  add(self,#TRCREADFRAMEGET,1,#(1,1));
  add(self,#TRCREADFRAMESET,1,#(1,1));
  add(self,#TRCNUMFRAMESGET,1,#(1,1));
  add(self,#TRCREADBUFFERGET,1,#(1,1));
  add(self,#TRCREADBUFFERSET,1,#(1,0));
  add(self,#TRCTRACECYCLESET,1,#(1,0));
  add(self,#TRCTRACECYCLEGET,1,#(1,1));
  add(self,#TRCGETHEADER, 1, #(1, 1, 0) ) ;
  add(self,#TRCTRACEBUFFERCLEAR,1,nil); 
  add(self,#TRCSEARCHBUFFERSET,1,#(1,0));
  add(self,#TRCSEARCHFRAMEGET,1,#(1,1));
  add(self,#TRCSEARCHFRAMESET,1,#(1,1));
  add(self,#TRCSEARCHEVENTSET,1,#(1,1));
  add(self,#TRCTRACESEARCH,1,#(1,1));
  add(self,#TRCTRACINGSET,1,#(0));
  add(self,#TRCTRACINGGET,1,#(1));
  add(self,#TRCUPDATETRACE,1,#(1));
  add(self,#TRCCOLLECTINGBUFFERGET,1,#(1));
  add(self,#TRCGETFRAMEINSTADDRESS,1,#(1,1,1,1));
  add(self,#TRCTIMESTAMPGET,1,#(1,1));
  add(self,#TRCTIMESTAMPSET,1,#(1,0));
  add(self,#TRCTIMESTAMPMODEGET,1,#(1,1));
  add(self,#TRCTIMESTAMPMODESET,1,#(1,0));
  add(self,#TRCTIMESTAMPZEROFRAMEGET,1,#(1,1));
  add(self,#TRCTIMESTAMPZEROFRAMESET,1,#(1,1));
  add(self,#TRCSYSTEMCRYSTALGET,1,#(1));
  add(self,#TRCSYSTEMCRYSTALSET,1,#(1));  
  add(self,#TRCBTMSET,1,#(0));
  add(self,#TRCBTMGET,1,#(1));
  add(self,#TRCOPTIONFLAGGET,1,#(1,0,1));
  add(self,#TRCOPTIONFLAGSET,1,#(1,0));    
  add(self,#TRCNEWESTTIMESTAMPGET,1,#(1));                     
}
!!

/* PRIVATE 
   Open and initialize trace server library & load trace dll
*/
Def initialize(self, path)
{ 
   setName( self, path ) ;
   addImportProcs( self ) ;
   load( self ) ;
   ^self ;
}
!!

/* PUBLIC -- create a trace instance */
Def traceOpen(self, fileName, cycleMode, frameMode | aTrace, rc, descriptorPtr) { 
  aTrace := new( Trace ) ;
  descriptorPtr := new( Struct, 4 ) ;
  if pcallLock(self)
  then
    rc := pcall(procs[#TRCTRACEOPEN], descriptorPtr, asciiz(fileName),
              cycleMode, frameMode) ;
    pcallUNLock(self);
  else 
    rc := ERR_PCALL_RECURSION ;
  endif ;
  displayError(ErrorTextLibClass$Inst,rc,FORCE_POPUP);
  if (rc <> 0) ^nil; endif;
  setDescriptor(aTrace, longAt(descriptorPtr, 0));
  setFileName(aTrace, fileName) ;
  setProcs(aTrace, procs(self)) ;
  ^aTrace
}!!

/* PUBLIC -- gingerbread for pcall */
Def trcCollectingBufferGet(self | result)
{ 
  /* result is #(rc, value) */
  result := prim_trcCollectingBufferGet( self ) ;

  displayError( ErrorTextLibClass$Inst, result[0], FORCE_POPUP ) ;
  ^result[1]  
}
!!

/* PUBLIC -- gingerbread for pcall */
Def trcFrameNumberAreaSizeGet(self | rc value)
{
  value := new( Struct, 4 ) ;
  if pcallLock(self)
  then
    rc := pcall(procs[#TRCFRAMENUMBERAREASIZEGET], value);
    pcallUNLock(self);
  else 
    rc := ERR_PCALL_RECURSION ;
  endif ;

  displayError(ErrorTextLibClass$Inst,rc,FORCE_POPUP);
  if (rc <> 0)
    ^0 ;
  else
    ^wordAt(value,0);
  endif ;  
}
!!

/* PUBLIC -- gingerbread for pcall */
Def trcSubBufferSizeGet(self | rc value)
{
  value := new( Struct, 4 ) ;
  if pcallLock(self)
  then
    rc := pcall(procs[#TRCSUBBUFFERSIZEGET], value);
    pcallUNLock(self);
  else 
    rc := ERR_PCALL_RECURSION ;
  endif ;

  displayError(ErrorTextLibClass$Inst,rc,FORCE_POPUP);
  if (rc <> 0)
    ^0 ;
  else
    ^wordAt(value,0);
  endif ;  
}
!!

/* PUBLIC -- gingerbread for pcall */
Def trcTraceBufferClear(self | rc)
{
  if pcallLock(self)
  then
    rc := pcall(procs[#TRCTRACEBUFFERCLEAR]);
    pcallUNLock(self);
  else 
    rc := ERR_PCALL_RECURSION ;
  endif ;

  displayError(ErrorTextLibClass$Inst,rc,FORCE_POPUP);
  ^rc;
}
!!

/* PUBLIC -- gingerbread for pcall */
Def trcTracingGet(self | result)
{
  /* result is #(rc, value) */
  result := prim_trcTracingGet( self ) ;

  displayError(ErrorTextLibClass$Inst, result[0], FORCE_POPUP);
  ^result[1] 
}
!!

/* PUBLIC -- gingerbread for pcall */
Def trcTracingSet(self, units | rc)
{
  if pcallLock(self)
  then
    /*
    printNewLine("TRCTRACINGSET tracing = "+asString(units));
    */
    rc := pcall(procs[#TRCTRACINGSET],units);
    pcallUNLock(self);
  else 
    rc := ERR_PCALL_RECURSION ;
  endif ;

  displayError(ErrorTextLibClass$Inst,rc,FORCE_POPUP);
  ^rc;
}
!!

/* PUBLIC -- gingerbread for pcall */
Def trcSystemCrystalGet(self | freqStr rc)
{
  freqStr := new(String, 40);
  if pcallLock(self)
  then
    rc := pcall(procs[#TRCSYSTEMCRYSTALGET],freqStr);
    pcallUNLock(self);
  else 
    rc := ERR_PCALL_RECURSION ;
  endif ;

  displayError(ErrorTextLibClass$Inst,rc,FORCE_POPUP);
  if( rc <> 0 )
  then
    ^"" ;
  else
    ^ removeNulls(freqStr);
  endif ;  
}
!!

/* PUBLIC -- gingerbread for pcall */
Def trcSystemCrystalSet(self, text | rc)
{
  if( not(text) )
    ^0;    /* don't set NIL to server */
  endif;
  if pcallLock(self)
    rc := pcall(procs[#TRCSYSTEMCRYSTALSET], asciiz(text));
    pcallUNLock(self);
  else
    rc := ERR_PCALL_RECURSION;
  endif;
  displayError(ErrorTextLibClass$Inst,rc,FORCE_POPUP);
  ^rc;
}
!!

/* TraceLib Class Initialization Code */
