home *** CD-ROM | disk | FTP | other *** search
/ Simtel MSDOS 1992 June / SIMTEL_0692.cdr / msdos / sysutl / log3.arc / LOG.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1987-05-09  |  15.5 KB  |  417 lines

  1. Program Log;
  2. (****************************************************************************)
  3. (*                                                                          *)
  4. (*                               LOG                                        *)
  5. (*                           Revision 2.0                                   *)
  6. (*             Written by Don Gancsos and Robert Wilcox                     *)
  7. (*                                                                          *)
  8. (*             A program which logs computer operation and calculates       *)
  9. (*             the elapsed time between log entries.                        *)
  10. (*             Logs date, time, elapsed time and comment to the             *)
  11. (*             file "TIME.LOG"                                              *)
  12. (*                                                                          *)
  13. (*                  Use: LOG comment                                        *)
  14. (*                                                                          *)
  15. (*             Modified by R. Wilcox 3-14-86                                *)
  16. (*                - Now puts log file in root directory of drive A          *)
  17. (*                  rather than in the logged-in directory.                 *)
  18. (*                - Uses command line (up to 50 characters) as comment.     *)
  19. (*                - Log file changed to 'TIME.LOG' (3-23-86)                *)
  20. (*             Rev 2.0 modifications 4-9-86                                 *)
  21. (*                - Uses random file allowing a "SHOW" program to           *)
  22. (*                  display start and end log entries with the              *)
  23. (*                  calculated elapsed time.                                *)
  24. (*                - Log file changed to 'NEWTIME.LOG'.                      *)
  25. (*                                                                          *)
  26. (*             Rev 3.0 modifications 5-10-87 by Rick Johnson                *)
  27. (*                                                                          *)
  28. (*                  Use: LOG <off|[comment] /a|/*>                          *)
  29. (*                                                                          *)
  30. (*                - Keeps log in random file 'TIME.LOG' in path             *)
  31. (*                  C:\UTILITY.                                             *)
  32. (*                - Random file records now store:                          *)
  33. (*                  1) Time category, e.g. 'B' for business.                *)
  34. (*                  2) Start date.                                          *)
  35. (*                  3) Start time.                                          *)
  36. (*                  4) End time.                                            *)
  37. (*                  5) Elapsed time (string).                               *)
  38. (*                  6) Elapsed time (real no.).                             *)
  39. (*                  8) Comment (up to 12 characters).                       *)
  40. (*                - Session terminated and elapsed time calculated          *)
  41. (*                  with 'LOG OFF' command.                                 *)
  42. (*                - Time category invoked with '/a' parameter where         *)
  43. (*                  'a' is any alpha character a through z.                 *)
  44. (*                - Time categories 'B' and 'P' noted as 'Business'         *)
  45. (*                  or 'Personal' use in program's screen messages          *)
  46. (*                - Times displayed and totaled with '/*' parameter         *)
  47. (*                  and optionally written to disk file:                    *)
  48. (*                  1) Text format for word processor or                    *)
  49. (*                  2) Comma-delimited for spreadsheet.                     *)
  50. (*                  SHOW.COM no longer needed.                              *)
  51. (*                                                                          *)
  52. (*                                                                          *)
  53. (****************************************************************************)
  54.  
  55.  
  56. Type
  57.   Datestring = string[8];
  58.   CommandString  = string[50];
  59.   CommentString = string[12];
  60.   Filename = string[24];
  61.   Registers    = record
  62.                 AX,BX,CX,DX,BP,SI,DI,DS,ES,Flags: Integer;
  63.             End;
  64.  
  65. LogRec = Record
  66.   usage: char;              {usage type, i.e. business/personal}
  67.   start_date_log : Datestring;     {start date}
  68.   start_time_log : Datestring;     {start time}
  69.   end_time_log   : Datestring;     {end time}
  70.   elapsed_log    : Datestring;     {elapsed time}
  71.   comment        : CommentString;  {comment}
  72.   elapsed_real   : Real;           {elapsed time stored as real no.}
  73.   end;
  74.  
  75. Var
  76.   Mon, Day, Year : string[2];
  77.   logfile        : file of LogRec;
  78.   realtime       : Real;
  79.   Buffer         : CommandString;
  80.   CL             : CommandString absolute cseg:$80;
  81.   log_data       : logrec;
  82.   logoff         : boolean;
  83.  
  84. Const
  85.   logfilename    : filename = 'C:\UTILITY\TIME.LOG';
  86.  
  87. Function Date: Datestring;
  88. var
  89.   RSet   : Registers;
  90.   tempstring: Datestring;
  91.   i: Integer;
  92. Begin
  93.   With Rset do
  94.   Begin
  95.     AX   :=  $2A00;        { DOS get date function }
  96.     MSDos(RSet);
  97.     CX := cx mod 100;
  98.     str(CX:2,year);
  99.     str(Hi(DX):2,mon);
  100.     str(Lo(DX):2,day);
  101.   End;
  102.   tempstring := mon + '/' + day + '/' + year;
  103.    for i := 1 to 8 do
  104.      if tempstring[i] = ' ' then
  105.        tempstring[i] := '0';
  106. Date := tempstring;
  107. End;
  108.  
  109. Function Time: Datestring;
  110. var
  111.   RSet                 : registers;
  112.   hour, minute, second : string[2];
  113.   i                    : integer;
  114.   tempstring           : Datestring;
  115. Begin
  116.    rset.ax := $2C * 256;
  117.    MsDos(rset);
  118.    str((rset.cx div 256):2,hour);
  119.    str((rset.cx mod 256):2,minute);
  120.    str((rset.dx div 256):2,second);
  121.    tempstring := hour + ':' + minute + ':' + second;
  122.    for i := 1 to 8 do
  123.      if tempstring[i] = ' ' then
  124.        tempstring[i] := '0';
  125.    Time := tempstring;
  126. End;  {Time}
  127.  
  128. Function SecToString(secs: integer): Datestring;
  129. var
  130.   t: integer;
  131.   result: Datestring;
  132.   tempresult: Datestring;
  133. Begin
  134.   Str(secs div 3600:2,tempresult);
  135.   result := tempresult + ':';
  136.   t:= secs mod 3600;
  137.   Str(t div 60:2,tempresult);
  138.   result := result +tempresult + ':';
  139.   Str(t mod 60:2,tempresult);
  140.   result := result + tempresult;
  141.   For t := 1 to 8 do if result[t] = ' ' then result[t] := '0';
  142.   SecToString := result;
  143. end;
  144.  
  145. Function elapsed_time(start_str, end_str: Datestring): Datestring;
  146. var
  147.   i,j,k,starttime,endtime: integer;
  148. begin
  149.   val(copy(end_str,1,2),i,j);
  150.   endtime := i * 3600;
  151.   val(copy(end_str,4,2),i,j);
  152.   endtime := endtime + (i * 60);
  153.   val(copy(end_str,7,2),i,j);
  154.   endtime := endtime + i;
  155.   val(copy(start_str,1,2),i,j);
  156.   starttime := i * 3600;
  157.   val(copy(start_str,4,2),i,j);
  158.   starttime := starttime + (i * 60);
  159.   val(copy(start_str,7,2),i,j);
  160.   starttime := starttime + i;
  161.   k := endtime - starttime;
  162.   elapsed_time := SecToString(k);
  163. end;
  164.  
  165. Function elapsed_real_time(tempstring: Datestring): real;
  166. var
  167.   temp, temptime : real;
  168.   err            : integer;
  169. begin
  170.   val(copy(tempstring,1,2),temptime,err); {get hours}
  171.   val(copy(tempstring,4,2),temp,err);
  172.   temptime := temptime + (temp/60);       {get fraction of hours from minutes}
  173.   val(copy(tempstring,7,2),temp,err);
  174.   temptime := temptime + (temp/3600);     {get fraction of hours from seconds}
  175.   elapsed_real_time := temptime;
  176. end;
  177.  
  178. Procedure initialize_variables;
  179. begin
  180.   logoff:=false;
  181.   log_data.usage := '@';
  182.   log_data.start_date_log:= Date;
  183.   log_data.start_time_log:= Time;
  184.   log_data.end_time_log:= Time;
  185.   log_data.elapsed_log:= '00:00:00';
  186.   log_data.comment:= '';
  187.   log_data.elapsed_real:=0;
  188. end;
  189.  
  190. Function uppercase(tempstring: CommandString; startpos, number: integer): CommandString;
  191. var temp : CommandString;
  192.     i    : integer;
  193. begin
  194.   if length(tempstring) >= startpos then
  195.   begin
  196.     tempstring:=copy(tempstring,startpos,number);
  197.     for i := 1 to length(tempstring) do
  198.       tempstring[i] := upcase(tempstring[i]);
  199.   end
  200.   else tempstring:='';
  201.   uppercase:=tempstring;
  202. end; {procedure uppercase}
  203.  
  204. procedure summarize;
  205. var
  206.     total_time       : real;
  207.     category_total   : array [64..90] of real;
  208.     i, counter       : integer;
  209.     ch               : char;
  210.     disk_file,
  211.     delimited        : boolean;
  212.     textfile         : text;
  213. const
  214.     textfilename     : filename = 'C:\UTILITY\TIME.TXT';
  215. begin
  216.   for i:=ord('A') to ord('Z') do
  217.     category_total[i]:=0;
  218.   total_time:=0;
  219.   lowvideo; writeln; writeln;
  220.   writeln('LOG''s summary function requested.  This function will list the contents');
  221.   writeln('of the TIME.LOG file to the screen.  You may also have a copy written');
  222.   writeln('to a disk file named TIME.TXT, which can be viewed by a word processor');
  223.   writeln('or read into a spreadsheet.'); writeln; writeln;
  224.   write('Would you like a disk file record of your logon/logoff file? (Y/N) ');
  225.   read(kbd,ch); writeln(upcase(ch));
  226.   writeln;
  227.   if upcase(ch)='Y' then disk_file:=true else disk_file:=false;
  228.   if disk_file then
  229.   begin {get file specs}
  230.     write('Would you like the file comma-delimited for spreadsheet use? (Y/N) ');
  231.     read(kbd,ch); writeln(upcase(ch));
  232.     if upcase(ch)='Y' then delimited:=true else delimited:=false;
  233.     writeln;
  234.     write('Would you like the file written to ',textfilename,'? (Y/N) ');
  235.     read(kbd,ch); writeln(upcase(ch));
  236.     if upcase(ch)='N' then
  237.     begin  {get new output filename}
  238.       writeln;
  239.       write('New drive/path/filename (max. 24 characters): ________________________');
  240.       gotoXY(47,whereY);
  241.       readln(textfilename);
  242.     end;
  243.     Assign(textfile,textfilename);
  244.     rewrite(textfile);
  245.     if delimited then
  246.     begin
  247.       write(textfile,'"USAGE","DATE","START TIME","END TIME",');
  248.       writeln(textfile,'"RECORDED TIME","CALCULATED TIME","COMMENT"');
  249.     end
  250.     else
  251.     begin
  252.       writeln;
  253.       writeln(textfile,'     DATE     START      END     ELAPSED     COMMENT');
  254.     end;
  255.   end;  {get file specs}
  256.   counter:=22;
  257.   for i:=0 to filesize(logfile)-1 do
  258.   begin {read file loop}
  259.     counter:=counter+1;
  260.     if counter=23 then
  261.     begin
  262.       write('Press any key to continue... ');
  263.       read(kbd,ch); writeln; writeln;
  264.       writeln('     DATE     START      END     ELAPSED    COMMENT');
  265.       counter:=0;
  266.     end;
  267.     seek(logfile,i);
  268.     read(logfile,log_data);
  269.     with log_data do
  270.       begin
  271.         write(usage,'  ',start_date_log,'  ',start_time_log,'  ');
  272.         writeln(end_time_log,'  ',elapsed_log,'  ',comment);
  273.         if disk_file then
  274.         begin
  275.           if delimited then
  276.           begin
  277.             write(textfile,'"',usage,'","',start_date_log,'","',start_time_log,'","');
  278.             write(textfile,end_time_log,'","',elapsed_log,'",');
  279.             writeln(textfile,elapsed_real:6:2,',"',comment,'"');
  280.           end  {if delimited}
  281.         else
  282.           begin
  283.             write(textfile,usage,'  ',start_date_log,'  ',start_time_log,'  ');
  284.             write(textfile,end_time_log,'  ',elapsed_log,' ');
  285.             writeln(textfile,elapsed_real:6:2,' hours, ',comment);
  286.           end;
  287.         end;  {if disk file}
  288.       end; {with log_data}
  289.     if not (log_data.usage in ['A'..'Z']) then
  290.       log_data.usage:='@';
  291.     category_total[ord(log_data.usage)]:=
  292.       category_total[ord(log_data.usage)]+log_data.elapsed_real;
  293.     total_time:=total_time+log_data.elapsed_real;
  294.   end;  {read file loop}
  295.   close(logfile);
  296.   writeln; writeln; writeln('TOTAL LOGGED TIME: ',total_time:6:2,' Hours'); writeln;
  297.   if disk_file then
  298.   begin
  299.     if delimited then
  300.     begin
  301.       writeln(textfile); writeln(textfile);
  302.       writeln(textfile,'"TOTAL LOGGED TIME",',total_time:6:2); writeln(textfile);
  303.     end
  304.     else
  305.     begin
  306.       writeln(textfile); writeln(textfile);
  307.       writeln(textfile,'TOTAL LOGGED TIME: ',total_time:6:2,' Hours');
  308.       writeln(textfile);
  309.     end;
  310.   end; {if disk file}{header}
  311.   for i:= 64 to 90 do
  312.   begin  {total time by category}
  313.     if category_total[i]>0 then
  314.     begin
  315.       writeln(chr(i),': ',category_total[i]:6:2,' Hours (',
  316.         category_total[i]/total_time:4:2,'% of total)');
  317.       if disk_file and (not delimited) then writeln(textfile,chr(i),': ',category_total[i]:6:2,
  318.         ' Hours (',category_total[i]/total_time:4:2,'% of total)');
  319.       if disk_file and delimited then writeln(textfile,'"',chr(i),'",',category_total[i]:6:2,
  320.         category_total[i]/total_time:4:2);
  321.     end;
  322.   end; {total time by category}
  323.   if disk_file then close(textfile);
  324.   writeln;
  325.   halt;
  326. end; {subroutine}
  327.  
  328. procedure give_instructions;
  329. begin
  330.   writeln;
  331.   writeln('LOG Time tracking utility');
  332.   writeln;
  333.   writeln('Usage:');
  334.   writeln('   "LOG APPLICATION /X"   to begin time tracking, where "APPLICATION"');
  335.   writeln('                          is the name of your project (up to 12');
  336.   writeln('                          characters) and the optional switch "X" can');
  337.   writeln('                          be any character A through Z (LOG assumes');
  338.   writeln('                          "B" is for business, "P" is for personal).');
  339.   writeln;
  340.   writeln('   "LOG OFF"              to end time tracking on current project');
  341.   writeln;
  342.   writeln('   "LOG /*"               to view log file and optionally write it');
  343.   writeln('                          to disk for use in an application program.');
  344.   writeln;
  345.   halt;
  346. end;
  347.  
  348. Begin   {program}
  349.   lowvideo;
  350.   initialize_variables;
  351.   Buffer := CL;        {move command line into buffer}
  352.   if length(buffer)=0 then give_instructions;
  353.   if length(buffer)>0 then
  354.   begin  {check for logoff flag}
  355.     if uppercase(buffer,2,length(buffer))='OFF' then
  356.     begin
  357.       logoff:=true;
  358.       delete(buffer,1,4);
  359.     end; {if uppercase... }
  360.     if (pos('/',buffer)>0) and (length(buffer)>pos('/',buffer)) then
  361.     begin  {set business/personal flag}
  362.       log_data.Usage:=upcase(buffer[pos('/',buffer)+1]);
  363.       Delete(buffer,pos('/',buffer),2);
  364.     end;
  365.     while (length(buffer)>0) and (buffer[1]=' ') do delete(buffer,1,1);
  366.     while (length(buffer)>0) and (buffer[length(buffer)]=' ') do delete(buffer,length(buffer),1);
  367.   end; {if length...}
  368.  
  369.   Assign(logfile,logfilename);
  370. {$I-}
  371.   Reset(logfile);
  372. {$I+}
  373.   If (ioresult = 1) then
  374.   begin
  375.     rewrite(logfile);
  376.     write(logfile,log_data);
  377.     writeln('Can''t find ',logfilename,',--creating new file.');
  378.     delay(200);
  379.   end;
  380.  
  381.   if log_data.usage='*' then summarize;
  382.  
  383.   if logoff then
  384.   begin
  385.     seek(logfile,filesize(logfile)-1);
  386.     read(logfile,log_data);
  387.     with log_data do
  388.     begin
  389.       end_time_log:= Time;
  390.       elapsed_log:= elapsed_time(start_time_log,end_time_log);
  391.       elapsed_real:=elapsed_real_time(elapsed_time(start_time_log,end_time_log));
  392.     end; {with log_data}
  393.   end {if logoff}
  394.     else seek(logfile,filesize(logfile));
  395.   if length(buffer)>0 then log_data.comment:=copy(buffer,1,12);
  396.   Write(logfile,log_data);
  397.   Close(logfile);
  398.  
  399.   if logoff then
  400.   begin
  401.     writeln;
  402.     writeln;
  403.     write('Logged ',log_data.elapsed_log);
  404.     if length(log_data.comment)>0 then write(' toward ',log_data.comment);
  405.     if log_data.usage<>#0 then
  406.     begin
  407.       if log_data.usage='B' then write(' as Business time');
  408.       if log_data.usage='P' then write(' as Personal time');
  409.       if log_data.usage='@' then write(' as Misc/Administrative time');
  410.       if not (log_data.usage in ['B','P','@']) then
  411.         write(' as "',log_data.usage,'" time');
  412.     end; {if usage <>#0}
  413.     writeln('.');
  414.     delay(2000);
  415.   end; {if logoff}
  416. End.
  417.