Homework #3

Assigned: September 23
Due: September 30, 4:30pm

Problem

(71 points) Implement a logging library which is similar to the list object developed in class. The logging utility allows the caller to save a message at the end of a list. The logger also records the time which the message was logged.

You can use the logging facility to save the messages which were printed by some of your programs, or for program debugging and testing.

Requirements

  1. Use the following header file loggerlib.h for your logging facility.
    #include <time.h>
    
    typedef struct data_struct {
       time_t time;
       char* string;
    } data_t;
    
    int   addmsg (data_t data);
    void  clearlog (void);
    char* getlog (void);
    int   savelog (char* filename);
    
  2. The data_t structure and the addmsg function have the roles described in class. Recall that addmsg copies the node and inserts it at the end of the list.
  3. The savelog function saves the logged messages to a disk file.
  4. The clearlog function releases all the storage which has been allocated for the logged messages and empties the list of logged messages.
  5. The getlog function allocates enough space for a string containing the entire log, copies the log into this string, and returns a pointer to the string. It is the responsibility of the calling program to free this memory when necessary.
  6. If successful, addmsg and savelog return 0. If unsuccessful, addmsg and savelog return -1.
  7. A successful getlog call returns a pointer to the log string. An unsuccessful getlog call returns NULL.
  8. Use the following format for the getlog and savelog output, where [ ] represents one single space character:
    Time:[ ]MM/DD/YY[ ]HH/MM/SS\n
    Message:[ ]This is message 1\n
    \n
    Time:[ ]MM/DD/YY[ ]HH/MM/SS\n
    Message:[ ]This is message 2\n
    \n
    ...
    ...
    
  9. The following programs demonstrates how to format the time in MM/DD/YY[ ]HH/MM/SS format:
    #include<stdio.h>
    #include<time.h>
    
    main() {
       time_t t;
       char* s = malloc (sizeof (char)*19);
    
       if (time (&t) == -1)
          return -1;
    
       struct tm* loct = localtime (&t);
       strftime (s, 18, "%x %X ", loct);
       printf ("%s\n", s);
    }
    
  10. If an application tries to invoke savelog on an empty list object, do not write any thing to the data file (do not even open and create it).
  11. If an application tries to invoke getlog on an empty list object, simply return NULL (the empty string). It is then the caller's responsibility to perform error checking, and check the value of the char* returned (e.g., before printing it) to make sure it points to valid memory. It might be a good idea to define a static isempty() function.
  12. Never allocate more memory than necessary for anything.
  13. All implementation details must be hidden from any application which uses the logging library.
  14. Your program must be written in C (not C++) and compile without errors or warnings using gcc on our system.
  15. Use the following skeleton for loggerlib.c.
    #include <stdlib.h>
    #include <string.h>
    #include "loggerlib.h"
     
    typedef struct list_struct {
       data_t item;
       struct list_struct* next;
    } log_t;
     
    static log_t* headptr = NULL;
    static log_t* tailptr = NULL;
     
    int addmsg (data_t data) {
       return 0;
    }
    
    void clearlog (void) {  
    } 
    
    char* getlog (void) {
       return NULL;
    }
     
    int savelog (char* filename) {
       return 0;
    }
    
If designed properly, the program required to solve this homework should occupy no more than 200 lines of code.

Sample application

The source code files logapp.c and logapplib.c constitute a sample application for the logging library developed in this assignment and can be used for purposes of testing. Remember, your library must work in any application which conforms to the prototypes of the services which the logging library provides.

How to submit

Note: All directory and filenames below are case-sensitive. You must use the directory and filenames exactly as shown below (i.e., all lower case).

Prepare your source code file as /home/<logname>/homeworks/hw3/loggerlib.c, where <logname> is your login name (e.g., cps444-n1.01). Only your source file loggerlib.c will be electronically collected from this location in your UNIX account on the deadline.

Failure to follow these submission requirements will result in a 10% penalty.

Evaluation

Ninety percent of your score will come from correctness and 10% of your score will come from following our programming style guide. Applicable submission penalties will then be applied.


Return Home