Добавлено логирование

This commit is contained in:
Данила Мочалов 2022-12-02 21:20:08 +04:00
parent 02ac7e5533
commit 8dd844aa14
6 changed files with 66 additions and 54 deletions

View File

@ -107,7 +107,4 @@ public class FormLocomotive extends JComponent{
if (_locomotive != null) _locomotive.DrawTransport(g2); if (_locomotive != null) _locomotive.DrawTransport(g2);
super.repaint(); super.repaint();
} }
public static void main(String[] args) {
new FormMapWithSetLocomotives();
}
} }

View File

@ -6,55 +6,7 @@ import java.awt.event.ActionListener;
import java.awt.image.BufferedImage; import java.awt.image.BufferedImage;
import java.text.ParseException; import java.text.ParseException;
import java.util.HashMap; import java.util.HashMap;
import java.util.logging.Logger;
// Остановился на первом коммите и этом файле. Проблема в том, что в СиШарпе при throw не нужно кусок кода
// обматывать в try catch, а в жабе это необходимо.
public class FormMapWithSetLocomotives extends JComponent { public class FormMapWithSetLocomotives extends JComponent {
private BufferedImage bufferImg = null; private BufferedImage bufferImg = null;
@ -127,6 +79,7 @@ public class FormMapWithSetLocomotives extends JComponent {
return; return;
} }
_mapsCollection.AddMap(textFieldNewMapName.getText(), _mapsDict.get(mapSelectComboBox.getSelectedItem().toString())); _mapsCollection.AddMap(textFieldNewMapName.getText(), _mapsDict.get(mapSelectComboBox.getSelectedItem().toString()));
Logger.getGlobal().info("Map " + textFieldNewMapName.getText() + " added");
ReloadMaps(); ReloadMaps();
}); });
statusPanel.add(addMapButton); statusPanel.add(addMapButton);
@ -136,6 +89,7 @@ public class FormMapWithSetLocomotives extends JComponent {
listBoxMaps.addListSelectionListener(e -> { listBoxMaps.addListSelectionListener(e -> {
if(listBoxMaps.getSelectedValue() == null) return; if(listBoxMaps.getSelectedValue() == null) return;
bufferImg = _mapsCollection.Get(listBoxMaps.getSelectedValue().toString()).ShowSet(); bufferImg = _mapsCollection.Get(listBoxMaps.getSelectedValue().toString()).ShowSet();
Logger.getGlobal().info("Map switched to " + listBoxMaps.getSelectedValue().toString());
repaint(); repaint();
}); });
statusPanel.add(listBoxMaps); statusPanel.add(listBoxMaps);
@ -154,6 +108,7 @@ public class FormMapWithSetLocomotives extends JComponent {
} }
if(listBoxMaps.getSelectedValue().toString() == null) return; if(listBoxMaps.getSelectedValue().toString() == null) return;
_mapsCollection.DelMap(listBoxMaps.getSelectedValue().toString()); _mapsCollection.DelMap(listBoxMaps.getSelectedValue().toString());
Logger.getGlobal().info("Map " +listBoxMaps.getSelectedValue().toString() +" deleted");
ReloadMaps(); ReloadMaps();
repaint(); repaint();
}); });
@ -174,6 +129,7 @@ public class FormMapWithSetLocomotives extends JComponent {
if (_mapsCollection.Get(listBoxMaps.getSelectedValue().toString()).Plus(objectLocomotive)!= -1){ if (_mapsCollection.Get(listBoxMaps.getSelectedValue().toString()).Plus(objectLocomotive)!= -1){
JOptionPane.showMessageDialog(formFrame, "Object added", "Success", JOptionPane.OK_CANCEL_OPTION); JOptionPane.showMessageDialog(formFrame, "Object added", "Success", JOptionPane.OK_CANCEL_OPTION);
bufferImg = _mapsCollection.Get(listBoxMaps.getSelectedValue().toString()).ShowSet(); bufferImg = _mapsCollection.Get(listBoxMaps.getSelectedValue().toString()).ShowSet();
Logger.getGlobal().info("Object " + locomotive + " added");
repaint(); repaint();
} }
else { else {
@ -182,9 +138,11 @@ public class FormMapWithSetLocomotives extends JComponent {
} }
catch (StorageOverflowException ex) { catch (StorageOverflowException ex) {
JOptionPane.showMessageDialog(formFrame, "Storage overflow error: "+ ex.getMessage()); JOptionPane.showMessageDialog(formFrame, "Storage overflow error: "+ ex.getMessage());
Logger.getGlobal().severe("Error " + ex.getMessage());
} }
catch (Exception ex) { catch (Exception ex) {
JOptionPane.showMessageDialog(formFrame, "Unknown error: "+ ex.getMessage()); JOptionPane.showMessageDialog(formFrame, "Unknown error: "+ ex.getMessage());
Logger.getGlobal().severe("Error " + ex.getMessage());
} }
} }
}); });
@ -217,6 +175,7 @@ public class FormMapWithSetLocomotives extends JComponent {
if (_mapsCollection.Get(listBoxMaps.getSelectedValue().toString()).Minus(position) != null) { if (_mapsCollection.Get(listBoxMaps.getSelectedValue().toString()).Minus(position) != null) {
JOptionPane.showMessageDialog(formFrame, "Object removed", "Success", JOptionPane.OK_CANCEL_OPTION); JOptionPane.showMessageDialog(formFrame, "Object removed", "Success", JOptionPane.OK_CANCEL_OPTION);
bufferImg = _mapsCollection.Get(listBoxMaps.getSelectedValue().toString()).ShowSet(); bufferImg = _mapsCollection.Get(listBoxMaps.getSelectedValue().toString()).ShowSet();
Logger.getGlobal().info("Locomotive deleted at position " + position);
repaint(); repaint();
} }
else{ else{
@ -224,9 +183,11 @@ public class FormMapWithSetLocomotives extends JComponent {
}} }}
catch (LocomotiveNotFoundException ex) { catch (LocomotiveNotFoundException ex) {
JOptionPane.showMessageDialog(formFrame, "Locomotive not found error: " + ex.getMessage()); JOptionPane.showMessageDialog(formFrame, "Locomotive not found error: " + ex.getMessage());
Logger.getGlobal().severe("Error " + ex.getMessage());
} }
catch (Exception ex) { catch (Exception ex) {
JOptionPane.showMessageDialog(formFrame, "Unknown error: "+ ex.getMessage()); JOptionPane.showMessageDialog(formFrame, "Unknown error: "+ ex.getMessage());
Logger.getGlobal().severe("Error " + ex.getMessage());
} }
}); });
statusPanel.add(deleteLocomotiveButton); statusPanel.add(deleteLocomotiveButton);
@ -256,6 +217,7 @@ public class FormMapWithSetLocomotives extends JComponent {
} }
catch (Exception ex) { catch (Exception ex) {
JOptionPane.showMessageDialog(null, "Unknown error: "+ ex.getMessage()); JOptionPane.showMessageDialog(null, "Unknown error: "+ ex.getMessage());
Logger.getGlobal().severe("Error " + ex.getMessage());
} }
}); });
statusPanel.add(showOnMapButton); statusPanel.add(showOnMapButton);
@ -343,9 +305,11 @@ public class FormMapWithSetLocomotives extends JComponent {
try { try {
_mapsCollection.SaveData(fileChooser.getSelectedFile().getAbsolutePath()); _mapsCollection.SaveData(fileChooser.getSelectedFile().getAbsolutePath());
JOptionPane.showMessageDialog(null, "Save success"); JOptionPane.showMessageDialog(null, "Save success");
Logger.getGlobal().info("Saved all to " + fileChooser.getSelectedFile().getAbsolutePath());
} }
catch (Exception ex) { catch (Exception ex) {
JOptionPane.showMessageDialog(null, "Save error: " + ex.getMessage()); JOptionPane.showMessageDialog(null, "Save error: " + ex.getMessage());
Logger.getGlobal().severe("Error " + ex.getMessage());
} }
} }
@ -360,11 +324,13 @@ public class FormMapWithSetLocomotives extends JComponent {
try { try {
_mapsCollection.LoadData(fileChooser.getSelectedFile().getAbsolutePath()); _mapsCollection.LoadData(fileChooser.getSelectedFile().getAbsolutePath());
JOptionPane.showMessageDialog(null, "Load success"); JOptionPane.showMessageDialog(null, "Load success");
Logger.getGlobal().info("Loaded all from " + fileChooser.getSelectedFile().getAbsolutePath());
ReloadMaps(); ReloadMaps();
repaint(); repaint();
} }
catch (Exception ex) { catch (Exception ex) {
JOptionPane.showMessageDialog(null, "Load error: " + ex.getMessage()); JOptionPane.showMessageDialog(null, "Load error: " + ex.getMessage());
Logger.getGlobal().severe("Error " + ex.getMessage());
} }
} }
}); });
@ -384,10 +350,12 @@ public class FormMapWithSetLocomotives extends JComponent {
try { try {
_mapsCollection.SaveMap(listBoxMaps.getSelectedValue().toString(), fileChooser.getSelectedFile().getAbsolutePath()); _mapsCollection.SaveMap(listBoxMaps.getSelectedValue().toString(), fileChooser.getSelectedFile().getAbsolutePath());
JOptionPane.showMessageDialog(null, "Map saving success"); JOptionPane.showMessageDialog(null, "Map saving success");
Logger.getGlobal().info("Saved map to " + fileChooser.getSelectedFile().getAbsolutePath());
repaint(); repaint();
} }
catch (Exception ex) { catch (Exception ex) {
JOptionPane.showMessageDialog(null, "Map saving error: " + ex.getMessage()); JOptionPane.showMessageDialog(null, "Map saving error: " + ex.getMessage());
Logger.getGlobal().severe("Error " + ex.getMessage());
} }
} }
}); });
@ -402,11 +370,13 @@ public class FormMapWithSetLocomotives extends JComponent {
try { try {
_mapsCollection.LoadMap(fileChooser.getSelectedFile().getAbsolutePath()); _mapsCollection.LoadMap(fileChooser.getSelectedFile().getAbsolutePath());
JOptionPane.showMessageDialog(null, "Load Map success"); JOptionPane.showMessageDialog(null, "Load Map success");
Logger.getGlobal().info("Loaded map from " + fileChooser.getSelectedFile().getAbsolutePath());
ReloadMaps(); ReloadMaps();
repaint(); repaint();
} }
catch (Exception ex) { catch (Exception ex) {
JOptionPane.showMessageDialog(null, "Load Map error: " + ex.getMessage()); JOptionPane.showMessageDialog(null, "Load Map error: " + ex.getMessage());
Logger.getGlobal().severe("Error " + ex.getMessage());
} }
} }
}); });

24
Main.java Normal file
View File

@ -0,0 +1,24 @@
import javax.swing.*;
import java.awt.*;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.logging.LogManager;
import java.util.logging.Logger;
public class Main {
private static Logger logger = null;
public static void main(String[] args) {
try {
FileInputStream stream = new FileInputStream(new File("C:/secondCourse/OOP/PIbd-23_Mochalov_D.V._Locomotive_Hard/log.config"));
LogManager.getLogManager().readConfiguration(stream);
logger = Logger.getGlobal();
stream.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
new FormMapWithSetLocomotives();
}
}

View File

@ -2,6 +2,7 @@ import java.io.*;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.HashMap; import java.util.HashMap;
import java.util.logging.Logger;
import java.util.zip.DataFormatException; import java.util.zip.DataFormatException;
public class MapsCollection { public class MapsCollection {
@ -64,6 +65,7 @@ public class MapsCollection {
for (var storage : _mapStorages.entrySet()) { for (var storage : _mapStorages.entrySet()) {
bw.write("" + storage.getKey() + separatorDict + storage.getValue().GetData(separatorDict.charAt(0), separatorData) + "\n"); bw.write("" + storage.getKey() + separatorDict + storage.getValue().GetData(separatorDict.charAt(0), separatorData) + "\n");
} }
bw.flush();
} }
// Сохранение одной выбранной карты // Сохранение одной выбранной карты
@ -81,6 +83,7 @@ public class MapsCollection {
for (var locomotive : map._setLocomotives.GetLocomotives()) { for (var locomotive : map._setLocomotives.GetLocomotives()) {
bw.write("" + locomotive.getInfo() + "\n"); bw.write("" + locomotive.getInfo() + "\n");
} }
bw.flush();
} }
@ -89,11 +92,13 @@ public class MapsCollection {
File file = new File(filename); File file = new File(filename);
if (!file.exists()) if (!file.exists())
{ {
Logger.getGlobal().severe("FileNotFoundException " + filename);
throw new FileNotFoundException("Файл не найден"); throw new FileNotFoundException("Файл не найден");
} }
BufferedReader br = new BufferedReader(new FileReader(filename)); BufferedReader br = new BufferedReader(new FileReader(filename));
String curLine = br.readLine(); String curLine = br.readLine();
if (!curLine.contains("SingleMap")) { if (!curLine.contains("SingleMap")) {
Logger.getGlobal().severe("FileFormatException " + filename);
throw new DataFormatException("Неверный формат данных"); throw new DataFormatException("Неверный формат данных");
} }
String mapName = br.readLine(); String mapName = br.readLine();
@ -131,12 +136,14 @@ public class MapsCollection {
File file = new File(filename); File file = new File(filename);
if (!file.exists()) if (!file.exists())
{ {
Logger.getGlobal().severe("FileNotFoundException " + filename);
throw new FileNotFoundException("Файл не найден"); throw new FileNotFoundException("Файл не найден");
} }
BufferedReader br = new BufferedReader(new FileReader(filename)); BufferedReader br = new BufferedReader(new FileReader(filename));
String curLine = br.readLine(); String curLine = br.readLine();
if (!curLine.contains("MapsCollection")) { if (!curLine.contains("MapsCollection")) {
Logger.getGlobal().severe("FileFormatException " + filename);
throw new DataFormatException("Неверный формат данных"); throw new DataFormatException("Неверный формат данных");
} }

View File

@ -1,5 +1,6 @@
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.logging.Logger;
public class SetLocomotivesGeneric <T> public class SetLocomotivesGeneric <T>
{ {
@ -24,14 +25,20 @@ public class SetLocomotivesGeneric <T>
public int Insert (T locomotive, int position) throws StorageOverflowException{ public int Insert (T locomotive, int position) throws StorageOverflowException{
if (position < 0) return -1; if (position < 0) return -1;
if (Count() >= _maxCount) throw new StorageOverflowException(_maxCount); if (Count() >= _maxCount) {
Logger.getGlobal().warning("StorageOverflowException");
throw new StorageOverflowException(_maxCount);
}
_places.add(position, locomotive); _places.add(position, locomotive);
return position; return position;
} }
public T Remove (int position) throws LocomotiveNotFoundException { public T Remove (int position) throws LocomotiveNotFoundException {
if (position >= _maxCount || position < 0) return null; if (position >= _maxCount || position < 0) return null;
if (_places.get(position) == null) throw new LocomotiveNotFoundException(position); if (_places.get(position) == null) {
Logger.getGlobal().warning("LocomotiveNotFoundException at " + position);
throw new LocomotiveNotFoundException(position);
}
T result = _places.get(position); T result = _places.get(position);
_places.set(position, null); _places.set(position, null);
return result; return result;

7
log.config Normal file
View File

@ -0,0 +1,7 @@
handlers = java.util.logging.FileHandler
java.util.logging.FileHandler.level = INFO
java.util.logging.FileHandler.formatter = java.util.logging.SimpleFormatter
java.util.logging.SimpleFormatter.format= %4$S %5$S (%1$td.%1$tm.%1$tY) %n
java.util.logging.FileHandler.append = true
java.util.logging.FileHandler.pattern = log.txt
java.util.logging.FileHandler.limit = 100000