home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1994…tember: Reference Library / Dev.CD Sep 94.toast / Periodicals / develop / develop Issue 6 / develop 6 code / TCP / NewsWatcher / NewsWatcher 2.0d15 source / source / log.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-08-23  |  15.5 KB  |  602 lines  |  [TEXT/KAHL]

  1. /*----------------------------------------------------------------------------
  2.  
  3.     log.c
  4.  
  5.     This module handles logging.
  6.     
  7.     Portions copyright © 1990, Apple Computer.
  8.     Portions copyright © 1993, Northwestern University.
  9.  
  10. ----------------------------------------------------------------------------*/
  11.  
  12. #include <Packages.h>
  13. #include <stdio.h>
  14. #include <string.h>
  15.  
  16. #include "glob.h"
  17. #include "log.h"
  18. #include "util.h"
  19. #include "dlgutil.h"
  20.  
  21.  
  22.  
  23. static short gRefNum = 0;
  24.  
  25.  
  26.  
  27. /*----------------------------------------------------------------------------
  28.     PutString 
  29.     
  30.     Writes a string to the log file.
  31.     
  32.     Entry:    str = string to write.
  33. ----------------------------------------------------------------------------*/
  34.  
  35. static void PutString (char *str)
  36. {
  37.     char line[513];
  38.     long len;
  39.     
  40.     sprintf(line, "%s\r", str);
  41.     len = strlen(line);
  42.     FSWrite(gRefNum, &len, line);
  43. }
  44.  
  45.  
  46.  
  47. /*----------------------------------------------------------------------------
  48.     LogBatchGroupCommandSend 
  49.     
  50.     Logs the batch sending of GROUP commands when checking for new
  51.     articles.
  52.     
  53.     Entry:    groupCmds = GROUP commands as sent to server.
  54.             groupCmdsLen = length of groupCmds buffer.
  55.             numGroupsToCheck = number of GROUP commands sent.
  56. ----------------------------------------------------------------------------*/
  57.  
  58. void LogBatchGroupCommandSend (char *groupCmds, short groupCmdsLen, short numGroupsToCheck)
  59. {
  60.     Handle tmpHandle;
  61.     long offset;
  62.     long len;
  63.     CStr255 msg;
  64.  
  65.     if (gRefNum == 0) return;
  66.     PutString("");
  67.     sprintf(msg, "Batch GROUP commands sent to news server.\r%d commands sent.", numGroupsToCheck);
  68.     PutString(msg);
  69.     PtrToHand(groupCmds, &tmpHandle, groupCmdsLen);
  70.     for (offset = 0; offset >= 0; offset = Munger(tmpHandle, offset, CRSTR, 1, "\\n", 2));
  71.     for (offset = 0; offset >= 0; offset = Munger(tmpHandle, offset, LFSTR, 1, "\\r\r", 3));
  72.     len = GetHandleSize(tmpHandle);
  73.     HLock(tmpHandle);
  74.     FSWrite(gRefNum, &len, *tmpHandle);
  75.     MyDisposHandle(tmpHandle);
  76. }
  77.  
  78.  
  79.  
  80. /*----------------------------------------------------------------------------
  81.     LogBatchGroupCommandResponse 
  82.     
  83.     Logs a buffer received from the server in response to the batch sending 
  84.     of GROUP commands when checking for new articles.
  85.     
  86.     Entry:    response = buffer received from server.
  87.             length = length of buffer.
  88.             numReceived = number of lines received from server so far.
  89. ----------------------------------------------------------------------------*/
  90.  
  91. void LogBatchGroupCommandResponse (char *response, long length, short numReceived)
  92. {
  93.     Handle tmpHandle;
  94.     long offset;
  95.     long len;
  96.     CStr255 msg;
  97.  
  98.     if (gRefNum == 0) return;
  99.     PutString("");
  100.     PutString("Buffer received from news server in response to batch GROUP commands.");
  101.     sprintf(msg, "%d responses received so far.", numReceived);
  102.     PutString(msg);
  103.     PtrToHand(response, &tmpHandle, length);
  104.     for (offset = 0; offset >= 0; offset = Munger(tmpHandle, offset, CRSTR, 1, "\\n", 2));
  105.     for (offset = 0; offset >= 0; offset = Munger(tmpHandle, offset, LFSTR, 1, "\\r\r", 3));
  106.     len = GetHandleSize(tmpHandle);
  107.     HLock(tmpHandle);
  108.     FSWrite(gRefNum, &len, *tmpHandle);
  109.     if ((*tmpHandle)[len-1] != '\r') {
  110.         len = 1;
  111.         FSWrite(gRefNum, &len, CRSTR);
  112.     }
  113.     MyDisposHandle(tmpHandle);
  114. }
  115.  
  116.  
  117.  
  118. /*----------------------------------------------------------------------------
  119.     LogBatchGroupCommandsFinished 
  120.     
  121.     Logs the completion of a batch GROUP command send and receive operation.
  122. ----------------------------------------------------------------------------*/
  123.  
  124. void LogBatchGroupCommandsFinished (void)
  125. {
  126.     if (gRefNum == 0) return;
  127.     PutString("");
  128.     PutString("All news server responses successfully received from batch GROUP commands.");
  129. }
  130.  
  131.  
  132.  
  133. /*----------------------------------------------------------------------------
  134.     LogGroupCommand 
  135.     
  136.     Log a GROUP command.
  137.     
  138.     Entry:    groupCmd = pointer to GROUP command, no CRLF terminator,
  139.                 0 terminator.
  140. ----------------------------------------------------------------------------*/
  141.  
  142. void LogGroupCommand (char *groupCmd)
  143. {
  144.     char msg[512];
  145.  
  146.     if (gRefNum == 0) return;
  147.     PutString("");
  148.     sprintf(msg, "GROUP command sent to server: %s", groupCmd);
  149.     PutString(msg);
  150. }
  151.  
  152.  
  153.  
  154. /*----------------------------------------------------------------------------
  155.     LogGroupCommandResponse 
  156.     
  157.     Log a response to a GROUP command.
  158.     
  159.     Entry:    response = pointer to response, with CRLF terminator.
  160.             length = length of response.
  161. ----------------------------------------------------------------------------*/
  162.  
  163. void LogGroupCommandResponse (char *response, unsigned short length)
  164. {
  165.     CStr255 resp;
  166.     char msg[512];
  167.     char *p, *pEnd;
  168.     
  169.     if (gRefNum == 0) return;
  170.     if (length > 255) length = 255;
  171.     pEnd = response + length;
  172.     for (p = response; *p != CR && p < pEnd; p++);
  173.     length = p - response;
  174.     BlockMove(response, resp, length);
  175.     resp[length] = 0;
  176.     sprintf(msg, "GROUP command response received: %s", resp);
  177.     PutString(msg);
  178. }
  179.  
  180.  
  181.  
  182. /*----------------------------------------------------------------------------
  183.     LogGetGroupArrayArticleRangesOneAtATimeBegin 
  184.     
  185.     Log the begin of a get group array article range one at a time operation.
  186. ----------------------------------------------------------------------------*/
  187.  
  188. void LogGetGroupArrayArticleRangesOneAtATimeBegin (void)
  189. {
  190.     if (gRefNum == 0) return;
  191.     PutString("");
  192.     PutString("Begin get group array article range one a time operation.");
  193. }
  194.  
  195.  
  196.  
  197. /*----------------------------------------------------------------------------
  198.     LogGetGroupArrayArticleRangesOneAtATimeEnd 
  199.     
  200.     Log the end of a get group array article range one at a time operation.
  201. ----------------------------------------------------------------------------*/
  202.  
  203. void LogGetGroupArrayArticleRangesOneAtATimeEnd (void)
  204. {
  205.     if (gRefNum == 0) return;
  206.     PutString("");
  207.     PutString("End get group array article range one a time operation.");
  208. }
  209.  
  210.  
  211.  
  212. /*----------------------------------------------------------------------------
  213.     LogNewsrcParseBegin 
  214.     
  215.     Log the start of a newsrc parse.
  216.     
  217.     Entry:    newsrc = handle to newsrc with 0 terminator.
  218. ----------------------------------------------------------------------------*/
  219.  
  220. void LogNewsrcParseBegin (Handle newsrc)
  221. {
  222.     long len;
  223.  
  224.     if (gRefNum == 0) return;
  225.     PutString("");
  226.     PutString("Begin newsrc parse.");
  227.     len = GetHandleSize(newsrc) - 1;
  228.     HLock(newsrc);
  229.     FSWrite(gRefNum, &len, *newsrc);
  230.     HUnlock(newsrc);
  231.     PutString("");
  232. }
  233.  
  234.  
  235.  
  236. /*----------------------------------------------------------------------------
  237.     LogNewsrcGroupNameTooLong 
  238.     
  239.     Log a newsrc group name too long error.
  240.     
  241.     Entry:    groupName = group name.
  242. ----------------------------------------------------------------------------*/
  243.  
  244. void LogNewsrcGroupNameTooLong (char *groupName)
  245. {
  246.     char msg[512];
  247.     
  248.     if (gRefNum == 0) return;
  249.     sprintf(msg, "Group name too long: %s", groupName);
  250.     PutString(msg);
  251. }
  252.  
  253.  
  254.  
  255. /*----------------------------------------------------------------------------
  256.     LogNewsrcGroupNotInFullGroupList 
  257.     
  258.     Log a newsrc group not in full group list error.
  259.     
  260.     Entry:    groupName = group name.
  261. ----------------------------------------------------------------------------*/
  262.  
  263. void LogNewsrcGroupNotInFullGroupList (char *groupName)
  264. {
  265.     char msg[512];
  266.     
  267.     if (gRefNum == 0) return;
  268.     sprintf(msg, "Group not in full group list: %s", groupName);
  269.     PutString(msg);
  270. }
  271.  
  272.  
  273.  
  274. /*----------------------------------------------------------------------------
  275.     LogNewsrcNoGroupName 
  276.     
  277.     Log a newsrc no group name error.
  278. ----------------------------------------------------------------------------*/
  279.  
  280. void LogNewsrcNoGroupName (void)
  281. {
  282.     if (gRefNum == 0) return;
  283.     PutString("Missing group name");
  284. }
  285.  
  286.  
  287.  
  288. /*----------------------------------------------------------------------------
  289.     LogNewsrcReadListSyntaxError 
  290.     
  291.     Log a newsrc group article read list syntax error.
  292.     
  293.     Entry:    groupName = group name.
  294. ----------------------------------------------------------------------------*/
  295.  
  296. void LogNewsrcReadListSyntaxError (char *groupName)
  297. {
  298.     char msg[512];
  299.     
  300.     if (gRefNum == 0) return;
  301.     sprintf(msg, "Syntax error in article read list: %s", groupName);
  302.     PutString(msg);
  303. }
  304.  
  305.  
  306.  
  307. /*----------------------------------------------------------------------------
  308.     LogNewsrcSubscribedOK 
  309.     
  310.     Log a newsrc group subscribed OK.
  311.     
  312.     Entry:    groupName = group name.
  313. ----------------------------------------------------------------------------*/
  314.  
  315. void LogNewsrcSubscribedOK (char *groupName)
  316. {
  317.     char msg[512];
  318.     
  319.     if (gRefNum == 0) return;
  320.     sprintf(msg, "Subscribed, parsed OK: %s", groupName);
  321.     PutString(msg);
  322. }
  323.  
  324.  
  325.  
  326. /*----------------------------------------------------------------------------
  327.     LogNewsrcUnsubscribedOK 
  328.     
  329.     Log a newsrc unsubscribed group.
  330.     
  331.     Entry:    groupName = group name.
  332. ----------------------------------------------------------------------------*/
  333.  
  334. void LogNewsrcUnsubscribedOK (char *groupName)
  335. {
  336.     char msg[512];
  337.     
  338.     if (gRefNum == 0) return;
  339.     sprintf(msg, "Unsubscribed, parsed OK: %s", groupName);
  340.     PutString(msg);
  341. }
  342.  
  343.  
  344.  
  345. /*----------------------------------------------------------------------------
  346.     LogNewsrcNoColonOrBang 
  347.     
  348.     Log a newsrc group no colon or bang syntax error.
  349.     
  350.     Entry:    groupName = group name.
  351. ----------------------------------------------------------------------------*/
  352.  
  353. void LogNewsrcNoColonOrBang (char *groupName)
  354. {
  355.     char msg[512];
  356.     
  357.     if (gRefNum == 0) return;
  358.     sprintf(msg, "No ':' or '!' following group name: %s", groupName);
  359.     PutString(msg);
  360. }
  361.  
  362.  
  363.  
  364. /*----------------------------------------------------------------------------
  365.     LogNewsrcGroupNotOnServer 
  366.     
  367.     Log a newsrc group not on server error.
  368.     
  369.     Entry:    groupName = group name.
  370. ----------------------------------------------------------------------------*/
  371.  
  372. void LogNewsrcGroupNotOnServer (char *groupName)
  373. {
  374.     char msg[512];
  375.     
  376.     if (gRefNum == 0) return;
  377.     sprintf(msg, "Group not on server: %s", groupName);
  378.     PutString(msg);
  379. }
  380.  
  381.  
  382.  
  383. /*----------------------------------------------------------------------------
  384.     DumpUserGroupArray 
  385.     
  386.     Dump a user group array to the log file.
  387.     
  388.     Entry:    userGroupArray = handle to user group array.
  389.             numUserGroups = number of groups in array.
  390.             unsubscribed = handle to saved unsubscribed groups.
  391. ----------------------------------------------------------------------------*/
  392.  
  393. static void DumpUserGroupArray (TGroup **userGroupArray, short numUserGroups, Handle unsubscribed)
  394. {
  395.     char msg[512];
  396.     TGroup theGroup;
  397.     short i;
  398.     TUnread **unread;
  399.     long len;
  400.  
  401.     if (gRefNum == 0) return;
  402.     sprintf(msg, "%d groups in user group list.", numUserGroups);
  403.     HLock(gGroupNames);
  404.     PutString(msg);
  405.     for (i = 0; i < numUserGroups; i++) {
  406.         theGroup = (*userGroupArray)[i];
  407.         sprintf(msg, "%s: firstMess = %ld, lastMess = %ld, numUnread = %ld", 
  408.             *gGroupNames + theGroup.nameOffset, theGroup.firstMess,
  409.             theGroup.lastMess, theGroup.numUnread);
  410.         PutString(msg);
  411.         PutString("   Unread list:");
  412.         for (unread = theGroup.unread; unread != nil; unread = (**unread).next) {
  413.             sprintf(msg, "      %ld-%ld", (**unread).firstUnread, (**unread).lastUnread);
  414.             PutString(msg);
  415.         }
  416.     }
  417.     HUnlock(gGroupNames);
  418.     len = GetHandleSize(unsubscribed);
  419.     if (len > 0) {
  420.         PutString("Unsubscribed groups:");
  421.         HLock(unsubscribed);
  422.         FSWrite(gRefNum, &len, *unsubscribed);
  423.         HUnlock(unsubscribed);
  424.     }
  425. }
  426.  
  427.  
  428.  
  429. /*----------------------------------------------------------------------------
  430.     LogNewsrcParseEnd 
  431.     
  432.     Log the end of a newsrc parse.
  433.     
  434.     Entry:    userGroupArray = handle to user group array.
  435.             numUserGroups = number of groups in array.
  436.             unsubscribed = handle to saved unsubscribed groups.
  437. ----------------------------------------------------------------------------*/
  438.  
  439. void LogNewsrcParseEnd (TGroup **userGroupArray, short numUserGroups, Handle unsubscribed)
  440. {
  441.     if (gRefNum == 0) return;
  442.     PutString("");
  443.     PutString("End of newsrc parse.");
  444.     DumpUserGroupArray(userGroupArray, numUserGroups, unsubscribed);
  445. }
  446.  
  447.  
  448.  
  449. /*----------------------------------------------------------------------------
  450.     LogNewsrcWriteBegin 
  451.     
  452.     Log the beginning of a newsrc write.
  453.     
  454.     Entry:    userGroupArray = handle to user group array.
  455.             numUserGroups = number of groups in array.
  456.             unsubscribed = handle to saved unsubscribed groups.
  457. ----------------------------------------------------------------------------*/
  458.  
  459. void LogNewsrcWriteBegin (TGroup **userGroupArray, short numUserGroups, Handle unsubscribed)
  460. {
  461.     if (gRefNum == 0) return;
  462.     PutString("");
  463.     PutString("Begin newsrc write.");
  464.     DumpUserGroupArray(userGroupArray, numUserGroups, unsubscribed);
  465. }
  466.  
  467.  
  468.  
  469. /*----------------------------------------------------------------------------
  470.     LogNewsrcWriteEnd 
  471.     
  472.     Log the end of a newsrc parse.
  473.     
  474.     Entry:    newsrc = handle to newsrc.
  475. ----------------------------------------------------------------------------*/
  476.  
  477. void LogNewsrcWriteEnd (Handle newsrc)
  478. {
  479.     long len;
  480.  
  481.     if (gRefNum == 0) return;
  482.     PutString("");
  483.     PutString("End newsrc write.");
  484.     len = GetHandleSize(newsrc);
  485.     HLock(newsrc);
  486.     FSWrite(gRefNum, &len, *newsrc);
  487.     HUnlock(newsrc);
  488. }
  489.  
  490.  
  491.  
  492. /*----------------------------------------------------------------------------
  493.     LogTestBatchGroupCommandsBegin 
  494.     
  495.     Log the beginning of a batch group commands test.
  496. ----------------------------------------------------------------------------*/
  497.  
  498. void LogTestBatchGroupCommandsBegin (void)
  499. {
  500.     if (gRefNum == 0) return;
  501.     PutString("");
  502.     PutString("Begin batch group commands test.");
  503. }
  504.  
  505.  
  506.  
  507. /*----------------------------------------------------------------------------
  508.     LogTestBatchGroupCommandsEnd 
  509.     
  510.     Log the end of a batch group commands test.
  511.     
  512.     Entry:    response = pointer to response text.
  513.             respLen = length of response text.
  514.             serverSupportsBatchGroupCommands = result of test.
  515. ----------------------------------------------------------------------------*/
  516.  
  517. void LogTestBatchGroupCommandsEnd (char *response, unsigned short respLen, 
  518.     Boolean serverSupportsBatchGroupCommands)
  519. {
  520.     Handle tmpHandle;
  521.     long offset, len;
  522.     CStr255 msg;
  523.  
  524.     if (gRefNum == 0) return;
  525.     PutString("");
  526.     sprintf(msg, "End of batch group commands test: gServerSupportsBatchGroupCommands = %s",
  527.         serverSupportsBatchGroupCommands ? "true" : "false");
  528.     PutString(msg);
  529.     PtrToHand(response, &tmpHandle, respLen);
  530.     for (offset = 0; offset >= 0; offset = Munger(tmpHandle, offset, CRSTR, 1, "\\n", 2));
  531.     for (offset = 0; offset >= 0; offset = Munger(tmpHandle, offset, LFSTR, 1, "\\r\r", 3));
  532.     len = GetHandleSize(tmpHandle);
  533.     HLock(tmpHandle);
  534.     FSWrite(gRefNum, &len, *tmpHandle);
  535.     MyDisposHandle(tmpHandle);
  536. }
  537.  
  538.  
  539.  
  540. /*----------------------------------------------------------------------------
  541.     OpenLogFile 
  542.     
  543.     Opens the log file.
  544. ----------------------------------------------------------------------------*/
  545.  
  546. void OpenLogFile (void)
  547. {
  548.     FCBPBRec pBlock;
  549.     FSSpec logFile;
  550.     OSErr err;
  551.     Handle vers1Resource;
  552.     Str255 versStr;
  553.     CStr255 str;
  554.     
  555.     if (gRefNum != 0) return;
  556.     pBlock.ioNamePtr = nil;
  557.     pBlock.ioVRefNum = 0;
  558.     pBlock.ioRefNum = CurApRefNum;
  559.     pBlock.ioFCBIndx = 0;
  560.     err = PBGetFCBInfo(&pBlock, false);
  561.     if (err != noErr) goto exit;
  562.     err = FSMakeFSSpec(pBlock.ioFCBVRefNum, pBlock.ioFCBParID, 
  563.         "\pNewsWatcher Log", &logFile);
  564.     if (err != noErr && err != fnfErr) goto exit;
  565.     err = FSpOpenDF(&logFile, fsRdWrPerm, &gRefNum);
  566.     if (err == fnfErr) {
  567.         err = FSpCreate(&logFile, gPrefs.textCreator, 'TEXT', smSystemScript);
  568.         if (err != noErr) goto exit;
  569.         err = FSpOpenDF(&logFile, fsRdWrPerm, &gRefNum);
  570.     }
  571.     if (err != noErr) goto exit;
  572.     err = SetEOF(gRefNum, 0);
  573.     if (err != noErr) goto exit;
  574.     PutString("Log opened.");
  575.     vers1Resource = GetResource('vers', 1);
  576.     pstrcpy(versStr, (StringPtr)*vers1Resource+6);
  577.     sprintf(str, "NewsWatcher version %#s", versStr);
  578.     PutString(str);
  579.     return;
  580.     
  581. exit:
  582.     if (gRefNum != 0) FSClose(gRefNum);
  583.     gRefNum = 0;
  584.     ErrorMessage("Could not open log file. No log will be written.");
  585. }
  586.  
  587.  
  588.  
  589. /*----------------------------------------------------------------------------
  590.     CloseLogFile 
  591.     
  592.     Closes the log file.
  593. ----------------------------------------------------------------------------*/
  594.  
  595. void CloseLogFile (void)
  596. {
  597.     if (gRefNum == 0) return;
  598.     PutString("");
  599.     PutString("Log closed.");
  600.     FSClose(gRefNum);
  601.     gRefNum = 0;
  602. }