Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7000883
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T20:43:49+00:00 2026-05-27T20:43:49+00:00

I had implemented a logging function as following: void log (String output) { if(debug)

  • 0

I had implemented a logging function as following:

void log (String output)
{
  if(debug)
  {
    String val = <some string captured>;
    Log.write("Logging: " + val + output, this.handle);
  }
}

This log is used at several places as,

log("value is " + str + " " + val);  // 'str','val' can be a string or class with '.toString()'
log("error: " + e1 + ". Exiting."); // 'e1' can be a class with overloaded '.tostring()'

Now, the problem is (got as code review comments), the class object to string conversion will happen even if debug condition is not ON. At several places, this will lead to unwanted performance downgrade.

I don’t want to clutter my code by putting that if() condition everywhere where only output matters and rest of the things are repeated. What is the best way of solving this problem using Java facilities?

I thought of using generics:

<T> void log (String output, T arg)
{
  if(debug)
  {
    String val = <some string captured>;
    Log.write("Logging: " + val + output + arg.toString(), this.handle);
  }
}

Will this approach make a good optimization ? Also, can I elegantly extend this generic function to take multiple arguments ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T20:43:50+00:00Added an answer on May 27, 2026 at 8:43 pm

    Actually, you don’t even need generics – you can use Object:

    void log (String output, Object arg) { ... }
    

    For multiple objects you can use varargs and MessageFormat, for example, as follows:

    void log (String pattern, Object... args) {
        if (debug) {
            Log.write(MessageFormat.format(pattern, args));
        }
    }
    

    .

    log("value is {0} {1}", str, val);  
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Need some help with this problem in implementing with XSLT, I had already implemented
I had started working on GPGPU some days ago and successfully implemented cholesky factorization
I was looking at how some site implemented rounded corners, and the CSS had
I've implemented a Sobel Edge Detector and had some questions about computing edge orientations.
I've got a UITableView with custom tablecells. First I had a version which implemented
I had try to implement the send me log feature into my apps but
I've had this long term issue in not quite understanding how to implement a
we've implemented a system similar to the one described in this other SO post
Recently, I've had a request to implement logging for all of the Controller classes
In Flex 3.2 I had implemented a tooltip manager and I used the topLevelSystemManager

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.