File indexing completed on 2024-05-12 05:26:00

0001 #pragma once
0002 
0003 #include "sink_export.h"
0004 #include <QDebug>
0005 
0006 namespace Sink {
0007 namespace Log {
0008 
0009 struct Context {
0010     Context() = default;
0011     Context(const QByteArray &n) : name(n) {}
0012     Context(const char *n) : name(n) {}
0013 
0014     QByteArray name;
0015     Context subContext(const QByteArray &sub) const {
0016         if (name.isEmpty()) {
0017             return Context{sub};
0018         }
0019         return Context{name + "." + sub};
0020     }
0021 };
0022 
0023 enum DebugLevel
0024 {
0025     Trace,
0026     Log,
0027     Warning,
0028     Error
0029 };
0030 
0031 void SINK_EXPORT setPrimaryComponent(const QString &component);
0032 QSet<QString> SINK_EXPORT debugAreas();
0033 
0034 QByteArray SINK_EXPORT debugLevelName(DebugLevel debugLevel);
0035 DebugLevel SINK_EXPORT debugLevelFromName(const QByteArray &name);
0036 
0037 /**
0038  * Sets the debug output level.
0039  *
0040  * Everything below is ignored.
0041  */
0042 void SINK_EXPORT setDebugOutputLevel(DebugLevel);
0043 DebugLevel SINK_EXPORT debugOutputLevel();
0044 
0045 enum FilterType
0046 {
0047     Area,
0048     ApplicationName
0049 };
0050 
0051 /**
0052  * Sets a debug output filter.
0053  *
0054  * Everything that is not matching the filter is ignored.
0055  * An empty filter matches everything.
0056  *
0057  * Note: In case of resources the application name is the identifier.
0058  */
0059 void SINK_EXPORT setDebugOutputFilter(FilterType, const QByteArrayList &filter);
0060 QByteArrayList SINK_EXPORT debugOutputFilter(FilterType type);
0061 
0062 /**
0063  * Set the debug output fields.
0064  *
0065  * Currently supported are:
0066  * * Name: Application name used for filter.
0067  * * Function: The function name:
0068  * * Location: The source code location.
0069  *
0070  * These are additional items to the default ones (level, area, message).
0071  */
0072 void SINK_EXPORT setDebugOutputFields(const QByteArrayList &filter);
0073 QByteArrayList SINK_EXPORT debugOutputFields();
0074 
0075 QDebug SINK_EXPORT debugStream(DebugLevel debugLevel, int line, const char *file, const char *function, const char *debugArea = nullptr, const char *debugComponent = nullptr);
0076 
0077 struct SINK_EXPORT TraceTime
0078 {
0079     TraceTime(int i) : time(i){};
0080     const int time;
0081 };
0082 
0083 SINK_EXPORT inline QDebug operator<<(QDebug d, const TraceTime &time)
0084 {
0085     d << time.time << "[ms]";
0086     return d;
0087 }
0088 
0089 SINK_EXPORT bool isFiltered(DebugLevel debugLevel, const char *debugArea, const char *debugComponent, const char *file);
0090 
0091 }
0092 }
0093 
0094 SINK_EXPORT const char *getComponentName() __attribute__ ((unused));
0095 
0096 #define SINK_DEBUG_STREAM_IMPL(LEVEL, AREA, COMPONENT) if (!Sink::Log::isFiltered(LEVEL, AREA, COMPONENT, __FILE__)) Sink::Log::debugStream(LEVEL, __LINE__, __FILE__, Q_FUNC_INFO, AREA, COMPONENT)
0097 
0098 #define Trace_area(AREA) SINK_DEBUG_STREAM_IMPL(Sink::Log::DebugLevel::Trace, AREA, nullptr)
0099 #define Log_area(AREA) SINK_DEBUG_STREAM_IMPL(Sink::Log::DebugLevel::Log, AREA, nullptr)
0100 #define Warning_area(AREA) SINK_DEBUG_STREAM_IMPL(Sink::Log::DebugLevel::Warning, AREA, nullptr)
0101 #define Error_area(AREA) SINK_DEBUG_STREAM_IMPL(Sink::Log::DebugLevel::Error, AREA, nullptr)
0102 
0103 #define SinkTrace_(COMPONENT, AREA) SINK_DEBUG_STREAM_IMPL(Sink::Log::DebugLevel::Trace, AREA, COMPONENT)
0104 #define SinkLog_(COMPONENT, AREA) SINK_DEBUG_STREAM_IMPL(Sink::Log::DebugLevel::Log, AREA, COMPONENT)
0105 #define SinkWarning_(COMPONENT, AREA) SINK_DEBUG_STREAM_IMPL(Sink::Log::DebugLevel::Warning, AREA, COMPONENT)
0106 #define SinkError_(COMPONENT, AREA) SINK_DEBUG_STREAM_IMPL(Sink::Log::DebugLevel::Error, AREA, COMPONENT)
0107 
0108 #define SinkTrace() SINK_DEBUG_STREAM_IMPL(Sink::Log::DebugLevel::Trace, nullptr, getComponentName())
0109 #define SinkLog() SINK_DEBUG_STREAM_IMPL(Sink::Log::DebugLevel::Log, nullptr, getComponentName())
0110 #define SinkWarning() SINK_DEBUG_STREAM_IMPL(Sink::Log::DebugLevel::Warning, nullptr, getComponentName())
0111 #define SinkError() SINK_DEBUG_STREAM_IMPL(Sink::Log::DebugLevel::Error, nullptr, getComponentName())
0112 
0113 #define SinkTraceCtx(CTX) SINK_DEBUG_STREAM_IMPL(Sink::Log::DebugLevel::Trace, CTX.name, nullptr)
0114 #define SinkLogCtx(CTX) SINK_DEBUG_STREAM_IMPL(Sink::Log::DebugLevel::Log, CTX.name, nullptr)
0115 #define SinkWarningCtx(CTX) SINK_DEBUG_STREAM_IMPL(Sink::Log::DebugLevel::Warning, CTX.name, nullptr)
0116 #define SinkErrorCtx(CTX) SINK_DEBUG_STREAM_IMPL(Sink::Log::DebugLevel::Error, CTX.name, nullptr)
0117 
0118 #define SINK_DEBUG_AREA(AREA) static constexpr const char* s_sinkDebugArea{AREA};
0119 #define SINK_DEBUG_COMPONENT(COMPONENT) const char* getComponentName() const { return COMPONENT; };
0120 #define SINK_DEBUG_COMPONENT_STATIC(COMPONENT) static const char* getComponentName() { return COMPONENT; };