File: log.h

package info (click to toggle)
wxpython4.0 4.2.0%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 232,540 kB
  • sloc: cpp: 958,937; python: 233,059; ansic: 150,441; makefile: 51,662; sh: 8,687; perl: 1,563; javascript: 584; php: 326; xml: 200
file content (145 lines) | stat: -rw-r--r-- 4,215 bytes parent folder | download
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
///////////////////////////////////////////////////////////////////////////////
// Name:        wx/gtk/private/log.h
// Purpose:     Support for filtering GTK log messages.
// Author:      Vadim Zeitlin
// Created:     2022-05-11
// Copyright:   (c) 2022 Vadim Zeitlin <vadim@wxwidgets.org>
// Licence:     wxWindows licence
///////////////////////////////////////////////////////////////////////////////

#ifndef _WX_GTK_PRIVATE_LOG_H_
#define _WX_GTK_PRIVATE_LOG_H_

#include <glib.h>

// Support for custom log writers is only available in glib 2.50 or later.
#if GLIB_CHECK_VERSION(2, 50, 0)
    #define wxHAS_GLIB_LOG_WRITER
#endif

namespace wxGTKImpl
{

#ifdef wxHAS_GLIB_LOG_WRITER

// LogFilter is the base class for filtering GTK log messages
//
// Note that all members of this class are defined in src/gtk/app.cpp.
class LogFilter
{
public:
    LogFilter()
    {
        m_next = NULL;
    }

    // Function to call to install this filter as the active one.
    // Does nothing and just returns false if run-time glib version is too old.
    bool Install();

protected:
    // Function to override in the derived class to actually filter: return
    // true if the message should be suppressed or false if it should be passed
    // through to the default writer (which may, or not, show it).
    virtual bool Filter(GLogLevelFlags log_level,
                        const GLogField* fields,
                        gsize n_fields) const = 0;

    // Typically called from the derived class dtor to stop using this filter.
    void Uninstall();

private:
    // The function used as glib log writer.
    static GLogWriterOutput
    wx_log_writer(GLogLevelFlags   log_level,
                  const GLogField *fields,
                  gsize            n_fields,
                  gpointer         user_data);

    // False initially, set to true when we install wx_log_writer() as the log
    // writer. Once we do it, we never change it any more.
    static bool ms_installed;

    // We maintain a simple linked list of log filters and this is the head of
    // this list.
    static LogFilter* ms_first;

    // Next entry in the linked list, may be null.
    LogFilter* m_next;

    wxDECLARE_NO_COPY_CLASS(LogFilter);
};

// LogFilterByLevel filters out all the messages at the specified level.
class LogFilterByLevel : public LogFilter
{
public:
    LogFilterByLevel() { }

    void SetLevelToIgnore(int flags)
    {
        m_logLevelToIgnore = flags;
    }

protected:
    bool Filter(GLogLevelFlags log_level,
                const GLogField* WXUNUSED(fields),
                gsize WXUNUSED(n_fields)) const wxOVERRIDE
    {
        return log_level & m_logLevelToIgnore;
    }

private:
    int m_logLevelToIgnore;

    wxDECLARE_NO_COPY_CLASS(LogFilterByLevel);
};

// LogFilterByMessage filters out all the messages with the specified content.
class LogFilterByMessage : public LogFilter
{
public:
    // Objects of this class are supposed to be created with literal strings as
    // argument, so don't bother copying the string but just use the pointer.
    explicit LogFilterByMessage(const char* message)
        : m_message(message)
    {
        // We shouldn't warn about anything if Install() failed.
        m_warnNotFiltered = Install();
    }

    // Remove this filter when the object goes out of scope.
    //
    // The dtor also checks if we actually filtered the message and logs a
    // trace message with the "gtklog" mask if we didn't: this allows checking
    // if the filter is actually being used.
    ~LogFilterByMessage();

protected:
    bool Filter(GLogLevelFlags WXUNUSED(log_level),
                const GLogField* fields,
                gsize n_fields) const wxOVERRIDE;

private:
    const char* const m_message;

    mutable bool m_warnNotFiltered;

    wxDECLARE_NO_COPY_CLASS(LogFilterByMessage);
};

#else // !wxHAS_GLIB_LOG_WRITER

// Provide stubs to avoid having to use preprocessor checks in the code using
// these classes.
class LogFilterByMessage
{
public:
    explicit LogFilterByMessage(const char* WXUNUSED(message)) { }
};

#endif // wxHAS_GLIB_LOG_WRITER/!wxHAS_GLIB_LOG_WRITER

} // namespace wxGTKImpl

#endif // _WX_GTK_PRIVATE_LOG_H_