Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qohoslogger.cpp
Go to the documentation of this file.
1// Copyright (C) 2025 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#include <QtCore/private/qohoslogger_p.h>
5#include <array>
6#include <cstdio>
7#include <cstring>
8
9QT_BEGIN_NAMESPACE
10
11const QLoggingCategory &QtForOhos()
12{
13 static const QLoggingCategory category("[QtForOhos]");
14 return category;
15}
16
17void qOhosLogMessage(LogLevel logLevel, const char *tag, const char *message)
18{
19 OH_LOG_Print(LOG_APP, logLevel, LOG_DOMAIN, tag, "%{public}s", message);
20}
21
22void qOhosVPrintf(LogLevel logLevel, const char *format, std::va_list ap)
23{
24 std::array<char, 4096> entryBuffer;
25
26 int prefixVsnprintfRes = std::snprintf(
27 entryBuffer.data(), entryBuffer.size(),
28 "[QtForOhos]: T: 0x%llx, M: ",
29 reinterpret_cast<unsigned long long>(QThread::currentThreadId()));
30
31 Q_ASSERT(prefixVsnprintfRes >= 0);
32
33 auto prefixSize = std::min(static_cast<size_t>(prefixVsnprintfRes), entryBuffer.size() - 1);
34
35 auto *msgBuffer = entryBuffer.data() + prefixSize;
36 auto msgBufferSize = entryBuffer.size() - prefixSize;
37
38 int vsnprintfRes = std::vsnprintf(msgBuffer, msgBufferSize, format, ap);
39 if (vsnprintfRes < 0) {
40 std::snprintf(msgBuffer, msgBufferSize, "[error formatting log msg: %s]", format);
41 } else if (static_cast<unsigned>(vsnprintfRes) >= msgBufferSize) {
42 constexpr auto *ellipsis = "...";
43 std::strcpy(msgBuffer + msgBufferSize - (std::strlen(ellipsis) + 1), ellipsis);
44 }
45
46 qOhosLogMessage(logLevel, "OHOS plugin", entryBuffer.data());
47}
48
49QT_END_NAMESPACE
void qOhosLogMessage(LogLevel logLevel, const char *tag, const char *message)
void qOhosVPrintf(LogLevel logLevel, const char *format, std::va_list ap)