forked from Lasercake/Lasercake
-
Notifications
You must be signed in to change notification settings - Fork 0
/
log.cpp
101 lines (85 loc) · 2.52 KB
/
log.cpp
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/*
Copyright Eli Dupree and Isaac Dupree, 2013
This file is part of Lasercake.
Lasercake is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
Lasercake is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with Lasercake. If not, see <http://www.gnu.org/licenses/>.
*/
#include <streambuf>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#if LASERCAKE_USE_QT
#include <QtCore/QThread>
#endif
#include "config.hpp"
#if defined(BOOST_NO_EXCEPTIONS)
namespace boost {
void throw_exception(std::exception const& e) {
LOG << "Exception (uncatchable because BOOST_NO_EXCEPTIONS):\n"
<< e.what() << std::endl;
exit(1);
}
}
#endif
// Use POSIX write() rather than C stdio to make sure
// there aren't any locks/mutexes/global state hiding out anywhere
// that thread cancellation could mess up.
static const int log_output_fd = 2 /*stderr, STDERR_FILENO*/;
static void do_write(int fd, const char* ptr, size_t n) {
int patience = 10;
while(n > 0 && patience > 0) {
const ssize_t ret = write(fd, ptr, n);
if(ret == -1) {
if(!(errno == EINTR || errno == EAGAIN)) {
break; //give up
}
}
else {
ptr += ret;
n -= ret;
}
#if LASERCAKE_USE_QT
QThread::yieldCurrentThread();
#endif
--patience;
}
}
namespace logger_impl {
void log_buf::write_buf_() {
const std::streamsize bufn = pptr() - pbase();
do_write(log_output_fd, pbase(), bufn);
pbump(-bufn);
}
std::streamsize log_buf::xsputn(const char* s, std::streamsize n) {
if(pptr() + n > epptr()) {
write_buf_();
if(n >= bufsize_) {
do_write(log_output_fd, s, n);
return n;
}
}
memcpy(pptr(), s, n);
pbump(n);
return n;
}
log_buf::int_type log_buf::overflow(int_type ch) {
if(pptr() >= epptr()) {
write_buf_();
}
if(ch != traits_type::eof()) {
*pptr() = ch;
pbump(1);
}
return traits_type::not_eof(ch); // which means "success"
}
log::log() : os_(&streambuf_) { os_.imbue(std::locale::classic()); os_ << std::boolalpha; }
log::~log() {}
}