• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 302
  • Last Modified:

Anything like C/C++ _LINE_ in JAVA ?

Does anyone have any code that will do what the _LINE_ in C/C++ does when logging messages?

Thanks in advance !
0
jefftk
Asked:
jefftk
  • 2
1 Solution
 
jefftkAuthor Commented:
Adjusted points to 100
0
 
msmolyakCommented:
The line number is contained in a stack trace which gets generated when the exception is thrown (if the code is compiled in debug mode). Can you simply print a stack trace if something happens (otherwise why do you need a line number)?
0
 
jefftkAuthor Commented:
I am trying to simulate the C/C++ environment...
There are _FILE_ and _LINE_  macros the supply the program name and line number to the logging facility...
0
 
yorenCommented:
There isn't a direct macro to give you the file and line numbers. If you're into kludges, however, you can do the following:

1. throw an exception
2. catch it
3. capture the stack trace in a string
4. parse it to get the file and line number

Here are steps 1-3:

    try {
      throw new Exception();
    }
    catch(Exception e) {
        StringBuffer sbTrace;
        StringWriter sw = new StringWriter();
        e.printStackTrace(new PrintWriter(sw));
       
        sbTrace = sw.getBuffer();

        // sbTrace has the error message that includes
        // the file and line number
    }


0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now