File: thread-names.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 (45 lines) | stat: -rw-r--r-- 1,249 bytes parent folder | download | duplicates (5)
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
//
// This file is part of Easylogging++ samples
//
// Demo for thread names
//
// Compile: g++ -std=c++11 -Wall -Werror thread-names.cpp -lpthread -o thread-names -DELPP_THREAD_SAFE
//
// Revision 1.0
// @author mkhan3189
//

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

INITIALIZE_EASYLOGGINGPP

int main(int,char**){
    el::Loggers::reconfigureAllLoggers(el::ConfigurationType::Format, "%datetime [%thread_name] %msg");
    LOG(INFO)<<"The program has started! (no thread name)";
    el::Helpers::setThreadName("main-thread");
    
    std::thread thread1([](){
		LOG(INFO) << "Setting thread name for thread1";
        el::Helpers::setThreadName("thread1");
		LOG(INFO) << "Done setting thread name for thread1";
        for (int i = 0; i < 100; ++i) {
            LOG(INFO) << "Current i = " << i << " from thread1";
        }
    });

    std::thread thread2([](){
		LOG(INFO) << "Setting thread name for thread2";
        el::Helpers::setThreadName("thread2");
		LOG(INFO) << "Done setting thread name for thread2";
        for (int i = 0; i < 100; ++i) {
            LOG(INFO) << "Current i = " << i << " from thread2";
        }
    });
    
    thread1.join();
    thread2.join();
    
    LOG(INFO) << "Shutting down.";
    return 0;
}