Results 1 to 9 of 9
  1. #1
    beezerbutt is offline Member
    Join Date
    Feb 2009
    Location
    South Africa
    Posts
    18
    Rep Power
    0

    Talking Redirecting output at Thread level

    Hi

    I am pretty sure i know the answer, but you never know...some smart guy out there might be able to clue me into a different perspective.

    What i would like to do is to tell the Thread...hey, dude, ALL your output regardless of what you are using: System.out, System.err etc. i want it directed to a file.

    Unfortunately i doubt such a facility exists...and that i have to replace all the System.xxx to a proprietary file i create using a FileWriter.

    It is a shame that i can not subclass System.out or System.err as they are final.
    Thanks for your consideration of this question.

  2. #2
    Nicholas Jordan's Avatar
    Nicholas Jordan is offline Senior Member
    Join Date
    Jun 2008
    Location
    Southwest
    Posts
    1,018
    Rep Power
    8

    Lightbulb Good question.

    I guess System dot file writes is JVM static, therefore one would have a mess at best.

    What I did was manage all threads from a central "Controller" class, redirecting there, sorta makes sense as there is a limit on open file handles:
    Java Code:
    java.lang.String osName = System.getProperty("os.name");
    // Whether we are working around Solaris.
    final boolean solarisWorkaround = ((osName != null) && (osName.indexOf("Solaris") != -1));
    Eranga has the correct id string for Mac. What I remember is 30 for winnie and 60 if you are on surface of Solaris during a search for two missing scientists.
    Introduction to Programming Using Java.
    Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor

  3. #3
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    7

    Default

    You can easily redirect the System.out and System.err streams, and System.in as well.

    Use setErr(), setIn(), setOut().

    Doing so is a smart way to implement logging with little effort.

  4. #4
    neilcoffey is offline Senior Member
    Join Date
    Nov 2008
    Posts
    286
    Rep Power
    7

    Default

    I think this is probably what Steve was getting at, but you can redirect System.out to your own made-up flavour of PrintStream whose print methods look at Thread.currentThread to decide where to redirect output to.

  5. #5
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    7

    Default

    Well, I have to admit I was thinking only of redirecting *all* threads to a stream pointing at a single file. Something like:

    File outFile = new File("c:/logs/SystemOut.txt");
    outFile.createNewFile();
    PrintStream outStream = new PrintStream(outFile);
    System.setOut(outStream);

  6. #6
    beezerbutt is offline Member
    Join Date
    Feb 2009
    Location
    South Africa
    Posts
    18
    Rep Power
    0

    Default

    Thanks everyone for your input.
    I think i have it now...Combo of Steve and Neil's comments should work just fine.

    The more Java code i write, the more i realize why EVERY single project i go to has a homegrown "Framework".
    For sure designing an object library means that you can not please everyone, but i wonder how well the Java libraries compare to the C++ (<--is that a dirty mnemonic on this forum :=) standard libraries...just a thought; i did not intend to prolong this topic.

    kind regards
    beez

  7. #7
    toadaly is offline Senior Member
    Join Date
    Jan 2009
    Posts
    671
    Rep Power
    6

    Default

    Hey. The software engineers here speak multiple languages, so c++ is not is not a dirty word(s?). In regards to Java libraries vs. STL, STL covers about 5% of what is included in the core Java API. If all you want is IO and collections, then STL is comparable to Java. But if you want a rich framework that does most of the route stuff for you and frees you to think at a more abstract level, Java is way better.

  8. #8
    Nicholas Jordan's Avatar
    Nicholas Jordan is offline Senior Member
    Join Date
    Jun 2008
    Location
    Southwest
    Posts
    1,018
    Rep Power
    8

    Default

    Yeah, listen to toadaly - we don't care about much of anything except code.

    STL can have runaway pointers, Java won't + libs are .... I think toadaly was being conservative in estimate of relative power of libs - you could always call into STL from Java if you wanted....

    Read AW Professional Series for a no bull ring around the competition.
    Introduction to Programming Using Java.
    Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor

  9. #9
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    7

    Default

    I used to do business applications in an IBM proprietary language. I knew I needed to move on at some point, so when I looked into C++. I saw a lot of power but also a tremendous effort to do relatively simple things. Not ideal for business apps, which tend to be simple from a system perspective but very involved in the manipulation of data. Eventually, I settled on Java.

    I would say that C++ is far superior for some things; in fact, there are things that can only be done in C++. On the other hand, Java can do a lot of things faster, easier, and more safely than C++.

    Neither one is "better". The challenge to the system architect is to choose the right language for the task at hand. Beware of the old saw (pun intended),

    "If your only tool is a hammer, everything starts to look like a nail."

Similar Threads

  1. Replies: 1
    Last Post: 10-21-2008, 10:21 AM
  2. Redirecting HTTP request to another webserver
    By barunva in forum Networking
    Replies: 0
    Last Post: 10-21-2008, 09:50 AM
  3. how to get empty fields when redirecting to previous page
    By sireesha in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 09-09-2008, 08:51 PM
  4. Row level locking........
    By jithan in forum New To Java
    Replies: 0
    Last Post: 09-02-2008, 08:09 AM
  5. row level locking
    By jithan in forum New To Java
    Replies: 1
    Last Post: 08-28-2008, 07:42 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •