• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Paul Clapham
  • paul wheaton
Sheriffs:
  • Tim Cooke
  • Devaka Cooray
  • Rob Spoor
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:

System.out.println() variation

 
Ranch Hand
Posts: 750
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I have currently have a class with some tools in.
The class is called O, and has the method:


Allowing me to just write O.p("Hello World"); for example.

What I would like to do is make a similar method that displays the class name.
For example, in the class Main, calling O.p("Hello World"); should output "Main: Hello World"

I'm not sure if theres a simple solution for this, but it would help me in debugging as I have quite a few classes in my programs.

Thanks for any help
 
Sheriff
Posts: 22769
130
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The only way to do so would be to use the stack trace (Thread.currentThread().getStackTrace()), then take the second element (the first element will be the p method call). This won't be efficient though.
 
Marshal
Posts: 27987
94
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Not efficient, maybe, but it's what log4j does when you ask it to report class names and that's rarely a problem.

(You could just use log4j, Colin, instead of trying to build your own logging package.)
 
Rob Spoor
Sheriff
Posts: 22769
130
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I hardly ever let log4j or slf4j print the class name. I let it print out the logger name which is usually the same as the class name*, but not the class name itself.

* Since I create my slf4j loggers as LoggerFactory.getLogger(CurrentClass.class)
 
colin shuker
Ranch Hand
Posts: 750
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for replies,

I need jar files for the those logging things don't I?

For some reason when I jar a project that has jars added, sometimes the jar runs fine on my pc, but not on others computers.
Something to do with not being able to find those classes, I don't get why it works on one pc, but not another, so would rather avoid that from happening.
 
Paul Clapham
Marshal
Posts: 27987
94
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

colin shuker wrote:I need jar files for the those logging things don't I?

For some reason when I jar a project that has jars added, sometimes the jar runs fine on my pc, but not on others computers.
Something to do with not being able to find those classes, I don't get why it works on one pc, but not another, so would rather avoid that from happening.



Well, yeah. You do need jar files. As for why it doesn't work on those other computers, that would be because you didn't set up the classpath properly there. It isn't hard to set up a class path, as I'm sure you know by now. It doesn't make sense to reinvent every wheel just for that reason.
 
colin shuker
Ranch Hand
Posts: 750
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Okay, I don't really get the thing with the classpath exactly.
 
If you settle for what they are giving you, you deserve what you get. Fight for this tiny ad!
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic