Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/drivers/apple/os_log_logger.cpp
9973 views
1
/**************************************************************************/
2
/* os_log_logger.cpp */
3
/**************************************************************************/
4
/* This file is part of: */
5
/* GODOT ENGINE */
6
/* https://godotengine.org */
7
/**************************************************************************/
8
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10
/* */
11
/* Permission is hereby granted, free of charge, to any person obtaining */
12
/* a copy of this software and associated documentation files (the */
13
/* "Software"), to deal in the Software without restriction, including */
14
/* without limitation the rights to use, copy, modify, merge, publish, */
15
/* distribute, sublicense, and/or sell copies of the Software, and to */
16
/* permit persons to whom the Software is furnished to do so, subject to */
17
/* the following conditions: */
18
/* */
19
/* The above copyright notice and this permission notice shall be */
20
/* included in all copies or substantial portions of the Software. */
21
/* */
22
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29
/**************************************************************************/
30
31
#include "os_log_logger.h"
32
33
#include "core/string/print_string.h"
34
35
#include <cstdlib> // For malloc/free
36
37
OsLogLogger::OsLogLogger(const char *p_subsystem) {
38
const char *subsystem = p_subsystem;
39
if (!subsystem) {
40
subsystem = "org.godotengine.godot";
41
os_log_info(OS_LOG_DEFAULT, "Missing subsystem for os_log logging; using %{public}s", subsystem);
42
}
43
44
log = os_log_create(subsystem, "engine");
45
error_log = os_log_create(subsystem, error_type_string(ErrorType::ERR_ERROR));
46
warning_log = os_log_create(subsystem, error_type_string(ErrorType::ERR_WARNING));
47
script_log = os_log_create(subsystem, error_type_string(ErrorType::ERR_SCRIPT));
48
shader_log = os_log_create(subsystem, error_type_string(ErrorType::ERR_SHADER));
49
}
50
51
void OsLogLogger::logv(const char *p_format, va_list p_list, bool p_err) {
52
constexpr int static_buf_size = 1024;
53
char static_buf[static_buf_size] = { '\0' };
54
char *buf = static_buf;
55
va_list list_copy;
56
va_copy(list_copy, p_list);
57
int len = vsnprintf(buf, static_buf_size, p_format, p_list);
58
if (len >= static_buf_size) {
59
buf = (char *)Memory::alloc_static(len + 1);
60
vsnprintf(buf, len + 1, p_format, list_copy);
61
}
62
va_end(list_copy);
63
64
// Choose appropriate log type based on error flag.
65
os_log_type_t log_type = p_err ? OS_LOG_TYPE_ERROR : OS_LOG_TYPE_INFO;
66
os_log_with_type(log, log_type, "%{public}s", buf);
67
68
if (len >= static_buf_size) {
69
Memory::free_static(buf);
70
}
71
}
72
73
void OsLogLogger::log_error(const char *p_function, const char *p_file, int p_line, const char *p_code, const char *p_rationale, bool p_editor_notify, ErrorType p_type, const Vector<Ref<ScriptBacktrace>> &p_script_backtraces) {
74
os_log_t selected_log;
75
switch (p_type) {
76
case ERR_WARNING:
77
selected_log = warning_log;
78
break;
79
case ERR_SCRIPT:
80
selected_log = script_log;
81
break;
82
case ERR_SHADER:
83
selected_log = shader_log;
84
break;
85
case ERR_ERROR:
86
default:
87
selected_log = error_log;
88
break;
89
}
90
const char *err_details;
91
if (p_rationale && *p_rationale) {
92
err_details = p_rationale;
93
} else {
94
err_details = p_code;
95
}
96
97
// Choose log level based on error type.
98
os_log_type_t log_type;
99
switch (p_type) {
100
case ERR_WARNING:
101
log_type = OS_LOG_TYPE_DEFAULT;
102
break;
103
case ERR_ERROR:
104
case ERR_SCRIPT:
105
case ERR_SHADER:
106
default:
107
log_type = OS_LOG_TYPE_ERROR;
108
break;
109
}
110
111
// Append script backtraces, if any.
112
String back_trace;
113
for (const Ref<ScriptBacktrace> &backtrace : p_script_backtraces) {
114
if (backtrace.is_valid() && !backtrace->is_empty()) {
115
back_trace += "\n";
116
back_trace += backtrace->format(strlen(error_type_indent(p_type)));
117
}
118
}
119
120
if (back_trace.is_empty()) {
121
os_log_with_type(selected_log, log_type, "%{public}s:%d:%{public}s(): %{public}s %{public}s", p_file, p_line, p_function, err_details, p_code);
122
} else {
123
os_log_with_type(selected_log, log_type, "%{public}s:%d:%{public}s(): %{public}s %{public}s%{public}s", p_file, p_line, p_function, err_details, p_code, back_trace.utf8().ptr());
124
}
125
}
126
127