I have a simple log class that I want to use for logging which file, line, and function created the log message. I am passing the information in the following way:
Logger::Log(LoggerLevel::ERROR, __FILE__ << ":" << __LINE__ << "::" << __FUNCTION__ << ":::" << logMessage);
Here is my header file for the Logger.
#pragma once
#include <iostream>
#include <sstream>
enum class LoggerLevel { ERROR = -1, WARN = 0, INFO = 1, DEBUG = 2, TRACE = 3 };
class Logger
{
public:
static LoggerLevel logLevel;
static LoggerLevel GetLoggerLevel();
static void LogMsg(LoggerLevel anLogLevel, const std::ostream &aMsg);
// static void Log(LoggerLevel anLogLevel, const std::basic_streambuf<char> *aMsg)
// {
// LogMsg(anLogLevel, std::stringstream() << aMsg);
// }
private:
};
#define Log(x, y) Logger::LogMsg(x, std::stringstream() << y)
I want to know how I can do the same thing without using the #define at the bottom, i.e. using a C++ function similar to the one that is commented out in my header file.
When I try to use the C++ function, I get the following error message:
MyClass.cpp:65:44: error: invalid operands of types ‘const char [24]’ and ‘const char [2]’ to binary ‘operator<<’
65 | Logger::Log(LoggerLevel::ERROR, __FILE__ << ":" << __LINE__ << "::" << __FUNCTION__ << ":::" << logMsg);
| ~~~~~~~~ ^~ ~~~
| | |
| | const char [2]
| const char [24]