Homework #3

Assigned: September 10
Due: September 18, 12:30pm

Problem

(60 points) Implement a logging library in Go 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 that were printed by some of your programs, or for program debugging and testing.

Requirements

  1. Your logging facility must support the follow interface:
    type Data_t struct {
       Logged_time time.Time
       Str string
    }
    
    type Log_t struct {
       item Data_t
       next *Log_t
    }
    
    // Public functions
    func Addmsg (data Data_t) (int,error)
    func Clearlog()
    func Getlog () string
    func Savelog (filename string) error
    
  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 writes 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 copies the entire log into a string, and returns a pointer to that string.
  6. If successful, Addmsg returns 0,nil. If unsuccessful, Addmsg returns -1,errors.New("filled in with appropriate error message").
  7. If successful, Getlog returns a pointer to the log string. If unsuccessful, Getlog returns nil.
  8. If successful, Savelog returns nil. If unsuccessful, Savelog returns err.
  9. Use the following format for the output of Getlog and Savelog, where [ ] represents one single space character:
    Time:[ ]MM/DD/YYYY[ ]HH:MM:SS\n
    Message:[ ]This is message 1\n
    \n
    Time:[ ]MM/DD/YYYY[ ]HH:MM:SS\n
    Message:[ ]This is message 2\n
    \n
    ...
    ...
    
  10. If an application tries to invoke Savelog on an empty list object, do not write any data to the disk file; do not even open and create it.
  11. If an application tries to invoke Getlog on an empty list object, simply return nil,errors.New("filled in with appropriate error message") (the empty string). It is then the caller's responsibility to perform error checking, and check the value of the string returned (e.g., before printing it) to make sure it points to valid memory. You may want to define an isempty() function.
  12. Do not exit from functions. Instead, return an error value to allow the calling program flexibility in handling the error.
  13. Never allocate more memory than necessary for anything.
  14. All implementation details must be hidden from any application which uses the logging library.
  15. Your program must be written in Go and compile without errors or warnings using go build on our system.
  16. Use the following skeleton for loggerlib.go.
    package loggerlib
    
    import (
       "time"
       "errors")
     
    type Data_t struct {
       Logged_time time.Time
       Str string
    }
    
    type Log_t struct {
       item Data_t
       next *Log_t
    }
    
    // global, private variables
    var headptr *Log_t
    var tailptr *Log_t
    
    func Addmsg (data Data_t) (int,error) {
       ...
    }
    
    func Clearlog() {
       ...	
    }
    
    func Getlog() string {
       ...
    }
    
    func Savelog (filename string) error {
       ... 
    }
    

Hints

If designed properly a priori, the program required to solve this homework should occupy no more than 150 lines of code.

The following program demonstrates one way to format the time in Go in MM/DD/YYYY[ ]HH/MM/SS format:

package main

import (
   "fmt"
   "time"
   "strings"
)
func main() {
    /* formats current system time as "MM/DD/YYYY[ ]HH:MM:SS" */

    var timestr string
    var months map[string]string

    months = make(map[string]string)

    months["Jan"] = "01"; months["Feb"] = "02"; months["Mar"] = "03"
    months["Apr"] = "04"; months["May"] = "05"; months["Jun"] = "06"
    months["Jul"] = "07"; months["Aug"] = "08"; months["Sep"] = "09"
    months["Oct"] = "10"; months["Nov"] = "11"; months["Dec"] = "12"

    current_time := time.Now().Local()
    const layout = "Jan 2 2006 15:04:05"
    timeslice := strings.Split(current_time.Format(layout), " ")
    timestr = months[timeslice[0]] + "/"

    if len(timeslice[1]) == 1 {
       timestr += "0"
    }
    timestr += timeslice[1] + "/" + timeslice[2] + " " + timeslice[3]
    fmt.Println (timestr)
}

Sample application

The source code files logapp.go and logapp_helperfuns.go constitute a sample application for the logging library developed in this assignment and can be used for purposes of testing. These files must not be modified at all. 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.go, where <logname> is your login name (e.g., FA_15_CPS444_03). Only your source file loggerlib.go will be electronically collected from this location in your Linux 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