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

  1. /*
  2.     log.c
  3.     
  4.     Handles Log functions
  5.     
  6.         Code version 1.01
  7.         
  8.         ©1994 by Dave Nebinger (dnebing@andy.bgsu.edu)
  9.         All Rights Reserved.
  10.         
  11.         Feel free to use this where ever you wish, just drop me some email
  12.         stating what you are doing with it.
  13.     
  14.     Change Log:  
  15.         5/7/94 dn - Modified a small bug which would nullify folder names, thus
  16.                     causing the InitLog routine to always create the log in the
  17.                     preferences folder and not in the logFoldName parameter.
  18.         7/3/94 dn - Modified to work with the Universal Headers.
  19. */
  20.  
  21.  
  22. #include "Log.h"
  23.  
  24. /*
  25.     InitLog
  26.     
  27.     Finds the prefs folder and the designated inside folder.  This should
  28.     only be called once.
  29.     
  30.     
  31.     logFoldName  -     Name of folder to find in the preferences folder.  If the
  32.                     folder does not exist, it will be created.  However, if 
  33.                     logFoldName is nil, no folder will be created.
  34.     volRefNum    -     returns the volume reference number where the prefs folder is
  35.                     located.
  36.     prefDirID    -     returns the directory id for the prefs folder.
  37.     foldDirID    -     returns the directory id for the newly created folder.  If nil
  38.                     was passed in the folder name, foldDirID will be the same 
  39.                     as prefDirID.
  40.     
  41.     Returns true if successful, false if there was some kind of error.
  42. */
  43. Boolean InitLog(Str255 logFoldName,short* volRefNum,long* prefDirID,long* foldDirID){
  44.     OSErr err;
  45.     
  46.     /* standard call to FindFolder to get the necessary information */
  47.     err=FindFolder(kOnSystemDisk,kPreferencesFolderType,kCreateFolder,volRefNum,prefDirID);
  48.     
  49.     if (err==noErr){
  50.         
  51.         if (logFoldName[0]==0){   /* DN - Duh, this is where I forgot an '=' :-( */
  52.         
  53.             *foldDirID=*prefDirID;
  54.             return true;
  55.         } else {
  56.             CInfoPBRec pb;
  57.             DirInfo *dpb=(DirInfo*)&pb; /* for the directory information */
  58.             
  59.             /* fill in our DirInfo record with our newly acquired information */
  60.             dpb->ioVRefNum=*volRefNum;
  61.             dpb->ioNamePtr=logFoldName;
  62.             dpb->ioDrDirID=*prefDirID;
  63.             dpb->ioCompletion=nil;
  64.             dpb->ioFDirIndex=0;
  65.             
  66.             /* call PBGetCatInfo to see if we can find the folder */
  67.             err=PBGetCatInfo(&pb,false);
  68.             
  69.             /*    if we get through the next if statement, then we are almost
  70.                 all set. */
  71.             if ((err==noErr)||(err==dirNFErr)||(err==fnfErr)) { 
  72.                 /*    then the folder should exist, use the dir id to put 
  73.                     us in that folder */
  74.                 
  75.                 if ((err==dirNFErr)||(err==fnfErr)) /* then we must create it because
  76.                                                         it does not yet exist... */
  77.                     err=DirCreate(*volRefNum,*prefDirID,logFoldName,foldDirID);
  78.                 else /* it already exists */
  79.                     *foldDirID=dpb->ioDrDirID;
  80.                 
  81.                 if (err==noErr)
  82.                     return true;
  83.             }
  84.         }
  85.     }
  86.     return false; /* error */
  87. }
  88.  
  89.  
  90.  
  91. /*
  92.     OpenLog
  93.     
  94.     Opens the log file in the prefs folder.  If the file does 
  95.     not exist, then we need to create it.
  96.     
  97.     logName - name of the file to open (and create)
  98.     volRefNum - the volume reference number
  99.     dirID - the directory id for where to put the new file.
  100.     
  101.     Returns the freshly opened file's reference number or -1 if there
  102.     was an error.
  103. */
  104. short OpenLog(Str255 logName,short volRefNum,long DirID){
  105.     OSErr err;
  106.     short refNum;
  107.     FSSpec tempSpec;
  108.     
  109.     /* make our FSSpec.  A fnfErr means the file doesn't exist, but the FSSpec is
  110.         still valid. */
  111.     err=FSMakeFSSpec(volRefNum,DirID,logName,&tempSpec);
  112.     
  113.     /* create the file if necessary */
  114.     if (err==fnfErr)
  115.         err=FSpCreate(&tempSpec,'R*ch','TEXT',smSystemScript);
  116.     
  117.     /* open the file if all is well. */
  118.     if (err==noErr){
  119.         err=FSpOpenDF(&tempSpec,fsRdWrPerm,&refNum);
  120.         
  121.         if (err==noErr){ 
  122.             /* then the log was opened without a flaw.  The next step is to set 
  123.                 the file pointer to the end so we can start appending data... */
  124.             err=SetFPos(refNum,fsFromLEOF,0);
  125.             
  126.             if (err==noErr){
  127.                 
  128.                 /* then we have successfully opened the file, and everything is ready
  129.                     to go. */
  130.                 
  131.                 return refNum;
  132.                 
  133.             } // SetFPos
  134.         }// FSpOpenDF
  135.     }// FSMakeFSSpec
  136.  
  137.     return -1; /* error */
  138. }
  139.  
  140.  
  141.  
  142. /*
  143.     Log
  144.     
  145.     A printf type function that takes random parameters, puts them together and 
  146.     then prints the new string to the specified log file.
  147.     
  148.     refNum - the log file's reference number
  149.     fmt - a printf-style format string
  150.     
  151.     returns true if everything went well, otherwise it will return false after
  152.     trying to close the log file.
  153. */
  154. Boolean Log(short refnum,const char* fmt,...){
  155.     char stringBuff[1000]; /* gives us plenty of room to work with */
  156.     OSErr err;
  157.     long llen; /* line length */
  158.       
  159.     /* first we expand the string... */
  160.     vsprintf(stringBuff,fmt,__va(fmt));
  161.   
  162.     /* now we need to know the length... */
  163.     llen=(long)strlen(stringBuff);
  164.     
  165.     /* now we can write the string... */
  166.     err=FSWrite(refnum,&llen,stringBuff);
  167.     
  168.     /* if we have an error then we close the log and return false. */
  169.     if (err!=noErr){
  170.         CloseLog(refnum);
  171.         return false; /* error */
  172.     } else
  173.         return true;
  174. }
  175.  
  176.  
  177.  
  178.  
  179. /*
  180.     LogTime
  181.     
  182.     A printf type function that takes random parameters, puts them together and 
  183.     then prints the new string to the specified log file.  Also included is the 
  184.     date and time...
  185.     
  186.     The date is printed using mo/da/yr, time is hr:min followed by am/pm.
  187.     
  188.     refNum - the log file's reference number
  189.     fmt - a printf-style format string
  190.     
  191.     returns true if everything went well, otherwise it will return false after
  192.     trying to close the log file.
  193. */
  194. Boolean LogTime(short refnum,const char* fmt,...){
  195.     char stringBuff[1000]; /* temporary string buffers */
  196.     char tempBuff[1000];
  197.     OSErr err;
  198.     long llen; /* string len */
  199.     DateTimeRec dtr; /* record for receiving the date and time info */
  200.     short y1,y2,y3; /* misc variables */
  201.   
  202.     /* first we expand the string... */
  203.     vsprintf(tempBuff,fmt,__va(fmt));
  204.   
  205.     /* now add the date and time... */
  206.     GetTime(&dtr);
  207.     y1=dtr.year;
  208.     
  209.     /* extract the last two numbers from the year */
  210.     if (y1>100){
  211.         y2=y1/100;
  212.         y2 *= 100;
  213.         
  214.         y1 -= y2;
  215.     }
  216.     
  217.     y2=dtr.hour; /* get the hour */
  218.     
  219.     /* determine am or pm, and yes this part could have been done a little
  220.         nicer, but it works and that's all you should be concerned about ;-) */
  221.     if (y2>=12){ /* then we are in pm */
  222.         if (y2>12) /* then decrement so it is normal time. */
  223.             y2 -= 12;
  224.         
  225.         /* now build the string */
  226.         sprintf(stringBuff,"%02d/%02d/%02d\t%02d:%02dpm\t%s",
  227.             dtr.month,dtr.day,y1, y2,dtr.minute, tempBuff);
  228.             
  229.   } else{ /* then we are in am */
  230.       if (y2==0) /* then pretend it is after midnight (use 12 instead of zero) */
  231.           y2=12;
  232.       
  233.         /* build the string */
  234.         sprintf(stringBuff,"%02d/%02d/%02d\t%02d:%02dam\t%s",
  235.             dtr.month,dtr.day,y1, y2,dtr.minute, tempBuff);
  236.     }
  237.     
  238.     /* now we need to know the length... */
  239.     llen=(long)strlen(stringBuff);
  240.  
  241.     /* now we can write the string... */
  242.     err=FSWrite(refnum,&llen,stringBuff);
  243.     
  244.     /* if we have an error then we close the log and return false. */
  245.     if (err!=noErr){
  246.         CloseLog(refnum);
  247.         return false; /* error */
  248.     } else
  249.         return true;
  250. }
  251.  
  252.  
  253.  
  254.  
  255. /*
  256.     CloseLog
  257.     
  258.     Closes the specified log file.
  259.     
  260.     refnum - reference number for the log file to close.
  261. */
  262. void CloseLog(short refnum){
  263.     
  264.     FSClose(refnum);
  265. }
  266.