IF Statement has strange behavior

Posted by BSchlinker on Stack Overflow See other posts from Stack Overflow or by BSchlinker
Published on 2010-06-18T15:35:22Z Indexed on 2010/06/18 15:43 UTC
Read the original article Hit count: 327

I've developed a 'custom' cout, so that I can display text to console and also print it to a log file. This cout class is passed a different integer on initialization, with the integer representing the verbosity level of the message. If the current verbosity level is greater then or equal to the verbosity level of the message, the message should print.

The problem is, I have messages printing even when the current verbosity level is too low. I went ahead and debugged it, expecting to find the problem. Instead, I found multiple scenarios where my if statements are not working as expected.

The statement if(ilralevel_passed <= ilralevel_set) will sometimes proceed even if ilralevel_set is LESS then ilralevel_passed. You can see this behavior in the following picture (my apologizes for using Twitpic) http://twitpic.com/1xtx4g/full. Notice how ilralevel_set is equal to zero, and ilralevel_passed is equal to one. Yet, the if statement has returned true and is now moving forward to pass the line to cout.

I've never seen this type of behavior before and I'm not exactly sure how to proceed debugging it. I'm not able to isolate the behavior either -- it only occurs in certain parts of my program. Any suggestions are appreciated as always.

//  Here is an example use of the function:
//  ilra_status << setfill('0') << setw(2) << dispatchtime.tm_sec << endl;
//  ilra_warning << "Dispatch time (seconds): " << mktime(&dispatchtime) << endl;

//  Here is the 'custom' cout function:
    #ifndef ILRA_H_
    #define ILRA_H_

    // System libraries
    #include <iostream>
    #include <ostream>
    #include <sstream>
    #include <iomanip>

    // Definitions
    #define ilra_talk ilra(__FUNCTION__,0)
    #define ilra_update ilra(__FUNCTION__,0)
    #define ilra_error ilra(__FUNCTION__,1)
    #define ilra_warning ilra(__FUNCTION__,2)
    #define ilra_status ilra(__FUNCTION__,3)

    // Statics
    static int ilralevel_set = 0;
    static int ilralevel_passed;

    // Classes
    class ilra
    {
    public:
        // constructor / destructor
        ilra(const std::string &funcName, int toset)
        {
            ilralevel_passed = toset;
        }
        ~ilra(){};

        // enable / disable irla functions
        static void ilra_verbose_level(int toset){
            ilralevel_set = toset;
        }

        // output
        template <class T>
        ilra &operator<<(const T &v)
        {
            if(ilralevel_passed <= ilralevel_set)
                std::cout << v;
            return *this;
        }

        ilra &operator<<(std::ostream&(*f)(std::ostream&))
        {
            if(ilralevel_passed <= ilralevel_set)
                std::cout << *f;
            return *this;
        }

    };  // end of the class

    #endif /* ILRA_H_ */

© Stack Overflow or respective owner

Related posts about c++

Related posts about debugging