~singpolyma/biboumi

a4c845ab6c54172ea305f33734c83238c75d421a — Florent Le Coz 9 years ago 2662ed8
Use the logger everywhere
M CMakeLists.txt => CMakeLists.txt +4 -3
@@ 64,6 64,7 @@ target_link_libraries(logger config)
file(GLOB source_network
  src/network/*.[hc]pp)
add_library(network STATIC ${source_network})
target_link_libraries(network logger)

#
## irclib


@@ 71,7 72,7 @@ add_library(network STATIC ${source_network})
file(GLOB source_irc
  src/irc/*.[hc]pp)
add_library(irc STATIC ${source_irc})
target_link_libraries(irc network utils)
target_link_libraries(irc network utils logger)

#
## xmpplib


@@ 79,7 80,7 @@ target_link_libraries(irc network utils)
file(GLOB source_xmpp
  src/xmpp/*.[hc]pp)
add_library(xmpp STATIC ${source_xmpp})
target_link_libraries(xmpp bridge network utils
target_link_libraries(xmpp bridge network utils logger
  ${CRYPTO++_LIBRARIES} ${EXPAT_LIBRARIES} pthread)

#


@@ 88,7 89,7 @@ target_link_libraries(xmpp bridge network utils
file(GLOB source_bridge
  src/bridge/*.[hc]pp)
add_library(bridge STATIC ${source_bridge})
target_link_libraries(bridge xmpp irc utils)
target_link_libraries(bridge xmpp irc utils logger)

#
## Main executable

M src/bridge/bridge.cpp => src/bridge/bridge.cpp +3 -3
@@ 3,7 3,7 @@
#include <xmpp/xmpp_component.hpp>
#include <network/poller.hpp>
#include <utils/encoding.hpp>

#include <logger/logger.hpp>
#include <utils/split.hpp>
#include <iostream>



@@ 78,13 78,13 @@ void Bridge::send_channel_message(const Iid& iid, const std::string& body)
  const std::string first_line = lines[0];
  if (iid.chan.empty() || iid.server.empty())
    {
      std::cout << "Cannot send message to channel: [" << iid.chan << "] on server [" << iid.server << "]" << std::endl;
      log_warning("Cannot send message to channel: [" << iid.chan << "] on server [" << iid.server << "]");
      return;
    }
  IrcClient* irc = this->get_irc_client(iid.server);
  if (!irc)
    {
      std::cout << "Cannot send message: no client exist for server " << iid.server << std::endl;
      log_warning("Cannot send message: no client exist for server " << iid.server);
      return;
    }
  if (first_line.substr(0, 6) == "/mode ")

M src/irc/irc_client.cpp => src/irc/irc_client.cpp +7 -10
@@ 4,7 4,7 @@
#include <irc/irc_user.hpp>

#include <utils/make_unique.hpp>
// #include <logger/logger.hpp>
#include <logger/logger.hpp>
#include <utils/tolower.hpp>
#include <utils/split.hpp>



@@ 18,12 18,10 @@ IrcClient::IrcClient(const std::string& hostname, const std::string& username, B
  bridge(bridge),
  welcomed(false)
{
  std::cout << "IrcClient()" << std::endl;
}

IrcClient::~IrcClient()
{
  std::cout << "~IrcClient()" << std::endl;
}

void IrcClient::start()


@@ 39,7 37,7 @@ void IrcClient::on_connected()

void IrcClient::on_connection_close()
{
  std::cout << "Connection closed by remote server." << std::endl;
  log_warning("Connection closed by remote server.");
}

IrcChannel* IrcClient::get_channel(const std::string& name)


@@ 74,18 72,19 @@ void IrcClient::parse_in_buffer()
      if (pos == std::string::npos)
        break ;
      IrcMessage message(this->in_buf.substr(0, pos));
      log_debug("IRC RECEIVING: " << message);
      this->in_buf = this->in_buf.substr(pos + 2, std::string::npos);
      std::cout << message << std::endl;
      auto cb = irc_callbacks.find(message.command);
      if (cb != irc_callbacks.end())
        (this->*(cb->second))(message);
      else
        std::cout << "No handler for command " << message.command << std::endl;
        log_info("No handler for command " << message.command);
    }
}

void IrcClient::send_message(IrcMessage&& message)
{
  log_debug("IRC SENDING: " << message);
  std::string res;
  if (!message.prefix.empty())
    res += ":" + std::move(message.prefix) + " ";


@@ 101,8 100,6 @@ void IrcClient::send_message(IrcMessage&& message)
      res += " " + arg;
    }
  res += "\r\n";
  std::cout << "=== IRC SENDING ===" << std::endl;
  std::cout << res << std::endl;
  this->send_data(std::move(res));
}



@@ 134,7 131,7 @@ bool IrcClient::send_channel_message(const std::string& chan_name, const std::st
  IrcChannel* channel = this->get_channel(chan_name);
  if (channel->joined == false)
    {
      std::cout << "Cannot send message to channel " << chan_name << ", it is not joined" << std::endl;
      log_warning("Cannot send message to channel " << chan_name << ", it is not joined");
      return false;
    }
  this->send_message(IrcMessage("PRIVMSG", {chan_name, body}));


@@ 185,7 182,7 @@ void IrcClient::set_and_forward_user_list(const IrcMessage& message)
      IrcUser* user = channel->add_user(nick);
      if (user->nick != channel->get_self()->nick)
        {
          std::cout << "Adding user [" << nick << "] to chan " << chan_name << std::endl;
          log_debug("Adding user [" << nick << "] to chan " << chan_name);
          this->bridge->send_user_join(this->hostname, chan_name, user->nick);
        }
    }

M src/irc/irc_user.cpp => src/irc/irc_user.cpp +0 -1
@@ 24,5 24,4 @@ IrcUser::IrcUser(const std::string& name)
        this->nick = name.substr(0, sep);
      this->host = name.substr(sep+1);
    }
  std::cout << "Created user: [" << this->nick << "!" << this->host << std::endl;
}

M src/logger/logger.cpp => src/logger/logger.cpp +0 -2
@@ 6,7 6,6 @@ Logger::Logger(const int log_level):
  log_level(log_level),
  stream(std::cout.rdbuf())
{
  std::cout << "Logger(1)" << std::endl;
}

Logger::Logger(const int log_level, const std::string& log_file):


@@ 14,7 13,6 @@ Logger::Logger(const int log_level, const std::string& log_file):
  ofstream(log_file.data(), std::ios_base::app),
  stream(ofstream.rdbuf())
{
  std::cout << "Logger(" << this->log_level << ")" << std::endl;
}

std::unique_ptr<Logger>& Logger::instance()

M src/network/poller.cpp => src/network/poller.cpp +0 -2
@@ 8,7 8,6 @@

Poller::Poller()
{
  std::cout << "Poller()" << std::endl;
#if POLLER == POLL
  memset(this->fds, 0, sizeof(this->fds));
  this->nfds = 0;


@@ 24,7 23,6 @@ Poller::Poller()

Poller::~Poller()
{
  std::cout << "~Poller()" << std::endl;
}

void Poller::add_socket_handler(std::shared_ptr<SocketHandler> socket_handler)

M src/network/socket_handler.cpp => src/network/socket_handler.cpp +5 -5
@@ 3,6 3,7 @@
#include <utils/scopeguard.hpp>
#include <network/poller.hpp>

#include <logger/logger.hpp>
#include <sys/socket.h>
#include <sys/types.h>
#include <unistd.h>


@@ 21,7 22,7 @@ SocketHandler::SocketHandler():

void SocketHandler::connect(const std::string& address, const std::string& port)
{
  std::cout << "Trying to connect to " << address << ":" << port << std::endl;
  log_info("Trying to connect to " << address << ":" << port);
  struct addrinfo hints;
  memset(&hints, 0, sizeof(struct addrinfo));
  hints.ai_flags = 0;


@@ 42,17 43,16 @@ void SocketHandler::connect(const std::string& address, const std::string& port)
    }
  for (struct addrinfo* rp = addr_res; rp; rp = rp->ai_next)
    {
      std::cout << "One result" << std::endl;
      if (::connect(this->socket, rp->ai_addr, rp->ai_addrlen) == 0)
        {
          std::cout << "Connection success." << std::endl;
          log_info("Connection success.");
          this->on_connected();
          return ;
        }
      std::cout << "Connection failed:" << std::endl;
      log_info("Connection failed:");
      perror("connect");
    }
  std::cout << "All connection attempts failed." << std::endl;
  log_error("All connection attempts failed.");
  this->close();
}


M src/xmpp/xmpp_component.cpp => src/xmpp/xmpp_component.cpp +12 -15
@@ 1,4 1,5 @@
#include <utils/make_unique.hpp>
#include <logger/logger.hpp>

#include <xmpp/xmpp_component.hpp>
#include <xmpp/jid.hpp>


@@ 52,14 53,14 @@ void XmppComponent::start()

void XmppComponent::send_stanza(const Stanza& stanza)
{
  std::cout << "====== Sending ========" << std::endl;
  std::cout << stanza.to_string() << std::endl;
  this->send_data(stanza.to_string());
  std::string str = stanza.to_string();
  log_debug("XMPP SENDING: " << str);
  this->send_data(std::move(str));
}

void XmppComponent::on_connected()
{
  std::cout << "connected to XMPP server" << std::endl;
  log_info("connected to XMPP server");
  XmlNode node("stream:stream", nullptr);
  node["xmlns"] = COMPONENT_NS;
  node["xmlns:stream"] = STREAM_NS;


@@ 69,7 70,7 @@ void XmppComponent::on_connected()

void XmppComponent::on_connection_close()
{
  std::cout << "XMPP server closed connection" << std::endl;
  log_info("XMPP server closed connection");
}

void XmppComponent::parse_in_buffer()


@@ 80,15 81,14 @@ void XmppComponent::parse_in_buffer()

void XmppComponent::on_remote_stream_open(const XmlNode& node)
{
  std::cout << "====== DOCUMENT_OPEN =======" << std::endl;
  std::cout << node.to_string() << std::endl;
  log_debug("XMPP DOCUMENT OPEN: " << node.to_string());
  try
    {
      this->stream_id = node["id"];
    }
  catch (const AttributeNotFound& e)
    {
      std::cout << "Error: no attribute 'id' found" << std::endl;
      log_error("Error: no attribute 'id' found");
      this->send_stream_error("bad-format", "missing 'id' attribute");
      this->close_document();
      return ;


@@ 109,14 109,12 @@ void XmppComponent::on_remote_stream_open(const XmlNode& node)

void XmppComponent::on_remote_stream_close(const XmlNode& node)
{
  std::cout << "====== DOCUMENT_CLOSE =======" << std::endl;
  std::cout << node.to_string() << std::endl;
  log_debug("XMPP DOCUMENT CLOSE " << node.to_string());
}

void XmppComponent::on_stanza(const Stanza& stanza)
{
  std::cout << "=========== STANZA ============" << std::endl;
  std::cout << stanza.to_string() << std::endl;
  log_debug("XMPP RECEIVING: " << stanza.to_string());
  std::function<void(const Stanza&)> handler;
  try
    {


@@ 124,7 122,7 @@ void XmppComponent::on_stanza(const Stanza& stanza)
    }
  catch (const std::out_of_range& exception)
    {
      std::cout << "No handler for stanza of type " << stanza.get_name() << std::endl;
      log_warning("No handler for stanza of type " << stanza.get_name());
      return;
    }
  handler(stanza);


@@ 145,8 143,7 @@ void XmppComponent::send_stream_error(const std::string& name, const std::string

void XmppComponent::close_document()
{
  std::cout << "====== Sending ========" << std::endl;
  std::cout << "</stream:stream>" << std::endl;
  log_debug("XMPP SENDING: </stream:stream>");
  this->send_data("</stream:stream>");
}


M src/xmpp/xmpp_stanza.cpp => src/xmpp/xmpp_stanza.cpp +0 -5
@@ 209,11 209,6 @@ std::string XmlNode::to_string() const
  return res;
}

void XmlNode::display() const
{
  std::cout << this->to_string() << std::endl;
}

bool XmlNode::has_children() const
{
  return !this->children.empty();

M src/xmpp/xmpp_stanza.hpp => src/xmpp/xmpp_stanza.hpp +0 -1
@@ 97,7 97,6 @@ public:
   * Serialize the stanza into a string
   */
  std::string to_string() const;
  void display() const;
  /**
   * Whether or not this node has at least one child (if not, this is a leaf
   * node)