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

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);
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.text.ParseException;
import java.util.HashMap;
// Остановился на первом коммите и этом файле. Проблема в том, что в СиШарпе при throw не нужно кусок кода
// обматывать в try catch, а в жабе это необходимо.
import java.util.logging.Logger;
public class FormMapWithSetLocomotives extends JComponent {
private BufferedImage bufferImg = null;
@ -127,6 +79,7 @@ public class FormMapWithSetLocomotives extends JComponent {
return;
}
_mapsCollection.AddMap(textFieldNewMapName.getText(), _mapsDict.get(mapSelectComboBox.getSelectedItem().toString()));
Logger.getGlobal().info("Map " + textFieldNewMapName.getText() + " added");
ReloadMaps();
});
statusPanel.add(addMapButton);
@ -136,6 +89,7 @@ public class FormMapWithSetLocomotives extends JComponent {
listBoxMaps.addListSelectionListener(e -> {
if(listBoxMaps.getSelectedValue() == null) return;
bufferImg = _mapsCollection.Get(listBoxMaps.getSelectedValue().toString()).ShowSet();
Logger.getGlobal().info("Map switched to " + listBoxMaps.getSelectedValue().toString());
repaint();
});
statusPanel.add(listBoxMaps);
@ -154,6 +108,7 @@ public class FormMapWithSetLocomotives extends JComponent {
}
if(listBoxMaps.getSelectedValue().toString() == null) return;
_mapsCollection.DelMap(listBoxMaps.getSelectedValue().toString());
Logger.getGlobal().info("Map " +listBoxMaps.getSelectedValue().toString() +" deleted");
ReloadMaps();
repaint();
});
@ -174,6 +129,7 @@ public class FormMapWithSetLocomotives extends JComponent {
if (_mapsCollection.Get(listBoxMaps.getSelectedValue().toString()).Plus(objectLocomotive)!= -1){
JOptionPane.showMessageDialog(formFrame, "Object added", "Success", JOptionPane.OK_CANCEL_OPTION);
bufferImg = _mapsCollection.Get(listBoxMaps.getSelectedValue().toString()).ShowSet();
Logger.getGlobal().info("Object " + locomotive + " added");
repaint();
}
else {
@ -182,9 +138,11 @@ public class FormMapWithSetLocomotives extends JComponent {
}
catch (StorageOverflowException ex) {
JOptionPane.showMessageDialog(formFrame, "Storage overflow error: "+ ex.getMessage());
Logger.getGlobal().severe("Error " + ex.getMessage());
}
catch (Exception ex) {
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) {
JOptionPane.showMessageDialog(formFrame, "Object removed", "Success", JOptionPane.OK_CANCEL_OPTION);
bufferImg = _mapsCollection.Get(listBoxMaps.getSelectedValue().toString()).ShowSet();
Logger.getGlobal().info("Locomotive deleted at position " + position);
repaint();
}
else{
@ -224,9 +183,11 @@ public class FormMapWithSetLocomotives extends JComponent {
}}
catch (LocomotiveNotFoundException ex) {
JOptionPane.showMessageDialog(formFrame, "Locomotive not found error: " + ex.getMessage());
Logger.getGlobal().severe("Error " + ex.getMessage());
}
catch (Exception ex) {
JOptionPane.showMessageDialog(formFrame, "Unknown error: "+ ex.getMessage());
Logger.getGlobal().severe("Error " + ex.getMessage());
}
});
statusPanel.add(deleteLocomotiveButton);
@ -256,6 +217,7 @@ public class FormMapWithSetLocomotives extends JComponent {
}
catch (Exception ex) {
JOptionPane.showMessageDialog(null, "Unknown error: "+ ex.getMessage());
Logger.getGlobal().severe("Error " + ex.getMessage());
}
});
statusPanel.add(showOnMapButton);
@ -343,9 +305,11 @@ public class FormMapWithSetLocomotives extends JComponent {
try {
_mapsCollection.SaveData(fileChooser.getSelectedFile().getAbsolutePath());
JOptionPane.showMessageDialog(null, "Save success");
Logger.getGlobal().info("Saved all to " + fileChooser.getSelectedFile().getAbsolutePath());
}
catch (Exception ex) {
JOptionPane.showMessageDialog(null, "Save error: " + ex.getMessage());
Logger.getGlobal().severe("Error " + ex.getMessage());
}
}
@ -360,11 +324,13 @@ public class FormMapWithSetLocomotives extends JComponent {
try {
_mapsCollection.LoadData(fileChooser.getSelectedFile().getAbsolutePath());
JOptionPane.showMessageDialog(null, "Load success");
Logger.getGlobal().info("Loaded all from " + fileChooser.getSelectedFile().getAbsolutePath());
ReloadMaps();
repaint();
}
catch (Exception ex) {
JOptionPane.showMessageDialog(null, "Load error: " + ex.getMessage());
Logger.getGlobal().severe("Error " + ex.getMessage());
}
}
});
@ -384,10 +350,12 @@ public class FormMapWithSetLocomotives extends JComponent {
try {
_mapsCollection.SaveMap(listBoxMaps.getSelectedValue().toString(), fileChooser.getSelectedFile().getAbsolutePath());
JOptionPane.showMessageDialog(null, "Map saving success");
Logger.getGlobal().info("Saved map to " + fileChooser.getSelectedFile().getAbsolutePath());
repaint();
}
catch (Exception ex) {
JOptionPane.showMessageDialog(null, "Map saving error: " + ex.getMessage());
Logger.getGlobal().severe("Error " + ex.getMessage());
}
}
});
@ -402,11 +370,13 @@ public class FormMapWithSetLocomotives extends JComponent {
try {
_mapsCollection.LoadMap(fileChooser.getSelectedFile().getAbsolutePath());
JOptionPane.showMessageDialog(null, "Load Map success");
Logger.getGlobal().info("Loaded map from " + fileChooser.getSelectedFile().getAbsolutePath());
ReloadMaps();
repaint();
}
catch (Exception ex) {
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.Collections;
import java.util.HashMap;
import java.util.logging.Logger;
import java.util.zip.DataFormatException;
public class MapsCollection {
@ -64,6 +65,7 @@ public class MapsCollection {
for (var storage : _mapStorages.entrySet()) {
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()) {
bw.write("" + locomotive.getInfo() + "\n");
}
bw.flush();
}
@ -89,11 +92,13 @@ public class MapsCollection {
File file = new File(filename);
if (!file.exists())
{
Logger.getGlobal().severe("FileNotFoundException " + filename);
throw new FileNotFoundException("Файл не найден");
}
BufferedReader br = new BufferedReader(new FileReader(filename));
String curLine = br.readLine();
if (!curLine.contains("SingleMap")) {
Logger.getGlobal().severe("FileFormatException " + filename);
throw new DataFormatException("Неверный формат данных");
}
String mapName = br.readLine();
@ -131,12 +136,14 @@ public class MapsCollection {
File file = new File(filename);
if (!file.exists())
{
Logger.getGlobal().severe("FileNotFoundException " + filename);
throw new FileNotFoundException("Файл не найден");
}
BufferedReader br = new BufferedReader(new FileReader(filename));
String curLine = br.readLine();
if (!curLine.contains("MapsCollection")) {
Logger.getGlobal().severe("FileFormatException " + filename);
throw new DataFormatException("Неверный формат данных");
}

View File

@ -1,5 +1,6 @@
import java.util.ArrayList;
import java.util.Collections;
import java.util.logging.Logger;
public class SetLocomotivesGeneric <T>
{
@ -24,14 +25,20 @@ public class SetLocomotivesGeneric <T>
public int Insert (T locomotive, int position) throws StorageOverflowException{
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);
return position;
}
public T Remove (int position) throws LocomotiveNotFoundException {
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);
_places.set(position, null);
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