File: multi-loggers-with-async.cpp

package info (click to toggle)
easyloggingpp 9.97.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye
  • size: 2,840 kB
  • sloc: cpp: 11,415; python: 2,336; sh: 337; makefile: 29
file content (51 lines) | stat: -rw-r--r-- 1,347 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
//
// This file is part of Easylogging++ samples
//
// Revision 1.0
//

#include <chrono>
#include <thread>
#include <future>
#include "easylogging++.h"

INITIALIZE_EASYLOGGINGPP

void f() {
    std::async(std::launch::async, [&]() {
        std::cout << "[internal] inside async()" << std::endl;
        std::this_thread::sleep_for(std::chrono::seconds(1));
        LOG(INFO) << "This is from async";
    });
}

class MyHandler : public el::LogDispatchCallback {
public:
    void handle(const el::LogDispatchData* d) {
        std::cout << "Message: " << d->logMessage()->message() << " [logger: " << d->logMessage()->logger()->id() << "]" << std::endl;
        if (d->logMessage()->logger()->id() != "default") {
            std::cout << "[internal] calling f()" << std::endl;
            f();
        }
    }
};



int main(int,char**){
    el::Loggers::addFlag(el::LoggingFlag::CreateLoggerAutomatically);
//    el::Helpers::uninstallLogDispatchCallback<el::base::DefaultLogDispatchCallback>("DefaultLogDispatchCallback");
    el::Helpers::installLogDispatchCallback<MyHandler>("MyHandler");

    LOG(INFO)<<"The program has started!";
    CLOG(INFO, "frommain") << "THis is another";
    
    std::thread t1([](){
        LOG(INFO) << "This is from thread";
    });
    
    t1.join();
    
    LOG(INFO) << "Shutting down.";
    return 0;
}