Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed Logging #63

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ cmake .. -DCMAKE_BUILD_TYPE=Release -DBUILD_TESTS=ON -DBUILD_EXAMPLES=ON
make
# Run tests and examples
ctest -VV
./bin/subscriber
./bin/client
./bin/cpp_redis_subscriber
./bin/cpp_redis_client
```

## 5. Code your changes
Expand Down
2 changes: 1 addition & 1 deletion clang_format.sh
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
#!/bin/sh

find sources includes tests examples \( -name '*.cpp' -o -name '*.hpp' -o -name '*.ipp' -o -name '*.c' -o -name '*.h' \) -exec clang-format -i {} ';'
find sources includes tests examples \( -name '*.cpp' -o -name '*.hpp' -o -name '*.ipp' -o -name '*.c' -o -name '*.h' \) -exec clang-format -style=file -fallback-style=none -i {} ';'
140 changes: 71 additions & 69 deletions examples/cpp_redis_client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,97 +19,99 @@
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
#include <string>
#include "winsock_initializer.h"
#include <cpp_redis/cpp_redis>
#include <cpp_redis/misc/macro.hpp>
#include "winsock_initializer.h"
#include <string>

#define ENABLE_SESSION = 1

int
main(void) {
winsock_initializer winsock_init;
//! Enable logging
cpp_redis::active_logger = std::unique_ptr<cpp_redis::logger>(new cpp_redis::logger);
winsock_initializer winsock_init;
//! Enable logging
cpp_redis::active_logger = std::unique_ptr<cpp_redis::logger>(new cpp_redis::logger);

cpp_redis::client client;
cpp_redis::client client;

client.connect("127.0.0.1", 6379,
[](const std::string &host, std::size_t port, cpp_redis::connect_state status) {
if (status == cpp_redis::connect_state::dropped) {
std::cout << "client disconnected from " << host << ":" << port << std::endl;
}
});
client.connect("127.0.0.1", 6379,
[](const std::string& host, std::size_t port, cpp_redis::connect_state status) {
if (status == cpp_redis::connect_state::dropped) {
std::cout << "client disconnected from " << host << ":" << port << std::endl;
}
});

auto replcmd = [](const cpp_redis::reply &reply) {
std::cout << "set hello 42: " << reply << std::endl;
// if (reply.is_string())
// do_something_with_string(reply.as_string())
};
auto replcmd = [](const cpp_redis::reply& reply) {
std::cout << "set hello 42: " << reply << std::endl;
// if (reply.is_string())
// do_something_with_string(reply.as_string())
};

const std::string group_name = "groupone";
const std::string session_name = "sessone";
const std::string consumer_name = "ABCD";
const std::string group_name = "groupone";
const std::string session_name = "sessone";
const std::string consumer_name = "ABCD";

std::multimap<std::string, std::string> ins;
ins.insert(std::pair<std::string, std::string>{"message", "hello"});
std::multimap<std::string, std::string> ins;
ins.insert(std::pair<std::string, std::string>{"message", "hello"});

#ifdef ENABLE_SESSION

client.xadd(session_name, "*", ins, replcmd);
client.xgroup_create(session_name, group_name, "0", replcmd);

client.sync_commit();

client.xrange(session_name, {"-", "+", 10}, replcmd);

client.xreadgroup({group_name,
consumer_name,
{{session_name}, {">"}},
1, // Count
0, // block milli
false, // no ack
}, [](cpp_redis::reply &reply) {
std::cout << "set hello 42: " << reply << std::endl;
auto msg = reply.as_array();
std::cout << "Mes: " << msg[0] << std::endl;
// if (reply.is_string())
// do_something_with_string(reply.as_string())
});
client.xadd(session_name, "*", ins, replcmd);
client.xgroup_create(session_name, group_name, "0", replcmd);

client.sync_commit();

client.xrange(session_name, {"-", "+", 10}, replcmd);

client.xreadgroup({
group_name,
consumer_name,
{{session_name}, {">"}},
1, // Count
0, // block milli
false, // no ack
},
[](cpp_redis::reply& reply) {
std::cout << "set hello 42: " << reply << std::endl;
auto msg = reply.as_array();
std::cout << "Mes: " << msg[0] << std::endl;
// if (reply.is_string())
// do_something_with_string(reply.as_string())
});

#else

// same as client.send({ "SET", "hello", "42" }, ...)
client.set("hello", "42", [](cpp_redis::reply &reply) {
std::cout << "set hello 42: " << reply << std::endl;
// if (reply.is_string())
// do_something_with_string(reply.as_string())
});

// same as client.send({ "DECRBY", "hello", 12 }, ...)
client.decrby("hello", 12, [](cpp_redis::reply &reply) {
std::cout << "decrby hello 12: " << reply << std::endl;
// if (reply.is_integer())
// do_something_with_integer(reply.as_integer())
});

// same as client.send({ "GET", "hello" }, ...)
client.get("hello", [](cpp_redis::reply &reply) {
std::cout << "get hello: " << reply << std::endl;
// if (reply.is_string())
// do_something_with_string(reply.as_string())
});
// same as client.send({ "SET", "hello", "42" }, ...)
client.set("hello", "42", [](cpp_redis::reply& reply) {
std::cout << "set hello 42: " << reply << std::endl;
// if (reply.is_string())
// do_something_with_string(reply.as_string())
});

// same as client.send({ "DECRBY", "hello", 12 }, ...)
client.decrby("hello", 12, [](cpp_redis::reply& reply) {
std::cout << "decrby hello 12: " << reply << std::endl;
// if (reply.is_integer())
// do_something_with_integer(reply.as_integer())
});

// same as client.send({ "GET", "hello" }, ...)
client.get("hello", [](cpp_redis::reply& reply) {
std::cout << "get hello: " << reply << std::endl;
// if (reply.is_string())
// do_something_with_string(reply.as_string())
});

#endif

// commands are pipelined and only sent when client.commit() is called
// client.commit();
// commands are pipelined and only sent when client.commit() is called
// client.commit();

// synchronous commit, no timeout
client.sync_commit();
// synchronous commit, no timeout
client.sync_commit();

// synchronous commit, timeout
// client.sync_commit(std::chrono::milliseconds(100));
// synchronous commit, timeout
// client.sync_commit(std::chrono::milliseconds(100));

return 0;
return 0;
}
82 changes: 41 additions & 41 deletions examples/cpp_redis_consumer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,64 +19,64 @@
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
#include <string>
#include <condition_variable>
#include <iostream>
#include <mutex>
#include <signal.h>
#include <string>

#include <cpp_redis/cpp_redis>
#include "winsock_initializer.h"
#include <cpp_redis/cpp_redis>

std::condition_variable should_exit;

void
sigint_handler(int) {
should_exit.notify_all();
should_exit.notify_all();
}

int
main() {
winsock_initializer winsock_init;
//! Enable logging
winsock_initializer winsock_init;
//! Enable logging

//const std::string group_name = "groupone";
const std::vector<std::string> group_names = {"groupone"}; //, "grouptwo"};
const std::string session_name = "sessone";
const std::string consumer_name = "ABCD";
//const std::string group_name = "groupone";
const std::vector<std::string> group_names = {"groupone"}; //, "grouptwo"};
const std::string session_name = "sessone";
const std::string consumer_name = "ABCD";

cpp_redis::active_logger = std::unique_ptr<cpp_redis::logger>(new cpp_redis::logger);
cpp_redis::active_logger = std::unique_ptr<cpp_redis::logger>(new cpp_redis::logger);

cpp_redis::consumer sub(session_name, consumer_name);
cpp_redis::consumer sub(session_name, consumer_name);

sub.connect("127.0.0.1", 6379,
[](const std::string &host, std::size_t port, cpp_redis::connect_state status) {
if (status == cpp_redis::connect_state::dropped) {
std::cout << "client disconnected from " << host << ":" << port << std::endl;
}
});
sub.connect("127.0.0.1", 6379,
[](const std::string& host, std::size_t port, cpp_redis::connect_state status) {
if (status == cpp_redis::connect_state::dropped) {
std::cout << "client disconnected from " << host << ":" << port << std::endl;
}
});

sub.auth("{redis_key}");
sub.auth("{redis_key}");

for (auto &group : group_names) {
for (auto& group : group_names) {

sub.subscribe(group,
[group](const cpp_redis::message_type msg) {
cpp_redis::consumer_response_t res;
// Callback will run for each message obtained from the queue
std::cout << "Group: " << group << std::endl;
std::cout << "Id in the cb: " << msg.get_id() << std::endl;
res.insert({"Id", msg.get_id()});
return res;
},
[group](int ack_status) {
// Callback will run upon return of xack
std::cout << "Group: " << group << std::endl;
std::cout << "Ack status: " << ack_status << std::endl;
});
}
sub.subscribe(group,
[group](const cpp_redis::message_type msg) {
cpp_redis::consumer_response_t res;
// Callback will run for each message obtained from the queue
std::cout << "Group: " << group << std::endl;
std::cout << "Id in the cb: " << msg.get_id() << std::endl;
res.insert({"Id", msg.get_id()});
return res;
},
[group](int ack_status) {
// Callback will run upon return of xack
std::cout << "Group: " << group << std::endl;
std::cout << "Ack status: " << ack_status << std::endl;
});
}

/*sub.subscribe(group_name,
/*sub.subscribe(group_name,
[](const cpp_redis::message_type msg) {
// Callback will run for each message obtained from the queue
std::cout << "Id in the cb: " << msg.get_id() << std::endl;
Expand All @@ -87,12 +87,12 @@ main() {
std::cout << "Ack status: " << ack_status << std::endl;
});*/

sub.commit();
sub.commit();

signal(SIGINT, &sigint_handler);
std::mutex mtx;
std::unique_lock<std::mutex> l(mtx);
should_exit.wait(l);
signal(SIGINT, &sigint_handler);
std::mutex mtx;
std::unique_lock<std::mutex> l(mtx);
should_exit.wait(l);

return 0;
return 0;
}
2 changes: 1 addition & 1 deletion examples/cpp_redis_future_client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@

#include <cpp_redis/cpp_redis>

#include <iostream>
#include "winsock_initializer.h"
#include <iostream>

int
main(void) {
Expand Down
2 changes: 1 addition & 1 deletion examples/cpp_redis_high_availability_client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@

#include <cpp_redis/cpp_redis>

#include <iostream>
#include "winsock_initializer.h"
#include <iostream>

int
main(void) {
Expand Down
2 changes: 1 addition & 1 deletion examples/cpp_redis_kill.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@

#include <cpp_redis/cpp_redis>

#include "winsock_initializer.h"
#include <iostream>
#include <sstream>
#include "winsock_initializer.h"

int
main(void) {
Expand Down
Loading