1 //
2 // This file is part of Easylogging++ samples
3 //
4 // Demo for thread names
5 //
6 // Compile: g++ -std=c++11 -Wall -Werror thread-names.cpp -lpthread -o thread-names -DELPP_THREAD_SAFE
7 //
8 // Revision 1.0
9 // @author mkhan3189
10 //
11 
12 #include <thread>
13 #include "easylogging++.h"
14 
15 INITIALIZE_EASYLOGGINGPP
16 
main(int,char **)17 int main(int,char**){
18     el::Loggers::reconfigureAllLoggers(el::ConfigurationType::Format, "%datetime [%thread_name] %msg");
19     LOG(INFO)<<"The program has started! (no thread name)";
20     el::Helpers::setThreadName("main-thread");
21 
22     std::thread thread1([](){
23 		LOG(INFO) << "Setting thread name for thread1";
24         el::Helpers::setThreadName("thread1");
25 		LOG(INFO) << "Done setting thread name for thread1";
26         for (int i = 0; i < 100; ++i) {
27             LOG(INFO) << "Current i = " << i << " from thread1";
28         }
29     });
30 
31     std::thread thread2([](){
32 		LOG(INFO) << "Setting thread name for thread2";
33         el::Helpers::setThreadName("thread2");
34 		LOG(INFO) << "Done setting thread name for thread2";
35         for (int i = 0; i < 100; ++i) {
36             LOG(INFO) << "Current i = " << i << " from thread2";
37         }
38     });
39 
40     thread1.join();
41     thread2.join();
42 
43     LOG(INFO) << "Shutting down.";
44     return 0;
45 }
46