/src/tesseract/src/ccutil/tesserrstream.h
Line | Count | Source |
1 | | // File: tesserrstream.h |
2 | | // Description: C++ stream which enhances tprintf |
3 | | // Author: Stefan Weil |
4 | | // |
5 | | // (C) Copyright 2024 |
6 | | // Licensed under the Apache License, Version 2.0 (the "License"); |
7 | | // you may not use this file except in compliance with the License. |
8 | | // You may obtain a copy of the License at |
9 | | // http://www.apache.org/licenses/LICENSE-2.0 |
10 | | // Unless required by applicable law or agreed to in writing, software |
11 | | // distributed under the License is distributed on an "AS IS" BASIS, |
12 | | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
13 | | // See the License for the specific language governing permissions and |
14 | | // limitations under the License. |
15 | | |
16 | | #ifndef TESSERACT_CCUTIL_TESSERRSTREAM_H |
17 | | #define TESSERACT_CCUTIL_TESSERRSTREAM_H |
18 | | |
19 | | #include "tprintf.h" |
20 | | #include <tesseract/export.h> // for TESS_API |
21 | | |
22 | | #include <ostream> // for std::ostream |
23 | | |
24 | | namespace tesseract { |
25 | | |
26 | | class TessStreamBuf : public std::streambuf { |
27 | | public: |
28 | 4 | TessStreamBuf() = default; |
29 | | |
30 | | protected: |
31 | 0 | virtual int_type overflow(int_type c) override { |
32 | 0 | if (c != EOF) { |
33 | 0 | if (debugfp == nullptr) { |
34 | 0 | debugfp = get_debugfp(); |
35 | 0 | } |
36 | 0 | if (fputc(c, debugfp) == EOF) { |
37 | 0 | return EOF; |
38 | 0 | } |
39 | 0 | } |
40 | 0 | return c; |
41 | 0 | } |
42 | | |
43 | 0 | virtual std::streamsize xsputn(const char* s, std::streamsize n) override { |
44 | 0 | if (debugfp == nullptr) { |
45 | 0 | debugfp = get_debugfp(); |
46 | 0 | } |
47 | 0 | return fwrite(s, 1, n, debugfp); |
48 | 0 | } |
49 | | |
50 | | private: |
51 | | FILE *debugfp = nullptr; |
52 | | }; |
53 | | |
54 | | class TessErrStream : public std::ostream { |
55 | | private: |
56 | | TessStreamBuf buf; |
57 | | |
58 | | public: |
59 | 4 | TessErrStream() : std::ostream(nullptr), buf() { |
60 | 4 | rdbuf(&buf); |
61 | 4 | } |
62 | | }; |
63 | | |
64 | | extern TESS_API TessErrStream tesserr; |
65 | | |
66 | | } // namespace tesseract |
67 | | |
68 | | #endif // TESSERACT_CCUTIL_TESSERRSTREAM_H |