c++spdlog

For Spdlog in C++, the typical example is to declare your log inside a function, but what if you want a single log used globally?


This may be a stupid, obvious typing question that I'm just overlooking a simple explanation for, so apologies. While using spdlog, the examples are ALL declared in functions, in my case like this:

void daily_example()
{   auto daily_logger = spdlog::daily_logger_mt("daily_logger", "logs/daily.txt", 2, 30);  }

This works without complaining about the declaration type. But, I'm writing a device driver, and I need to create a single /var/log/xxx.log rotated daily. So, I need the log instance available outside of any single function or even src file. I've tried inheriting this as class log: public spdlog..., but the compiler informs it is not a class. IN fact, it identifies as a double variable(?). So, the next obvious step would be to move the declaration to being a global:

auto daily_logger = spdlog::daily_logger_mt("daily_logger", "logs/daily.txt", 2, 30);

And then, when global, the compiler complains that daily_logger's methods can't be used:

error: request for member ‘debug’ in ‘* log’, which is of non-class type ‘double(double) 

So, it can't be used globally. Declared as a double, it complains that the type is still not right. I've seen one example of where this was inherited inside a function, but for some reason the same doesn't appear to work globally. So, I've even tried to just say scr*w it, I'll wrap the whole thing in my OWN class which should be exactly like a function, like this, which also doesn't work:

class log {
    public:
        std::string filename = "/var/log/name-redacted.log";
        log () {
            auto daily_log = spdlog::daily_logger_st("daily_logger", filename, 0, 1 );
        }
};

(same error: error: request for member ‘debug’ in ‘* log’, which is of non-class type ‘double(double) }

This seems like a namespace issue, but since I'm referencing the namespace directly, I can't imagine why it would behave differently. What am I doing wrong here? Also, honestly, I can't imagine a logger where declaring it for use only in a single function is useful. The examples for this seem to completely miss the mark first glance, so I think I'm misunderstanding how its intended to be used in namespaces. Referencing the namespace doesn't seem to work, and using it directly as spdlog::{method}...seems so repetitive globally. But this has to be used across multiple code files, etc.

I've tried:

//Globals
auto daily_logger = spdlog::daily_logger_mt("daily_logger", "logs/daily.txt", 2, 30);

And:

class daily_log: public spdlog {
     public:
          void daily_log () {
               daily_logger_mt("daily_logger", "logs/daily.txt", 2, 30);
          }
}

And:

class log {
    public:
        std::string filename = "/var/log/name-redacted.log";
        log () {
            auto daily_log = spdlog::daily_logger_st("daily_logger", filename, 0, 1 );
        }
};

Simple full example of what I'm seeing:

// For Logging!
#include "spdlog/spdlog.h"
#include "spdlog/cfg/env.h"  // support for loading levels from the environment variableinclude "spdlog/fmt/ostr.h" // support for user defined types
#include "spdlog/sinks/daily_file_sink.h"


class log {
    public:
        std::string filename = "/var/log/name-redacted.log";
        log () {
            auto daily_log = spdlog::daily_logger_st("daily_logger", filename, 0, 1 );
        }
};


int main( int argc, char *argv[] )
{
    log.debug("Logging Started.");
}

Gives the output:

$ g++ simple-spdlog.cpp  -o logger -I/usr/include/spdlog -lspdlog
simple-spdlog.cpp: In function ‘int main(int, char**)’:
simple-spdlog.cpp:18:9: error: request for member ‘debug’ in ‘log’, which is of non-class type ‘double(double) throw ()’ {aka ‘double(double)’}
     log.debug("Logging Started.");


Solution

  • This was the answer. I don't know how to make a comment an answer, so I'll have to post it using this method.

    Did you want to make the method static ? Then you can call it without an instance – 463035818_is_not_an_ai CommentedMar 27, 2023 at 14:46