Skip to content

Commit

Permalink
Merge pull request #59 from Zhilin-Huang/logging-v1.0
Browse files Browse the repository at this point in the history
Add logging for Parser, Ui, DeleteCommand, ListCommand, and FlashcardList
  • Loading branch information
Zhilin-Huang authored Mar 15, 2020
2 parents 294dc96 + 3edf977 commit 3ba09d4
Show file tree
Hide file tree
Showing 14 changed files with 181 additions and 51 deletions.
8 changes: 6 additions & 2 deletions src/main/java/seedu/tp/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,18 @@ private void setup() {
parser = new Parser(flashcardFactory, flashcardList, groupFactory, groupList, ui);

LoggerUtils.createFolder(LOG_FOLDER);

try {
Flashcard.setupLogger();
FlashcardFactory.setupLogger();
FlashcardList.setupLogger();
FlashcardGroup.setupLogger();
Command.setupLogger();
Ui.setupLogger();
Parser.setupLogger();
} catch (IOException e) {
ui.sendLoggingSetupFailedMessage();
}
}
}

private void runLoop() {
Expand Down
24 changes: 24 additions & 0 deletions src/main/java/seedu/tp/commands/Command.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,35 @@

import seedu.tp.exceptions.HistoryFlashcardException;

import java.io.IOException;
import java.util.logging.FileHandler;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.logging.SimpleFormatter;

import static seedu.tp.utils.Constants.LOG_FOLDER;

/**
* Command used in the application.
*/
public abstract class Command {

protected static final Logger LOGGER = Logger.getLogger(Command.class.getName());
private static final String FILE_PATH = LOG_FOLDER + "command.log";

/**
* Set up the command logger. Call once at the start of the program.
*
* @throws IOException when logger set up failed
*/
public static void setupLogger() throws IOException {
LOGGER.setLevel(Level.ALL);
LOGGER.setUseParentHandlers(false);
FileHandler fileHandler = new FileHandler(FILE_PATH, true);
fileHandler.setFormatter(new SimpleFormatter());
LOGGER.addHandler(fileHandler);
}

/**
* Executes the command.
*/
Expand Down
7 changes: 4 additions & 3 deletions src/main/java/seedu/tp/commands/DeleteCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,17 +40,18 @@ public FlashcardList getFlashcardList() {
}

@Override
public void execute()
throws InvalidFlashcardIndexException {
public void execute() throws InvalidFlashcardIndexException {
LOGGER.info("Deleting flashcard at index: " + index);
flashcardList.deleteFlashcard(index);
LOGGER.info("Deleted flashcard at index: " + index);
}

@Override
public boolean equals(Object obj) {
if (!(obj instanceof DeleteCommand)) {
return false;
}
if (obj == this) {
if (this == obj) {
return true;
}

Expand Down
8 changes: 5 additions & 3 deletions src/main/java/seedu/tp/commands/ListCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ public class ListCommand extends Command {
private Ui ui;

/**
* Constructor for ListCommand.
* Constructor for list command.
*
* @param flashcardList the flashcard list to be used in the command
* @param ui the UI object ot be used in the command
* @param flashcardList flashcard list for the command to execute on
* @param ui the UI class to be used by the command
*/
public ListCommand(FlashcardList flashcardList, Ui ui) {
assert flashcardList != null : "Invalid null FlashcardList!";
Expand All @@ -35,7 +35,9 @@ public FlashcardList getFlashcardList() {

@Override
public void execute() {
LOGGER.info("Listing flashcards...");
ui.listAllFlashcards(flashcardList);
LOGGER.info("Listed flashcards");
}

@Override
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/seedu/tp/flashcard/EventFlashcard.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
public class EventFlashcard extends Flashcard {
private LocalDate startDate;
private LocalDate endDate;

/**
* Constructs an <code>EventFlashcard</code>.
*/
Expand Down
9 changes: 5 additions & 4 deletions src/main/java/seedu/tp/flashcard/Flashcard.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,14 @@
* Abstract flashcard class to represent basic properties of flashcard.
*/
public abstract class Flashcard implements Comparable<Flashcard> {
protected static final String FILE_PATH = LOG_FOLDER + "flashcard.log";
protected static final Logger LOGGER = Logger.getLogger(Flashcard.class.getName());

protected String name;
protected String summary;
protected List<String> details;
protected boolean isReviewed;
protected PriorityLevel pl;
protected static final Logger LOGGER = Logger.getLogger(Flashcard.class.getName());
public static final String FILE_PATH = LOG_FOLDER + "flashcard.log";

protected Flashcard(String name, String summary, List<String> details) {
this.name = name;
Expand All @@ -31,7 +32,7 @@ protected Flashcard(String name, String summary, List<String> details) {

/**
* Set up the flashcard logger. Call once at the start of the program.
*
*
* @throws IOException when logger set up failed
*/
public static void setupLogger() throws IOException {
Expand All @@ -41,7 +42,7 @@ public static void setupLogger() throws IOException {
fileHandler.setFormatter(new SimpleFormatter());
LOGGER.addHandler(fileHandler);
}

protected static String getDetailsString(List<String> details) {
StringBuilder detailsStringBuilder = new StringBuilder();
for (String detail : details) {
Expand Down
8 changes: 3 additions & 5 deletions src/main/java/seedu/tp/flashcard/FlashcardFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,13 @@
import java.util.logging.Logger;
import java.util.logging.SimpleFormatter;

import static seedu.tp.utils.Constants.LOG_FOLDER;

/**
* Flashcard factory class to create flashcards given string.
*/
public class FlashcardFactory {
private Ui ui;
private static Logger LOGGER = Logger.getLogger(FlashcardFactory.class.getName());

private Ui ui;

public FlashcardFactory(Ui ui) {
this.ui = ui;
}
Expand All @@ -34,7 +32,7 @@ public static void setupLogger() throws IOException {
fileHandler.setFormatter(new SimpleFormatter());
LOGGER.addHandler(fileHandler);
}

/**
* Create a <code>Flashcard</code> given a string.
*
Expand Down
51 changes: 44 additions & 7 deletions src/main/java/seedu/tp/flashcard/FlashcardList.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,44 @@

import seedu.tp.exceptions.InvalidFlashcardIndexException;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.logging.FileHandler;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.logging.SimpleFormatter;

import static seedu.tp.utils.Constants.LOG_FOLDER;

/**
* List of flashcards.
*/
public class FlashcardList {

private static final String FILE_PATH = LOG_FOLDER + "flashcard_list.log";
private static final Logger LOGGER = Logger.getLogger(FlashcardList.class.getName());

private List<Flashcard> flashcards;

/**
* Constructor for FlashcardList.
*/
public FlashcardList() {
this.flashcards = new ArrayList<Flashcard>();
this.flashcards = new ArrayList<>();
}

/**
* Constructor for FlashcardList.
*
* @param flashcardList the list of flashcards to be added to the flashcard list
* @param flashcardList the list of flashcards to be added
*/
public FlashcardList(List<Flashcard> flashcardList) {
this();

assert flashcardList != null : "Invalid null flashcard list!";

this.flashcards = new ArrayList<>();
this.flashcards.addAll(flashcardList);
}

Expand All @@ -38,14 +50,28 @@ public FlashcardList(List<Flashcard> flashcardList) {
* @param flashcardList the flashcard list to be copied from
*/
public FlashcardList(FlashcardList flashcardList) {
this();

assert flashcardList != null : "Invalid null FlashcardList!";

this.flashcards = new ArrayList<>();
for (int i = 0; i < flashcardList.getTotalFlashcardNum(); i++) {
this.flashcards.add(flashcardList.getFlashcardAtIdx(i));
}
}

/**
* Set up the FlashcardList logger. Call once at the start of the program.
*
* @throws IOException when logger set up failed
*/
public static void setupLogger() throws IOException {
LOGGER.setLevel(Level.ALL);
LOGGER.setUseParentHandlers(false);
FileHandler fileHandler = new FileHandler(FILE_PATH, true);
fileHandler.setFormatter(new SimpleFormatter());
LOGGER.addHandler(fileHandler);
}

/**
* Adds a flashcard to the list.
*
Expand All @@ -56,6 +82,7 @@ public FlashcardList addFlashcard(Flashcard flashcard) {
assert flashcard != null : "Invalid null flashcard!";

flashcards.add(flashcard);
LOGGER.info("Added flashcard " + flashcard.getName() + " to list");
return this;
}

Expand All @@ -67,15 +94,19 @@ public FlashcardList addFlashcard(Flashcard flashcard) {
*/
public Flashcard deleteFlashcard(int index) throws InvalidFlashcardIndexException {
try {
return flashcards.remove(index);
Flashcard flashcard = flashcards.remove(index);
LOGGER.info("Deleted flashcard " + flashcard.getName() + " from list");
return flashcard;
} catch (IndexOutOfBoundsException e) {
LOGGER.warning("IndexOutOfBoundsException occurred when deleting flashcard at index " + index);
LOGGER.warning("Throwing InvalidFlashcardIndexException...");
throw new InvalidFlashcardIndexException();
}
}

/**
* Return whether or not this FlashcardList contains specified flashcard.
*
*
* @param flashcard the flashcard to check
* @return whether or not this FlashcardList contains specified flashcard
*/
Expand Down Expand Up @@ -128,6 +159,13 @@ public List<Flashcard> getFlashcards() {
*/
@Override
public boolean equals(Object obj) {
if (!(obj instanceof FlashcardList)) {
return false;
}
if (this == obj) {
return true;
}

FlashcardList otherFlashcards = (FlashcardList) obj;
if (this.getTotalFlashcardNum() != otherFlashcards.getTotalFlashcardNum()) {
return false;
Expand All @@ -137,7 +175,6 @@ public boolean equals(Object obj) {
Collections.sort(flashcardList);
List<Flashcard> otherFlashcardList = new ArrayList<Flashcard>(otherFlashcards.getFlashcards());
Collections.sort(otherFlashcardList);

for (int idx = 0; idx < this.getTotalFlashcardNum(); idx++) {
if (!flashcardList.get(idx).equals(otherFlashcardList.get(idx))) {
return false;
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/seedu/tp/group/FlashcardGroup.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@
* A group of flashcards which have some of the same characteristics.
*/
public class FlashcardGroup {
protected static final Logger LOGGER = Logger.getLogger(FlashcardGroup.class.getName());
private static final String FILE_PATH = LOG_FOLDER + "flashcard_group.log";
private String name;
private String description;
private FlashcardList groupCards = new FlashcardList();
protected static final Logger LOGGER = Logger.getLogger(FlashcardGroup.class.getName());
private static final String FILE_PATH = LOG_FOLDER + "flashcard_group.log";

/**
* Constructs a <code>FlashcardGroup</code> using some existing cards from the users original list.
Expand Down
8 changes: 1 addition & 7 deletions src/main/java/seedu/tp/group/GroupList.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,10 @@
import seedu.tp.flashcard.FlashcardList;
import seedu.tp.ui.Ui;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.FileHandler;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.logging.SimpleFormatter;

import static seedu.tp.utils.Constants.INDEX_FIELD;
import static seedu.tp.utils.Constants.LOG_FOLDER;
import static seedu.tp.utils.Constants.NAME_FIELD;

/**
Expand All @@ -31,7 +25,7 @@ public class GroupList {
public GroupList() {
this.groups = new ArrayList<FlashcardGroup>();
}

/**
* Adds a new group to the group list.
*
Expand Down
Loading

0 comments on commit 3ba09d4

Please sign in to comment.