Guest User

Untitled

a guest
Dec 11th, 2017
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.14 KB | None | 0 0
  1. diff --git i/examples/gui/rasterwindow/main.cpp w/examples/gui/rasterwindow/main.cpp
  2. index bd5c0df1fc..bc7009e641 100644
  3. --- i/examples/gui/rasterwindow/main.cpp
  4. +++ w/examples/gui/rasterwindow/main.cpp
  5. @@ -50,6 +50,8 @@
  6.  
  7. #include "rasterwindow.h"
  8.  
  9. +//#include <Foundation/Foundation.h>
  10. +
  11. //! [1]
  12. int main(int argc, char **argv)
  13. {
  14. @@ -57,6 +59,9 @@ int main(int argc, char **argv)
  15.  
  16. RasterWindow window;
  17. window.show();
  18. +
  19. + // NSLog(@"Testing NSLOG");
  20. + printf("%s\n", qgetenv("OS_ACTIVITY_DT_MODE").constData());
  21.  
  22. return app.exec();
  23. }
  24. diff --git i/examples/gui/rasterwindow/rasterwindow.cpp w/examples/gui/rasterwindow/rasterwindow.cpp
  25. index 68d1d7f524..239a242976 100644
  26. --- i/examples/gui/rasterwindow/rasterwindow.cpp
  27. +++ w/examples/gui/rasterwindow/rasterwindow.cpp
  28. @@ -50,11 +50,13 @@
  29.  
  30. #include "rasterwindow.h"
  31.  
  32. +#include <QDebug>
  33. //! [1]
  34. RasterWindow::RasterWindow(QWindow *parent)
  35. : QWindow(parent)
  36. , m_backingStore(new QBackingStore(this))
  37. {
  38. + qDebug() << "heisan";
  39. setGeometry(100, 100, 300, 200);
  40. }
  41. //! [1]
  42. diff --git i/src/corelib/global/qlogging.cpp w/src/corelib/global/qlogging.cpp
  43. index b5ba935194..e97a89dd0f 100644
  44. --- i/src/corelib/global/qlogging.cpp
  45. +++ w/src/corelib/global/qlogging.cpp
  46. @@ -68,6 +68,17 @@
  47. #include <android/log.h>
  48. #endif
  49.  
  50. +#if defined(Q_OS_DARWIN) && \
  51. + (QT_MACOS_PLATFORM_SDK_EQUAL_OR_ABOVE(__MAC_10_12) || !defined(Q_OS_MACOS))
  52. +#define QT_USE_APPLE_UNIFIED_LOGGING
  53. +#include <CoreFoundation/CoreFoundation.h>
  54. +#include <os/availability.h>
  55. +#include <os/log.h>
  56. +#include "private/qcore_mac_p.h"
  57. +#include <sys/uio.h>
  58. +#include <QThreadStorage>
  59. +#endif
  60. +
  61. #if QT_CONFIG(journald)
  62. # define SD_JOURNAL_SUPPRESS_LOCATION
  63. # include <systemd/sd-journal.h>
  64. @@ -171,6 +182,11 @@ static bool isFatal(QtMsgType msgType)
  65. return false;
  66. }
  67.  
  68. +static bool is_default_category(const char *category)
  69. +{
  70. + return !category || strcmp(category, "default") == 0;
  71. +}
  72. +
  73. static bool willLogToConsole()
  74. {
  75. #if defined(Q_OS_WINRT)
  76. @@ -976,7 +992,6 @@ static const char emptyTokenC[] = "";
  77.  
  78. static const char defaultPattern[] = "%{if-category}%{category}: %{endif}%{message}";
  79.  
  80. -
  81. struct QMessagePattern {
  82. QMessagePattern();
  83. ~QMessagePattern();
  84. @@ -997,7 +1012,6 @@ struct QMessagePattern {
  85. };
  86. QVector<BacktraceParams> backtraceArgs; // backtrace argumens in sequence of %{backtrace
  87. #endif
  88. -
  89. bool fromEnvironment;
  90. static QBasicMutex mutex;
  91. };
  92. @@ -1339,33 +1353,10 @@ static void slog2_default_handler(QtMsgType msgType, const char *message)
  93.  
  94. Q_GLOBAL_STATIC(QMessagePattern, qMessagePattern)
  95.  
  96. -/*!
  97. - \relates <QtGlobal>
  98. - \since 5.4
  99. -
  100. - Generates a formatted string out of the \a type, \a context, \a str arguments.
  101. -
  102. - qFormatLogMessage returns a QString that is formatted according to the current message pattern.
  103. - It can be used by custom message handlers to format output similar to Qt's default message
  104. - handler.
  105. -
  106. - The function is thread-safe.
  107. -
  108. - \sa qInstallMessageHandler(), qSetMessagePattern()
  109. - */
  110. -QString qFormatLogMessage(QtMsgType type, const QMessageLogContext &context, const QString &str)
  111. +static QString qFormatLogMessage(QtMsgType type, const QMessageLogContext &context, const QString &str, const QMessagePattern &pattern)
  112. {
  113. QString message;
  114.  
  115. - QMutexLocker lock(&QMessagePattern::mutex);
  116. -
  117. - QMessagePattern *pattern = qMessagePattern();
  118. - if (!pattern) {
  119. - // after destruction of static QMessagePattern instance
  120. - message.append(str);
  121. - return message;
  122. - }
  123. -
  124. bool skip = false;
  125.  
  126. #ifndef QT_BOOTSTRAPPED
  127. @@ -1376,8 +1367,8 @@ QString qFormatLogMessage(QtMsgType type, const QMessageLogContext &context, con
  128. #endif
  129.  
  130. // we do not convert file, function, line literals to local encoding due to overhead
  131. - for (int i = 0; pattern->tokens[i] != 0; ++i) {
  132. - const char *token = pattern->tokens[i];
  133. + for (int i = 0; pattern.tokens[i] != 0; ++i) {
  134. + const char *token = pattern.tokens[i];
  135. if (token == endifTokenC) {
  136. skip = false;
  137. } else if (skip) {
  138. @@ -1428,15 +1419,15 @@ QString qFormatLogMessage(QtMsgType type, const QMessageLogContext &context, con
  139. message.append(QString::number(qlonglong(QThread::currentThread()->currentThread()), 16));
  140. #ifdef QLOGGING_HAVE_BACKTRACE
  141. } else if (token == backtraceTokenC) {
  142. - QMessagePattern::BacktraceParams backtraceParams = pattern->backtraceArgs.at(backtraceArgsIdx);
  143. + QMessagePattern::BacktraceParams backtraceParams = pattern.backtraceArgs.at(backtraceArgsIdx);
  144. backtraceArgsIdx++;
  145. message.append(formatBacktraceForLogMessage(backtraceParams, context.function));
  146. #endif
  147. } else if (token == timeTokenC) {
  148. - QString timeFormat = pattern->timeArgs.at(timeArgsIdx);
  149. + QString timeFormat = pattern.timeArgs.at(timeArgsIdx);
  150. timeArgsIdx++;
  151. if (timeFormat == QLatin1String("process")) {
  152. - quint64 ms = pattern->timer.elapsed();
  153. + quint64 ms = pattern.timer.elapsed();
  154. message.append(QString::asprintf("%6d.%03d", uint(ms / 1000), uint(ms % 1000)));
  155. } else if (timeFormat == QLatin1String("boot")) {
  156. // just print the milliseconds since the elapsed timer reference
  157. @@ -1454,7 +1445,7 @@ QString qFormatLogMessage(QtMsgType type, const QMessageLogContext &context, con
  158. }
  159. #endif // !QT_BOOTSTRAPPED
  160. } else if (token == ifCategoryTokenC) {
  161. - if (!context.category || (strcmp(context.category, "default") == 0))
  162. + if (is_default_category(context.category))
  163. skip = true;
  164. #define HANDLE_IF_TOKEN(LEVEL) \
  165. } else if (token == if##LEVEL##TokenC) { \
  166. @@ -1472,6 +1463,30 @@ QString qFormatLogMessage(QtMsgType type, const QMessageLogContext &context, con
  167. return message;
  168. }
  169.  
  170. +/*!
  171. + \relates <QtGlobal>
  172. + \since 5.4
  173. +
  174. + Generates a formatted string out of the \a type, \a context, \a str arguments.
  175. +
  176. + qFormatLogMessage returns a QString that is formatted according to the current message pattern.
  177. + It can be used by custom message handlers to format output similar to Qt's default message
  178. + handler.
  179. +
  180. + The function is thread-safe.
  181. +
  182. + \sa qInstallMessageHandler(), qSetMessagePattern()
  183. + */
  184. +QString qFormatLogMessage(QtMsgType type, const QMessageLogContext &context, const QString &str)
  185. +{
  186. + QMutexLocker lock(&QMessagePattern::mutex);
  187. + QMessagePattern *pattern = qMessagePattern();
  188. + if (!pattern)
  189. + return str; // after destruction of static QMessagePattern instance
  190. +
  191. + return qFormatLogMessage(type, context, str, *pattern);
  192. +}
  193. +
  194. #if !QT_DEPRECATED_SINCE(5, 0)
  195. // make sure they're defined to be exported
  196. typedef void (*QtMsgHandler)(QtMsgType, const char *);
  197. @@ -1566,6 +1581,93 @@ static void android_default_message_handler(QtMsgType type,
  198. }
  199. #endif //Q_OS_ANDROID
  200.  
  201. +#if defined(QT_USE_APPLE_UNIFIED_LOGGING)
  202. +
  203. +#if 0
  204. +QThreadStorage<bool> stderrMirroringDisabled;
  205. +
  206. +static ssize_t writev_without_stderr_mirroring(int fd, const struct iovec *iov, int iovcnt)
  207. +{
  208. + static bool mirroringToStderr = qEnvironmentVariableIsSet("OS_ACTIVITY_DT_MODE")
  209. + || qEnvironmentVariableIsSet("ACTIVITY_LOG_STDERR")
  210. + || qEnvironmentVariableIsSet("CFLOG_FORCE_STDERR");
  211. +
  212. + if (fd == STDERR_FILENO && Q_UNLIKELY(mirroringToStderr) && stderrMirroringDisabled.localData()) {
  213. + ssize_t len = 0;
  214. + for (int i = 0; i < iovcnt; ++i)
  215. + len += iov[i].iov_len;
  216. + return len;
  217. + }
  218. +
  219. + return writev(fd, iov, iovcnt);
  220. +}
  221. +
  222. +
  223. +#define DYLD_INTERPOSE(_replacee, _replacement) \
  224. + __attribute__((used)) static struct { const void* replacement; const void* replacee; } _interpose_##_replacee \
  225. + __attribute__ ((section ("__DATA,__interpose"))) = { (const void*)(unsigned long)&_replacement, (const void*)(unsigned long)&_replacee };
  226. +
  227. +DYLD_INTERPOSE(writev, writev_without_stderr_mirroring);
  228. +#endif
  229. +
  230. +static void apple_unified_logging_message_handler(QtMsgType type,
  231. + const QMessageLogContext &context,
  232. + const QString &message)
  233. + API_AVAILABLE(macos(10.12), ios(10.0), tvos(10.0), watchos(3.0))
  234. +{
  235. + os_log_type_t priority = OS_LOG_TYPE_DEFAULT;
  236. + switch (type) {
  237. + case QtDebugMsg: priority = OS_LOG_TYPE_DEBUG; break;
  238. + case QtInfoMsg: priority = OS_LOG_TYPE_INFO; break;
  239. + case QtWarningMsg: priority = OS_LOG_TYPE_DEFAULT; break;
  240. + case QtCriticalMsg: priority = OS_LOG_TYPE_ERROR; break;
  241. + case QtFatalMsg: priority = OS_LOG_TYPE_FAULT; break;
  242. + }
  243. +
  244. + static QHash<const char *, os_log_t> appleNativeLogContexts;
  245. + os_log_t log = is_default_category(context.category) ? OS_LOG_DEFAULT
  246. + : appleNativeLogContexts.value(context.category);
  247. +
  248. + if (!log) {
  249. + QString subsystem;
  250. + if (CFBundleRef bundle = CFBundleGetMainBundle()) {
  251. + if (CFStringRef identifier = CFBundleGetIdentifier(bundle))
  252. + subsystem = QString::fromCFString(identifier);
  253. + }
  254. + log = os_log_create(subsystem.toLocal8Bit().constData(), context.category);
  255. + appleNativeLogContexts.insert(context.category, log);
  256. +
  257. + // Technically we should release the os_log_t resource when done
  258. + // with it, but since we don't know when a category is disabled
  259. + // we keep all cached os_log_t instances until shutdown, where
  260. + // the OS will clean them up for us.
  261. + }
  262. +
  263. + // Logging best practices says we should not include symbolication
  264. + // information or source file line numbers in messages, as the system
  265. + // will automatically captures this information. In our case, what
  266. + // the system captures is the call below, which isn't really useful,
  267. + // but we still don't want to include the context's info, as that
  268. + // would clutter the logging output. Swift's _swift_os_log has the
  269. + // same issue, so when Apple fixes that hopefully they will add an
  270. + // API that lets you specify the file and line numbers.
  271. +
  272. + // Disable _os_log_impl_mirror_to_stderr's writes to stderr while
  273. + // logging out message, as we do our own stderr printing, without
  274. + // any timestamp/pid/process name formatting.
  275. + //stderrMirroringDisabled.setLocalData(true);
  276. +
  277. + // The format must be a string constant, so we can't pass on the
  278. + // message. This means we won't be able to take advantage of the
  279. + // unified logging's custom format specifiers such as %{BOOL}d.
  280. + os_log_with_type(log, priority, "%s", qPrintable(message));
  281. +
  282. + // Restore normal writev operation for this thread
  283. + //stderrMirroringDisabled.setLocalData(false);
  284. +}
  285. +
  286. +#endif // QT_USE_APPLE_UNIFIED_LOGGING
  287. +
  288. /*!
  289. \internal
  290. */
  291. @@ -1579,6 +1681,17 @@ static void qDefaultMessageHandler(QtMsgType type, const QMessageLogContext &con
  292. if (logMessage.isNull())
  293. return;
  294.  
  295. +#if defined(QT_USE_APPLE_UNIFIED_LOGGING)
  296. + if (__builtin_available(macOS 10.12, iOS 10, tvOS 10, watchOS 3, *)) {
  297. + apple_unified_logging_message_handler(type, context, logMessage);
  298. + static bool mirroringToStderr = qEnvironmentVariableIsSet("OS_ACTIVITY_DT_MODE")
  299. + || qEnvironmentVariableIsSet("ACTIVITY_LOG_STDERR")
  300. + || qEnvironmentVariableIsSet("CFLOG_FORCE_STDERR");
  301. + if (mirroringToStderr)
  302. + return; // Don't log to stderr twice
  303. + }
  304. +#endif
  305. +
  306. if (!qt_logging_to_console()) {
  307. #if defined(Q_OS_WIN)
  308. logMessage.append(QLatin1Char('\n'));
  309. @@ -1639,11 +1752,10 @@ static void qt_message_print(QtMsgType msgType, const QMessageLogContext &contex
  310. {
  311. #ifndef QT_BOOTSTRAPPED
  312. // qDebug, qWarning, ... macros do not check whether category is enabled
  313. - if (!context.category || (strcmp(context.category, "default") == 0)) {
  314. - if (QLoggingCategory *defaultCategory = QLoggingCategory::defaultCategory()) {
  315. + if (is_default_category(context.category)) {
  316. + if (QLoggingCategory *defaultCategory = QLoggingCategory::defaultCategory())
  317. if (!defaultCategory->isEnabled(msgType))
  318. return;
  319. - }
  320. }
  321. #endif
Add Comment
Please, Sign In to add comment