aboutsummaryrefslogtreecommitdiff
path: root/works/life/computer-network-experiment/Output.h
blob: 0e533639f5148a1b34c48b0a5bf30980caa0bb35 (plain)
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
#pragma once
#include "StringUtil.hpp"

#include <iostream>

#include <fmt/format.h>
#include <folly/MPMCPipeline.h>
#include <folly/MPMCQueue.h>

enum class OutputType { Normal, Error };

struct Output {
  Output() = default;
  Output(std::wstring message, OutputType type = OutputType::Normal)
      : message(std::move(message)), type(type) {}

  CRU_DEFAULT_COPY(Output)
  CRU_DEFAULT_MOVE(Output)
  ~Output() = default;

  std::wstring message;
  OutputType type;
};

extern folly::MPMCQueue<Output> output_queue;

inline void SendOutput(Output output) {
  output_queue.blockingWrite(std::move(output));
}

inline void SendOutput(std::wstring output) {
  SendOutput(std::move(output));
}

template <typename... Args>
void SendOutput(std::wstring_view format, Args &&...args) {
  output_queue.blockingWrite(fmt::format(format, std::forward<Args>(args)...));
}

template <typename... Args>
void SendOutput(OutputType type, std::wstring_view format, Args &&...args) {
  output_queue.blockingWrite(
      Output{fmt::format(format, std::forward<Args>(args)...), type});
}

void OutputThread();