From d80ba0c3ce2ee5aa9274fe51e289879e5e2394fc Mon Sep 17 00:00:00 2001 From: aj Date: Thu, 2 May 2019 10:04:35 +0100 Subject: [PATCH] added logging --- .../java/sarsoo/fmframework/fm/FmNetwork.java | 153 ++++++------------ .../sarsoo/fmframework/fm/FmUserNetwork.java | 49 +++--- .../fmframework/fx/TextAreaConsole.java | 2 +- .../fx/controller/RootController.java | 7 +- src/main/java/sarsoo/fmframework/log/Log.java | 68 ++++++++ .../java/sarsoo/fmframework/log/Logger.java | 15 ++ .../{util => log/console}/Console.java | 2 +- .../{util => log/console}/ConsoleHandler.java | 2 +- .../fmframework/log/entry/ErrorEntry.java | 38 +++++ .../fmframework/log/entry/InfoEntry.java | 27 ++++ .../fmframework/log/entry/LogEntry.java | 60 +++++++ .../sarsoo/fmframework/util/Reference.java | 1 + .../fmframework/fm/FmUserNetworkTest.java | 5 + 13 files changed, 296 insertions(+), 133 deletions(-) create mode 100644 src/main/java/sarsoo/fmframework/log/Log.java create mode 100644 src/main/java/sarsoo/fmframework/log/Logger.java rename src/main/java/sarsoo/fmframework/{util => log/console}/Console.java (59%) rename src/main/java/sarsoo/fmframework/{util => log/console}/ConsoleHandler.java (85%) create mode 100644 src/main/java/sarsoo/fmframework/log/entry/ErrorEntry.java create mode 100644 src/main/java/sarsoo/fmframework/log/entry/InfoEntry.java create mode 100644 src/main/java/sarsoo/fmframework/log/entry/LogEntry.java diff --git a/src/main/java/sarsoo/fmframework/fm/FmNetwork.java b/src/main/java/sarsoo/fmframework/fm/FmNetwork.java index e69729d..d04b38e 100644 --- a/src/main/java/sarsoo/fmframework/fm/FmNetwork.java +++ b/src/main/java/sarsoo/fmframework/fm/FmNetwork.java @@ -1,5 +1,11 @@ package sarsoo.fmframework.fm; +import sarsoo.fmframework.log.Log; +import sarsoo.fmframework.log.Logger; +import sarsoo.fmframework.log.console.ConsoleHandler; +import sarsoo.fmframework.log.entry.ErrorEntry; +import sarsoo.fmframework.log.entry.InfoEntry; +import sarsoo.fmframework.log.entry.LogEntry; import sarsoo.fmframework.music.Album; import sarsoo.fmframework.music.Album.AlbumBuilder; import sarsoo.fmframework.music.Artist; @@ -8,7 +14,6 @@ import sarsoo.fmframework.music.FMObj; import sarsoo.fmframework.music.Track; import sarsoo.fmframework.music.Track.TrackBuilder; import sarsoo.fmframework.music.Wiki; -import sarsoo.fmframework.util.ConsoleHandler; import java.util.HashMap; @@ -49,8 +54,9 @@ public class FmNetwork { * @return Album */ public Album getAlbum(String name, String artist) { - if (ConsoleHandler.isVerbose()) - ConsoleHandler.getConsole().write(">>getAlbum: " + name + " " + artist); + + Log log = Logger.getLog(); + log.log(new LogEntry("getAlbum").addArg(name).addArg(artist)); HashMap parameters = new HashMap(); @@ -77,43 +83,32 @@ public class FmNetwork { try { builder.setMbid(albumJson.getString("mbid")); } catch (JSONException e) { - if (ConsoleHandler.isVerbose()) - ConsoleHandler.getConsole().write("ERROR: No MBID for " + nameIn + " , " + e.getMessage()); - else - System.err.println("ERROR: No MBID for " + nameIn + " , " + e.getMessage()); + log.log(new InfoEntry("getAlbum").addArg("no mbid for").addArg(nameIn).addArg(e.getMessage())); } try { builder.setUrl(albumJson.getString("url")); } catch (JSONException e) { - if (ConsoleHandler.isVerbose()) - ConsoleHandler.getConsole().write("ERROR: No Url for " + nameIn + " , " + e.getMessage()); - else - System.err.println("ERROR: No Url for " + nameIn + " , " + e.getMessage()); + log.log(new InfoEntry("getAlbum").addArg("no url for").addArg(nameIn).addArg(e.getMessage())); } try { builder.setListeners(albumJson.getInt("listeners")); } catch (JSONException e) { - if (ConsoleHandler.isVerbose()) - ConsoleHandler.getConsole().write("ERROR: No listeners for " + nameIn + " , " + e.getMessage()); - else - System.err.println("ERROR: No listeners for " + nameIn + " , " + e.getMessage()); + log.log(new InfoEntry("getAlbum").addArg("no listeners for").addArg(nameIn).addArg(e.getMessage())); } try { builder.setPlayCount(albumJson.getInt("playcount")); } catch (JSONException e) { - if (ConsoleHandler.isVerbose()) - ConsoleHandler.getConsole().write("ERROR: No play count for " + nameIn + " , " + e.getMessage()); - else - System.err.println("ERROR: No play count for " + nameIn + " , " + e.getMessage()); + log.log(new InfoEntry("getAlbum").addArg("no play count for").addArg(nameIn).addArg(e.getMessage())); } try { builder.setUserPlayCount(albumJson.getInt("userplaycount")); } catch (JSONException e) { - + log.log(new InfoEntry("getAlbum").addArg("no user play count for").addArg(nameIn) + .addArg(e.getMessage())); } try { @@ -125,19 +120,13 @@ public class FmNetwork { builder.setWiki(wiki); } catch (JSONException e) { - if (ConsoleHandler.isVerbose()) - ConsoleHandler.getConsole().write("ERROR: No wiki for " + nameIn + " , " + e.getMessage()); - else - System.err.println("ERROR: No wiki for " + nameIn + " , " + e.getMessage()); + log.log(new InfoEntry("getAlbum").addArg("no wiki for").addArg(nameIn).addArg(e.getMessage())); } return builder.build(); } catch (JSONException e) { - if (ConsoleHandler.isVerbose()) - ConsoleHandler.getConsole().write("ERROR: Album Name Not Found, " + e.getMessage()); - else - System.err.println("ERROR: Album Name Not Found, " + e.getMessage()); + log.log(new InfoEntry("getAlbum").addArg("album name not found").addArg(e.getMessage())); } return null; @@ -151,8 +140,9 @@ public class FmNetwork { * @return Artist */ public Artist getArtist(String name) { - if (ConsoleHandler.isVerbose()) - ConsoleHandler.getConsole().write(">>getArtist: " + name); + + Log log = Logger.getLog(); + log.log(new LogEntry("getArtist").addArg(name)); HashMap parameters = new HashMap(); @@ -176,44 +166,34 @@ public class FmNetwork { try { builder.setMbid(artistJson.getString("mbid")); } catch (JSONException e) { - if (ConsoleHandler.isVerbose()) - ConsoleHandler.getConsole().write("ERROR: No MBID for " + artistName + " , " + e.getMessage()); - else - System.err.println("ERROR: No MBID for " + artistName + " , " + e.getMessage()); + log.log(new InfoEntry("getArtist").addArg("no mbid for").addArg(artistName).addArg(e.getMessage())); } try { builder.setUrl(artistJson.getString("url")); } catch (JSONException e) { - if (ConsoleHandler.isVerbose()) - ConsoleHandler.getConsole().write("ERROR: No Url for " + artistName + " , " + e.getMessage()); - else - System.err.println("ERROR: No Url for " + artistName + " , " + e.getMessage()); + log.log(new InfoEntry("getArtist").addArg("no url for").addArg(artistName).addArg(e.getMessage())); } try { builder.setListeners(artistJson.getJSONObject("stats").getInt("listeners")); } catch (JSONException e) { - if (ConsoleHandler.isVerbose()) - ConsoleHandler.getConsole().write("ERROR: No listeners for " + artistName + " , " + e.getMessage()); - else - System.err.println("ERROR: No listeners for " + artistName + " , " + e.getMessage()); + log.log(new InfoEntry("getArtist").addArg("no listeners for").addArg(artistName) + .addArg(e.getMessage())); } try { builder.setPlayCount(artistJson.getJSONObject("stats").getInt("playcount")); } catch (JSONException e) { - if (ConsoleHandler.isVerbose()) - ConsoleHandler.getConsole() - .write("ERROR: No play count for " + artistName + " , " + e.getMessage()); - else - System.err.println("ERROR: No play count for " + artistName + " , " + e.getMessage()); + log.log(new InfoEntry("getArtist").addArg("no play count for").addArg(artistName) + .addArg(e.getMessage())); } try { builder.setUserPlayCount(artistJson.getJSONObject("stats").getInt("userplaycount")); } catch (JSONException e) { - + log.log(new InfoEntry("getArtist").addArg("no user play count for").addArg(artistName) + .addArg(e.getMessage())); } try { @@ -225,19 +205,13 @@ public class FmNetwork { builder.setWiki(wiki); } catch (JSONException e) { - if (ConsoleHandler.isVerbose()) - ConsoleHandler.getConsole().write("ERROR: No wiki for " + artistName + " , " + e.getMessage()); - else - System.err.println("ERROR: No wiki for " + artistName + " , " + e.getMessage()); + log.log(new InfoEntry("getArtist").addArg("no wiki for").addArg(artistName).addArg(e.getMessage())); } return builder.build(); } catch (JSONException e) { - if (ConsoleHandler.isVerbose()) - ConsoleHandler.getConsole().write("ERROR: Arist Name Not Found, " + e.getMessage()); - else - System.err.println("ERROR: Arist Name Not Found, " + e.getMessage()); + log.log(new InfoEntry("getArtist").addArg("artist name not found").addArg(e.getMessage())); } return null; @@ -252,8 +226,9 @@ public class FmNetwork { * @return Track */ public Track getTrack(String name, String artist) { - if (ConsoleHandler.isVerbose()) - ConsoleHandler.getConsole().write(">>getTrack: " + name + " " + artist); + + Log log = Logger.getLog(); + log.log(new LogEntry("getTrack").addArg(name).addArg(artist)); HashMap parameters = new HashMap(); @@ -280,43 +255,32 @@ public class FmNetwork { try { builder.setMbid(trackJson.getString("mbid")); } catch (JSONException e) { - if (ConsoleHandler.isVerbose()) - ConsoleHandler.getConsole().write("ERROR: No MBID for " + nameIn + " , " + e.getMessage()); - else - System.err.println("ERROR: No MBID for " + nameIn + " , " + e.getMessage()); + log.log(new InfoEntry("getTrack").addArg("no mbid for").addArg(nameIn).addArg(e.getMessage())); } try { builder.setUrl(trackJson.getString("url")); } catch (JSONException e) { - if (ConsoleHandler.isVerbose()) - ConsoleHandler.getConsole().write("ERROR: No Url for " + nameIn + " , " + e.getMessage()); - else - System.err.println("ERROR: No Url for " + nameIn + " , " + e.getMessage()); + log.log(new InfoEntry("getTrack").addArg("no url for").addArg(nameIn).addArg(e.getMessage())); } try { builder.setListeners(trackJson.getInt("listeners")); } catch (JSONException e) { - if (ConsoleHandler.isVerbose()) - ConsoleHandler.getConsole().write("ERROR: No listeners for " + nameIn + " , " + e.getMessage()); - else - System.err.println("ERROR: No listeners for " + nameIn + " , " + e.getMessage()); + log.log(new InfoEntry("getTrack").addArg("no listeners for").addArg(nameIn).addArg(e.getMessage())); } try { builder.setPlayCount(trackJson.getInt("playcount")); } catch (JSONException e) { - if (ConsoleHandler.isVerbose()) - ConsoleHandler.getConsole().write("ERROR: No play count for " + nameIn + " , " + e.getMessage()); - else - System.err.println("ERROR: No play count for " + nameIn + " , " + e.getMessage()); + log.log(new InfoEntry("getTrack").addArg("no play count for").addArg(nameIn).addArg(e.getMessage())); } try { builder.setUserPlayCount(trackJson.getInt("userplaycount")); } catch (JSONException e) { - + log.log(new InfoEntry("getTrack").addArg("no user play count for").addArg(nameIn) + .addArg(e.getMessage())); } try { @@ -328,19 +292,13 @@ public class FmNetwork { builder.setWiki(wiki); } catch (JSONException e) { - if (ConsoleHandler.isVerbose()) - ConsoleHandler.getConsole().write("ERROR: No wiki for " + nameIn + " , " + e.getMessage()); - else - System.err.println("ERROR: No wiki for " + nameIn + " , " + e.getMessage()); + log.log(new InfoEntry("getTrack").addArg("no wiki for").addArg(nameIn).addArg(e.getMessage())); } return builder.build(); } catch (JSONException e) { - if (ConsoleHandler.isVerbose()) - ConsoleHandler.getConsole().write("ERROR: Album Name Not Found, " + e.getMessage()); - else - System.err.println("ERROR: Album Name Not Found, " + e.getMessage()); + log.log(new InfoEntry("getTrack").addArg("track name not found").addArg(e.getMessage())); } return null; @@ -353,8 +311,8 @@ public class FmNetwork { * @return Refreshed Album */ public Album refresh(Album album) { - if (ConsoleHandler.isVerbose()) - ConsoleHandler.getConsole().write(">>refreshAlbum: " + album.getName() + " " + album.getArtist().getName()); + + Logger.getLog().log(new LogEntry("refreshAlbum").addArg(album.getName()).addArg(album.getArtist().getName())); return getAlbum(album.getName(), album.getArtist().getName()); } @@ -366,8 +324,8 @@ public class FmNetwork { * @return Refreshed Artist */ public Artist refresh(Artist artist) { - if (ConsoleHandler.isVerbose()) - ConsoleHandler.getConsole().write(">>refreshArtist: " + artist.getName()); + + Logger.getLog().log(new LogEntry("refreshArtist").addArg(artist.getName())); return getArtist(artist.getName()); } @@ -379,8 +337,8 @@ public class FmNetwork { * @return Refreshed Track */ public Track refresh(Track track) { - if (ConsoleHandler.isVerbose()) - ConsoleHandler.getConsole().write(">>refreshTrack: " + track.getName() + " " + track.getArtist().getName()); + + Logger.getLog().log(new LogEntry("refreshTrack").addArg(track.getName()).addArg(track.getArtist().getName())); Track refreshedTrack = getTrack(track.getName(), track.getArtist().getName()); @@ -440,11 +398,7 @@ public class FmNetwork { return new JSONObject(response.getBody().toString()); } else { - System.out.println(response.getBody()); - if (ConsoleHandler.isVerbose()) - ConsoleHandler.getConsole().write("ERROR : HTTP Request Error " + response.getStatus()); - else - System.err.println("ERROR : HTTP Request Error " + response.getStatus()); + Logger.getLog().logError(new ErrorEntry("HTTP Get").setErrorCode(response.getStatus())); return null; } } catch (UnirestException e) { @@ -453,15 +407,4 @@ public class FmNetwork { return null; } - - protected class RequestParam { - - int intparam; - String strparam; - - public RequestParam(int param) { - - } - - } } diff --git a/src/main/java/sarsoo/fmframework/fm/FmUserNetwork.java b/src/main/java/sarsoo/fmframework/fm/FmUserNetwork.java index ef25937..5eeeab1 100644 --- a/src/main/java/sarsoo/fmframework/fm/FmUserNetwork.java +++ b/src/main/java/sarsoo/fmframework/fm/FmUserNetwork.java @@ -8,10 +8,14 @@ import java.util.HashMap; import org.json.JSONArray; import org.json.JSONObject; +import sarsoo.fmframework.log.Log; +import sarsoo.fmframework.log.Logger; +import sarsoo.fmframework.log.console.ConsoleHandler; +import sarsoo.fmframework.log.entry.InfoEntry; +import sarsoo.fmframework.log.entry.LogEntry; import sarsoo.fmframework.music.Artist; import sarsoo.fmframework.music.Tag; import sarsoo.fmframework.music.Track; -import sarsoo.fmframework.util.ConsoleHandler; import sarsoo.fmframework.util.FMObjList; public class FmUserNetwork extends FmNetwork { @@ -63,8 +67,8 @@ public class FmUserNetwork extends FmNetwork { * @return User */ public User getUser() { - if (ConsoleHandler.isVerbose()) - ConsoleHandler.getConsole().write(">>getUser"); + + Logger.getLog().log(new LogEntry("getUser")); HashMap parameters = new HashMap(); @@ -84,8 +88,8 @@ public class FmUserNetwork extends FmNetwork { * @return User real name */ public String getUserRealName() { - if (ConsoleHandler.isVerbose()) - ConsoleHandler.getConsole().write(">>getUserRealname"); + + Logger.getLog().log(new LogEntry("getUserRealName")); return getUser().getRealName(); } @@ -96,8 +100,8 @@ public class FmUserNetwork extends FmNetwork { * @return Total scrobble count */ public int getUserScrobbleCount() { - if (ConsoleHandler.isVerbose()) - ConsoleHandler.getConsole().write(">>getUserScrobbleCount"); + + Logger.getLog().log(new LogEntry("getUserScrobbleCount")); return getUser().getScrobbleCount(); } @@ -108,8 +112,8 @@ public class FmUserNetwork extends FmNetwork { * @return Last track */ public Track getLastTrack() { - if (ConsoleHandler.isVerbose()) - ConsoleHandler.getConsole().write(">>getLastTrack"); + + Logger.getLog().log(new LogEntry("getLastTrack")); HashMap parameters = new HashMap(); @@ -135,8 +139,8 @@ public class FmUserNetwork extends FmNetwork { * @return Scrobble count today */ public int getScrobblesToday() { - if (ConsoleHandler.isVerbose()) - ConsoleHandler.getConsole().write(">>getScrobblesToday"); + + Logger.getLog().log(new LogEntry("getScrobblesToday")); LocalDate local = LocalDate.now(); @@ -166,8 +170,9 @@ public class FmUserNetwork extends FmNetwork { * @return Scrobble count */ public int getScrobbleCountByDate(int day, int month, int year) { - if (ConsoleHandler.isVerbose()) - ConsoleHandler.getConsole().write(">>getScrobblesByDate " + day + "." + month + "." + year); + + Logger.getLog().log(new LogEntry("getScrobblesByDate").addArg(Integer.toString(day)) + .addArg(Integer.toString(month)).addArg(Integer.toString(year))); LocalDate startDate = LocalDate.of(year, month, day); @@ -197,8 +202,8 @@ public class FmUserNetwork extends FmNetwork { * @return Scrobble count */ public int getScrobbleCountByDeltaDay(int day) { - if (ConsoleHandler.isVerbose()) - ConsoleHandler.getConsole().write(">>getScrobblesByDeltaDay " + day); + + Logger.getLog().log(new LogEntry("getScrobblesByDeltaDay").addArg(Integer.toString(day))); LocalDate local = LocalDate.now(); @@ -228,8 +233,8 @@ public class FmUserNetwork extends FmNetwork { * @return List of tags */ public ArrayList getTags() { - if (ConsoleHandler.isVerbose()) - ConsoleHandler.getConsole().write(">>getTags"); + + Logger.getLog().log(new LogEntry("getTags")); HashMap parameters = new HashMap(); @@ -265,8 +270,8 @@ public class FmUserNetwork extends FmNetwork { * @return FMObjList of artists */ public FMObjList getTag(String tagName) { - if (ConsoleHandler.isVerbose()) - ConsoleHandler.getConsole().write(">>getTag: " + tagName); + + Logger.getLog().log(new LogEntry("getTag").addArg(tagName)); HashMap parameters = new HashMap(); @@ -277,8 +282,7 @@ public class FmUserNetwork extends FmNetwork { JSONObject obj = makeGetRequest("user.getpersonaltags", parameters); - JSONArray tagJsonArray = obj.getJSONObject("taggings") - .getJSONObject("artists").getJSONArray("artist"); + JSONArray tagJsonArray = obj.getJSONObject("taggings").getJSONObject("artists").getJSONArray("artist"); JSONObject artistJson; @@ -293,8 +297,7 @@ public class FmUserNetwork extends FmNetwork { Artist artist = getArtist(artistJson.getString("name")); - if (ConsoleHandler.isVerbose()) - ConsoleHandler.getConsole().write(">Tag: " + tagName + ", " + artist.getName()); + Logger.getLog().log(new InfoEntry("Tag").addArg(tagName).addArg(artist.getName())); list.add(artist); diff --git a/src/main/java/sarsoo/fmframework/fx/TextAreaConsole.java b/src/main/java/sarsoo/fmframework/fx/TextAreaConsole.java index f4dbc3a..a869cdf 100644 --- a/src/main/java/sarsoo/fmframework/fx/TextAreaConsole.java +++ b/src/main/java/sarsoo/fmframework/fx/TextAreaConsole.java @@ -2,7 +2,7 @@ package sarsoo.fmframework.fx; import javafx.application.Platform; import javafx.scene.control.TextArea; -import sarsoo.fmframework.util.Console; +import sarsoo.fmframework.log.console.Console; public class TextAreaConsole implements Console{ diff --git a/src/main/java/sarsoo/fmframework/fx/controller/RootController.java b/src/main/java/sarsoo/fmframework/fx/controller/RootController.java index e0fa3bb..4fb6ae1 100644 --- a/src/main/java/sarsoo/fmframework/fx/controller/RootController.java +++ b/src/main/java/sarsoo/fmframework/fx/controller/RootController.java @@ -28,13 +28,15 @@ import sarsoo.fmframework.fx.tab.FMObjListEditTab; import sarsoo.fmframework.fx.tab.FMObjListTab; import sarsoo.fmframework.fx.tab.ScrobbleChartTab; import sarsoo.fmframework.fx.tab.TrackTab; +import sarsoo.fmframework.log.Log; +import sarsoo.fmframework.log.Logger; +import sarsoo.fmframework.log.console.ConsoleHandler; import sarsoo.fmframework.fx.FmFramework; import sarsoo.fmframework.music.Album; import sarsoo.fmframework.music.Artist; import sarsoo.fmframework.music.Tag; import sarsoo.fmframework.music.Track; import sarsoo.fmframework.net.Key; -import sarsoo.fmframework.util.ConsoleHandler; import sarsoo.fmframework.util.FMObjList; import sarsoo.fmframework.util.Reference; import sarsoo.fmframework.util.tagpool.TagPool; @@ -54,7 +56,8 @@ public class RootController { public void initialize() { // Reference.setUserName("sarsoo"); - ConsoleHandler.setVerbose(TextAreaConsole.getInstance()); + Logger.setLog(new Log(TextAreaConsole.getInstance(), false)); +// ConsoleHandler.setVerbose(TextAreaConsole.getInstance()); refresh(); } diff --git a/src/main/java/sarsoo/fmframework/log/Log.java b/src/main/java/sarsoo/fmframework/log/Log.java new file mode 100644 index 0000000..4899382 --- /dev/null +++ b/src/main/java/sarsoo/fmframework/log/Log.java @@ -0,0 +1,68 @@ +package sarsoo.fmframework.log; + +import java.util.ArrayList; + +import sarsoo.fmframework.log.console.Console; +import sarsoo.fmframework.log.entry.ErrorEntry; +import sarsoo.fmframework.log.entry.InfoEntry; +import sarsoo.fmframework.log.entry.LogEntry; + +public class Log { + + private ArrayList consoles = new ArrayList(); + + private Boolean writeToSTDOut; + + private ArrayList logList = new ArrayList(); + private ArrayList infoList = new ArrayList(); + private ArrayList errorList = new ArrayList(); + + public Log() { + writeToSTDOut = true; + } + + public Log(Console console, Boolean toSTD) { + + consoles.add(console); + + writeToSTDOut = toSTD; + + } + + public void clearConsoles() { + consoles.clear(); + writeToSTDOut = true; + } + + public void log(LogEntry entry) { + logList.add(entry); + writeLog(entry.toString()); + + } + + public void logInfo(InfoEntry entry) { + infoList.add(entry); + writeLog(entry.toString()); + + } + + public void logError(ErrorEntry entry) { + errorList.add(entry); + writeLog(entry.toString()); + + } + + protected void writeLog(String logString) { + + if (consoles.size() > 0) { + for (Console i : consoles) { + i.write(logString); + } + } + + if (writeToSTDOut == true) { + System.out.println(logString); + } + } + +} diff --git a/src/main/java/sarsoo/fmframework/log/Logger.java b/src/main/java/sarsoo/fmframework/log/Logger.java new file mode 100644 index 0000000..3792436 --- /dev/null +++ b/src/main/java/sarsoo/fmframework/log/Logger.java @@ -0,0 +1,15 @@ +package sarsoo.fmframework.log; + +public class Logger { + + private static Log log = new Log(); + + public static Log getLog() { + return log; + } + + public static void setLog(Log logIn) { + log = logIn; + } + +} diff --git a/src/main/java/sarsoo/fmframework/util/Console.java b/src/main/java/sarsoo/fmframework/log/console/Console.java similarity index 59% rename from src/main/java/sarsoo/fmframework/util/Console.java rename to src/main/java/sarsoo/fmframework/log/console/Console.java index 7c5b244..8382c52 100644 --- a/src/main/java/sarsoo/fmframework/util/Console.java +++ b/src/main/java/sarsoo/fmframework/log/console/Console.java @@ -1,4 +1,4 @@ -package sarsoo.fmframework.util; +package sarsoo.fmframework.log.console; public interface Console { diff --git a/src/main/java/sarsoo/fmframework/util/ConsoleHandler.java b/src/main/java/sarsoo/fmframework/log/console/ConsoleHandler.java similarity index 85% rename from src/main/java/sarsoo/fmframework/util/ConsoleHandler.java rename to src/main/java/sarsoo/fmframework/log/console/ConsoleHandler.java index 5ea7d70..1141888 100644 --- a/src/main/java/sarsoo/fmframework/util/ConsoleHandler.java +++ b/src/main/java/sarsoo/fmframework/log/console/ConsoleHandler.java @@ -1,4 +1,4 @@ -package sarsoo.fmframework.util; +package sarsoo.fmframework.log.console; public class ConsoleHandler { diff --git a/src/main/java/sarsoo/fmframework/log/entry/ErrorEntry.java b/src/main/java/sarsoo/fmframework/log/entry/ErrorEntry.java new file mode 100644 index 0000000..679012d --- /dev/null +++ b/src/main/java/sarsoo/fmframework/log/entry/ErrorEntry.java @@ -0,0 +1,38 @@ +package sarsoo.fmframework.log.entry; + +public class ErrorEntry extends LogEntry { + + protected int errorCode; + + public ErrorEntry(String methodIn) { + super(methodIn); + } + + public ErrorEntry setErrorCode(int error) { + errorCode = error; + return this; + } + + public String toString() { + + String logString = String.format("%s !!%s", timestamp, method); + + if (errorCode != 0) { + logString += String.format(" (%i)", errorCode); + } + + if (args != null) { + if (args.size() > 0) { + + logString += ":"; + + for (String i : args) { + logString += " " + i; + } + } + } + + return logString; + } + +} diff --git a/src/main/java/sarsoo/fmframework/log/entry/InfoEntry.java b/src/main/java/sarsoo/fmframework/log/entry/InfoEntry.java new file mode 100644 index 0000000..5046062 --- /dev/null +++ b/src/main/java/sarsoo/fmframework/log/entry/InfoEntry.java @@ -0,0 +1,27 @@ +package sarsoo.fmframework.log.entry; + +public class InfoEntry extends LogEntry { + + public InfoEntry(String methodIn) { + super(methodIn); + } + + public String toString() { + + String logString = String.format("%s \t>%s", timestamp, method); + + if (args != null) { + if (args.size() > 0) { + + logString += ":"; + + for (String i : args) { + logString += " " + i; + } + } + } + + return logString; + } + +} diff --git a/src/main/java/sarsoo/fmframework/log/entry/LogEntry.java b/src/main/java/sarsoo/fmframework/log/entry/LogEntry.java new file mode 100644 index 0000000..39446c9 --- /dev/null +++ b/src/main/java/sarsoo/fmframework/log/entry/LogEntry.java @@ -0,0 +1,60 @@ +package sarsoo.fmframework.log.entry; + +import java.time.LocalDateTime; +import java.util.ArrayList; + +public class LogEntry { + + protected String method; + protected ArrayList args; + protected LocalDateTime timestamp; + + public LogEntry(String methodIn) { + method = methodIn; + timestamp = LocalDateTime.now(); + } + + public String getMethod() { + return method; + } + + public ArrayList getArgs() { + return args; + } + + public LogEntry setArgs(ArrayList argsIn) { + args = argsIn; + return this; + } + + public LocalDateTime getTimeStamp() { + return timestamp; + } + + public LogEntry addArg(String arg) { + if (args == null) { + args = new ArrayList(); + } + args.add(arg); + return this; + } + + public String toString() { + + String logString = String.format("%s >>%s", timestamp, method); + + if (args != null) { + if (args.size() > 0) { + + logString += ":"; + + for (String i : args) { + logString += " " + i; + } + } + } + + return logString; + } + +} diff --git a/src/main/java/sarsoo/fmframework/util/Reference.java b/src/main/java/sarsoo/fmframework/util/Reference.java index 6d7198d..83831c4 100644 --- a/src/main/java/sarsoo/fmframework/util/Reference.java +++ b/src/main/java/sarsoo/fmframework/util/Reference.java @@ -2,6 +2,7 @@ package sarsoo.fmframework.util; import java.util.ArrayList; +import sarsoo.fmframework.log.console.Console; import sarsoo.fmframework.music.Album; import sarsoo.fmframework.music.Artist; diff --git a/src/test/java/sarsoo/fmframework/fm/FmUserNetworkTest.java b/src/test/java/sarsoo/fmframework/fm/FmUserNetworkTest.java index 2767ef8..7ff3a51 100644 --- a/src/test/java/sarsoo/fmframework/fm/FmUserNetworkTest.java +++ b/src/test/java/sarsoo/fmframework/fm/FmUserNetworkTest.java @@ -12,4 +12,9 @@ public class FmUserNetworkTest { public void testNetworkInstantiate() { assertNotNull(new FmUserNetwork(Key.getKey(), "sarsoo")); } + + @Test + public void testGetLastTrack() { + assertNotNull(new FmUserNetwork(Key.getKey(), "sarsoo").getLastTrack()); + } }