FreeCAD

Форк
0
/
Console.cpp 
836 строк · 26.6 Кб
1
/***************************************************************************
2
 *   Copyright (c) 2002 Jürgen Riegel <juergen.riegel@web.de>              *
3
 *                                                                         *
4
 *   This file is part of the FreeCAD CAx development system.              *
5
 *                                                                         *
6
 *   This program is free software; you can redistribute it and/or modify  *
7
 *   it under the terms of the GNU Library General Public License (LGPL)   *
8
 *   as published by the Free Software Foundation; either version 2 of     *
9
 *   the License, or (at your option) any later version.                   *
10
 *   for detail see the LICENCE text file.                                 *
11
 *                                                                         *
12
 *   FreeCAD is distributed in the hope that it will be useful,            *
13
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
14
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
15
 *   GNU Library General Public License for more details.                  *
16
 *                                                                         *
17
 *   You should have received a copy of the GNU Library General Public     *
18
 *   License along with FreeCAD; if not, write to the Free Software        *
19
 *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  *
20
 *   USA                                                                   *
21
 *                                                                         *
22
 ***************************************************************************/
23

24
#include "PreCompiled.h"
25

26
#ifndef _PreComp_
27
#if defined(FC_OS_WIN32)
28
#include <windows.h>
29
#elif defined(FC_OS_LINUX) || defined(FC_OS_MACOSX)
30
#include <unistd.h>
31
#endif
32
#include <cstring>
33
#include <functional>
34
#endif
35

36
#include "Console.h"
37
#include "Exception.h"
38
#include "PyObjectBase.h"
39
#include <QCoreApplication>
40

41

42
using namespace Base;
43

44

45
//=========================================================================
46

47
namespace Base
48
{
49

50
class ConsoleEvent: public QEvent
51
{
52
public:
53
    ConsoleSingleton::FreeCAD_ConsoleMsgType msgtype;
54
    IntendedRecipient recipient;
55
    ContentType content;
56
    std::string notifier;
57
    std::string msg;
58

59
    ConsoleEvent(ConsoleSingleton::FreeCAD_ConsoleMsgType type,
60
                 IntendedRecipient recipient,
61
                 ContentType content,
62
                 const std::string& notifier,
63
                 const std::string& msg)
64
        : QEvent(QEvent::User)
65
        , msgtype(type)
66
        , recipient(recipient)
67
        , content(content)
68
        , notifier(notifier)
69
        , msg(msg)
70
    {}
71
};
72

73
class ConsoleOutput: public QObject  // clazy:exclude=missing-qobject-macro
74
{
75
public:
76
    static ConsoleOutput* getInstance()
77
    {
78
        if (!instance) {
79
            instance = new ConsoleOutput;
80
        }
81
        return instance;
82
    }
83
    static void destruct()
84
    {
85
        delete instance;
86
        instance = nullptr;
87
    }
88

89
    void customEvent(QEvent* ev) override
90
    {
91
        if (ev->type() == QEvent::User) {
92
            ConsoleEvent* ce = static_cast<ConsoleEvent*>(ev);
93
            switch (ce->msgtype) {
94
                case ConsoleSingleton::MsgType_Txt:
95
                    Console().notifyPrivate(LogStyle::Message,
96
                                            ce->recipient,
97
                                            ce->content,
98
                                            ce->notifier,
99
                                            ce->msg);
100
                    break;
101
                case ConsoleSingleton::MsgType_Log:
102
                    Console().notifyPrivate(LogStyle::Log,
103
                                            ce->recipient,
104
                                            ce->content,
105
                                            ce->notifier,
106
                                            ce->msg);
107
                    break;
108
                case ConsoleSingleton::MsgType_Wrn:
109
                    Console().notifyPrivate(LogStyle::Warning,
110
                                            ce->recipient,
111
                                            ce->content,
112
                                            ce->notifier,
113
                                            ce->msg);
114
                    break;
115
                case ConsoleSingleton::MsgType_Err:
116
                    Console().notifyPrivate(LogStyle::Error,
117
                                            ce->recipient,
118
                                            ce->content,
119
                                            ce->notifier,
120
                                            ce->msg);
121
                    break;
122
                case ConsoleSingleton::MsgType_Critical:
123
                    Console().notifyPrivate(LogStyle::Critical,
124
                                            ce->recipient,
125
                                            ce->content,
126
                                            ce->notifier,
127
                                            ce->msg);
128
                    break;
129
                case ConsoleSingleton::MsgType_Notification:
130
                    Console().notifyPrivate(LogStyle::Notification,
131
                                            ce->recipient,
132
                                            ce->content,
133
                                            ce->notifier,
134
                                            ce->msg);
135
                    break;
136
            }
137
        }
138
    }
139

140
private:
141
    static ConsoleOutput* instance;  // NOLINT
142
};
143

144
ConsoleOutput* ConsoleOutput::instance = nullptr;  // NOLINT
145

146
}  // namespace Base
147

148
//**************************************************************************
149
// Construction destruction
150

151

152
ConsoleSingleton::ConsoleSingleton()
153
#ifdef FC_DEBUG
154
    : _defaultLogLevel(FC_LOGLEVEL_LOG)
155
#else
156
    : _defaultLogLevel(FC_LOGLEVEL_MSG)
157
#endif
158
{}
159

160
ConsoleSingleton::~ConsoleSingleton()
161
{
162
    ConsoleOutput::destruct();
163
    for (ILogger* Iter : _aclObservers) {
164
        delete Iter;
165
    }
166
}
167

168

169
//**************************************************************************
170
// methods
171

172
/**
173
 *  sets the console in a special mode
174
 */
175
void ConsoleSingleton::SetConsoleMode(ConsoleMode mode)
176
{
177
    if (mode & Verbose) {
178
        _bVerbose = true;
179
    }
180
}
181

182
/**
183
 *  unsets the console from a special mode
184
 */
185
void ConsoleSingleton::UnsetConsoleMode(ConsoleMode mode)
186
{
187
    if (mode & Verbose) {
188
        _bVerbose = false;
189
    }
190
}
191

192
/**
193
 * \a type can be OR'ed with any of the FreeCAD_ConsoleMsgType flags to enable -- if \a b is true --
194
 * or to disable -- if \a b is false -- a console observer with name \a sObs.
195
 * The return value is an OR'ed value of all message types that have changed their state. For
196
 * example
197
 * @code
198
 * // switch off warnings and error messages
199
 * ConsoleMsgFlags ret = Base::Console().SetEnabledMsgType("myObs",
200
 *                       Base:ConsoleSingleton::MsgType_Wrn|Base::ConsoleSingleton::MsgType_Err,
201
 * false);
202
 * // do something without notifying observer myObs
203
 * ...
204
 * // restore the former configuration again
205
 * Base::Console().SetEnabledMsgType("myObs", ret, true);
206
 * @endcode
207
 * switches off warnings and error messages and restore the state before the modification.
208
 * If the observer \a sObs doesn't exist then nothing happens.
209
 */
210
ConsoleMsgFlags ConsoleSingleton::SetEnabledMsgType(const char* sObs, ConsoleMsgFlags type, bool on)
211
{
212
    ILogger* pObs = Get(sObs);
213
    if (pObs) {
214
        ConsoleMsgFlags flags = 0;
215

216
        if (type & MsgType_Err) {
217
            if (pObs->bErr != on) {
218
                flags |= MsgType_Err;
219
            }
220
            pObs->bErr = on;
221
        }
222
        if (type & MsgType_Wrn) {
223
            if (pObs->bWrn != on) {
224
                flags |= MsgType_Wrn;
225
            }
226
            pObs->bWrn = on;
227
        }
228
        if (type & MsgType_Txt) {
229
            if (pObs->bMsg != on) {
230
                flags |= MsgType_Txt;
231
            }
232
            pObs->bMsg = on;
233
        }
234
        if (type & MsgType_Log) {
235
            if (pObs->bLog != on) {
236
                flags |= MsgType_Log;
237
            }
238
            pObs->bLog = on;
239
        }
240
        if (type & MsgType_Critical) {
241
            if (pObs->bCritical != on) {
242
                flags |= MsgType_Critical;
243
            }
244
            pObs->bCritical = on;
245
        }
246
        if (type & MsgType_Notification) {
247
            if (pObs->bNotification != on) {
248
                flags |= MsgType_Notification;
249
            }
250
            pObs->bNotification = on;
251
        }
252

253
        return flags;
254
    }
255

256
    return 0;
257
}
258

259
bool ConsoleSingleton::IsMsgTypeEnabled(const char* sObs, FreeCAD_ConsoleMsgType type) const
260
{
261
    ILogger* pObs = Get(sObs);
262
    if (pObs) {
263
        switch (type) {
264
            case MsgType_Txt:
265
                return pObs->bMsg;
266
            case MsgType_Log:
267
                return pObs->bLog;
268
            case MsgType_Wrn:
269
                return pObs->bWrn;
270
            case MsgType_Err:
271
                return pObs->bErr;
272
            case MsgType_Critical:
273
                return pObs->bCritical;
274
            case MsgType_Notification:
275
                return pObs->bNotification;
276
            default:
277
                return false;
278
        }
279
    }
280

281
    return false;
282
}
283

284
void ConsoleSingleton::SetConnectionMode(ConnectionMode mode)
285
{
286
    connectionMode = mode;
287

288
    // make sure this method gets called from the main thread
289
    if (connectionMode == Queued) {
290
        ConsoleOutput::getInstance();
291
    }
292
}
293

294
//**************************************************************************
295
// Observer stuff
296

297
/** Attaches an Observer to Console
298
 *  Use this method to attach a ILogger derived class to
299
 *  the Console. After the observer is attached all messages will also
300
 *  be forwarded to it.
301
 *  @see ILogger
302
 */
303
void ConsoleSingleton::AttachObserver(ILogger* pcObserver)
304
{
305
    // double insert !!
306
    assert(_aclObservers.find(pcObserver) == _aclObservers.end());
307

308
    _aclObservers.insert(pcObserver);
309
}
310

311
/** Detaches an Observer from Console
312
 *  Use this method to detach a ILogger derived class.
313
 *  After detaching you can destruct the Observer or reinsert it later.
314
 *  @see ILogger
315
 */
316
void ConsoleSingleton::DetachObserver(ILogger* pcObserver)
317
{
318
    _aclObservers.erase(pcObserver);
319
}
320

321
void Base::ConsoleSingleton::notifyPrivate(LogStyle category,
322
                                           IntendedRecipient recipient,
323
                                           ContentType content,
324
                                           const std::string& notifiername,
325
                                           const std::string& msg)
326
{
327
    for (ILogger* Iter : _aclObservers) {
328
        if (Iter->isActive(category)) {
329
            Iter->SendLog(notifiername,
330
                          msg,
331
                          category,
332
                          recipient,
333
                          content);  // send string to the listener
334
        }
335
    }
336
}
337

338
void ConsoleSingleton::postEvent(ConsoleSingleton::FreeCAD_ConsoleMsgType type,
339
                                 IntendedRecipient recipient,
340
                                 ContentType content,
341
                                 const std::string& notifiername,
342
                                 const std::string& msg)
343
{
344
    QCoreApplication::postEvent(ConsoleOutput::getInstance(),
345
                                new ConsoleEvent(type, recipient, content, notifiername, msg));
346
}
347

348
ILogger* ConsoleSingleton::Get(const char* Name) const
349
{
350
    const char* OName {};
351
    for (ILogger* Iter : _aclObservers) {
352
        OName = Iter->Name();  // get the name
353
        if (OName && strcmp(OName, Name) == 0) {
354
            return Iter;
355
        }
356
    }
357
    return nullptr;
358
}
359

360
int* ConsoleSingleton::GetLogLevel(const char* tag, bool create)
361
{
362
    if (!tag) {
363
        tag = "";
364
    }
365
    if (_logLevels.find(tag) != _logLevels.end()) {
366
        return &_logLevels[tag];
367
    }
368
    if (!create) {
369
        return nullptr;
370
    }
371
    int& ret = _logLevels[tag];
372
    ret = -1;
373
    return &ret;
374
}
375

376
void ConsoleSingleton::Refresh()
377
{
378
    if (_bCanRefresh) {
379
        qApp->processEvents(QEventLoop::ExcludeUserInputEvents);
380
    }
381
}
382

383
void ConsoleSingleton::EnableRefresh(bool enable)
384
{
385
    _bCanRefresh = enable;
386
}
387

388
//**************************************************************************
389
// Singleton stuff
390

391
ConsoleSingleton* ConsoleSingleton::_pcSingleton = nullptr;
392

393
void ConsoleSingleton::Destruct()
394
{
395
    // not initialized or double destructed!
396
    assert(_pcSingleton);
397
    delete _pcSingleton;
398
    _pcSingleton = nullptr;
399
}
400

401
ConsoleSingleton& ConsoleSingleton::Instance()
402
{
403
    // not initialized?
404
    if (!_pcSingleton) {
405
        _pcSingleton = new ConsoleSingleton();
406
    }
407
    return *_pcSingleton;
408
}
409

410
//**************************************************************************
411
// Python stuff
412

413
// ConsoleSingleton Methods structure
414
PyMethodDef ConsoleSingleton::Methods[] = {
415
    {"PrintMessage",
416
     ConsoleSingleton::sPyMessage,
417
     METH_VARARGS,
418
     "PrintMessage(obj) -> None\n\n"
419
     "Print a message to the output.\n\n"
420
     "obj : object\n    The string representation is printed."},
421
    {"PrintLog",
422
     ConsoleSingleton::sPyLog,
423
     METH_VARARGS,
424
     "PrintLog(obj) -> None\n\n"
425
     "Print a log message to the output.\n\n"
426
     "obj : object\n    The string representation is printed."},
427
    {"PrintError",
428
     ConsoleSingleton::sPyError,
429
     METH_VARARGS,
430
     "PrintError(obj) -> None\n\n"
431
     "Print an error message to the output.\n\n"
432
     "obj : object\n    The string representation is printed."},
433
    {"PrintDeveloperError",
434
     ConsoleSingleton::sPyDeveloperError,
435
     METH_VARARGS,
436
     "PrintDeveloperError(obj) -> None\n\n"
437
     "Print an error message intended only for Developers to the output.\n\n"
438
     "obj : object\n    The string representation is printed."},
439
    {"PrintUserError",
440
     ConsoleSingleton::sPyUserError,
441
     METH_VARARGS,
442
     "PrintUserError(obj) -> None\n\n"
443
     "Print an error message intended only for the User to the output.\n\n"
444
     "obj : object\n    The string representation is printed."},
445
    {"PrintTranslatedUserError",
446
     ConsoleSingleton::sPyTranslatedUserError,
447
     METH_VARARGS,
448
     "PrintTranslatedUserError(obj) -> None\n\n"
449
     "Print an already translated error message intended only for the User to the output.\n\n"
450
     "obj : object\n    The string representation is printed."},
451
    {"PrintWarning",
452
     ConsoleSingleton::sPyWarning,
453
     METH_VARARGS,
454
     "PrintWarning(obj) -> None\n\n"
455
     "Print a warning message to the output.\n\n"
456
     "obj : object\n    The string representation is printed."},
457
    {"PrintDeveloperWarning",
458
     ConsoleSingleton::sPyDeveloperWarning,
459
     METH_VARARGS,
460
     "PrintDeveloperWarning(obj) -> None\n\n"
461
     "Print an warning message intended only for Developers to the output.\n\n"
462
     "obj : object\n    The string representation is printed."},
463
    {"PrintUserWarning",
464
     ConsoleSingleton::sPyUserWarning,
465
     METH_VARARGS,
466
     "PrintUserWarning(obj) -> None\n\n"
467
     "Print a warning message intended only for the User to the output.\n\n"
468
     "obj : object\n    The string representation is printed."},
469
    {"PrintTranslatedUserWarning",
470
     ConsoleSingleton::sPyTranslatedUserWarning,
471
     METH_VARARGS,
472
     "PrintTranslatedUserWarning(obj) -> None\n\n"
473
     "Print an already translated warning message intended only for the User to the output.\n\n"
474
     "obj : object\n    The string representation is printed."},
475
    {"PrintCritical",
476
     ConsoleSingleton::sPyCritical,
477
     METH_VARARGS,
478
     "PrintCritical(obj) -> None\n\n"
479
     "Print a critical message to the output.\n\n"
480
     "obj : object\n    The string representation is printed."},
481
    {"PrintNotification",
482
     ConsoleSingleton::sPyNotification,
483
     METH_VARARGS,
484
     "PrintNotification(obj) -> None\n\n"
485
     "Print a user notification to the output.\n\n"
486
     "obj : object\n    The string representation is printed."},
487
    {"PrintTranslatedNotification",
488
     ConsoleSingleton::sPyTranslatedNotification,
489
     METH_VARARGS,
490
     "PrintTranslatedNotification(obj) -> None\n\n"
491
     "Print an already translated notification to the output.\n\n"
492
     "obj : object\n    The string representation is printed."},
493
    {"SetStatus",
494
     ConsoleSingleton::sPySetStatus,
495
     METH_VARARGS,
496
     "SetStatus(observer, type, status) -> None\n\n"
497
     "Set the status for either 'Log', 'Msg', 'Wrn' or 'Error' for an observer.\n\n"
498
     "observer : str\n    Logging interface name.\n"
499
     "type : str\n    Message type.\n"
500
     "status : bool"},
501
    {"GetStatus",
502
     ConsoleSingleton::sPyGetStatus,
503
     METH_VARARGS,
504
     "GetStatus(observer, type) -> bool or None\n\n"
505
     "Get the status for either 'Log', 'Msg', 'Wrn' or 'Error' for an observer.\n"
506
     "Returns None if the specified observer doesn't exist.\n\n"
507
     "observer : str\n    Logging interface name.\n"
508
     "type : str\n    Message type."},
509
    {"GetObservers",
510
     ConsoleSingleton::sPyGetObservers,
511
     METH_VARARGS,
512
     "GetObservers() -> list of str\n\n"
513
     "Get the names of the current logging interfaces."},
514
    {nullptr, nullptr, 0, nullptr} /* Sentinel */
515
};
516

517
namespace
518
{
519
PyObject* FC_PYCONSOLE_MSG(std::function<void(const char*, const char*)> func, PyObject* args)
520
{
521
    PyObject* output {};
522
    PyObject* notifier {};
523

524
    const char* notifierStr = "";
525

526
    auto retrieveString = [](PyObject* pystr) {
527
        PyObject* unicode = nullptr;
528

529
        const char* outstr = nullptr;
530

531
        if (PyUnicode_Check(pystr)) {
532
            outstr = PyUnicode_AsUTF8(pystr);
533
        }
534
        else {
535
            unicode = PyObject_Str(pystr);
536
            if (unicode) {
537
                outstr = PyUnicode_AsUTF8(unicode);
538
            }
539
        }
540

541
        Py_XDECREF(unicode);
542

543
        return outstr;
544
    };
545

546

547
    if (!PyArg_ParseTuple(args, "OO", &notifier, &output)) {
548
        PyErr_Clear();
549
        if (!PyArg_ParseTuple(args, "O", &output)) {
550
            return nullptr;
551
        }
552
    }
553
    else {  // retrieve notifier
554
        PY_TRY
555
        {
556
            notifierStr = retrieveString(notifier);
557
        }
558
        PY_CATCH
559
    }
560

561
    PY_TRY
562
    {
563
        const char* string = retrieveString(output);
564

565
        if (string) {
566
            func(notifierStr, string); /*process message*/
567
        }
568
    }
569
    PY_CATCH
570
    Py_Return;
571
}
572
}  // namespace
573

574
PyObject* ConsoleSingleton::sPyMessage(PyObject* /*self*/, PyObject* args)
575
{
576
    return FC_PYCONSOLE_MSG(
577
        [](const std::string& notifier, const char* msg) {
578
            Instance()
579
                .Send<Base::LogStyle::Message,
580
                      Base::IntendedRecipient::Developer,
581
                      Base::ContentType::Untranslatable>(notifier, "%s", msg);
582
        },
583
        args);
584
}
585

586
PyObject* ConsoleSingleton::sPyWarning(PyObject* /*self*/, PyObject* args)
587
{
588
    return FC_PYCONSOLE_MSG(
589
        [](const std::string& notifier, const char* msg) {
590
            Instance().Warning(notifier, "%s", msg);
591
        },
592
        args);
593
}
594

595
PyObject* ConsoleSingleton::sPyDeveloperWarning(PyObject* /*self*/, PyObject* args)
596
{
597
    return FC_PYCONSOLE_MSG(
598
        [](const std::string& notifier, const char* msg) {
599
            Instance()
600
                .Send<Base::LogStyle::Warning,
601
                      Base::IntendedRecipient::Developer,
602
                      Base::ContentType::Untranslatable>(notifier, "%s", msg);
603
        },
604
        args);
605
}
606

607
PyObject* ConsoleSingleton::sPyUserWarning(PyObject* /*self*/, PyObject* args)
608
{
609
    return FC_PYCONSOLE_MSG(
610
        [](const std::string& notifier, const char* msg) {
611
            Instance()
612
                .Send<Base::LogStyle::Warning,
613
                      Base::IntendedRecipient::User,
614
                      Base::ContentType::Untranslated>(notifier, "%s", msg);
615
        },
616
        args);
617
}
618

619
PyObject* ConsoleSingleton::sPyTranslatedUserWarning(PyObject* /*self*/, PyObject* args)
620
{
621
    return FC_PYCONSOLE_MSG(
622
        [](const std::string& notifier, const char* msg) {
623
            Instance()
624
                .Send<Base::LogStyle::Warning,
625
                      Base::IntendedRecipient::User,
626
                      Base::ContentType::Translated>(notifier, "%s", msg);
627
        },
628
        args);
629
}
630

631
PyObject* ConsoleSingleton::sPyError(PyObject* /*self*/, PyObject* args)
632
{
633
    return FC_PYCONSOLE_MSG(
634
        [](const std::string& notifier, const char* msg) {
635
            Instance()
636
                .Send<Base::LogStyle::Error,
637
                      Base::IntendedRecipient::All,
638
                      Base::ContentType::Untranslated>(notifier, "%s", msg);
639
        },
640
        args);
641
}
642

643
PyObject* ConsoleSingleton::sPyDeveloperError(PyObject* /*self*/, PyObject* args)
644
{
645
    return FC_PYCONSOLE_MSG(
646
        [](const std::string& notifier, const char* msg) {
647
            Instance()
648
                .Send<Base::LogStyle::Error,
649
                      Base::IntendedRecipient::Developer,
650
                      Base::ContentType::Untranslatable>(notifier, "%s", msg);
651
        },
652
        args);
653
}
654

655
PyObject* ConsoleSingleton::sPyUserError(PyObject* /*self*/, PyObject* args)
656
{
657
    return FC_PYCONSOLE_MSG(
658
        [](const std::string& notifier, const char* msg) {
659
            Instance()
660
                .Send<Base::LogStyle::Error,
661
                      Base::IntendedRecipient::User,
662
                      Base::ContentType::Untranslated>(notifier, "%s", msg);
663
        },
664
        args);
665
}
666

667
PyObject* ConsoleSingleton::sPyTranslatedUserError(PyObject* /*self*/, PyObject* args)
668
{
669
    return FC_PYCONSOLE_MSG(
670
        [](const std::string& notifier, const char* msg) {
671
            Instance()
672
                .Send<Base::LogStyle::Error,
673
                      Base::IntendedRecipient::User,
674
                      Base::ContentType::Translated>(notifier, "%s", msg);
675
        },
676
        args);
677
}
678

679
PyObject* ConsoleSingleton::sPyLog(PyObject* /*self*/, PyObject* args)
680
{
681
    return FC_PYCONSOLE_MSG(
682
        [](const std::string& notifier, const char* msg) {
683
            Instance()
684
                .Send<Base::LogStyle::Log,
685
                      Base::IntendedRecipient::Developer,
686
                      Base::ContentType::Untranslatable>(notifier, "%s", msg);
687
        },
688
        args);
689
}
690

691
PyObject* ConsoleSingleton::sPyCritical(PyObject* /*self*/, PyObject* args)
692
{
693
    return FC_PYCONSOLE_MSG(
694
        [](const std::string& notifier, const char* msg) {
695
            Instance()
696
                .Send<Base::LogStyle::Critical,
697
                      Base::IntendedRecipient::All,
698
                      Base::ContentType::Untranslated>(notifier, "%s", msg);
699
        },
700
        args);
701
}
702

703
PyObject* ConsoleSingleton::sPyNotification(PyObject* /*self*/, PyObject* args)
704
{
705
    return FC_PYCONSOLE_MSG(
706
        [](const std::string& notifier, const char* msg) {
707
            Instance()
708
                .Send<Base::LogStyle::Notification,
709
                      Base::IntendedRecipient::User,
710
                      Base::ContentType::Untranslated>(notifier, "%s", msg);
711
        },
712
        args);
713
}
714

715
PyObject* ConsoleSingleton::sPyTranslatedNotification(PyObject* /*self*/, PyObject* args)
716
{
717
    return FC_PYCONSOLE_MSG(
718
        [](const std::string& notifier, const char* msg) {
719
            Instance()
720
                .Send<Base::LogStyle::Notification,
721
                      Base::IntendedRecipient::User,
722
                      Base::ContentType::Translated>(notifier, "%s", msg);
723
        },
724
        args);
725
}
726

727
PyObject* ConsoleSingleton::sPyGetStatus(PyObject* /*self*/, PyObject* args)
728
{
729
    char* pstr1 {};
730
    char* pstr2 {};
731
    if (!PyArg_ParseTuple(args, "ss", &pstr1, &pstr2)) {
732
        return nullptr;
733
    }
734

735
    PY_TRY
736
    {
737
        bool b = false;
738
        ILogger* pObs = Instance().Get(pstr1);
739
        if (!pObs) {
740
            Py_Return;
741
        }
742

743
        if (strcmp(pstr2, "Log") == 0) {
744
            b = pObs->bLog;
745
        }
746
        else if (strcmp(pstr2, "Wrn") == 0) {
747
            b = pObs->bWrn;
748
        }
749
        else if (strcmp(pstr2, "Msg") == 0) {
750
            b = pObs->bMsg;
751
        }
752
        else if (strcmp(pstr2, "Err") == 0) {
753
            b = pObs->bErr;
754
        }
755
        else if (strcmp(pstr2, "Critical") == 0) {
756
            b = pObs->bCritical;
757
        }
758
        else if (strcmp(pstr2, "Notification") == 0) {
759
            b = pObs->bNotification;
760
        }
761
        else {
762
            Py_Error(Base::PyExc_FC_GeneralError,
763
                     "Unknown message type (use 'Log', 'Err', 'Wrn', 'Msg', 'Critical' or "
764
                     "'Notification')");
765
        }
766

767
        return PyBool_FromLong(b ? 1 : 0);
768
    }
769
    PY_CATCH;
770
}
771

772
PyObject* ConsoleSingleton::sPySetStatus(PyObject* /*self*/, PyObject* args)
773
{
774
    char* pstr1 {};
775
    char* pstr2 {};
776
    PyObject* pyStatus {};
777
    if (!PyArg_ParseTuple(args, "ssO!", &pstr1, &pstr2, &PyBool_Type, &pyStatus)) {
778
        return nullptr;
779
    }
780

781
    PY_TRY
782
    {
783
        bool status = asBoolean(pyStatus);
784
        ILogger* pObs = Instance().Get(pstr1);
785
        if (pObs) {
786
            if (strcmp(pstr2, "Log") == 0) {
787
                pObs->bLog = status;
788
            }
789
            else if (strcmp(pstr2, "Wrn") == 0) {
790
                pObs->bWrn = status;
791
            }
792
            else if (strcmp(pstr2, "Msg") == 0) {
793
                pObs->bMsg = status;
794
            }
795
            else if (strcmp(pstr2, "Err") == 0) {
796
                pObs->bErr = status;
797
            }
798
            else if (strcmp(pstr2, "Critical") == 0) {
799
                pObs->bCritical = status;
800
            }
801
            else if (strcmp(pstr2, "Notification") == 0) {
802
                pObs->bNotification = status;
803
            }
804
            else {
805
                Py_Error(Base::PyExc_FC_GeneralError,
806
                         "Unknown message type (use 'Log', 'Err', 'Wrn', 'Msg', 'Critical' or "
807
                         "'Notification')");
808
            }
809

810
            Py_Return;
811
        }
812

813
        Py_Error(Base::PyExc_FC_GeneralError, "Unknown logger type");
814
    }
815
    PY_CATCH;
816
}
817

818
PyObject* ConsoleSingleton::sPyGetObservers(PyObject* /*self*/, PyObject* args)
819
{
820
    if (!PyArg_ParseTuple(args, "")) {
821
        return nullptr;
822
    }
823

824
    PY_TRY
825
    {
826
        Py::List list;
827
        for (auto i : Instance()._aclObservers) {
828
            list.append(Py::String(i->Name() ? i->Name() : ""));
829
        }
830

831
        return Py::new_reference_to(list);
832
    }
833
    PY_CATCH
834
}
835

836
Base::ILogger::~ILogger() = default;
837

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.