Thread: Logging headaches

  1. #1
    Registered User
    Join Date
    Nov 2008
    Posts
    127

    Logging headaches

    Let's say I have a log function

    Code:
    void my_log(char * msg, unsigned int arg)
    {
      char buf[500];
      snprintf(buf, 500, msg, arg);
      
      printf(buf);
    }
    So I can do something like

    Code:
    int val = 3;
    my_log("Value = %d.\n", (unsigned int)val)
    And I'll get the output "Value = 3." But what if I want to log a floating point value? When I try

    Code:
    float val = 46.666666667f;
    my_log("Value = %f.\n", (unsigned int)val)
    I get -0.000000 output. At the very least, I would have expected to get 46.0000.

    So how would I have a wrapper logging function that would let me pass both floats and non-floats to it? One thought is to build the string beforehand, but my logger also has log levels and will avoid string construction if the logger isn't at the correct level since building a string can be expensive, especially if it's not even going to end up getting logged.

    Of course, another thought is to have a different prototype, but this example is simplified and my logger takes up to 10 args to be formatted into the string. So that would be a lot of combinations of prototypes, not to mention all the basically duplicated code it would create.

    Anyone else run into this before? What did you do?

  2. #2
    Registered User
    Join Date
    Nov 2008
    Posts
    127
    So, like usual, even though I spent 45 making my post and thinking about other ways to solve the issue before posting, it's not until just after I post that I solve the problem. I forgot that templates work on functions (not just classes). I made a templated function and now I can log whatever I want.

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Why are you using snprintf, char*, C-style char arrays and C-style (dangerous) formatting in C++?
    Do you realize there are better alternatives?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Internet Superhero
    Join Date
    Sep 2006
    Location
    Denmark
    Posts
    964
    You should look into Boost.Log. Very easy to use, and if you need it, the advanced features are there.
    How I need a drink, alcoholic in nature, after the heavy lectures involving quantum mechanics.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Char Headaches
    By SunnyDays in forum C Programming
    Replies: 4
    Last Post: 11-12-2012, 02:53 PM
  2. Caesar cipher bug headaches.
    By Arik in forum C Programming
    Replies: 9
    Last Post: 05-15-2010, 10:06 PM
  3. mingw environment gives me headaches...
    By pheres in forum C++ Programming
    Replies: 7
    Last Post: 08-16-2007, 01:34 PM
  4. Linked List headaches
    By Strait in forum C++ Programming
    Replies: 3
    Last Post: 02-16-2005, 08:40 PM
  5. DirectMusic headaches...
    By jdinger in forum Game Programming
    Replies: 1
    Last Post: 04-13-2002, 04:37 PM