tprintf: Add C++ stream for log messages

Signed-off-by: Stefan Weil <sw@weilnetz.de>
This commit is contained in:
Stefan Weil 2024-08-25 11:36:46 +02:00
parent a63e7ec2e6
commit 33d673c46d
3 changed files with 78 additions and 4 deletions

View File

@ -0,0 +1,67 @@
// File: tesserrstream.h
// Description: C++ stream which enhances tprintf
// Author: Stefan Weil
//
// (C) Copyright 2024
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef TESSERACT_CCUTIL_TESSERRSTREAM_H
#define TESSERACT_CCUTIL_TESSERRSTREAM_H
#include "tprintf.h"
#include <ostream> // for std::ostream
namespace tesseract {
class TessStreamBuf : public std::streambuf {
public:
TessStreamBuf() = default;
protected:
virtual int_type overflow(int_type c) override {
if (c != EOF) {
if (debugfp == nullptr) {
debugfp = get_debugfp();
}
if (fputc(c, debugfp) == EOF) {
return EOF;
}
}
return c;
}
virtual std::streamsize xsputn(const char* s, std::streamsize n) override {
if (debugfp == nullptr) {
debugfp = get_debugfp();
}
return fwrite(s, 1, n, debugfp);
}
private:
FILE *debugfp = nullptr;
};
class TessErrStream : public std::ostream {
private:
TessStreamBuf buf;
public:
TessErrStream() : std::ostream(nullptr), buf() {
rdbuf(&buf);
}
};
extern TessErrStream tesserr;
} // namespace tesseract
#endif // TESSERACT_CCUTIL_TESSERRSTREAM_H

View File

@ -21,6 +21,7 @@
# include "config_auto.h"
#endif
#include "tesserrstream.h"
#include "tprintf.h"
#include "params.h"
@ -36,7 +37,7 @@ INT_VAR(log_level, INT_MAX, "Logging level");
static STRING_VAR(debug_file, "", "File to send tprintf output to");
// File for debug output.
static FILE *debugfp;
FILE *debugfp;
// Set output for log messages.
// The output is written to stderr if debug_file is empty.
@ -49,7 +50,7 @@ static FILE *debugfp;
// tprintf("write to /tmp/log\n");
// debug_file = "";
// tprintf("write to stderr\n");
static void set_debugfp() {
FILE *get_debugfp() {
if (debug_file.empty()) {
// Write to stderr.
if (debugfp != stderr && debugfp != nullptr) {
@ -66,15 +67,18 @@ static void set_debugfp() {
#endif
debugfp = fopen(debug_file.c_str(), "wb");
}
return debugfp;
}
// Trace printf.
void tprintf(const char *format, ...) {
set_debugfp();
FILE *f = get_debugfp();
va_list args; // variable args
va_start(args, format); // variable list
vfprintf(debugfp, format, args);
vfprintf(f, format, args);
va_end(args);
}
TessErrStream tesserr;
} // namespace tesseract

View File

@ -36,6 +36,9 @@ extern TESS_API void tprintf( // Trace printf
const char *format, ...) // Message
__attribute__((format(printf, 1, 2)));
// Get file for debug output.
FILE *get_debugfp();
} // namespace tesseract
#undef __attribute__