bemeyer
bemeyer

Reputation: 6231

troubles with a simple logger

I am trying to create a simple logging class with just static methods but i get troubles when i try to log some const char* variable and wonder why. It says Error: expression must have integral or unscoped enum type. I looked for it at stackoverflow and google but somehow it doesn't help me to get it right. The logging methods are this:

void Logger::log(const char* message){
#ifdef _DEBUG
    std::cout<< "[INFO]" << message << std::endl;
#endif
}

void Logger::err(const char* message){
#ifdef _DEBUG
    std::cout << "[ERROR]" << message << std::endl;
#endif
}

And i am trying to call them here:

char* Config::getConfigValue(const char* name){
    if (m_doc.first_node())
    {
        xml_node<>* node = m_doc.first_node()->first_node(name);
        if (node)
        {
            Logger::log("Getting value: " + name + ": "+ node->value());
            return node->value();
        }
    }
    Logger::log("Getting value: " + name + " FAILED!");
    return 0;
}

Am i totaly wrong with the idea of such a simple logger?

And should i do #ifdef at every point where i log or can i do it like this?

For any hints to solve this issue and to help me to get a "good" logger i would be greatful.

Upvotes: 0

Views: 85

Answers (3)

linux0107
linux0107

Reputation: 33

The expression

"Getting value: " + name + " FAILED!"

where name is const char* name is an error because there is no operator+ in global namespace that takes const char*. You have to use strcat, strcpy, etc.

In C++ you should use std::string which is very flexible ( i.e resizes dynamically):

char* Config::getConfigValue( std::string name){
    //...
    Logger::log("Getting value: " + name + ": "+ node->value());
}

Upvotes: 1

Chnossos
Chnossos

Reputation: 10496

char * or char const * is a pointer type, not a class or struct, thus does not support concatenation using + operator.

You're using C++, so use the C++ way of handling strings : replace const char * with const std::string &.

Upvotes: 1

merlin2011
merlin2011

Reputation: 75629

The problem has nothing to do with your logging. You cannot concatenate C strings using +.

"Getting value: " + name // Not going to work.

If you are in c++ anyways, I would stick with std::string when possible because that one does support + for concatenation.

Upvotes: 3

Related Questions