• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===-- OsLogger.cpp --------------------------------------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "OsLogger.h"
10 #include <Availability.h>
11 
12 #if (LLDB_USE_OS_LOG) && (__MAC_OS_X_VERSION_MAX_ALLOWED >= 101200)
13 
14 #include <os/log.h>
15 
16 #include "DNBDefs.h"
17 #include "DNBLog.h"
18 
19 #define LLDB_OS_LOG_MAX_BUFFER_LENGTH 256
20 
21 namespace {
22 // Darwin os_log logging callback that can be registered with
23 // DNBLogSetLogCallback
DarwinLogCallback(void * baton,uint32_t flags,const char * format,va_list args)24 void DarwinLogCallback(void *baton, uint32_t flags, const char *format,
25                        va_list args) {
26   if (format == nullptr)
27     return;
28 
29   static os_log_t g_logger;
30   if (!g_logger) {
31     g_logger = os_log_create("com.apple.dt.lldb", "debugserver");
32     if (!g_logger)
33       return;
34   }
35 
36   os_log_type_t log_type;
37   if (flags & DNBLOG_FLAG_FATAL)
38     log_type = OS_LOG_TYPE_FAULT;
39   else if (flags & DNBLOG_FLAG_ERROR)
40     log_type = OS_LOG_TYPE_ERROR;
41   else if (flags & DNBLOG_FLAG_WARNING)
42     log_type = OS_LOG_TYPE_DEFAULT;
43   else if (flags & DNBLOG_FLAG_VERBOSE)
44     log_type = OS_LOG_TYPE_DEBUG;
45   else
46     log_type = OS_LOG_TYPE_DEFAULT;
47 
48   // This code is unfortunate.  os_log* only takes static strings, but
49   // our current log API isn't set up to make use of that style.
50   char buffer[LLDB_OS_LOG_MAX_BUFFER_LENGTH];
51   vsnprintf(buffer, sizeof(buffer), format, args);
52   os_log_with_type(g_logger, log_type, "%{public}s", buffer);
53 }
54 }
55 
GetLogFunction()56 DNBCallbackLog OsLogger::GetLogFunction() { return DarwinLogCallback; }
57 
58 #else
59 
GetLogFunction()60 DNBCallbackLog OsLogger::GetLogFunction() { return nullptr; }
61 
62 #endif
63 
64