-
-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathPrinting.cpp
More file actions
61 lines (53 loc) · 1.99 KB
/
Printing.cpp
File metadata and controls
61 lines (53 loc) · 1.99 KB
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include <mutex>
#include "Constants.h"
#include "Printing.h"
#include "SocketHelper.h"
#include "json.hpp"
std::mutex print_mutex;
void print_core(const char *str, FILE *destinaton) {
// We need to lock the print method because in some cases we print different
// parts of messages from different threads.
print_mutex.lock();
LengthEncodedMessage length_encoded_message =
get_message_with_encoded_length(str);
char *buff = new char[length_encoded_message.length];
std::setvbuf(destinaton, buff, _IOFBF, length_encoded_message.length);
fwrite(length_encoded_message.message, length_encoded_message.length, 1,
destinaton);
fflush(destinaton);
delete[] buff;
print_mutex.unlock();
}
void print(const char *str) { print_core(str, stdout); }
void trace(const char *str) { print_core(str, stderr); }
void trace(int num) {
std::stringstream str;
str << num;
trace(str.str().c_str());
}
void print(const nlohmann::json &message) {
std::string str = message.dump();
print(str.c_str());
}
void print_error(const char *message, std::string device_identifier,
std::string method_id, int code) {
nlohmann::json exception;
exception[kError][kMessage] = message;
exception[kError][kCode] = code;
exception[kError][kDeviceId] = device_identifier;
exception[kDeviceId] = device_identifier;
exception[kId] = method_id;
// Decided it's a better idea to print everything to stdout
// Not a good practice, but the client process wouldn't have to monitor both
// streams fprintf(stderr, "%s", exception.dump().c_str());
print(exception);
}
void print_errors(std::vector<std::string> &messages,
std::string device_identifier, std::string method_id,
int code) {
std::ostringstream joined_error_message;
std::copy(messages.begin(), messages.end(),
std::ostream_iterator<std::string>(joined_error_message, "\n"));
print_error(joined_error_message.str().c_str(), device_identifier, method_id,
code);
}