source: trunk/java-utils/src/main/java/de/ugoe/cs/util/console/TextConsole.java @ 674

Last change on this file since 674 was 674, checked in by sherbold, 12 years ago
  • modified startup-parameter trace of runner from binary into java.util.Logger.Level to define the tracing granularity; defaults to WARNING)
  • modified TextConsole? and SWTConsole to trace according to trace-parameter
File size: 3.3 KB
Line 
1package de.ugoe.cs.util.console;
2
3import java.io.IOException;
4import java.nio.charset.Charset;
5import java.util.logging.Level;
6
7import de.ugoe.cs.util.console.listener.IErrorListener;
8import de.ugoe.cs.util.console.listener.IExceptionListener;
9import de.ugoe.cs.util.console.listener.IOutputListener;
10import de.ugoe.cs.util.console.listener.ITraceListener;
11
12/**
13 * <p>
14 * Implements a simple console observer that prints normal text to
15 * {@code stdout}, errors to {@code stderr} and reads from {@code stdin}.
16 * </p>
17 *
18 * @author Steffen Herbold
19 * @version 1.0
20 */
21public class TextConsole implements IOutputListener, IErrorListener,
22                ITraceListener, IExceptionListener {
23       
24        /**
25         * <p>Defines the trace level used by this console.</p>
26         */
27        private Level traceLevel;
28       
29        /**
30         * <p>
31         * Creates a new text console and automatically registers it as observer. The trace level is {@link Level#WARNING}.
32         * </p>
33         */
34        public TextConsole() {
35            this(Level.WARNING);
36        }
37
38        /**
39         * <p>
40         * Creates a new text console and automatically registers it as observer.
41         * </p>
42         * @param traceLevel trace level used by this text console
43         */
44        public TextConsole(Level traceLevel) {
45                Console.getInstance().registerOutputListener(this);
46                Console.getInstance().registerErrorListener(this);
47                Console.getInstance().registerTraceListener(this);
48                Console.getInstance().registerExceptionListener(this);
49                this.traceLevel = traceLevel;
50        }
51
52        /**
53         * <p>
54         * Prints messages to {@code stdout}.
55         * </p>
56         *
57         * @see ConsoleObserver#outputMsg(java.lang.String)
58         */
59        public void outputMsg(String newMessage) {
60                System.out.print(newMessage);
61        }
62
63        /**
64         * <p>
65         * Prints messages to {@code stderr}.
66         * </p>
67         *
68         * @see ConsoleObserver#errorMsg(String)
69         */
70        @Override
71        public void errorMsg(String errMessage) {
72                System.err.print(errMessage);
73        }
74
75        /**
76         * <p>
77         * Prints the stacktrace of an exception to {@code stderr}.
78         * </p>
79         *
80         * @see ConsoleObserver#logException(Exception)
81         */
82        @Override
83        public void logException(Exception e) {
84                System.err.println(e.getMessage());
85        }
86
87        /**
88         * <p>
89         * Prints messages to {@code stdout}. These messages are only printed, if
90         * the console is run in debug mode.
91         * </p>
92         */
93        @Override
94        public void traceMsg(String traceMessage, Level level) {
95                if (level.intValue()>=traceLevel.intValue()) {
96                        System.out.print("[" + level.toString() + "] " + traceMessage);
97                }
98        }
99
100        /**
101         * <p>
102         * Starts a new TextConsole. If the text console is started, it can be used
103         * not only to print message, but also to execute commands by reading
104         * {@code stdin}.
105         * </p>
106         */
107        public void run() {
108                CommandExecuter exec = CommandExecuter.getInstance();
109                while (true) {
110                        System.out.print("> ");
111                        String command = getCommand().trim();
112                        if (!command.equals("")) {
113                                exec.exec(command);
114                        }
115                }
116        }
117
118        /**
119         * <p>
120         * Reads a new command from {@code stdin}.
121         * </p>
122         *
123         * @return a string with a command
124         */
125        protected String getCommand() {
126                byte[] buffer = new byte[1024];
127                int bytesRead = 0;
128                String command;
129                try {
130                        bytesRead = System.in.read(buffer);
131                } catch (IOException e) {
132
133                }
134                if (bytesRead == 0) {
135                        command = "";
136                } else {
137                        command = new String(buffer, Charset.defaultCharset());
138                }
139                return command;
140        }
141
142}
Note: See TracBrowser for help on using the repository browser.