The First Lab, Logging

CMPS 2020 Overview

website layout

connecting with atom and terminal

this lab is not that difficult but there are many specific parts that need to be done.
You need to make absolutely sure that you have completed the assignment and have done it correctly

1. create assignments directory
2. go to the proper directory and copy the required files into place
3. add code for the 2 functions and code to the main to call and test them
4. verify that the output to the console when you run your program matches the example
5. verify that the output to the log file when you run your program matches the logfile of the example
6. check that you have met all the requirements by answering the questions in checklist.txt
7. make sure that your files are complete and ready to be copied at 9pm by running 'make setPermissions'.

if your not familiar with the Linux file system here is a short video

file commands

Watch a video about using the logging library for debugging purposes

for all your labs and homework assignments you will need to create a special directory

if you do not have your login for Odin check your  (not ) email account
for the remainder of this class I will not sure this email I will contact you via your  email account

we will be doing labs on campus
working from home you can use the Atom editor to work on your homework
setting up the Atom editor


to create your assignments directory

run the following script


this will prompt you for your login credentials

it will then create all your directories for you homework and labs, this is where they will be collected from when they are due.
you do not need to email the instructor to notify them you are done or send any files.


on the command line run " ls -lah ~ | grep assignments "

it should not have the text student anywhere on the line that this command displays, if so stop right here and ask for help

cd to the lab01 folder

cd ~/assignments/cmps2020/lab/lab01/

this is where you will do your work for lab 1

copy all the files over from /home/fac/msarr/public_files/cmps2020/lab/lab01/

you can do that with the following

cp -r /home/fac/msarr/public_files/cmps2020/lab/lab01/* /home/stu/YOURUSERNAME/assignments/cmps2020/lab/lab01/
or ( note the . instead of the destination folder, it means "this directory")
cp -r /home/fac/msarr/public_files/cmps2020/lab/lab01/* .

this is the same process you will use to copy the files to start every lab, just replace the lab number

read the file checklist.txt, type yes after each statement so that I know you have read and understand the statements. File Logging is a common method of finding errors in code. Many applications that run in the background for an operating system use file logging Linux has the /var/log/ directory that contains operational logs from countless applications running right now on this server. Windows system have a built in event log and the Event Viewer application to read the logged messages. We will primarily be using logging to provide diagnostic information about the code you will be writing. REQUIREMENTS: 1. You must include the logging library functions.... #include "easylogging++.h" 2. You must define the logging struct above your main.... INITIALIZE_EASYLOGGINGPP this should be done in the main file for a few reasons, it is only needed when it actually runs, not for compilation, there can be only one instance in any runable file. so ONLY put this in your main file. 3. You must include the code to set up the logfile at the start of your main and nowhere else. 4. For any files where you would like to use the logging macros or functions you will want to include "easylogging++.h" LOOK AT THE LOGGING EXAMPLES before starting this lab, the link is on the main class page here is a minimal main File: sample.cpp

#include "cmpslib19.h"  // general purpose functions for use in this class 
#include "easylogging++.h"  // function defenitions for the logging functions we will be using 
INITIALIZE_EASYLOGGINGPP    // this must be above your main

int main()
  InitializeLogger("main1.log"); // this is the name of the file the logging output will go to

  // you can now use the logging functions
  LOG (INFO) << "this text will be written to the filename we opened above.\n";

  return 0;

you can use this as the start for programs using the library since we have some logging functions that we need to use that are in both a .h and .cpp file we can compile those first. g++ -D ELPP_FRESH_LOG_FILE -D ELPP_NO_DEFAULT_LOG_FILE -c this will produce the file easylogging.o that we will need to compile our main1.cpp, main2.cpp and main3.cpp PART 1 you will emulate the behavior of example1 run it with ./example1 you wont see any output to the console , it writes to a file only look at example1.log that executing the program created cat example1.log File: example1.log

2023-08-28 19:03:54,537 INFO [default] I'm at line 18

2023-08-28 19:03:54,537 INFO [default] I'm in function main

2023-08-28 19:03:54,537 INFO [default] I'm in function int main()

2023-08-28 19:03:54,537 INFO [default] I was compiled on Aug 28 2023 at 19:03:51

2023-08-28 19:03:54,537 INFO [default] this code is in the file main1.cpp

modify the provided main1.cpp to match the example1 you will need to use some of the macros defined here and here when you compile your code the macros like __LINE__ , __FILE__ the preprocessor will replace the macro with a piece of text with the desired output for example LOG (INFO) << "this is on line " << __LINE__ << endl; cout << "this is on line " << __LINE__ << endl; __LINE__ will be replaced with the line # make your main1.ccp have the same output as example1 have it write to the logfile "main1.log" compile with g++ -Wall -o runme1 -D LOGGING_ON main1.cpp easylogging++.o run with ./runme1 PART 2 complete main2.cpp to add logging in the loop that will write the line number, the function that is about to be called and the value passed to it WHY? every time this program runs it will stop at a different place. Your task is not to fix this program but instead to learn how to use logging to determine how to isolate where the error likely is. This is the first thing we do in this class is to learn about logging as it is required in everything you do in this class unless I say otherwise logging is required. after modifying this main the log file should contain information that we can use to diagnose the problem... what was this last thing this program did before it stopped? well look at the last line in the logfile, its just that easy.. if you do the logging properly. File: main2.cpp

#include <algorithm>    // std::shuffle
#include <random>       // std::default_random_engine
#include <chrono>       // std::chrono::system_clock

#include "main2_functions.h"  // include the file with the function prototypes for this task

#include "cmpslib19.h"  // general purpose functions for use in this class

#include "easylogging++.h"  // function defenitions for the logging functions we will be using
INITIALIZE_EASYLOGGINGPP    // this must be above your main

int main()
  // set up your code to log to "main3.log"
  // you will need to call the InitializeLogger funciton , use main1 or main2 to see this in use

  // make an array of some values
  int myarray[] =  {5,10,17,20,27,30,37,40,45,50,55,60,65,70,73,80,85,90,95,100};
  // get an integer value from the system clock, every time you run the program it will be different
  unsigned seed = std::chrono::system_clock::now().time_since_epoch().count();
  // shuffle the values in the array
  shuffle (myarray,myarray+9, std::default_random_engine(seed));

  // dont worry if you dont understand the code above to shuffle the array , we will get to that later

  // now loop from 0-9 and call the functions with each value
  bool isprime,iseven,isodd;
  for(int loop=0;loop<20;loop++)
    // (done for you ) log before you call the IsPrime Function, make sure it matches the log of the example
    LOG (INFO) << "at line " << __LINE__ << " calling IsPrime(" << myarray[loop] << ")" << endl;
    isprime = IsPrime(myarray[loop]);
    // log before you call the IsEven Function, make sure it matches the log of the example
    iseven  = IsEven(myarray[loop]);
    // log before you call the IsOdd Function, make sure it matches the log of the example
    isodd   = IsOdd(myarray[loop]);

    cout << myarray[loop] <<   "  even: " << boolalpha << iseven << " odd: " << isodd << " prime: " << isprime << endl;

  return 0;

before the calls to IsOdd, IsEven and IsPrime log what line of code you are at, what function you are going to call next and the value you will be passing to that function. each of these functions will sometimes throw an exception and cause the program to stop running, your job is not to try to catch the exception but instead add in the required logging. every time you run this program it will stop running at a different location. if your logging is correct you should be able to tell exactly what function called when the program was stopped and what value was passed to it. This is a diagnostic skill you must develop during this class. compile with g++ -Wall -o runme2 -D LOGGING_ON main2.cpp easylogging++.o main2_functions.o you can run the example with ./example2 it will log to example2.log run it several times to see the difference each time it runs look at the output in example2.log