Log File class in .header to log events while running

Viewed 72

I'm pretty new to c++ and I need your help, I think it's pretty easy questions but i cant get over the error

I have a .h file which includes methods to log error in txt file given but actually I dont really know how to use it could you please help me ? I try to test in the main by wrinting some error but i cant use the operator "<<"

here is the .h file

class FileLogger {

        public:


            // If you can´t/dont-want-to use C++11, remove the "class" word after enum
            enum e_logType { LOG_ERROR, LOG_WARNING, LOG_INFO };


            // ctor (remove parameters if you don´t need them)
            explicit FileLogger (const char *engine_version, const char *fname = "log.txt")
                    :   numWarnings (0U),
                        numErrors (0U)
            {

                myFile.open (fname);

                // Write the first lines
                if (myFile.is_open()) {
                    myFile << "Engine, version " << engine_version << std::endl;
                    myFile << "Log file created" << std::endl << std::endl;
                } // if

            }


            // dtor
            ~FileLogger () {

                if (myFile.is_open()) {
                    myFile << std::endl << std::endl;

                    // Report number of errors and warnings
                    myFile << numWarnings << " warnings" << std::endl;
                    myFile << numErrors << " errors" << std::endl;

                    myFile.close();
                } // if

            }


            // Overload << operator using log type
            friend FileLogger &operator << (FileLogger &logger, const e_logType l_type) {

                switch (l_type) {
                    case FileLogger::e_logType::LOG_ERROR:
                        logger.myFile << "[ERROR]: ";
                        ++logger.numErrors;
                        break;

                    case FileLogger::e_logType::LOG_WARNING:
                        logger.myFile << "[WARNING]: ";
                        ++logger.numWarnings;
                        break;

                    default:
                        logger.myFile << "[INFO]: ";
                        break;
                } // sw


                return logger;

            }


            // Overload << operator using C style strings
            // No need for std::string objects here
            friend FileLogger &operator << (FileLogger &logger, const char *text) {

                logger.myFile << text << std::endl;
                return logger;

            }


            // Make it Non Copyable (or you can inherit from sf::NonCopyable if you want)
            FileLogger (const FileLogger &) = delete;
            FileLogger &operator= (const FileLogger &) = delete;



        private:

            std::ofstream           myFile;

            unsigned int            numWarnings;
            unsigned int            numErrors;

    }; // class end




#endif // FILELOGGER_HPP

and the main

#include <iostream>
#include <fstream>
#include <vector>
#include <tuple>
#include <mutex>
#include <chrono>
#include <ctime>
#include "Log_File.h"
int main ()
{
FileLogger test("oke", "testloginfo" );
test<<(test, 'LOG_ERROR'); // HERE I got a problem i just want to write an error 




};

The error I got is strange , it's "error:ambiguous overload for 'operator<<' operand types are 'FileLogger' and 'int'"

Thank you!

1 Answers

This operator

friend FileLogger &operator << (FileLogger &logger, const char *text)

is called like this:

test << "some text";

Your error message is a little obscure, because of the comma operator, which evaluates both operands and returns the right one, ie your call is the same as:

test << 'LOG_ERROR';

And 'LOG_ERROR' is a multi-character literal:

Ordinary multicharacter literal, e.g. 'AB', is conditionally-supported, has type int and implementation-defined value.

String literals use double quotes " instead of single quotes '.

If you want to call the other overload:

friend FileLogger &operator << (FileLogger &logger, const e_logType l_type)

you need an instance of the enum:

test << FileLogger::LOG_ERROR;
Related