| 1 | /**************************************************************************/ |
| 2 | /* 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 "logger.h" |
| 32 | |
| 33 | #include "core/config/project_settings.h" |
| 34 | #include "core/core_globals.h" |
| 35 | #include "core/io/dir_access.h" |
| 36 | #include "core/os/os.h" |
| 37 | #include "core/os/time.h" |
| 38 | #include "core/string/print_string.h" |
| 39 | |
| 40 | #if defined(MINGW_ENABLED) || defined(_MSC_VER) |
| 41 | #define sprintf sprintf_s |
| 42 | #endif |
| 43 | |
| 44 | bool Logger::should_log(bool p_err) { |
| 45 | return (!p_err || CoreGlobals::print_error_enabled) && (p_err || CoreGlobals::print_line_enabled); |
| 46 | } |
| 47 | |
| 48 | bool Logger::_flush_stdout_on_print = true; |
| 49 | |
| 50 | void Logger::set_flush_stdout_on_print(bool value) { |
| 51 | _flush_stdout_on_print = value; |
| 52 | } |
| 53 | |
| 54 | void Logger::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) { |
| 55 | if (!should_log(true)) { |
| 56 | return; |
| 57 | } |
| 58 | |
| 59 | const char *err_type = "ERROR" ; |
| 60 | switch (p_type) { |
| 61 | case ERR_ERROR: |
| 62 | err_type = "ERROR" ; |
| 63 | break; |
| 64 | case ERR_WARNING: |
| 65 | err_type = "WARNING" ; |
| 66 | break; |
| 67 | case ERR_SCRIPT: |
| 68 | err_type = "SCRIPT ERROR" ; |
| 69 | break; |
| 70 | case ERR_SHADER: |
| 71 | err_type = "SHADER ERROR" ; |
| 72 | break; |
| 73 | default: |
| 74 | ERR_PRINT("Unknown error type" ); |
| 75 | break; |
| 76 | } |
| 77 | |
| 78 | const char *err_details; |
| 79 | if (p_rationale && *p_rationale) { |
| 80 | err_details = p_rationale; |
| 81 | } else { |
| 82 | err_details = p_code; |
| 83 | } |
| 84 | |
| 85 | if (p_editor_notify) { |
| 86 | logf_error("%s: %s\n" , err_type, err_details); |
| 87 | } else { |
| 88 | logf_error("USER %s: %s\n" , err_type, err_details); |
| 89 | } |
| 90 | logf_error(" at: %s (%s:%i)\n" , p_function, p_file, p_line); |
| 91 | } |
| 92 | |
| 93 | void Logger::logf(const char *p_format, ...) { |
| 94 | if (!should_log(false)) { |
| 95 | return; |
| 96 | } |
| 97 | |
| 98 | va_list argp; |
| 99 | va_start(argp, p_format); |
| 100 | |
| 101 | logv(p_format, argp, false); |
| 102 | |
| 103 | va_end(argp); |
| 104 | } |
| 105 | |
| 106 | void Logger::logf_error(const char *p_format, ...) { |
| 107 | if (!should_log(true)) { |
| 108 | return; |
| 109 | } |
| 110 | |
| 111 | va_list argp; |
| 112 | va_start(argp, p_format); |
| 113 | |
| 114 | logv(p_format, argp, true); |
| 115 | |
| 116 | va_end(argp); |
| 117 | } |
| 118 | |
| 119 | void RotatedFileLogger::clear_old_backups() { |
| 120 | int max_backups = max_files - 1; // -1 for the current file |
| 121 | |
| 122 | String basename = base_path.get_file().get_basename(); |
| 123 | String extension = base_path.get_extension(); |
| 124 | |
| 125 | Ref<DirAccess> da = DirAccess::open(base_path.get_base_dir()); |
| 126 | if (da.is_null()) { |
| 127 | return; |
| 128 | } |
| 129 | |
| 130 | da->list_dir_begin(); |
| 131 | String f = da->get_next(); |
| 132 | HashSet<String> backups; |
| 133 | while (!f.is_empty()) { |
| 134 | if (!da->current_is_dir() && f.begins_with(basename) && f.get_extension() == extension && f != base_path.get_file()) { |
| 135 | backups.insert(f); |
| 136 | } |
| 137 | f = da->get_next(); |
| 138 | } |
| 139 | da->list_dir_end(); |
| 140 | |
| 141 | if (backups.size() > (uint32_t)max_backups) { |
| 142 | // since backups are appended with timestamp and Set iterates them in sorted order, |
| 143 | // first backups are the oldest |
| 144 | int to_delete = backups.size() - max_backups; |
| 145 | for (HashSet<String>::Iterator E = backups.begin(); E && to_delete > 0; ++E, --to_delete) { |
| 146 | da->remove(*E); |
| 147 | } |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | void RotatedFileLogger::rotate_file() { |
| 152 | file.unref(); |
| 153 | |
| 154 | if (FileAccess::exists(base_path)) { |
| 155 | if (max_files > 1) { |
| 156 | String timestamp = Time::get_singleton()->get_datetime_string_from_system().replace(":" , "." ); |
| 157 | String backup_name = base_path.get_basename() + timestamp; |
| 158 | if (!base_path.get_extension().is_empty()) { |
| 159 | backup_name += "." + base_path.get_extension(); |
| 160 | } |
| 161 | |
| 162 | Ref<DirAccess> da = DirAccess::open(base_path.get_base_dir()); |
| 163 | if (da.is_valid()) { |
| 164 | da->copy(base_path, backup_name); |
| 165 | } |
| 166 | clear_old_backups(); |
| 167 | } |
| 168 | } else { |
| 169 | Ref<DirAccess> da = DirAccess::create(DirAccess::ACCESS_USERDATA); |
| 170 | if (da.is_valid()) { |
| 171 | da->make_dir_recursive(base_path.get_base_dir()); |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | file = FileAccess::open(base_path, FileAccess::WRITE); |
| 176 | file->detach_from_objectdb(); // Note: This FileAccess instance will exist longer than ObjectDB, therefore can't be registered in ObjectDB. |
| 177 | } |
| 178 | |
| 179 | RotatedFileLogger::RotatedFileLogger(const String &p_base_path, int p_max_files) : |
| 180 | base_path(p_base_path.simplify_path()), |
| 181 | max_files(p_max_files > 0 ? p_max_files : 1) { |
| 182 | rotate_file(); |
| 183 | } |
| 184 | |
| 185 | void RotatedFileLogger::logv(const char *p_format, va_list p_list, bool p_err) { |
| 186 | if (!should_log(p_err)) { |
| 187 | return; |
| 188 | } |
| 189 | |
| 190 | if (file.is_valid()) { |
| 191 | const int static_buf_size = 512; |
| 192 | char static_buf[static_buf_size]; |
| 193 | char *buf = static_buf; |
| 194 | va_list list_copy; |
| 195 | va_copy(list_copy, p_list); |
| 196 | int len = vsnprintf(buf, static_buf_size, p_format, p_list); |
| 197 | if (len >= static_buf_size) { |
| 198 | buf = (char *)Memory::alloc_static(len + 1); |
| 199 | vsnprintf(buf, len + 1, p_format, list_copy); |
| 200 | } |
| 201 | va_end(list_copy); |
| 202 | file->store_buffer((uint8_t *)buf, len); |
| 203 | |
| 204 | if (len >= static_buf_size) { |
| 205 | Memory::free_static(buf); |
| 206 | } |
| 207 | |
| 208 | if (p_err || _flush_stdout_on_print) { |
| 209 | // Don't always flush when printing stdout to avoid performance |
| 210 | // issues when `print()` is spammed in release builds. |
| 211 | file->flush(); |
| 212 | } |
| 213 | } |
| 214 | } |
| 215 | |
| 216 | void StdLogger::logv(const char *p_format, va_list p_list, bool p_err) { |
| 217 | if (!should_log(p_err)) { |
| 218 | return; |
| 219 | } |
| 220 | |
| 221 | if (p_err) { |
| 222 | vfprintf(stderr, p_format, p_list); |
| 223 | } else { |
| 224 | vprintf(p_format, p_list); |
| 225 | if (_flush_stdout_on_print) { |
| 226 | // Don't always flush when printing stdout to avoid performance |
| 227 | // issues when `print()` is spammed in release builds. |
| 228 | fflush(stdout); |
| 229 | } |
| 230 | } |
| 231 | } |
| 232 | |
| 233 | CompositeLogger::CompositeLogger(Vector<Logger *> p_loggers) : |
| 234 | loggers(p_loggers) { |
| 235 | } |
| 236 | |
| 237 | void CompositeLogger::logv(const char *p_format, va_list p_list, bool p_err) { |
| 238 | if (!should_log(p_err)) { |
| 239 | return; |
| 240 | } |
| 241 | |
| 242 | for (int i = 0; i < loggers.size(); ++i) { |
| 243 | va_list list_copy; |
| 244 | va_copy(list_copy, p_list); |
| 245 | loggers[i]->logv(p_format, list_copy, p_err); |
| 246 | va_end(list_copy); |
| 247 | } |
| 248 | } |
| 249 | |
| 250 | void CompositeLogger::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) { |
| 251 | if (!should_log(true)) { |
| 252 | return; |
| 253 | } |
| 254 | |
| 255 | for (int i = 0; i < loggers.size(); ++i) { |
| 256 | loggers[i]->log_error(p_function, p_file, p_line, p_code, p_rationale, p_editor_notify, p_type); |
| 257 | } |
| 258 | } |
| 259 | |
| 260 | void CompositeLogger::add_logger(Logger *p_logger) { |
| 261 | loggers.push_back(p_logger); |
| 262 | } |
| 263 | |
| 264 | CompositeLogger::~CompositeLogger() { |
| 265 | for (int i = 0; i < loggers.size(); ++i) { |
| 266 | memdelete(loggers[i]); |
| 267 | } |
| 268 | } |
| 269 | |