List of usage examples for java.lang System setErr
public static void setErr(PrintStream err)
From source file:edu.umn.natsrl.ticas.plugin.srte.SRTEMainPanel.java
/** * Resotre output//from www . j ava 2s .com */ public void restoreOutput() { if (backupOut == null) return; System.setOut(backupOut); System.setErr(backupErr); }
From source file:com.salmonllc.ideTools.Tomcat50Engine.java
/** * Start a new server instance./*from ww w. j av a 2 s.c o m*/ */ public void load() { initDirs(); // Before digester - it may be needed initNaming(); // Create and execute our Digester Digester digester = createStartDigester(); long t1 = System.currentTimeMillis(); Exception ex = null; InputSource inputSource = null; InputStream inputStream = null; try { File file = configFile(); inputStream = new FileInputStream(file); inputSource = new InputSource("file://" + file.getAbsolutePath()); } catch (Exception e) { ; } if (inputStream == null) { try { inputStream = getClass().getClassLoader().getResourceAsStream(getConfigFile()); inputSource = new InputSource(getClass().getClassLoader().getResource(getConfigFile()).toString()); } catch (Exception e) { ; } } if (inputStream == null) { System.out.println("Can't load server.xml"); return; } try { inputSource.setByteStream(inputStream); digester.push(this); digester.parse(inputSource); inputStream.close(); } catch (Exception e) { System.out.println("Catalina.start using " + getConfigFile() + ": " + e); e.printStackTrace(System.out); return; } // Replace System.out and System.err with a custom PrintStream // TODO: move to Embedded, make it configurable SystemLogHandler systemlog = new SystemLogHandler(System.out); System.setOut(systemlog); System.setErr(systemlog); // Start the new server if (_server instanceof Lifecycle) { try { _server.initialize(); } catch (LifecycleException e) { log.error("Catalina.start", e); } } long t2 = System.currentTimeMillis(); log.info("Initialization processed in " + (t2 - t1) + " ms"); }
From source file:com.enioka.jqm.tools.MiscTest.java
@Test public void testMultiLog() throws Exception { PrintStream out_ini = System.out; PrintStream err_ini = System.err; Helpers.setSingleParam("logFilePerLaunch", "true", em); CreationTools.createJobDef(null, true, "App", null, "jqm-tests/jqm-test-datetimemaven/target/test.jar", TestHelpers.qVip, 42, "MarsuApplication", null, "Franquin", "ModuleMachin", "other", "other", true, em);//w w w. j ava 2 s . c o m int i = JobRequest.create("MarsuApplication", "TestUser").submit(); addAndStartEngine(); TestHelpers.waitFor(1, 20000, em); String fileName = StringUtils.leftPad("" + i, 10, "0") + ".stdout.log"; File f = new File(FilenameUtils.concat(((MultiplexPrintStream) System.out).rootLogDir, fileName)); Assert.assertEquals(1, TestHelpers.getOkCount(em)); Assert.assertEquals(0, TestHelpers.getNonOkCount(em)); Assert.assertTrue(f.exists()); System.setErr(err_ini); System.setOut(out_ini); }
From source file:org.dita.dost.AbstractIntegrationTest.java
/** * Run test conversion/*from w w w . ja va2s . c o m*/ * * @param srcDir test source directory * @param transtype transtype to test * @return list of log messages * @throws Exception if conversion failed */ private List<TestListener.Message> runOt(final File srcDir, final Transtype transtype, final File tempBaseDir, final File resBaseDir, final Map<String, String> args, final String[] targets) throws Exception { final File tempDir = new File(tempBaseDir, transtype.toString()); final File resDir = new File(resBaseDir, transtype.toString()); deleteDirectory(resDir); deleteDirectory(tempDir); final TestListener listener = new TestListener(System.out, System.err); final PrintStream savedErr = System.err; final PrintStream savedOut = System.out; try { final File buildFile = new File(ditaDir, "build.xml"); final Project project = new Project(); project.addBuildListener(listener); System.setOut(new PrintStream(new DemuxOutputStream(project, false))); System.setErr(new PrintStream(new DemuxOutputStream(project, true))); project.fireBuildStarted(); project.init(); project.setUserProperty("transtype", transtype.name); if (transtype.equals("pdf") || transtype.equals("pdf2")) { project.setUserProperty("pdf.formatter", "fop"); project.setUserProperty("fop.formatter.output-format", "text/plain"); } project.setUserProperty("generate-debug-attributes", "false"); project.setUserProperty("preprocess.copy-generated-files.skip", "true"); project.setUserProperty("ant.file", buildFile.getAbsolutePath()); project.setUserProperty("ant.file.type", "file"); project.setUserProperty("dita.dir", ditaDir.getAbsolutePath()); project.setUserProperty("output.dir", resDir.getAbsolutePath()); project.setUserProperty("dita.temp.dir", tempDir.getAbsolutePath()); project.setUserProperty("clean.temp", "no"); args.entrySet().forEach(e -> project.setUserProperty(e.getKey(), e.getValue())); project.setKeepGoingMode(false); ProjectHelper.configureProject(project, buildFile); final Vector<String> ts = new Vector<>(); if (targets != null) { ts.addAll(Arrays.asList(targets)); } else { ts.addAll(Arrays.asList(transtype.targets)); } project.executeTargets(ts); return listener.messages; } finally { System.setOut(savedOut); System.setErr(savedErr); } }
From source file:org.apache.hadoop.hdfs.TestDFSShell.java
/** * check command error outputs and exit statuses. *///from w ww . java2 s.c o m @Test public void testErrOutPut() throws Exception { Configuration conf = new HdfsConfiguration(); MiniDFSCluster cluster = null; PrintStream bak = null; try { cluster = new MiniDFSCluster.Builder(conf).numDataNodes(2).build(); FileSystem srcFs = cluster.getFileSystem(); Path root = new Path("/nonexistentfile"); bak = System.err; ByteArrayOutputStream out = new ByteArrayOutputStream(); PrintStream tmp = new PrintStream(out); System.setErr(tmp); String[] argv = new String[2]; argv[0] = "-cat"; argv[1] = root.toUri().getPath(); int ret = ToolRunner.run(new FsShell(), argv); assertEquals(" -cat returned 1 ", 1, ret); String returned = out.toString(); assertTrue("cat does not print exceptions ", (returned.lastIndexOf("Exception") == -1)); out.reset(); argv[0] = "-rm"; argv[1] = root.toString(); FsShell shell = new FsShell(); shell.setConf(conf); ret = ToolRunner.run(shell, argv); assertEquals(" -rm returned 1 ", 1, ret); returned = out.toString(); out.reset(); assertTrue("rm prints reasonable error ", (returned.lastIndexOf("No such file or directory") != -1)); argv[0] = "-rmr"; argv[1] = root.toString(); ret = ToolRunner.run(shell, argv); assertEquals(" -rmr returned 1", 1, ret); returned = out.toString(); assertTrue("rmr prints reasonable error ", (returned.lastIndexOf("No such file or directory") != -1)); out.reset(); argv[0] = "-du"; argv[1] = "/nonexistentfile"; ret = ToolRunner.run(shell, argv); returned = out.toString(); assertTrue(" -du prints reasonable error ", (returned.lastIndexOf("No such file or directory") != -1)); out.reset(); argv[0] = "-dus"; argv[1] = "/nonexistentfile"; ret = ToolRunner.run(shell, argv); returned = out.toString(); assertTrue(" -dus prints reasonable error", (returned.lastIndexOf("No such file or directory") != -1)); out.reset(); argv[0] = "-ls"; argv[1] = "/nonexistenfile"; ret = ToolRunner.run(shell, argv); returned = out.toString(); assertTrue(" -ls does not return Found 0 items", (returned.lastIndexOf("Found 0") == -1)); out.reset(); argv[0] = "-ls"; argv[1] = "/nonexistentfile"; ret = ToolRunner.run(shell, argv); assertEquals(" -lsr should fail ", 1, ret); out.reset(); srcFs.mkdirs(new Path("/testdir")); argv[0] = "-ls"; argv[1] = "/testdir"; ret = ToolRunner.run(shell, argv); returned = out.toString(); assertTrue(" -ls does not print out anything ", (returned.lastIndexOf("Found 0") == -1)); out.reset(); argv[0] = "-ls"; argv[1] = "/user/nonxistant/*"; ret = ToolRunner.run(shell, argv); assertEquals(" -ls on nonexistent glob returns 1", 1, ret); out.reset(); argv[0] = "-mkdir"; argv[1] = "/testdir"; ret = ToolRunner.run(shell, argv); returned = out.toString(); assertEquals(" -mkdir returned 1 ", 1, ret); assertTrue(" -mkdir returned File exists", (returned.lastIndexOf("File exists") != -1)); Path testFile = new Path("/testfile"); OutputStream outtmp = srcFs.create(testFile); outtmp.write(testFile.toString().getBytes()); outtmp.close(); out.reset(); argv[0] = "-mkdir"; argv[1] = "/testfile"; ret = ToolRunner.run(shell, argv); returned = out.toString(); assertEquals(" -mkdir returned 1", 1, ret); assertTrue(" -mkdir returned this is a file ", (returned.lastIndexOf("not a directory") != -1)); out.reset(); argv = new String[3]; argv[0] = "-mv"; argv[1] = "/testfile"; argv[2] = "file"; ret = ToolRunner.run(shell, argv); assertEquals("mv failed to rename", 1, ret); out.reset(); argv = new String[3]; argv[0] = "-mv"; argv[1] = "/testfile"; argv[2] = "/testfiletest"; ret = ToolRunner.run(shell, argv); returned = out.toString(); assertTrue("no output from rename", (returned.lastIndexOf("Renamed") == -1)); out.reset(); argv[0] = "-mv"; argv[1] = "/testfile"; argv[2] = "/testfiletmp"; ret = ToolRunner.run(shell, argv); returned = out.toString(); assertTrue(" unix like output", (returned.lastIndexOf("No such file or") != -1)); out.reset(); argv = new String[1]; argv[0] = "-du"; srcFs.mkdirs(srcFs.getHomeDirectory()); ret = ToolRunner.run(shell, argv); returned = out.toString(); assertEquals(" no error ", 0, ret); assertTrue("empty path specified", (returned.lastIndexOf("empty string") == -1)); out.reset(); argv = new String[3]; argv[0] = "-test"; argv[1] = "-d"; argv[2] = "/no/such/dir"; ret = ToolRunner.run(shell, argv); returned = out.toString(); assertEquals(" -test -d wrong result ", 1, ret); assertTrue(returned.isEmpty()); } finally { if (bak != null) { System.setErr(bak); } if (cluster != null) { cluster.shutdown(); } } }
From source file:org.rzo.yajsw.app.WrapperManagerImpl.java
/** * Tee system streams.//from ww w. j av a 2 s . c om * * @param outFile * the out file * @param path * the path * @param visible * the visible */ private void teeSystemStreams(String outFile, String path, boolean visible) { File fOut = createRWfile(path, "out_" + outFile); // if (fOut.exists()) // fOut.delete(); File fErr = createRWfile(path, "err_" + outFile); // if (fErr.exists()) // fErr.delete(); File fIn = createRWfile(path, "in_" + outFile); try { PrintStream wrapperOut = (PrintStream) new CyclicBufferFilePrintStream(fOut); TeeOutputStream newOut = (TeeOutputStream) new TeeOutputStream(); newOut.connect(wrapperOut); // pipe output to console only if it is visible if (visible) newOut.connect(System.out); _outStream = wrapperOut; System.setOut(new PrintStream(newOut)); } catch (Throwable e) { e.printStackTrace(); } try { PrintStream wrapperErr = (PrintStream) new CyclicBufferFilePrintStream(fErr); TeeOutputStream newErr = (TeeOutputStream) new TeeOutputStream(); newErr.connect(wrapperErr); // pipe output to console only if it is visible if (visible) newErr.connect(System.err); _errStream = newErr; System.setErr(new PrintStream(newErr)); } catch (Throwable e) { e.printStackTrace(); } try { CyclicBufferFileInputStream wrapperIn = new CyclicBufferFileInputStream(fIn); TeeInputStream newIn = (TeeInputStream) new TeeInputStream(); newIn.connect(wrapperIn); newIn.connect(System.in); System.setIn(newIn); } catch (Throwable e) { e.printStackTrace(); } }
From source file:science.atlarge.graphalytics.graphmat.GraphmatPlatform.java
public static void startPlatformLogging(Path fileName) { sysOut = System.out;/*from w w w . ja v a 2 s . c om*/ sysErr = System.err; try { File file = null; file = fileName.toFile(); file.getParentFile().mkdirs(); file.createNewFile(); FileOutputStream fos = new FileOutputStream(file); TeeOutputStream bothStream = new TeeOutputStream(System.out, fos); PrintStream ps = new PrintStream(bothStream); System.setOut(ps); System.setErr(ps); } catch (Exception e) { e.printStackTrace(); throw new IllegalArgumentException("cannot redirect to output file"); } }
From source file:catalina.startup.Catalina.java
/** * Start a new server instance./*www. ja va 2s . co m*/ */ protected void start() { // Create and execute our Digester Digester digester = createStartDigester(); File file = configFile(); try { InputSource is = new InputSource("file://" + file.getAbsolutePath()); FileInputStream fis = new FileInputStream(file); is.setByteStream(fis); digester.push(this); digester.parse(is); fis.close(); } catch (Exception e) { System.out.println("Catalina.start: " + e); e.printStackTrace(System.out); System.exit(1); } // Setting additional variables if (!useNaming) { System.setProperty("catalina.useNaming", "false"); } else { System.setProperty("catalina.useNaming", "true"); String value = "org.apache.naming"; String oldValue = System.getProperty(javax.naming.Context.URL_PKG_PREFIXES); if (oldValue != null) { value = value + ":" + oldValue; } System.setProperty(javax.naming.Context.URL_PKG_PREFIXES, value); value = System.getProperty(javax.naming.Context.INITIAL_CONTEXT_FACTORY); if (value == null) { System.setProperty(javax.naming.Context.INITIAL_CONTEXT_FACTORY, "org.apache.naming.java.javaURLContextFactory"); } } // If a SecurityManager is being used, set properties for // checkPackageAccess() and checkPackageDefinition if (System.getSecurityManager() != null) { String access = Security.getProperty("package.access"); if (access != null && access.length() > 0) access += ","; else access = "sun.,"; Security.setProperty("package.access", access + "org.apache.catalina.,org.apache.jasper."); String definition = Security.getProperty("package.definition"); if (definition != null && definition.length() > 0) definition += ","; else definition = "sun.,"; Security.setProperty("package.definition", // FIX ME package "javax." was removed to prevent HotSpot // fatal internal errors definition + "java.,org.apache.catalina.,org.apache.jasper."); } // Replace System.out and System.err with a custom PrintStream SystemLogHandler log = new SystemLogHandler(System.out); System.setOut(log); System.setErr(log); Thread shutdownHook = new CatalinaShutdownHook(); // Start the new server if (server instanceof Lifecycle) { try { server.initialize(); ((Lifecycle) server).start(); try { // Register shutdown hook Runtime.getRuntime().addShutdownHook(shutdownHook); } catch (Throwable t) { // This will fail on JDK 1.2. Ignoring, as Tomcat can run // fine without the shutdown hook. } // Wait for the server to be told to shut down server.await(); } catch (LifecycleException e) { System.out.println("Catalina.start: " + e); e.printStackTrace(System.out); if (e.getThrowable() != null) { System.out.println("----- Root Cause -----"); e.getThrowable().printStackTrace(System.out); } } } // Shut down the server if (server instanceof Lifecycle) { try { try { // Remove the ShutdownHook first so that server.stop() // doesn't get invoked twice Runtime.getRuntime().removeShutdownHook(shutdownHook); } catch (Throwable t) { // This will fail on JDK 1.2. Ignoring, as Tomcat can run // fine without the shutdown hook. } ((Lifecycle) server).stop(); } catch (LifecycleException e) { System.out.println("Catalina.stop: " + e); e.printStackTrace(System.out); if (e.getThrowable() != null) { System.out.println("----- Root Cause -----"); e.getThrowable().printStackTrace(System.out); } } } }
From source file:com.cyberway.issue.io.arc.ARCWriterTest.java
protected void lengthTooLong(String name, boolean compress, boolean strict) throws IOException { ARCWriter writer = createArcWithOneRecord(name, compress); // Add a record with a length that is too long. String content = getContent(); writeRecord(writer, SOME_URL, "text/html", content.length() + 10, getBais(content)); writeRecord(writer, SOME_URL, "text/html", content.length(), getBais(content)); writer.close();//from w w w. j a v a 2 s . co m // Catch System.err. ByteArrayOutputStream os = new ByteArrayOutputStream(); System.setErr(new PrintStream(os)); ARCReader r = ARCReaderFactory.get(writer.getFile()); r.setStrict(strict); int count = iterateRecords(r); assertTrue("Count wrong " + count, count == 4); // Make sure we get the warning string which complains about the // trailing bytes. String err = os.toString(); assertTrue("No message " + err, err.startsWith("WARNING Premature EOF before end-of-record")); }
From source file:science.atlarge.graphalytics.graphmat.GraphmatPlatform.java
public static void stopPlatformLogging() { System.setOut(sysOut); System.setErr(sysErr); }