File size: 10,602 Bytes
985c397
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
// SPDX-License-Identifier: LGPL-2.1-or-later

/***************************************************************************

 *   Copyright (c) 2002 Jürgen Riegel <juergen.riegel@web.de>              *

 *                                                                         *

 *   This file is part of the FreeCAD CAx development system.              *

 *                                                                         *

 *   This program is free software; you can redistribute it and/or modify  *

 *   it under the terms of the GNU Library General Public License (LGPL)   *

 *   as published by the Free Software Foundation; either version 2 of     *

 *   the License, or (at your option) any later version.                   *

 *   for detail see the LICENCE text file.                                 *

 *                                                                         *

 *   FreeCAD 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 Library General Public License for more details.                  *

 *                                                                         *

 *   You should have received a copy of the GNU Library General Public     *

 *   License along with FreeCAD; if not, write to the Free Software        *

 *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  *

 *   USA                                                                   *

 *                                                                         *

 ***************************************************************************/

#include <FCConfig.h>

#ifdef FC_OS_WIN32
# include <windows.h>
#endif
#include <cstring>
#include <Python.h>

#include <frameobject.h>

#include "ConsoleObserver.h"
#include "Interpreter.h"
#include "Tools.h"


using namespace Base;


//=========================================================================
// some special observers

ConsoleObserverFile::ConsoleObserverFile(const char* sFileName)
    : cFileStream(Base::FileInfo(sFileName))  // can be in UTF8
{
    if (!cFileStream.is_open()) {
        Console().warning("Cannot open log file '%s'.\n", sFileName);
    }
    // mark the file as a UTF-8 encoded file
    unsigned char bom[3] = {0xef, 0xbb, 0xbf};
    cFileStream.write(reinterpret_cast<const char*>(bom), 3 * sizeof(char));
}

ConsoleObserverFile::~ConsoleObserverFile()
{
    cFileStream.close();
}

void ConsoleObserverFile::sendLog(

    const std::string& notifiername,

    const std::string& msg,

    LogStyle level,

    IntendedRecipient recipient,

    ContentType content

)

{
    (void)notifiername;

    // Do not log translated messages, or messages intended only to the user to log file
    if (recipient == IntendedRecipient::User || content == ContentType::Translated) {
        return;
    }

    std::string prefix;
    switch (level) {
        case LogStyle::Warning:
            prefix = "Wrn: ";
            break;
        case LogStyle::Message:
            prefix = "Msg: ";
            break;
        case LogStyle::Error:
            prefix = "Err: ";
            break;
        case LogStyle::Log:
            prefix = "Log: ";
            break;
        case LogStyle::Critical:
            prefix = "Critical: ";
            break;
        default:
            break;
    }

    cFileStream << prefix << msg;
    cFileStream.flush();
}

ConsoleObserverStd::ConsoleObserverStd()
    :
#if defined(FC_OS_WIN32)
    useColorStderr(true)
#elif defined(FC_OS_LINUX) || defined(FC_OS_MACOSX) || defined(FC_OS_BSD)
    useColorStderr(isatty(STDERR_FILENO))
#else
    useColorStderr(false)
#endif
{
    bLog = false;
}

ConsoleObserverStd::~ConsoleObserverStd() = default;

void ConsoleObserverStd::sendLog(

    const std::string& notifiername,

    const std::string& msg,

    LogStyle level,

    IntendedRecipient recipient,

    ContentType content

)

{
    (void)notifiername;

    // Do not log translated messages, or messages intended only to the user to std log
    if (recipient == IntendedRecipient::User || content == ContentType::Translated) {
        return;
    }

    switch (level) {
        case LogStyle::Warning:
            this->Warning(msg.c_str());
            break;
        case LogStyle::Message:
            this->Message(msg.c_str());
            break;
        case LogStyle::Error:
            this->Error(msg.c_str());
            break;
        case LogStyle::Log:
            this->Log(msg.c_str());
            break;
        case LogStyle::Critical:
            this->Critical(msg.c_str());
            break;
        default:
            break;
    }
}

void ConsoleObserverStd::Message(const char* sMsg)

{
    printf("%s", sMsg);
}

void ConsoleObserverStd::Warning(const char* sWarn)

{
    if (useColorStderr) {
#if defined(FC_OS_WIN32)
        ::SetConsoleTextAttribute(::GetStdHandle(STD_ERROR_HANDLE), FOREGROUND_GREEN | FOREGROUND_BLUE);
#elif defined(FC_OS_LINUX) || defined(FC_OS_MACOSX) || defined(FC_OS_BSD)
        fprintf(stderr, "\033[1;33m");
#endif
    }

    fprintf(stderr, "%s", sWarn);

    if (useColorStderr) {
#if defined(FC_OS_WIN32)
        ::SetConsoleTextAttribute(
            ::GetStdHandle(STD_ERROR_HANDLE),
            FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE
        );
#elif defined(FC_OS_LINUX) || defined(FC_OS_MACOSX) || defined(FC_OS_BSD)
        fprintf(stderr, "\033[0m");
#endif
    }
}

void ConsoleObserverStd::Error(const char* sErr)

{
    if (useColorStderr) {
#if defined(FC_OS_WIN32)
        ::SetConsoleTextAttribute(
            ::GetStdHandle(STD_ERROR_HANDLE),
            FOREGROUND_RED | FOREGROUND_INTENSITY
        );
#elif defined(FC_OS_LINUX) || defined(FC_OS_MACOSX) || defined(FC_OS_BSD)
        fprintf(stderr, "\033[1;31m");
#endif
    }

    fprintf(stderr, "%s", sErr);

    if (useColorStderr) {
#if defined(FC_OS_WIN32)
        ::SetConsoleTextAttribute(
            ::GetStdHandle(STD_ERROR_HANDLE),
            FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE
        );
#elif defined(FC_OS_LINUX) || defined(FC_OS_MACOSX) || defined(FC_OS_BSD)
        fprintf(stderr, "\033[0m");
#endif
    }
}

void ConsoleObserverStd::Log(const char* sLog)

{
    if (useColorStderr) {
#if defined(FC_OS_WIN32)
        ::SetConsoleTextAttribute(::GetStdHandle(STD_ERROR_HANDLE), FOREGROUND_RED | FOREGROUND_GREEN);
#elif defined(FC_OS_LINUX) || defined(FC_OS_MACOSX) || defined(FC_OS_BSD)
        fprintf(stderr, "\033[1;36m");
#endif
    }

    fprintf(stderr, "%s", sLog);

    if (useColorStderr) {
#if defined(FC_OS_WIN32)
        ::SetConsoleTextAttribute(
            ::GetStdHandle(STD_ERROR_HANDLE),
            FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE
        );
#elif defined(FC_OS_LINUX) || defined(FC_OS_MACOSX) || defined(FC_OS_BSD)
        fprintf(stderr, "\033[0m");
#endif
    }
}

void ConsoleObserverStd::Critical(const char* sCritical)

{
    if (useColorStderr) {
#if defined(FC_OS_WIN32)
        ::SetConsoleTextAttribute(::GetStdHandle(STD_ERROR_HANDLE), FOREGROUND_GREEN | FOREGROUND_BLUE);
#elif defined(FC_OS_LINUX) || defined(FC_OS_MACOSX) || defined(FC_OS_BSD)
        fprintf(stderr, "\033[1;33m");
#endif
    }

    fprintf(stderr, "%s", sCritical);

    if (useColorStderr) {
#if defined(FC_OS_WIN32)
        ::SetConsoleTextAttribute(
            ::GetStdHandle(STD_ERROR_HANDLE),
            FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE
        );
#elif defined(FC_OS_LINUX) || defined(FC_OS_MACOSX) || defined(FC_OS_BSD)
        fprintf(stderr, "\033[0m");
#endif
    }
}

RedirectStdOutput::RedirectStdOutput()
{
    buffer.reserve(80);
}

int RedirectStdOutput::overflow(int ch)

{
    if (ch != EOF) {
        buffer.push_back(static_cast<char>(ch));
    }
    return ch;
}

int RedirectStdOutput::sync()

{
    // Print as log as this might be verbose
    if (!buffer.empty() && buffer.back() == '\n') {
        Base::Console().log("%s", buffer.c_str());
        buffer.clear();
    }
    return 0;
}

RedirectStdLog::RedirectStdLog()
{
    buffer.reserve(80);
}

int RedirectStdLog::overflow(int ch)

{
    if (ch != EOF) {
        buffer.push_back(static_cast<char>(ch));
    }
    return ch;
}

int RedirectStdLog::sync()

{
    // Print as log as this might be verbose
    if (!buffer.empty() && buffer.back() == '\n') {
        Base::Console().log("%s", buffer.c_str());
        buffer.clear();
    }
    return 0;
}

RedirectStdError::RedirectStdError()
{
    buffer.reserve(80);
}

int RedirectStdError::overflow(int ch)

{
    if (ch != EOF) {
        buffer.push_back(static_cast<char>(ch));
    }
    return ch;
}

int RedirectStdError::sync()

{
    if (!buffer.empty() && buffer.back() == '\n') {
        Base::Console().error("%s", buffer.c_str());
        buffer.clear();
    }
    return 0;
}

//---------------------------------------------------------

std::stringstream& LogLevel::prefix(std::stringstream& str, const char* src, int line)

{
    static FC_TIME_POINT s_tstart;
    static bool s_timing = false;
    if (print_time) {
        if (!s_timing) {
            s_timing = true;
            _FC_TIME_INIT(s_tstart);
        }
        auto tnow = std::chrono::FC_TIME_CLOCK::now();
        auto dc = std::chrono::duration_cast<FC_DURATION>(tnow - s_tstart);
        str << dc.count() << ' ';
    }
    if (print_tag) {
        str << '<' << tag << "> ";
    }
    if (print_src == 2) {
        Base::PyGILStateLocker lock;
        PyFrameObject* frame = PyEval_GetFrame();
        if (frame) {
            line = PyFrame_GetLineNumber(frame);
#if PY_VERSION_HEX < 0x030b0000
            src = PyUnicode_AsUTF8(frame->f_code->co_filename);
#else
            PyCodeObject* code = PyFrame_GetCode(frame);
            src = PyUnicode_AsUTF8(code->co_filename);
            Py_DECREF(code);
#endif
        }
    }
    if (print_src && !Base::Tools::isNullOrEmpty(src)) {
#ifdef FC_OS_WIN32
        const char* _f = std::strrchr(src, '\\');
#else
        const char* _f = std::strrchr(src, '/');
#endif
        str << (_f ? _f + 1 : src) << "(" << line << "): ";
    }
    return str;
}