Skip to content
This repository has been archived by the owner on Apr 6, 2019. It is now read-only.

Examples

Simon Ninon edited this page Dec 11, 2016 · 4 revisions

TCP Server

#include <tacopie/tacopie>

#include <iostream>
#include <mutex>
#include <condition_variable>
#include <signal.h>

std::condition_variable cv;

void
signint_handler(int) {
  cv.notify_all();
}

void
on_new_client(tacopie::tcp_client& client, const tacopie::tcp_client::read_result& res) {
  if (res.success) {
    std::cout << "Client recv data" << std::endl;
    client.async_write({ res.buffer, nullptr });
    client.async_read({ 1024, &on_new_client });
  }
  else {
    std::cout << "Client disconnected" << std::endl;
    client.disconnect();
  }
}

int
main(void) {
  tacopie::tcp_server s;
  s.start("127.0.0.1", 3001, [] (tacopie::tcp_client& client) -> bool {
    std::cout << "New client" << std::endl;
    client.async_read({ 1024, &on_new_client });
    return true;
  });

  signal(SIGINT, &signint_handler);

  std::mutex mtx;
  std::unique_lock<std::mutex> lock(mtx);
  cv.wait(lock);

  return 0;
}

Logger

#include <tacopie/tacopie>

#include <iostream>

class my_logger : public tacopie::logger_iface {
public:
  //! ctor & dtor
  my_logger(void)  = default;
  ~my_logger(void) = default;

  //! copy ctor & assignment operator
  my_logger(const my_logger&) = default;
  my_logger& operator=(const my_logger&) = default;

public:
  void
  debug(const std::string& msg, const std::string& file, std::size_t line) {
    std::cout << "debug: " << msg << " @ " << file << ":" << line << std::endl;
  }

  void
  info(const std::string& msg, const std::string& file, std::size_t line) {
    std::cout << "info: " << msg << " @ " << file << ":" << line << std::endl;
  }

  void
  warn(const std::string& msg, const std::string& file, std::size_t line) {
    std::cout << "warn: " << msg << " @ " << file << ":" << line << std::endl;
  }

  void
  error(const std::string& msg, const std::string& file, std::size_t line) {
    std::cerr << "error: " << msg << " @ " << file << ":" << line << std::endl;
  }
};


int
main(void) {
  //! By default, no logging
  //! Force logger call, just for the example (you will never have to do that by yourself)
  std::cout << "By default: no logging" << std::endl;
  __TACOPIE_LOG(debug, "This is a debug message");
  __TACOPIE_LOG(info, "This is an info message");
  __TACOPIE_LOG(warn, "This is a warn message");
  __TACOPIE_LOG(error, "This is an error message");
  std::cout << std::endl;

  //! Use the default logger, provided with the library
  tacopie::active_logger = std::unique_ptr<tacopie::logger>(new tacopie::logger(tacopie::logger::log_level::debug));
  //! Force logger call, just for the example (you will never have to do that by yourself)
  std::cout << "With the library provided logger" << std::endl;
  __TACOPIE_LOG(debug, "This is a debug message");
  __TACOPIE_LOG(info, "This is an info message");
  __TACOPIE_LOG(warn, "This is a warn message");
  __TACOPIE_LOG(error, "This is an error message");
  std::cout << std::endl;

  //! Use your custom logger
  tacopie::active_logger = std::unique_ptr<my_logger>(new my_logger);
  //! Force logger call, just for the example (you will never have to do that by yourself)
  std::cout << "With an example of custom logger" << std::endl;
  __TACOPIE_LOG(debug, "This is a debug message");
  __TACOPIE_LOG(info, "This is an info message");
  __TACOPIE_LOG(warn, "This is a warn message");
  __TACOPIE_LOG(error, "This is an error message");
  std::cout << std::endl;

  return 0;
}
Clone this wiki locally