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