Devs.site

Use the spdlog library to create logs in C++

In this brief tutorial you will learn how to install your tools, clone a GIT repository and use the spdlog library to create logs.

First you will send an info log to the screen then you will save it to a file. Spdlog has more log targets that you can use once you get the basics of it.



Code

    /* Our application's code */

    #include <iostream>
    #include "spdlog/spdlog.h"
    #include "spdlog/sinks/basic_file_sink.h"

    int main() {
        // log message to file
        try {
            auto mylogger = spdlog::basic_logger_mt("mylogger", "logs.txt");
            mylogger->info("Hello world!");
        } catch (const spdlog::spdlog_ex &ex) {
            std::cout << ex.what() << std::endl;
        }
    }

Requirements

You will need a Linux operating system that has the Advanced Package Tool installed on it (Ubuntu was used in this example) where you have root access.

A simple text editor is enough (even better with syntax highlighting).

0 comments

Specify your e-mail if you want to receive notifications about new comments and replies