home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / C / Libraries / Log Library 1.01 / LogLibComponent ƒ / main.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-07-03  |  10.1 KB  |  482 lines  |  [TEXT/KAHL]

  1. /*
  2.     Main.c
  3.     
  4.     This application will open some log components and write some information to the
  5.     logs that they keep.  This is intended to be a demonstration of how to use the
  6.     log component.
  7.     
  8.     4/20/94 dn - Created.
  9.     7/3/94 dn - Modified to work with the Universal Headers.
  10. */
  11.  
  12. //#define DEBUGIT 2            /* uncomment this line for debugging */
  13.  
  14. #include <Libsprintf.h>
  15. #include <Folders.h>
  16.  
  17. #include "LogLibComponent.h"
  18.  
  19. /*
  20.     Prototypes
  21. */
  22. void InitToolbox(void);
  23. void main(void);
  24. Boolean CheckComponentMgr(void);
  25. OSErr SetLog1(ComponentInstance log1);
  26. OSErr SetLog2(ComponentInstance log2);
  27. OSErr SetLog3(ComponentInstance log3);
  28. OSErr WriteTo(ComponentInstance log,char* buffer,Boolean time);
  29. OSErr WriteToLogs(ComponentInstance log1,ComponentInstance log2,ComponentInstance log3);
  30. void AlertError(StringPtr errstr);
  31. void GiveSystemTime(void);
  32.  
  33. #ifdef DEBUGIT
  34.  
  35. #include "LogLibComponent Private.h"
  36.  
  37. Component Registerem(void);
  38.  
  39. #else
  40. Component GetLogComponent(void);
  41. #endif
  42.  
  43. /*
  44.     Globals
  45. */
  46.  
  47. Component logComp;
  48.  
  49. ComponentInstance LogFile1,LogFile2,LogFile3;
  50.  
  51. /*
  52.     InitToolbox
  53.     
  54.     Initializes the Mac Toolbox.
  55. */
  56. void InitToolbox(){
  57.     InitGraf((Ptr) &qd.thePort);
  58.     InitFonts();
  59.     InitWindows();
  60.     InitMenus();
  61.     FlushEvents(everyEvent,0);
  62.     TEInit();
  63.     InitDialogs(0L);
  64.     InitCursor();
  65. }
  66.  
  67. /*
  68.     CheckComponentMgr
  69.     
  70.     Uses Gestalt to check for the availability of the Component Manager.  We assume that if Gestalt
  71.     does not return an error then the Component Manager exists.  If we were concerned with what
  72.     version of the Component Manager that we had we could check the result to see if it is at least
  73.     the version that we need.
  74. */
  75. Boolean CheckComponentMgr(){
  76.     OSErr err;
  77.     long result;
  78.     
  79.     err=Gestalt(gestaltComponentMgr,&result);
  80.     
  81.     if (err!=noErr)
  82.         return false;
  83.     else
  84.         return true;
  85. }
  86.  
  87. /*
  88.     main
  89.     
  90.     Main routine.  This doesn't really do much.  It simply creates 3 logs and writes some information
  91.     into them.
  92. */
  93. void main(){
  94.     Component        logComp;
  95.     OSErr            err;
  96.     
  97.     LogFile1=LogFile2=LogFile3=(ComponentInstance)0;
  98.     
  99.     InitToolbox();
  100.     
  101.     GiveSystemTime();
  102.     
  103.     // is the component mgr available?
  104.     if (CheckComponentMgr()){
  105.         
  106.         GiveSystemTime();
  107.  
  108.         // ok, first we have to find the component...
  109. #ifdef DEBUGIT
  110.         {    // this function is defined in LogLibComponent.c.  It is here because somehow we have to get over to
  111.             // the file whilst in the debugger.  By stepping into this call, the debugger will switch to that file where
  112.             // we can set up all of our nifty breakpoints...
  113.             
  114.             extern void DumbTest(void);
  115.             
  116.             DumbTest();
  117.         }
  118.         
  119.         logComp=Registerem();
  120. #else
  121.         logComp=GetLogComponent();
  122. #endif
  123.         
  124.         GiveSystemTime();
  125.  
  126.         if (logComp!=(Component)0){// then we were successful, procede
  127.             
  128.             GiveSystemTime();
  129.  
  130.             // ok, now open our three instances...
  131.             LogFile1=OpenComponent(logComp);
  132.             
  133.             GiveSystemTime();
  134.  
  135.             LogFile2=OpenComponent(logComp);
  136.             
  137.             GiveSystemTime();
  138.  
  139.             LogFile3=OpenComponent(logComp);
  140.             
  141.             GiveSystemTime();
  142.  
  143.             // they are open, now to change some of the defaults...
  144.             // no change for LogFile1
  145.             
  146.             // change the creator for LogFile2
  147.             err=LogDefaults(LogFile2,'KAHL',kLogDefaultType,kLogDefaultOpenSetting);
  148.             
  149.             GiveSystemTime();
  150.  
  151.             // change the open setting for LogFile3
  152.             err=LogDefaults(LogFile3,kLogDefaultCreator,kLogDefaultType,true);
  153.             
  154.             GiveSystemTime();
  155.  
  156.             // ok, the defaults are set, now set up the exact files...
  157.             err=SetLog1(LogFile1);
  158.             
  159.             GiveSystemTime();
  160.  
  161.             err=SetLog2(LogFile2);
  162.             
  163.             GiveSystemTime();
  164.  
  165.             err=SetLog3(LogFile3);
  166.             
  167.             GiveSystemTime();
  168.  
  169.             // call a procedure to write to the 3 logs...
  170.             WriteToLogs(LogFile1,LogFile2,LogFile3);
  171.             
  172.             GiveSystemTime();
  173.  
  174.             // finally, we have to close the logs before we exit...
  175.             
  176.             LogTime(LogFile1,"Closing the log.\r\r");
  177.             
  178.             err=CloseComponent(LogFile1);
  179.             
  180.             GiveSystemTime();
  181.             
  182.             LogTime(LogFile2,"Closing the log.\r\r");
  183.             
  184.             err=CloseComponent(LogFile2);
  185.             
  186.             GiveSystemTime();
  187.             
  188.             LogTime(LogFile3,"Closing the log.\r\r");
  189.             
  190.             err=CloseComponent(LogFile3);
  191.             
  192.             GiveSystemTime();
  193.  
  194.         } else
  195.             AlertError("\pLog Component not installed!");
  196.         
  197.         GiveSystemTime();
  198.  
  199.     } else
  200.         AlertError("\pComponent Manager not installed!");
  201. }
  202.  
  203. /*
  204.     SetLog1
  205.     
  206.     Sets up log 1.  Indicates to the log component that the log should be named "Log File #1" and kept
  207.     in the same directory as this application.  It uses the Process Manager to retrieve information about
  208.     where the application is.
  209. */
  210. OSErr SetLog1(ComponentInstance log1){
  211.     ProcessSerialNumber psn;
  212.     ProcessInfoRecPtr pir;
  213.     FSSpec spec2;
  214.     OSErr err;
  215.     Str255 aname="\pLog File #1";
  216.     FSSpec spec;
  217.     short vref;
  218.     long dirid;
  219.  
  220.     err=GetCurrentProcess(&psn);
  221.     
  222.     if (err!=noErr){
  223.         AlertError("\pGetCurrentProcess Error.");
  224.         ExitToShell();
  225.     }
  226.     
  227.     pir=(ProcessInfoRecPtr)NewPtrClear(sizeof(ProcessInfoRec));
  228.     pir->processInfoLength=sizeof(ProcessInfoRec);
  229.     pir->processName=(StringPtr)NewPtr(32);
  230.     pir->processAppSpec=&spec2;
  231.     
  232.     err=GetProcessInformation(&psn,pir);
  233.     
  234.     if (err!=noErr){
  235.         AlertError("\pGetProcessInformation Error.");
  236.         ExitToShell();
  237.     }
  238.  
  239.     GiveSystemTime();
  240.  
  241.     // use the FSSpec in the ProcessInfoRec for log file info, but use our name...
  242.     spec.vRefNum=pir->processAppSpec->vRefNum;
  243.     spec.parID=pir->processAppSpec->parID;
  244.     BlockMove((Ptr)(aname),(Ptr)(spec.name),(aname[0])+1);
  245.     
  246.     err=LogSetupFSSpec(log1,&spec);
  247.  
  248.     if (err!=noErr){
  249.         AlertError("\pLogSetupFSSpec Error.");
  250.         ExitToShell();
  251.     }
  252.         
  253.     // get rid of the space that we recorded...
  254.     DisposePtr((Ptr)pir->processName);
  255.     DisposePtr((Ptr)pir);
  256.     
  257.     GiveSystemTime();
  258.  
  259.     return err;
  260. }
  261.  
  262. /*
  263.     SetLog2
  264.     
  265.     Sets up log2.  This log will go into the prefs folder in the active system file.  It will be named
  266.     "Log File #2".  Uses FindFolder to get the information.
  267. */
  268. OSErr SetLog2(ComponentInstance log2){
  269.     OSErr err;
  270.     Str255 name="\pLog File #2";
  271.     short vref;
  272.     long dirid;
  273.     
  274.     err=FindFolder(kOnSystemDisk,kPreferencesFolderType,kCreateFolder,&vref,&dirid);
  275.     
  276.     GiveSystemTime();
  277.  
  278.     err=LogSetup(log2,name,vref,dirid);
  279.     
  280.     GiveSystemTime();
  281.  
  282.     return err;
  283. }
  284.  
  285. /*
  286.     SetLog3
  287.     
  288.     Sets up log3.  This log will go on the desktop (of the system drive).  It will be named (unbelievably)
  289.     "Log File #3".  Uses FindFolder to get the information.
  290. */
  291. OSErr SetLog3(ComponentInstance log3){
  292.     OSErr err;
  293.     Str255 name="\pLog File #3";
  294.     short vref;
  295.     long dirid;
  296.     
  297.     err=FindFolder(kOnSystemDisk,kDesktopFolderType,kCreateFolder,&vref,&dirid);
  298.     
  299.     GiveSystemTime();
  300.  
  301.     err=LogSetup(log3,name,vref,dirid);
  302.     
  303.     GiveSystemTime();
  304.  
  305.     return err;
  306. }
  307.  
  308. /*
  309.     WriteTo
  310.     
  311.     Writes a buffer to a specified log.
  312. */
  313. OSErr WriteTo(ComponentInstance log,char* buffer,Boolean time){
  314.     OSErr err;
  315.     
  316.     if (time)
  317.         err= LogTime(log,buffer);
  318.     else
  319.         err= LogText(log,buffer);
  320.     
  321.     GiveSystemTime();
  322.  
  323.     return err;
  324. }
  325.  
  326. /*
  327.     WriteToLogs
  328.     
  329.     Writes to the three logs.
  330. */
  331. OSErr WriteToLogs(ComponentInstance log1,ComponentInstance log2,ComponentInstance log3){
  332.     char buffer[400];
  333.     short fref;
  334.     ComponentResult cres;
  335.     
  336.     WriteTo(log1,"Starting test #1.\r\r",true);
  337.     WriteTo(log2,"Starting test #2.\r\r",true);
  338.     WriteTo(log3,"Starting test #3.\r\r",true);
  339.     
  340.     WriteTo(log2,"This is some test data...",false);
  341.     WriteTo(log2,"   That goes on the same line.\r",false);
  342.     
  343.     WriteTo(log1,"The current time and date is: ",false);
  344.     WriteTo(log1,".\r\r",true);
  345.     
  346.     WriteTo(log3,"This is pretty boring, huh?\r\rWell, lets try the new libsprintf routines...\r\r",false);
  347.     
  348.     libsprintf(buffer,"This is a test of libsprintf:\r\t%% \t\tvalue\t\tresult\r");
  349.     WriteTo(log1,buffer,false);
  350.     
  351.     libsprintf(buffer,"\td\t\t15\t\t\t%d\r",15);
  352.     WriteTo(log1,buffer,false);
  353.     
  354.     libsprintf(buffer,"\t08x\t\t35\t\t\t%08x\r",35);
  355.     WriteTo(log1,buffer,false);
  356.     
  357.     libsprintf(buffer,"\tc\t\t\'x\'\t\t\t%c\r\r",'x');
  358.     WriteTo(log1,buffer,false);
  359.     
  360.     {
  361.         char buf1[]="\rThis line (or two) is being added straight from the application.\r";
  362.         char buf2[]="An application might want to do this if it has alot of stuff to add to the file,\r";
  363.         char buf3[]="or just because it has data that is not a C or Pascal string that needs to be logged...\r\r";
  364.         long ch1,ch2,ch3;
  365.         char* cp;
  366.         
  367.         ch1=ch2=ch3=0L;
  368.         
  369.         for (cp=buf1;*cp;cp++)
  370.             ch1++;
  371.         
  372.         for (cp=buf2;*cp;cp++)
  373.             ch2++;
  374.         
  375.         for (cp=buf3;*cp;cp++)
  376.             ch3++;
  377.         
  378.         
  379.         cres=LogWrite(log2,&ch1,(Ptr)buf1);
  380.         cres=LogWrite(log2,&ch2,(Ptr)buf2);
  381.         cres=LogWrite(log2,&ch3,(Ptr)buf3);
  382.         
  383.     }
  384.     
  385.     return cres;
  386. }
  387.  
  388. /*
  389.     AlertError
  390.     
  391.     Does an alert for the user to see.  Only if no component mgr or no log component.
  392. */
  393. void AlertError(StringPtr msg){
  394.     
  395.     ParamText(msg,"\p","\p","\p");
  396.     
  397. #if NEW_HEADERS_AVAILABLE
  398.     StopAlert(128,(ModalFilterUPP)0);
  399. #else
  400.     StopAlert(128,(ProcPtr)0);
  401. #endif
  402. }
  403.  
  404. /*
  405.     GiveSystemTime
  406.     
  407.     Gives time to the system to process other items while we are waiting for
  408.     something to happen.
  409. */
  410. void GiveSystemTime(void){
  411.     EventRecord er;
  412.     
  413.     if (WaitNextEvent(everyEvent,&er,100,(RgnHandle)0)){
  414.         if (er.what==keyDown){
  415.             // check for command-period
  416.             char ch;
  417.             
  418.             ch=er.message&charCodeMask;
  419.             
  420.             if ((er.modifiers&cmdKey)&&(ch=='.')){
  421.                 // the user wants to cancel...
  422.  
  423.                 AlertError("\pReceived signal to cancel execution, aborting...");
  424.                 
  425.                 if (LogFile1!=(ComponentInstance)0){    // then the logs could be open
  426.                     CloseComponent(LogFile1);
  427.                     CloseComponent(LogFile2);
  428.                     CloseComponent(LogFile3);
  429.                 }
  430.                 
  431.                 ExitToShell();
  432.             }
  433.         }
  434.     }
  435. }
  436.  
  437. #ifdef DEBUGIT
  438.  
  439. static Str255 nameLog="\pLog Component (Debug)";
  440. static Str255 infoLog="\pLogs items to a file.";
  441.  
  442. /*
  443.     Registerem
  444.     
  445.     Registers the test component (if the DEBUGIT flag is set)
  446. */
  447. Component Registerem(){
  448.     Component res;
  449.     ComponentDescription desc;
  450.     
  451.     desc.componentType='LogD';// uses 'LogD' for debug instead of the standard 'LogC'
  452.     desc.componentSubType=kComponentWildCard;
  453.     desc.componentManufacturer='appl';
  454.     desc.componentFlags=kComponentWildCard;
  455.     desc.componentFlagsMask=kComponentWildCard;
  456.     
  457.     res=RegisterComponent(&desc,(ComponentRoutine)_LogDispatch,0,nil,nil,nil);
  458.     
  459.     return res;
  460. }
  461. #else
  462. /*
  463.     GetLogComponent
  464.     
  465.     Try to find the log component.
  466. */
  467. Component GetLogComponent(){
  468.     Component comp;
  469.     ComponentDescription desc;
  470.     
  471.     desc.componentType=kLogLibComponentType;
  472.     desc.componentSubType=kComponentWildCard;
  473.     desc.componentManufacturer=kComponentWildCard;
  474.     desc.componentFlags=kComponentWildCard;
  475.     desc.componentFlagsMask=kComponentWildCard;
  476.     
  477.     comp=FindNextComponent((Component)0,&desc);
  478.     
  479.     return comp;
  480. }
  481. #endif
  482.