115 lines · cpp
1//===-- DNBError.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-exception6//7//===----------------------------------------------------------------------===//8//9// Created by Greg Clayton on 6/26/07.10//11//===----------------------------------------------------------------------===//12 13#include "DNBError.h"14#include "CFString.h"15#include "DNBLog.h"16 17#ifdef WITH_SPRINGBOARD18#include <SpringBoardServices/SpringBoardServer.h>19#endif20 21const char *DNBError::AsString() const {22 if (Success())23 return NULL;24 25 if (m_str.empty()) {26 const char *s = NULL;27 switch (m_flavor) {28 case MachKernel:29 s = ::mach_error_string(m_err);30 break;31 32 case POSIX:33 s = ::strerror(m_err);34 break;35 36#ifdef WITH_SPRINGBOARD37 case SpringBoard: {38 CFStringRef statusStr = SBSApplicationLaunchingErrorString(m_err);39 if (CFString::UTF8(statusStr, m_str) == NULL)40 m_str.clear();41 } break;42#endif43#ifdef WITH_BKS44 case BackBoard: {45 // You have to call ObjC routines to get the error string from46 // BackBoardServices.47 // Not sure I want to make DNBError.cpp an .mm file. For now just make48 // sure you49 // pre-populate the error string when you make the DNBError of type50 // BackBoard.51 m_str.assign(52 "Should have set BackBoard error when making the error string.");53 } break;54#endif55#ifdef WITH_FBS56 case FrontBoard: {57 // You have to call ObjC routines to get the error string from58 // FrontBoardServices.59 // Not sure I want to make DNBError.cpp an .mm file. For now just make60 // sure you61 // pre-populate the error string when you make the DNBError of type62 // FrontBoard.63 m_str.assign(64 "Should have set FrontBoard error when making the error string.");65 } break;66#endif67 default:68 break;69 }70 if (s)71 m_str.assign(s);72 }73 if (m_str.empty())74 return NULL;75 return m_str.c_str();76}77 78void DNBError::LogThreadedIfError(const char *format, ...) const {79 if (Fail()) {80 char *arg_msg = NULL;81 va_list args;82 va_start(args, format);83 ::vasprintf(&arg_msg, format, args);84 va_end(args);85 86 if (arg_msg != NULL) {87 const char *err_str = AsString();88 if (err_str == NULL)89 err_str = "???";90 DNBLogThreaded("error: %s err = %s (0x%8.8x)", arg_msg, err_str, m_err);91 free(arg_msg);92 }93 }94}95 96void DNBError::LogThreaded(const char *format, ...) const {97 char *arg_msg = NULL;98 va_list args;99 va_start(args, format);100 ::vasprintf(&arg_msg, format, args);101 va_end(args);102 103 if (arg_msg != NULL) {104 if (Fail()) {105 const char *err_str = AsString();106 if (err_str == NULL)107 err_str = "???";108 DNBLogThreaded("error: %s err = %s (0x%8.8x)", arg_msg, err_str, m_err);109 } else {110 DNBLogThreaded("%s err = 0x%8.8x", arg_msg, m_err);111 }112 free(arg_msg);113 }114}115