Bitcoin Core
31.1.0
P2P Digital Currency
Toggle main menu visibility
Loading...
Searching...
No Matches
src
ipc
libmultiprocess
example
printer.cpp
Go to the documentation of this file.
1
// Copyright (c) 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 <
printer.h
>
6
7
#include <init.capnp.h>
8
#include <init.capnp.proxy.h>
// NOLINT(misc-include-cleaner) // IWYU pragma: keep
9
10
#include <charconv>
11
#include <cstring>
12
#include <fstream>
13
#include <iostream>
14
#include <kj/async.h>
15
#include <kj/common.h>
16
#include <kj/memory.h>
17
#include <memory>
18
#include <
mp/proxy-io.h
>
19
#include <stdexcept>
20
#include <string>
21
#include <system_error>
22
23
class
PrinterImpl
:
public
Printer
24
{
25
public
:
26
void
print
(
const
std::string& message)
override
{ std::cout <<
"mpprinter: "
<< message << std::endl; }
27
};
28
29
class
InitImpl
:
public
Init
30
{
31
public
:
32
std::unique_ptr<Printer>
makePrinter
()
override
{
return
std::make_unique<PrinterImpl>(); }
33
};
34
35
static
void
LogPrint
(
mp::LogMessage
log_data)
36
{
37
if
(log_data.
level
==
mp::Log::Raise
)
throw
std::runtime_error(log_data.
message
);
38
std::ofstream(
"debug.log"
, std::ios_base::app) << log_data.
message
<< std::endl;
39
}
40
41
int
main
(
int
argc,
char
** argv)
42
{
43
if
(argc != 2) {
44
std::cout <<
"Usage: mpprinter <fd>\n"
;
45
return
1;
46
}
47
int
fd;
48
if
(std::from_chars(argv[1], argv[1] + strlen(argv[1]), fd).ec != std::errc{}) {
49
std::cerr << argv[1] <<
" is not a number or is larger than an int\n"
;
50
return
1;
51
}
52
mp::EventLoop
loop(
"mpprinter"
,
LogPrint
);
53
std::unique_ptr<Init>
init
= std::make_unique<InitImpl>();
54
mp::ServeStream<InitInterface>
(loop, fd, *
init
);
55
loop.
loop
();
56
return
0;
57
}
LogPrint
static void LogPrint(bool raise, const std::string &message)
Definition
calculator.cpp:42
Init
Definition
init.h:13
InitImpl
Definition
calculator.cpp:33
InitImpl::makePrinter
std::unique_ptr< Printer > makePrinter() override
Definition
printer.cpp:32
Printer
Definition
printer.h:11
PrinterImpl
Definition
printer.cpp:24
PrinterImpl::print
void print(const std::string &message) override
Definition
printer.cpp:26
mp::EventLoop
Definition
proxy-io.h:239
mp::EventLoop::loop
void loop()
Definition
proxy.cpp:227
main
int main(void)
Definition
bench.c:156
init
Definition
bitcoin-gui.cpp:17
mp::ServeStream
void ServeStream(EventLoop &loop, int fd, InitImpl &init)
Definition
proxy-io.h:857
mp::Log::Raise
@ Raise
Definition
proxy-io.h:133
LogPrint
static void LogPrint(mp::LogMessage log_data)
Definition
printer.cpp:35
printer.h
proxy-io.h
mp::LogMessage
Definition
proxy-io.h:138
mp::LogMessage::level
Log level
The severity level of this message.
Definition
proxy-io.h:144
mp::LogMessage::message
std::string message
Message to be logged.
Definition
proxy-io.h:141
Generated on
for Bitcoin Core by
1.17.0