aboutsummaryrefslogtreecommitdiff
path: root/backend/print.cpp
blob: 1cb6a7552b2733bfec8699921fd0ac8ea55993c3 (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
47
48
49
50
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <unistd.h>

#include "print.h"
#include "util.h"

void lprtf(const char * fmt, ...) {
	va_list args;
	va_start(args, fmt);
	String formatted = String::va_fmt(args, fmt);
	va_end(args);

	fwrite(formatted.c_str(), 1, formatted.size(), stdout);
	fflush(stdout);

	SessionLog::get().append(formatted);
}

SessionLog & SessionLog::get() {
	static SessionLog instance;
	return instance;
}

SessionLog::SessionLog() {
	if (!this->enable) return;

	String filename = String::fmt("%lu.log", getpid());
	FILE * file = fopen(filename.c_str(), "w+");
}

SessionLog::~SessionLog() {
	safe_free(this->file);
}

void SessionLog::append(const String & str) const {
	this->append(str.data(), str.size());
}

void SessionLog::append(const char * str) const {
	this->append(str, strlen(str));
}

void SessionLog::append(const char * buf, size_t buf_size) const {
	if (this->file == nullptr) return;
	if (buf_size == 0) return;
	fwrite(buf, 1, buf_size, this->file);
}