Redirecting ostream to file does not work

I have a custom logging system that allows me to send information to the log file and console, depending on the verbosity currently selected. Right now, the problem that I encountered is related to the output to the file, to the output to the console, which works fine.

Here is an example:

     ilra_talk << "Local IP: " << systemIP() << " |  Hostname: " << systemhostname() << endl;
     // the systemIP() and systemhostname() functions have already been defined

This should cause the current local IP and hostname of the system to be printed to a file. However, this only leads to the fact that information is displayed on the console, despite the fact that the function is overloaded, which leads to its printing on both.

I set out the code below. Any help is appreciated (as always).

There is currently a definition for ilra_talk, which creates a new class object:

#define ilra_talk ilra(__FUNCTION__,0)

Class definition:

class ilra
{
    static int ilralevel_set; // properly initialized in my main .cpp
    static int ilralevel_passed; // properly initialized in my main .cpp
    static bool relay_enabled; // properly initialized in my main .cpp
    static bool log_enabled; // properly initialized in my main .cpp
    static ofstream logfile; // properly initialized in my main .cpp
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;
    }
    static void ilra_log_enabled(bool toset){
        log_enabled = toset;

        if (log_enabled == true){
            // get current time
            time_t rawtime;
            time ( &rawtime );

            // name of log file (based on time of application start)
            stringstream logname_s;
            string logname = "rclient-";
            logname_s << rawtime;
            logname.append(logname_s.str());

            // open a log file
            logfile.open(logname.c_str());
        }
    }

    // output
    template <class T>
    ilra &operator<<(const T &v)
    {
        if(log_enabled == true){ // log_enabled is set to true
            logfile << v; 
            logfile << "Test" << endl;  // test will show up, but intended information will not appear
            }
        if(ilralevel_passed <= ilralevel_set)
            std::cout << v;
        return *this;
    }

    ilra &operator<<(std::ostream&(*f)(std::ostream&))
    {
        if(log_enabled == true) // log_enabled is set to true
            logfile << *f;
        if(ilralevel_passed <= ilralevel_set)
            std::cout << *f;
        return *this;
    }
};  // end of the class
+3
2

, :

  • logfile.flush() ilra:: ~ ilra(). - .

  • static ofstream logfile static ofstream *logfile: / ilra_log_enabled() NULL < . .

, - , iostreams printf(): . : , . , , . systemIP() systemhostname(), /etc , . , , ( : , ).

+1

, "", , - , log_enabled , . logfile log_enabled , operator<<?

cout ostream logfile ofstream. , ofstream?

0

Source: https://habr.com/ru/post/1753398/


All Articles