1 //===-- LogMessageOsLog.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 "LogMessageOsLog.h"
10 
11 #include "ActivityStore.h"
12 #include "ActivityStreamSPI.h"
13 
14 namespace {
15 static os_log_copy_formatted_message_t s_log_copy_formatted_message;
16 }
17 
SetFormatterFunction(os_log_copy_formatted_message_t format_func)18 void LogMessageOsLog::SetFormatterFunction(
19     os_log_copy_formatted_message_t format_func) {
20   s_log_copy_formatted_message = format_func;
21 }
22 
LogMessageOsLog(const ActivityStore & activity_store,ActivityStreamEntry & entry)23 LogMessageOsLog::LogMessageOsLog(const ActivityStore &activity_store,
24                                  ActivityStreamEntry &entry)
25     : LogMessage(), m_activity_store(activity_store), m_entry(entry),
26       m_message() {}
27 
HasActivity() const28 bool LogMessageOsLog::HasActivity() const { return m_entry.activity_id != 0; }
29 
GetActivity() const30 const char *LogMessageOsLog::GetActivity() const {
31   return m_activity_store.GetActivityForID(m_entry.activity_id);
32 }
33 
GetActivityChain() const34 std::string LogMessageOsLog::GetActivityChain() const {
35   return m_activity_store.GetActivityChainForID(m_entry.activity_id);
36 }
37 
HasCategory() const38 bool LogMessageOsLog::HasCategory() const {
39   return m_entry.log_message.category && (m_entry.log_message.category[0] != 0);
40 }
41 
GetCategory() const42 const char *LogMessageOsLog::GetCategory() const {
43   return m_entry.log_message.category;
44 }
45 
HasSubsystem() const46 bool LogMessageOsLog::HasSubsystem() const {
47   return m_entry.log_message.subsystem &&
48          (m_entry.log_message.subsystem[0] != 0);
49 }
50 
GetSubsystem() const51 const char *LogMessageOsLog::GetSubsystem() const {
52   return m_entry.log_message.subsystem;
53 }
54 
GetMessage() const55 const char *LogMessageOsLog::GetMessage() const {
56   if (m_message.empty()) {
57     std::unique_ptr<char[]> formatted_message(
58         s_log_copy_formatted_message(&m_entry.log_message));
59     if (formatted_message)
60       m_message = formatted_message.get();
61     // else
62     //     TODO log
63   }
64 
65   // This is safe to return as we're not modifying it once we've formatted it.
66   return m_message.c_str();
67 }
68