[SOLVED] 0.6.0 Logger - placing custom logger within a class

I want to provide custom loggers within classes so that I may control the output of logging from the main line.

Problem is that the following does not compile for me -

class MyClass
{
  public:
  
    MyClass();
    
     Logger log("app.myclass");      // <=== PROBLEM HERE
    
  private:

}

as it gives this compiler error:

error: expected identifier before string constant
Logger log(“app.myclass”);

Is this achievable?

You may find some info about the capabilities of the LogHandlers here

@ScruffR, the answer (worked out by #1 son) is to use “C++ uniform initialisation syntax”

class MyClass
{
public:

MyClass();
Logger log {"app.myclass"}; // uniform initialisation syntax

private:

}

1 Like

Thank you, thank you! This was driving me nutty too. :smiley:

@drwheetos, due to your post I revisited this post because the original problem code I quoted was the same as the “solved” code. This being the case, I looked at my test code again:

/* LOG_TEST_02

Testing v0.6.0 logging

*/

/* Version History
2016-11-28 HR
- Created

*/

class TestClass {
    public:
        TestClass();
        void TestLogging(void);
        
    private:
        Logger mLog {"app.testclass"}; // uniform initialisation syntax
};


/*
LOG_LEVEL_ALL : special value that can be used to enable logging of all messages
LOG_LEVEL_TRACE : verbose output for debugging purposes
LOG_LEVEL_INFO : regular information messages
LOG_LEVEL_WARN : warnings and non-critical errors
LOG_LEVEL_ERROR : error messages
LOG_LEVEL_NONE : special value that can be used to disable logging of any messages

*/

SerialLogHandler logHandler(LOG_LEVEL_NONE, {   // Logging level for non-application messages
        { "app", LOG_LEVEL_NONE },                  // Default logging level for all application messages
        { "app.network", LOG_LEVEL_TRACE },          // Logging level for networking messages
        { "app.setup", LOG_LEVEL_WARN },           // Logging level for setup messages
        { "app.test", LOG_LEVEL_WARN }            // Logging level for setup messages
    });


// Set up my own logger instance
Logger LogSetup("app.setup");

// Test logging within class
TestClass testClass = TestClass();

void setup() {

    delay(2000L);
 
    LogSetup(LOG_LEVEL_INFO, "Setup() completed");
}

void loop() {
    
    // Specify logging level directly
    Log(LOG_LEVEL_TRACE, "This is a trace message");
    Log(LOG_LEVEL_INFO, "This is an info message");
    Log(LOG_LEVEL_WARN, "This is a warn message");
    Log(LOG_LEVEL_ERROR, "This is an error message");

    delay(1000L);
    
    testClass.TestLogging();
}


TestClass::TestClass(void)
{
  
}


void TestClass::TestLogging(void)
{
    mLog(LOG_LEVEL_TRACE, "TestLogging() - trace message");
    mLog(LOG_LEVEL_INFO, "TestLogging() - info message");
    mLog(LOG_LEVEL_WARN, "TestLogging() - warn message");
    mLog(LOG_LEVEL_ERROR, "TestLogging() - error message");
}

The above gives two compile errors:

/workspace/log_test_02.cpp:45:33: error: use of deleted function 'TestClass::TestClass(TestClass&&)'
 
                                 ^

../wiring/inc/spark_wiring_logging.h:388:5: error: declared here
     Logger(const Logger&) = delete;
     ^

So I am now at a loss - has something changed?