Bitcoin Core 29.99.0
P2P Digital Currency
printer.cpp
Go to the documentation of this file.
1// Copyright (c) 2021 The Bitcoin Core developers
2// Distributed under the MIT software license, see the accompanying
3// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4
5#include <charconv>
6#include <fstream>
7#include <init.capnp.h>
8#include <init.capnp.proxy.h> // NOLINT(misc-include-cleaner)
9#include <init.h>
10#include <iostream>
11#include <memory>
12#include <mp/proxy-io.h>
13#include <printer.h>
14#include <stdexcept>
15#include <string>
16
17class PrinterImpl : public Printer
18{
19public:
20 void print(const std::string& message) override { std::cout << "mpprinter: " << message << std::endl; }
21};
22
23class InitImpl : public Init
24{
25public:
26 std::unique_ptr<Printer> makePrinter() override { return std::make_unique<PrinterImpl>(); }
27};
28
29static void LogPrint(bool raise, const std::string& message)
30{
31 if (raise) throw std::runtime_error(message);
32 std::ofstream("debug.log", std::ios_base::app) << message << std::endl;
33}
34
35int main(int argc, char** argv)
36{
37 if (argc != 2) {
38 std::cout << "Usage: mpprinter <fd>\n";
39 return 1;
40 }
41 int fd;
42 if (std::from_chars(argv[1], argv[1] + strlen(argv[1]), fd).ec != std::errc{}) {
43 std::cerr << argv[1] << " is not a number or is larger than an int\n";
44 return 1;
45 }
46 mp::EventLoop loop("mpprinter", LogPrint);
47 std::unique_ptr<Init> init = std::make_unique<InitImpl>();
48 mp::ServeStream<InitInterface>(loop, fd, *init);
49 loop.loop();
50 return 0;
51}
Definition: init.h:13
std::unique_ptr< Printer > makePrinter() override
Definition: printer.cpp:26
void print(const std::string &message) override
Definition: printer.cpp:20
Event loop implementation.
Definition: proxy-io.h:134
void loop()
Run event loop.
Definition: proxy.cpp:185
int main(int argc, char **argv)
Definition: printer.cpp:35
static void LogPrint(bool raise, const std::string &message)
Definition: printer.cpp:29