jeudi 25 janvier 2018

Superclass for ostream and fstream

I'm trying to make my own logging class in C++ using a wrapper class in which i overloaded operator<< that sends it to the cout. Now I want to change it, so that when i create instance of that class, i can pass and argument that logs data in std::cout or some file i create. What is the exact type that is superclass of both fstream and ostream? I tried with std::ios&, std::basic_ios&, std::basic_ostream& and none of them seems to work (throwing me compilation error).

class myostream {
public:

    static int getlogLevel() {
         return loglevel;
    }
    static void setlogLevel(int i) {
         loglevel = i;
    }
    myostream(std::basic_ios& cout, int level)
    : _cout(cout), _level(level)
    {}

    template<class T>
    std::ostream& operator<<(T t) {
        if(_level >= loglevel) {
             _cout << loglevelcolor[_level] << loglevelname[_level]  << " "  << t << COL_RESET << std::endl;

        }

        return _cout;
    }
private:
    static int loglevel;    
    std::basic_ostream& _cout;
    int _level;
};

Aucun commentaire:

Enregistrer un commentaire