Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/drivers/apple/os_log_logger.cpp
20888 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/object/script_backtrace.h"
34
#include "core/string/print_string.h"
35
36
#include <cstdlib> // For malloc/free
37
38
OsLogLogger::OsLogLogger(const char *p_subsystem) {
39
const char *subsystem = p_subsystem;
40
if (!subsystem) {
41
subsystem = "org.godotengine.godot";
42
os_log_info(OS_LOG_DEFAULT, "Missing subsystem for os_log logging; using %{public}s", subsystem);
43
}
44
45
log = os_log_create(subsystem, "engine");
46
error_log = os_log_create(subsystem, error_type_string(ErrorType::ERR_ERROR));
47
warning_log = os_log_create(subsystem, error_type_string(ErrorType::ERR_WARNING));
48
script_log = os_log_create(subsystem, error_type_string(ErrorType::ERR_SCRIPT));
49
shader_log = os_log_create(subsystem, error_type_string(ErrorType::ERR_SHADER));
50
}
51
52
void OsLogLogger::logv(const char *p_format, va_list p_list, bool p_err) {
53
constexpr int static_buf_size = 1024;
54
char static_buf[static_buf_size] = { '\0' };
55
char *buf = static_buf;
56
va_list list_copy;
57
va_copy(list_copy, p_list);
58
int len = vsnprintf(buf, static_buf_size, p_format, p_list);
59
if (len >= static_buf_size) {
60
buf = (char *)Memory::alloc_static(len + 1);
61
vsnprintf(buf, len + 1, p_format, list_copy);
62
}
63
va_end(list_copy);
64
65
// Choose appropriate log type based on error flag.
66
os_log_type_t log_type = p_err ? OS_LOG_TYPE_ERROR : OS_LOG_TYPE_INFO;
67
os_log_with_type(log, log_type, "%{public}s", buf);
68
69
if (len >= static_buf_size) {
70
Memory::free_static(buf);
71
}
72
}
73
74
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) {
75
os_log_t selected_log;
76
switch (p_type) {
77
case ERR_WARNING:
78
selected_log = warning_log;
79
break;
80
case ERR_SCRIPT:
81
selected_log = script_log;
82
break;
83
case ERR_SHADER:
84
selected_log = shader_log;
85
break;
86
case ERR_ERROR:
87
default:
88
selected_log = error_log;
89
break;
90
}
91
const char *err_details;
92
if (p_rationale && *p_rationale) {
93
err_details = p_rationale;
94
} else {
95
err_details = p_code;
96
}
97
98
// Choose log level based on error type.
99
os_log_type_t log_type;
100
switch (p_type) {
101
case ERR_WARNING:
102
log_type = OS_LOG_TYPE_DEFAULT;
103
break;
104
case ERR_ERROR:
105
case ERR_SCRIPT:
106
case ERR_SHADER:
107
default:
108
log_type = OS_LOG_TYPE_ERROR;
109
break;
110
}
111
112
// Append script backtraces, if any.
113
String back_trace;
114
for (const Ref<ScriptBacktrace> &backtrace : p_script_backtraces) {
115
if (backtrace.is_valid() && !backtrace->is_empty()) {
116
back_trace += "\n";
117
back_trace += backtrace->format(strlen(error_type_indent(p_type)));
118
}
119
}
120
121
if (back_trace.is_empty()) {
122
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);
123
} else {
124
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());
125
}
126
}
127
128