diff options
Diffstat (limited to 'works')
| -rw-r--r-- | works/life/computer-network-experiment/Output.cpp | 27 | ||||
| -rw-r--r-- | works/life/computer-network-experiment/Output.h | 22 | ||||
| -rw-r--r-- | works/life/computer-network-experiment/client.cpp | 3 | ||||
| -rw-r--r-- | works/life/computer-network-experiment/server.cpp | 7 | 
4 files changed, 49 insertions, 10 deletions
diff --git a/works/life/computer-network-experiment/Output.cpp b/works/life/computer-network-experiment/Output.cpp index 8efb525..e3dc69e 100644 --- a/works/life/computer-network-experiment/Output.cpp +++ b/works/life/computer-network-experiment/Output.cpp @@ -1,5 +1,9 @@  #include "Output.h"
 -#include "folly/CancellationToken.h"
 +
 +#include <folly/CancellationToken.h>
 +
 +#include <ostream>
 +#include <type_traits>
  folly::MPMCQueue<Output> output_queue(100);
 @@ -8,12 +12,29 @@ folly::CancellationSource cancellation_source;  std::thread output_thread(OutputThread);
  void PrintOutput(const Output &output) {
 +  std::basic_ostream<Char> *stream;
 +
    switch (output.type) {
    case OutputType::Error:
 -    error_stream << output.message;
 +    stream = &error_stream;
      break;
    default:
 -    output_stream << output.message;
 +    stream = &output_stream;
 +    break;
 +  }
 +
 +  switch (output.color) {
 +  case OutputColor::Normal:
 +    (*stream) << output.message;
 +    break;
 +  case OutputColor::Green:
 +    (*stream) << CRUT("\x1b[32m") << output.message << CRUT("\x1b[0m");
 +    break;
 +  case OutputColor::Red:
 +    (*stream) << CRUT("\x1b[31m") << output.message << CRUT("\x1b[0m");
 +    break;
 +  case OutputColor::Yellow:
 +    (*stream) << CRUT("\x1b[33m") << output.message << CRUT("\x1b[0m");
      break;
    }
  }
 diff --git a/works/life/computer-network-experiment/Output.h b/works/life/computer-network-experiment/Output.h index 22b913a..2d16eb0 100644 --- a/works/life/computer-network-experiment/Output.h +++ b/works/life/computer-network-experiment/Output.h @@ -3,19 +3,28 @@  #include "StringUtil.hpp"
  #include <fmt/format.h>
 +#include <folly/CancellationToken.h>
  #include <folly/MPMCPipeline.h>
  #include <folly/MPMCQueue.h>
 -#include <folly/CancellationToken.h>
 -#include <thread>
  #include <iostream>
 +#include <thread>
  enum class OutputType { Normal, Error };
 +enum class OutputColor { Normal, Green, Red, Yellow };
  struct Output {
    Output() = default;
    Output(String message, OutputType type = OutputType::Normal)
 -      : message(std::move(message)), type(type) {}
 +      : message(std::move(message)), type(type),
 +        color(type == OutputType::Error ? OutputColor::Red
 +                                        : OutputColor::Normal) {}
 +
 +  Output(String message, OutputColor color)
 +      : message(std::move(message)), type(OutputType::Normal), color(color) {}
 +
 +  Output(String message, OutputType type, OutputColor color)
 +      : message(std::move(message)), type(type), color(color) {}
    CRU_DEFAULT_COPY(Output)
    CRU_DEFAULT_MOVE(Output)
 @@ -23,6 +32,7 @@ struct Output {    String message;
    OutputType type;
 +  OutputColor color;
  };
  extern folly::MPMCQueue<Output> output_queue;
 @@ -43,6 +53,12 @@ void SendOutput(OutputType type, StringView format, Args &&...args) {        Output{fmt::format(format, std::forward<Args>(args)...), type});
  }
 +template <typename... Args>
 +void SendOutput(OutputColor color, StringView format, Args &&...args) {
 +  output_queue.blockingWrite(
 +      Output{fmt::format(format, std::forward<Args>(args)...), color});
 +}
 +
  void OutputThread();
  void SignalAndWaitForOutputThreadStop();
 diff --git a/works/life/computer-network-experiment/client.cpp b/works/life/computer-network-experiment/client.cpp index 5d5075e..922ecdc 100644 --- a/works/life/computer-network-experiment/client.cpp +++ b/works/life/computer-network-experiment/client.cpp @@ -48,7 +48,8 @@ int Main() {    std::string s(buffer, received_number);
 -  SendOutput(CRUT("Received message:\n{}\n"), ConvertCharString(s));
 +  SendOutput(OutputColor::Green, CRUT("Received message:\n"));
 +  SendOutput(OutputColor::Normal, CRUT("{}\n"), ConvertCharString(s));
    CloseSocket(client_socket);
    return 0;
 diff --git a/works/life/computer-network-experiment/server.cpp b/works/life/computer-network-experiment/server.cpp index 14987f3..f12630a 100644 --- a/works/life/computer-network-experiment/server.cpp +++ b/works/life/computer-network-experiment/server.cpp @@ -49,7 +49,7 @@ void ResponseThreadProc(int socket, sockaddr_in address) {      byte_count_sent += byte_actually_sent;
    }
 -  SendOutput(CRUT("Succeeded to send message to {}!\n"),
 +  SendOutput(OutputColor::Green, CRUT("Succeeded to send message to {}!\n"),
               ConvertCharString(address_string));
    CloseSocket(socket);
 @@ -78,9 +78,10 @@ int Main() {      PrintErrorMessageAndExit(CRUT("Failed to listen."));
    }
 -  while (true) {
 -    SendOutput(CRUT("Now start to accept incoming connection.\n"));
 +  SendOutput(OutputColor::Green,
 +             CRUT("Now start to accept incoming connection.\n"));
 +  while (true) {
      sockaddr_in client_address;
      int client_socket;
      unsigned sin_size = sizeof(sockaddr_in);
  | 
