templatized logging

Discussion in 'C++' started by Luther Baker, May 21, 2004.

  1. Luther Baker

    Luther Baker Guest

    I'd like to include some very simple logging in my app - removable at
    compile time.

    Avoiding macros .. I want to make sure this nonsense would be
    optimized away by the compiler if I don't need it.


    template<bool b>
    void
    log(const char* msg)
    {
    if (b == true)
    {
    std::cout << msg << std::endl;
    }
    }

    ....

    const bool is_log_on = false;

    int
    main(int argc, char* argv)
    {
    ...
    log<is_log_on>("I am here.");
    ...
    }



    Since "is_log_on" evaluates to false at compile time, I'm hoping the
    compiler optimizes it away so there is absolutely no affect on
    performance. Is there a well-known problem with this approach or
    extending it into a full blown out logging template class?

    <offtopic>
    Google and my ISPs newsgroup servers are so slow. Can anyone recommend
    a free or relatively inexpensive good newgroup server to use?
    </offtopic>

    Thanks,

    -Luther
     
    Luther Baker, May 21, 2004
    #1
    1. Advertising

  2. Luther Baker wrote:
    > I'd like to include some very simple logging in my app - removable at
    > compile time.


    Why not run-time?

    > Avoiding macros .. I want to make sure this nonsense would be
    > optimized away by the compiler if I don't need it.


    Once you get used to logging, you will always need it.

    > template<bool b>
    > void
    > log(const char* msg)
    > {
    > if (b == true)
    > {
    > std::cout << msg << std::endl;
    > }
    > }
    >
    > ...
    >
    > const bool is_log_on = false;
    >
    > int
    > main(int argc, char* argv)
    > {
    > ...
    > log<is_log_on>("I am here.");


    That doesn't _remove_ it. It just prevents it from outputting.

    > ...
    > }
    >
    >
    >
    > Since "is_log_on" evaluates to false at compile time, I'm hoping the
    > compiler optimizes it away so there is absolutely no affect on
    > performance.


    If performance is your concern, you should simply turn the logging off by
    default when running. And turn it on if needed.

    > Is there a well-known problem with this approach or
    > extending it into a full blown out logging template class?


    A problem I know of is often poor knowledge of available solutions that
    leads to spending too much time on re-inventing wheels making them square
    or triangular. Look at

    http://logging.apache.org/log4cxx/

    Victor
     
    Victor Bazarov, May 21, 2004
    #2
    1. Advertising

  3. "Luther Baker" <> wrote in message
    news:...
    > I'd like to include some very simple logging in my app - removable at
    > compile time.
    >
    > Avoiding macros .. I want to make sure this nonsense would be
    > optimized away by the compiler if I don't need it.
    >
    >
    > template<bool b>
    > void
    > log(const char* msg)
    > {
    > if (b == true)
    > {
    > std::cout << msg << std::endl;
    > }
    > }
    >
    > ...
    >
    > const bool is_log_on = false;
    >
    > int
    > main(int argc, char* argv)
    > {
    > ...
    > log<is_log_on>("I am here.");
    > ...
    > }
    >
    >
    >
    > Since "is_log_on" evaluates to false at compile time, I'm hoping the
    > compiler optimizes it away so there is absolutely no affect on
    > performance. Is there a well-known problem with this approach or
    > extending it into a full blown out logging template class?


    If you use a template class then you can specialise it rather than using a
    runtime if statement.

    template <bool on>
    class Logger
    {
    public:
    static void log()
    {
    cout << "here\n";
    }
    };

    template <>
    class Logger<false>
    {
    public:
    void log()
    {
    }
    };

    const bool logging = true;

    int main()
    {
    X<logging>::f();
    }

    More chance of the compiler performing the necessary optimisation if you
    give it a little help.

    john
     
    John Harrison, May 22, 2004
    #3
  4. Luther Baker

    Siemel Naran Guest

    (Luther Baker) wrote in message

    > I'd like to include some very simple logging in my app - removable at
    > compile time.
    >
    > Avoiding macros .. I want to make sure this nonsense would be
    > optimized away by the compiler if I don't need it.
    >
    >
    > template<bool b>
    > void
    > log(const char* msg)
    > {
    > if (b == true)
    > {
    > std::cout << msg << std::endl;
    > }
    > }
    >
    > ...
    >
    > const bool is_log_on = false;
    >
    > int
    > main(int argc, char* argv)
    > {
    > ...
    > log<is_log_on>("I am here.");
    > ...
    > }
    >
    >
    >
    > Since "is_log_on" evaluates to false at compile time, I'm hoping the
    > compiler optimizes it away so there is absolutely no affect on
    > performance. Is there a well-known problem with this approach or
    > extending it into a full blown out logging template class?


    The optimization may happen on your compiler -- check the assembly to
    be sure. To improve the chances of optimization, make the function
    inline. If this doesn't work, provide a specialization of log<false>
    which does nothing and dispense with the if statement inside
    log<true>. Let us know your findings, what compiler you used,
    compiler flags, etc.

    Note one normally says,

    log << "hello" << x+1;

    So even if logging is off, the compiler may still evaluate "hello" and
    x+1. If it knows that evaluating x+1 has no side effects and
    operator<< does nothing if bool is_log_on = false, then it need not
    evaluate x+1. But I don't think most compilers do this level of
    optimization.

    As a design issue, I'd declare const bool _log_on = true or false at
    the top, and so in code I just write log("I am here."); instead of
    log<is_log_on>("I am here.");. But that's your call.
     
    Siemel Naran, May 22, 2004
    #4
  5. Luther Baker

    Luther Baker Guest

    John Harrison wrote:

    > "Luther Baker" <> wrote in message
    > news:...

    ....
    >
    >
    > If you use a template class then you can specialise it rather than using a
    > runtime if statement.
    >
    > template <bool on>
    > class Logger
    > {
    > public:
    > static void log()
    > {
    > cout << "here\n";
    > }
    > };
    >
    > template <>
    > class Logger<false>
    > {
    > public:
    > void log()
    > {
    > }
    > };
    >
    > const bool logging = true;
    >
    > int main()
    > {
    > X<logging>::f();
    > }
    >
    > More chance of the compiler performing the necessary optimisation if you
    > give it a little help.


    Oh yes. I overlooked that completely - so much cleaner than a macro.

    Thanks again,

    -Luther
     
    Luther Baker, May 22, 2004
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. skscpp

    templatized state model

    skscpp, Sep 8, 2003, in forum: C++
    Replies:
    1
    Views:
    300
    Kasper van den Berg
    Sep 8, 2003
  2. hall
    Replies:
    2
    Views:
    300
  3. Paul Escherton

    Templatized operator () overload

    Paul Escherton, Nov 10, 2003, in forum: C++
    Replies:
    2
    Views:
    371
    tom_usenet
    Nov 10, 2003
  4. ma740988
    Replies:
    3
    Views:
    383
    Cy Edmunds
    Nov 6, 2004
  5. mrstephengross
    Replies:
    5
    Views:
    598
    Larry I Smith
    May 18, 2005
Loading...

Share This Page