wangrong
2025-01-18 8b0e31121a9a29b92a41fc575138a2e9b4c12d8e
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#include "../utils/vp_utils.h"
#include "../utils/logger/vp_logger.h"
 
#include <iostream>
#include <chrono>
#include <thread>
 
/*
* ## sample for vp_logger ##
* show how vp_logger works
*/
 
int main() {
    // config for log content
    // VP_SET_LOG_INCLUDE_THREAD_ID(false);
    // VP_SET_LOG_INCLUDE_CODE_LOCATION(false);
    // VP_SET_LOG_INCLUDE_LEVEL(false);
 
    // config for output
    // VP_SET_LOG_TO_CONSOLE(false);
    // VP_SET_LOG_TO_FILE(false);
 
    // config for log folder
    VP_SET_LOG_DIR("./log");
 
    // config log level
    VP_SET_LOG_LEVEL(vp_utils::vp_log_level::DEBUG);
 
    // config for kafka, ignored automatically if not prepared for kafka
    VP_SET_LOG_TO_KAFKA(true);  // false by default if not set
    VP_SET_LOG_KAFKA_SERVERS_AND_TOPIC("192.168.130.228:9092/vp_log");
    
    // init
    VP_LOGGER_INIT();
 
    // 6 threads logging separately
    auto func1 = []() {
        while (true) {
            /* code */
            auto id = std::this_thread::get_id();
            std::stringstream ss;
            ss << std::hex << id;
            auto thread_id = ss.str(); 
            VP_ERROR(vp_utils::string_format("thread id: %s", thread_id.c_str()));
            std::this_thread::sleep_for(std::chrono::milliseconds(2));
        }
        
    };
 
    auto func2 = []() {
        while (true) {
            /* code */
            auto id = std::this_thread::get_id();
            std::stringstream ss;
            ss << std::hex << id;
            auto thread_id = ss.str(); 
            VP_DEBUG(vp_utils::string_format("thread id: %s", thread_id.c_str()));
            std::this_thread::sleep_for(std::chrono::milliseconds(13));
        }
        
    };
 
    auto func3 = []() {
        while (true) {
            /* code */
            auto id = std::this_thread::get_id();
            std::stringstream ss;
            ss << std::hex << id;
            auto thread_id = ss.str(); 
            VP_INFO(vp_utils::string_format("thread id: %s", thread_id.c_str()));
            std::this_thread::sleep_for(std::chrono::milliseconds(4));
        }
        
    };
 
    auto func4 = []() {
        while (true) {
            /* code */
            auto id = std::this_thread::get_id();
            std::stringstream ss;
            ss << std::hex << id;
            auto thread_id = ss.str(); 
            VP_WARN(vp_utils::string_format("thread id: %s", thread_id.c_str()));
            std::this_thread::sleep_for(std::chrono::milliseconds(10));
        }
        
    };
 
    auto func5 = []() {
        while (true) {
            /* code */
            auto id = std::this_thread::get_id();
            std::stringstream ss;
            ss << std::hex << id;
            auto thread_id = ss.str(); 
            VP_ERROR(vp_utils::string_format("thread id: %s", thread_id.c_str()));
            std::this_thread::sleep_for(std::chrono::milliseconds(10));
        }
        
    };
 
    auto func6 = []() {
        while (true) {
            /* code */
            auto id = std::this_thread::get_id();
            std::stringstream ss;
            ss << std::hex << id;
            auto thread_id = ss.str(); 
            VP_INFO(vp_utils::string_format("thread id: %s", thread_id.c_str()));
            std::this_thread::sleep_for(std::chrono::milliseconds(10));
        }
        
    };
 
    std::thread t1(func1);
    std::thread t2(func2);
    std::thread t3(func3);
    std::thread t4(func4);
    std::thread t5(func5);
    std::thread t6(func6);
 
    t1.join();
    t2.join();
    t3.join();
    t4.join();
    t5.join();
    t6.join();
 
    std::getchar();
}