Is it possible to listen to ofLog messages from OF?

#1

Hi, I’m using OF as a dynamic library and I would like to be able to print OF error messages to host app’s console.

For example, if loading an image fails, OF will print something like

loadImage(): couldn't load image from "/path/to/img.png"

Is it possible to listen to this ofLogError message as a string in realtime?

#2

Yes you can!

You can use ofSetLoggerChannel function:

I have used it a couple of times but I think there is no example for that…

Eduard

1 Like
#3

Thank you very much for your answer.

Do you mind posting the simplest example of using the function?

How to get the log as a string?

#4

I think I figured out

In ofApp.h above class ofApp

/// \brief A logger channel that logs its messages to the console.
class MyChannel: public ofBaseLoggerChannel{
public:
    /// \brief Destroy the console logger channel.
    virtual ~MyChannel(){};
    void log(ofLogLevel level, const std::string & module, const std::string & message)
    {
        cout << "MSG : " << message << endl;
    }
    void log(ofLogLevel level, const std::string & module, const char* format, ...) OF_PRINTF_ATTR(4, 5)
    {
        
    }
    void log(ofLogLevel level, const std::string & module, const char* format, va_list args)
    {
        
    }
};

In ofApp.cpp

//--------------------------------------------------------------
void ofApp::setup(){

    channel = make_shared<MyChannel>();
    ofSetLoggerChannel(channel);
    
    ofImage image;
    image.load("test.jpg");
    
    ofExit();
}

The result:

MSG : loadImage(): couldn’t load image from ““test.jpg””

1 Like