~dvedder95/ecologia/ecologia-base

« back to all changes in this revision

Viewing changes to src/main/EcologiaIO.java

  • Committer: Daniel Vedder
  • Date: 2016-05-25 13:12:06 UTC
  • Revision ID: d.vedder@web.de-20160525131206-rh29z4t8z2183cqx
Added --analyse and --autorun options.

Also plenty of refactoring and code cleanup.

Show diffs side-by-side

added added

removed removed

Lines of Context:
7
7
import java.io.IOException;
8
8
import java.text.SimpleDateFormat;
9
9
import java.util.Date;
 
10
import javax.swing.JOptionPane;
10
11
 
11
12
import controller.World;
12
13
 
19
20
 */
20
21
public abstract class EcologiaIO
21
22
{
 
23
        public static boolean verbose = false;
22
24
        public static boolean debugging = false;
23
 
        public static boolean verbose = false;
 
25
        public static boolean analysing = false;
24
26
        public static boolean logging = false;
25
27
        
26
28
        public final static int CONTINUABLE_ERROR = 0;
27
29
        public final static int BREAK_ERROR = 1;
28
30
        public final static int FATAL_ERROR = 2;
 
31
 
 
32
        /**
 
33
         * Print a log message if the verbose flag is set.
 
34
         * This is meant to be used for important runtime events in the program,
 
35
         * and for fundamental (e.g. life and death) events during the simulation.
 
36
         * For more detailed output, use either debug() or analysis().
 
37
         *
 
38
         * @param message
 
39
         */
 
40
        public static void log(String message)
 
41
        {
 
42
                if (verbose) {
 
43
                        message = "LOG: "+message;
 
44
                        System.out.println(message);
 
45
                        if (logging) writeFile(message);
 
46
                }
 
47
        }
29
48
        
30
49
        /**
31
 
         * Print a debug message if the debug flag is set
 
50
         * Print a debug message if the debug flag is set.
 
51
         * This is meant to be used to show that Ecologia is functioning
 
52
         * properly when the --debug flag is set, as well as for temporary
 
53
         * debug statements during development.
 
54
         *
32
55
         * @param message
33
56
         */
34
57
        public static void debug(String message)
41
64
        }
42
65
 
43
66
        /**
44
 
         * Print a log message if the verbose flag is set
 
67
         * Print an analysis message if the analysing flag is set.
 
68
         * This is meant to be used for simulation data output relevant only to a
 
69
         * current experiment.
 
70
         *
45
71
         * @param message
46
72
         */
47
 
        public static void log(String message)
 
73
        public static void analysis(String message)
48
74
        {
49
 
                if (verbose) {
50
 
                        message = "LOG: "+message;
 
75
                if (analysing) {
 
76
                        message = "ANALYSIS: "+message;
51
77
                        System.out.println(message);
52
78
                        if (logging) writeFile(message);
53
79
                }
84
110
         */
85
111
        public static void error(String message, int errorType)
86
112
        {
87
 
                if (errorType == CONTINUABLE_ERROR) {
88
 
                        message = "ERROR: "+message;
89
 
                }
90
 
                else if (errorType == BREAK_ERROR) {
 
113
                String logMessage = "ERROR: "+message;
 
114
                if (errorType == BREAK_ERROR) {
91
115
                        World.getInstance().setRunning(false);
92
 
                        message = "ERROR: "+message+" - simulation paused";                     
 
116
                        logMessage = logMessage+" - simulation paused";
 
117
                        JOptionPane.showMessageDialog(null, message, "Error!",
 
118
                                                                                  JOptionPane.ERROR_MESSAGE);
93
119
                }
94
120
                else if (errorType == FATAL_ERROR) {
95
 
                        message = "ERROR: "+message+" - simulation will terminate";
 
121
                        logMessage = logMessage+" - simulation will terminate";
 
122
                        JOptionPane.showMessageDialog(null, message+"\nEcologia is shutting down.",
 
123
                                                                                  "Error!", JOptionPane.ERROR_MESSAGE);
96
124
                }
97
 
                System.out.println(message);
98
 
                if (logging) writeFile(message);
99
 
                // TODO Add a graphic warning of imminent shutdown
 
125
                System.out.println(logMessage);
 
126
                if (logging) writeFile(logMessage);
100
127
                if (errorType == FATAL_ERROR) System.exit(0);
101
128
        }
102
129
        
145
172
        {
146
173
                if (logging) EcologiaIO.debug("Logging ON");
147
174
                else EcologiaIO.debug("Logging OFF");
148
 
                if (EcologiaIO.debugging) EcologiaIO.debug("Debugging ON");
149
175
                if (EcologiaIO.verbose) EcologiaIO.debug("Verbose ON");
150
176
                else EcologiaIO.debug("Verbose OFF");
 
177
                if (EcologiaIO.analysing) EcologiaIO.debug("Analysing ON");
 
178
                else EcologiaIO.debug("Analysing OFF");
 
179
                if (EcologiaIO.debugging) EcologiaIO.debug("Debugging ON");
151
180
        }
152
181
        
153
182
        /**