()
| 69 | |
| 70 | |
| 71 | static public void startup() { |
| 72 | if (systemOut != null) { |
| 73 | // TODO fix this dreadful style choice in how the Console is initialized |
| 74 | // (This is not good code.. startup() should gracefully deal with this. |
| 75 | // It's just a low priority relative to the likelihood of trouble.) |
| 76 | new Exception("startup() called more than once").printStackTrace(systemErr); |
| 77 | return; |
| 78 | } |
| 79 | systemOut = System.out; |
| 80 | systemErr = System.err; |
| 81 | |
| 82 | // placing everything inside a try block because this can be a dangerous |
| 83 | // time for the lights to blink out and crash for and obscure reason. |
| 84 | try { |
| 85 | SimpleDateFormat formatter = new SimpleDateFormat("yyMMdd_HHmmss"); |
| 86 | // Moving away from a random string in 0256 (and adding hms) because |
| 87 | // the random digits looked like times anyway, causing confusion. |
| 88 | //String randy = String.format("%04d", (int) (1000 * Math.random())); |
| 89 | //final String stamp = formatter.format(new Date()) + "_" + randy; |
| 90 | final String stamp = formatter.format(new Date()); |
| 91 | |
| 92 | File consoleDir = Base.getSettingsFile("console"); |
| 93 | if (consoleDir.exists()) { |
| 94 | // clear old debug files |
| 95 | File[] stdFiles = consoleDir.listFiles(new FileFilter() { |
| 96 | final String todayPrefix = stamp.substring(0, 4); |
| 97 | |
| 98 | public boolean accept(File file) { |
| 99 | if (!file.isDirectory()) { |
| 100 | String name = file.getName(); |
| 101 | if (name.endsWith(".err") || name.endsWith(".out")) { |
| 102 | // don't delete any of today's debug messages |
| 103 | return !name.startsWith(todayPrefix); |
| 104 | } |
| 105 | } |
| 106 | return false; |
| 107 | } |
| 108 | }); |
| 109 | // Remove any files that aren't from today |
| 110 | for (File file : stdFiles) { |
| 111 | file.delete(); |
| 112 | } |
| 113 | } else { |
| 114 | consoleDir.mkdirs(); |
| 115 | consoleDir.setWritable(true, false); |
| 116 | } |
| 117 | |
| 118 | File outFile = new File(consoleDir, stamp + ".out"); |
| 119 | outFile.setWritable(true, false); |
| 120 | stdoutFile = new FileOutputStream(outFile); |
| 121 | File errFile = new File(consoleDir, stamp + ".err"); |
| 122 | errFile.setWritable(true, false); |
| 123 | stderrFile = new FileOutputStream(errFile); |
| 124 | |
| 125 | consoleOut = new PrintStream(new ConsoleStream(false)); |
| 126 | consoleErr = new PrintStream(new ConsoleStream(true)); |
| 127 | |
| 128 | System.setOut(consoleOut); |
no test coverage detected