Logging

[ c++  ]
Written on October 10, 2018

When I was working on EVE Online I was accustomed to having a log viewer running at all times, capturing the log output from the EVE client (and the proxy/server when running a local server). We used to have an old program, implemented in Borland Delphi, simply called LogViewer, but then my colleague finally got fed up with it and wrote a new log viewer, dubbed LogLite.

LogLite is an open source project, so even though I’m no longer working for CCP, I can keep using it for my own projects - thanks, Filipp!

For my simple Vulkan sprite renderer, I’ve incorporated spdlog and implemented a custom log target to output the logs to LogLite.

LogLiteSample

In my .cpp files where I want to log stuff I have a line like this near the top:

static auto logger = GetLogger("main");

The GetLogger function looks like this:

std::shared_ptr<spdlog::logger> GetLogger(const char *name) {
    auto logger = spdlog::get(name);
    if(logger) {
        return logger;
    }
    auto sink = std::make_shared<LogLiteSink_mt>();
    logger = std::make_shared<spdlog::logger>(name, sink);
    logger->set_level(spdlog::level::debug);
    spdlog::register_logger(logger);
    return logger;
}

Now I can log from anywhere like this:

    logger->debug("Added {} to atlas", s);

I’m really happy with spdlog - I’ve done my share of low-level logging implementations in my career. I’m glad not to have to do this again, and still have all the features I know that I want.

If you’re curious about the implementation details on how to log to LogLite, look at the source