Changeset 1355 for trunk/java-utils/src/main/java/de/ugoe
- Timestamp:
- 02/07/14 18:00:28 (11 years ago)
- Location:
- trunk/java-utils/src/main/java/de/ugoe/cs/util/console
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/java-utils/src/main/java/de/ugoe/cs/util/console/CommandExecuter.java
r1271 r1355 164 164 } 165 165 catch (IllegalArgumentException e) { 166 Console.println("invalid parameter provided: " + e.getMessage()); 167 Console.println("Usage: " + cmd.help()); 168 } 169 catch (Exception e) { 170 Console.println("error executing command: " + e); 171 Console.logException(e); 166 172 Console.println("Usage: " + cmd.help()); 167 173 } -
trunk/java-utils/src/main/java/de/ugoe/cs/util/console/TextConsole.java
r1318 r1355 28 28 /** 29 29 * <p> 30 * Implements a simple console observer that prints normal text to 31 * {@code std out}, errors to {@code stderr} and reads from {@code stdin}.30 * Implements a simple console observer that prints normal text to {@code stdout}, errors to 31 * {@code stderr} and reads from {@code stdin}. 32 32 * </p> 33 33 * … … 35 35 * @version 1.0 36 36 */ 37 public class TextConsole implements IOutputListener, IErrorListener, 38 ITraceListener, IExceptionListener { 39 40 /** 41 * <p>Defines the trace level used by this console.</p> 42 */ 43 private Level traceLevel; 44 45 private final SimpleDateFormat ft = new SimpleDateFormat("HH:mm:ss"); 46 47 /** 48 * <p> 49 * Creates a new text console and automatically registers it as observer. The trace level is {@link Level#WARNING}. 50 * </p> 51 */ 52 public TextConsole() { 53 this(Level.WARNING); 54 } 37 public class TextConsole implements IOutputListener, IErrorListener, ITraceListener, 38 IExceptionListener 39 { 55 40 56 /** 57 * <p> 58 * Creates a new text console and automatically registers it as observer. 59 * </p> 60 * @param traceLevel trace level used by this text console 61 */ 62 public TextConsole(Level traceLevel) { 63 Console.getInstance().registerOutputListener(this); 64 Console.getInstance().registerErrorListener(this); 65 Console.getInstance().registerTraceListener(this); 66 Console.getInstance().registerExceptionListener(this); 67 this.traceLevel = traceLevel; 68 } 41 /** 42 * <p> 43 * Defines the trace level used by this console. 44 * </p> 45 */ 46 private Level traceLevel; 69 47 70 /** 71 * <p> 72 * Prints messages to {@code stdout}. 73 * </p> 74 * 75 * @see ConsoleObserver#outputMsg(java.lang.String) 76 */ 77 public void outputMsg(String newMessage) { 78 System.out.print(newMessage); 79 } 48 private final SimpleDateFormat ft = new SimpleDateFormat("HH:mm:ss"); 80 49 81 /** 82 * <p> 83 * Prints messages to {@code stderr}. 84 * </p> 85 * 86 * @see ConsoleObserver#errorMsg(String) 87 */ 88 @Override 89 public void errorMsg(String errMessage) { 90 System.err.print(errMessage); 91 } 50 /** 51 * <p> 52 * Creates a new text console and automatically registers it as observer. The trace level is 53 * {@link Level#WARNING}. 54 * </p> 55 */ 56 public TextConsole() { 57 this(Level.WARNING); 58 } 92 59 93 /** 94 * <p> 95 * Prints the stacktrace of an exception to {@code stderr}. 96 * </p> 97 * 98 * @see ConsoleObserver#logException(Exception) 99 */ 100 @Override 101 public void logException(Exception e) { 102 System.err.println(e.getMessage()); 103 } 60 /** 61 * <p> 62 * Creates a new text console and automatically registers it as observer. 63 * </p> 64 * 65 * @param traceLevel 66 * trace level used by this text console 67 */ 68 public TextConsole(Level traceLevel) { 69 Console.getInstance().registerOutputListener(this); 70 Console.getInstance().registerErrorListener(this); 71 Console.getInstance().registerTraceListener(this); 72 Console.getInstance().registerExceptionListener(this); 73 this.traceLevel = traceLevel; 74 } 104 75 105 /** 106 * <p> 107 * Prints messages to {@code stdout}. These messages are only printed, if 108 * the console is run in debug mode. 109 * </p> 110 */ 111 @Override 112 public void traceMsg(String traceMessage, Level level) { 113 if (level.intValue()>=traceLevel.intValue()) { 114 System.out.print("[" + level.toString() + "] [" + ft.format(new Date()) + "] " + traceMessage); 115 } 116 } 76 /** 77 * <p> 78 * Prints messages to {@code stdout}. 79 * </p> 80 * 81 * @see ConsoleObserver#outputMsg(java.lang.String) 82 */ 83 public void outputMsg(String newMessage) { 84 System.out.print(newMessage); 85 } 117 86 118 /** 119 * <p> 120 * Starts a new TextConsole. If the text console is started, it can be used 121 * not only to print message, but also to execute commands by reading 122 * {@code stdin}. 123 * </p> 124 */ 125 public void run() { 126 CommandExecuter exec = CommandExecuter.getInstance(); 127 while (true) { 128 System.out.print("> "); 129 String command = getCommand().trim(); 130 if (!command.equals("")) { 131 exec.exec(command); 132 } 133 } 134 } 87 /** 88 * <p> 89 * Prints messages to {@code stderr}. 90 * </p> 91 * 92 * @see ConsoleObserver#errorMsg(String) 93 */ 94 @Override 95 public void errorMsg(String errMessage) { 96 System.err.print(errMessage); 97 } 135 98 136 /** 137 * <p> 138 * Reads a new command from {@code stdin}. 139 * </p> 140 * 141 * @return a string with a command 142 */ 143 protected String getCommand() { 144 byte[] buffer = new byte[1024]; 145 int bytesRead = 0; 146 String command; 147 try { 148 bytesRead = System.in.read(buffer); 149 } catch (IOException e) { 99 /** 100 * <p> 101 * Prints the stacktrace of an exception to {@code stderr} if the log level is more or equally 102 * detailed to <code>Level.FINE</code>. Otherwise, it just prints a line naming the exception 103 * or only the message, if any. 104 * </p> 105 * 106 * @see ConsoleObserver#logException(Exception) 107 */ 108 @Override 109 public void logException(Exception e) { 110 if (traceLevel.intValue() > Level.FINE.intValue()) { 111 if (e.getMessage() != null) { 112 System.err.println(e.getMessage()); 113 } 114 else { 115 System.err.println(e); 116 } 117 } 118 else { 119 e.printStackTrace(System.err); 120 } 121 } 150 122 151 } 152 if (bytesRead == 0) { 153 command = ""; 154 } else { 155 command = new String(buffer, Charset.defaultCharset()); 156 } 157 return command; 158 } 123 /** 124 * <p> 125 * Prints messages to {@code stdout}. These messages are only printed, if the console is run in 126 * debug mode. 127 * </p> 128 */ 129 @Override 130 public void traceMsg(String traceMessage, Level level) { 131 if (level.intValue() >= traceLevel.intValue()) { 132 System.out.print("[" + level.toString() + "] [" + ft.format(new Date()) + "] " + 133 traceMessage); 134 } 135 } 136 137 /** 138 * <p> 139 * Starts a new TextConsole. If the text console is started, it can be used not only to print 140 * message, but also to execute commands by reading {@code stdin}. 141 * </p> 142 */ 143 public void run() { 144 CommandExecuter exec = CommandExecuter.getInstance(); 145 while (true) { 146 System.out.print("> "); 147 String command = getCommand().trim(); 148 if (!command.equals("")) { 149 exec.exec(command); 150 } 151 } 152 } 153 154 /** 155 * <p> 156 * Reads a new command from {@code stdin}. 157 * </p> 158 * 159 * @return a string with a command 160 */ 161 protected String getCommand() { 162 byte[] buffer = new byte[1024]; 163 int bytesRead = 0; 164 String command; 165 try { 166 bytesRead = System.in.read(buffer); 167 } 168 catch (IOException e) { 169 170 } 171 if (bytesRead == 0) { 172 command = ""; 173 } 174 else { 175 command = new String(buffer, Charset.defaultCharset()); 176 } 177 return command; 178 } 159 179 160 180 }
Note: See TracChangeset
for help on using the changeset viewer.