-
Notifications
You must be signed in to change notification settings - Fork 2
/
test-log.cpp
143 lines (111 loc) · 3.43 KB
/
test-log.cpp
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
131
132
133
134
135
136
137
138
139
140
141
142
143
#include <string>
#include <thread>
#include <sstream>
#include <iostream>
#include <mutex>
#include <unistd.h>
#include <zmq.hpp>
#include "chlog.hpp"
using namespace std;
using namespace ch_frb_io;
std::mutex cout_mutex;
static string msg_string(zmq::message_t &msg) {
return string(static_cast<const char*>(msg.data()), msg.size());
}
static void* server_main(zmq::context_t* ctx, string port, int sid) {
zmq::socket_t sock(*ctx, ZMQ_SUB);
sock.setsockopt(ZMQ_SUBSCRIBE, "", 0);
sock.bind(port.c_str());
{
std::lock_guard<std::mutex> lock(cout_mutex);
cout << "server_main " << sid << ": bound " << port << endl;
}
for (;;) {
zmq::message_t msg;
try {
if (!sock.recv(&msg)) {
cout << "log server: failed to receive message" << endl;
break;
}
} catch (const zmq::error_t& e) {
std::lock_guard<std::mutex> lock(cout_mutex);
cout << "log server: error receiving message: " << e.what() << endl;
break;
}
{
std::lock_guard<std::mutex> lock(cout_mutex);
cout << "Server " << sid << ": " << msg_string(msg) << endl;
}
}
{
std::lock_guard<std::mutex> lock(cout_mutex);
cout << "server_main " << sid << " done." << endl;
}
return NULL;
}
void log_client(int num) {
chime_log_set_thread_name(stringprintf("Client-%i", num));
for (int i=0; i<10; i++) {
chlog("Hello I am client " << num << ", message " << i);
}
}
int main() {
zmq::context_t* ctx = new zmq::context_t();
// Try opening the socket with a given context.
chime_log_set_name("myputer");
chime_log_set_thread_name("Main");
chime_log_open_socket(ctx);
chime_log_local(false);
chime_log_server* ser = new chime_log_server(cout, NULL, "127.0.0.1");
cout << "Server addr: " << ser->get_address() << endl;
ser->start();
chime_log_add_server(ser->get_address());
//chime_log_server ser2;
//cout << "Server addr: " << ser2.get_address() << endl;
/*
string port = "tcp://127.0.0.1:6666";
thread serverthread(std::bind(server_main, &ctx, port, 1));
serverthread.detach();
chime_log_add_server(port);
string port2 = "tcp://127.0.0.1:6667";
thread serverthread2(std::bind(server_main, &ctx, port2, 2));
serverthread2.detach();
chime_log_add_server(port2);
*/
// What if we connect to a server that doesn't exist?
string port3 = "tcp://127.0.0.1:6668";
chime_log_add_server(port3);
usleep(100000);
chlog("Hello world");
chlog("Hello " << 1 << ", " << 2 << ", 3");
chdebug("Debug" << 42+43);
chlogf("Gotta love %s style; %03i", "printf", 7);
usleep(100000);
/*
chime_log_close_socket();
// Now re-open with a new context
chime_log_open_socket(NULL);
chime_log_add_server(port);
chime_log_add_server(port2);
// (wait for connect)
usleep(100000);
*/
thread logger1(std::bind(log_client, 1));
thread logger2(std::bind(log_client, 2));
logger1.join();
logger2.join();
usleep(100000);
chime_log_close_socket();
usleep(100000);
chime_log_local(true);
chassert(4 < 7);
// Don't assert(false) when you want your unit tests to succeed!
//chassert(7 < 4);
ser->stop();
usleep(1100000);
delete ser;
usleep(100000);
delete ctx;
usleep(100000);
return 0;
}