Skip to content

Commit 1463358

Browse files
sy-cBarthelemy
authored andcommitted
simpleLog add redirection of stdout/stderr to custom FDs (#32)
1 parent 6fd47a6 commit 1463358

2 files changed

Lines changed: 28 additions & 11 deletions

File tree

include/Common/SimpleLog.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ class SimpleLog {
2626
// \param logFilePath Path to log file. If NULL, using stdout/stderr.
2727
int setLogFile(const char* logFilePath=NULL);
2828

29+
// Change file descriptors used for stdout/stderr with provided ones
30+
// They must be valid for the lifetime of this object (or until overwritten), and are not closed.
31+
void setFileDescriptors(int fdStdout, int fdStderr);
2932

3033
enum FormatOption : int {
3134
ShowTimeStamp = 0x1,

src/SimpleLog.cxx

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
#include <time.h>
44
#include <sys/time.h>
5+
#include <unistd.h>
56

67
class SimpleLog::Impl {
78
public:
@@ -24,7 +25,9 @@ class SimpleLog::Impl {
2425
protected:
2526
FILE *fp; // descriptor to be used. If NULL, using stdout/stderr.
2627
int formatOptions;
27-
28+
int fdStdout;
29+
int fdStderr;
30+
2831
friend class SimpleLog;
2932
};
3033

@@ -33,6 +36,8 @@ SimpleLog::Impl::Impl() {
3336
formatOptions = SimpleLog::FormatOption::ShowTimeStamp
3437
| SimpleLog::FormatOption::ShowSeveritySymbol
3538
| SimpleLog::FormatOption::ShowMessage;
39+
fdStdout = fileno(stdout);
40+
fdStderr = fileno(stderr);
3641
}
3742

3843
SimpleLog::Impl::~Impl() {
@@ -46,7 +51,7 @@ SimpleLog::Impl::~Impl() {
4651
int SimpleLog::Impl::logV(SimpleLog::Impl::Severity s, const char *message, va_list ap)
4752
{
4853
char buffer[1024] = "";
49-
size_t len = sizeof(buffer);
54+
size_t len = sizeof(buffer) - 2;
5055
size_t ix = 0;
5156

5257
if (formatOptions & SimpleLog::FormatOption::ShowTimeStamp) {
@@ -95,20 +100,23 @@ int SimpleLog::Impl::logV(SimpleLog::Impl::Severity s, const char *message, va_l
95100
ix+=vsnprintf(&buffer[ix], len-ix, message, ap);
96101
if (ix>len) { ix=len; }
97102
}
98-
103+
104+
buffer[ix] = '\n';
105+
ix++;
99106
buffer[ix]=0;
100107

101-
FILE *fpOut=stdout;
102-
103-
if (fp==NULL) {
108+
int fd;
109+
if (fp != NULL) {
110+
fd = fileno(fp);
111+
} else {
104112
if (s==Severity::Error) {
105-
fpOut=stderr;
113+
fd = fdStderr;
114+
} else {
115+
fd = fdStdout;
106116
}
107-
} else {
108-
fpOut=fp;
109117
}
110-
fprintf(fpOut,"%s\n", buffer);
111-
fflush(fpOut);
118+
write(fd, buffer, ix);
119+
112120
return 0;
113121
}
114122

@@ -182,4 +190,10 @@ void SimpleLog::setOutputFormat(int opts) {
182190
pImpl->formatOptions=opts;
183191
}
184192

193+
void SimpleLog::setFileDescriptors(int fdStdout, int fdStderr)
194+
{
195+
pImpl->fdStdout = fdStdout;
196+
pImpl->fdStderr = fdStderr;
197+
}
198+
185199
/// \todo: thread to flush output every 1 second

0 commit comments

Comments
 (0)