/**************************************************************************/1/* logger.h */2/**************************************************************************/3/* This file is part of: */4/* GODOT ENGINE */5/* https://godotengine.org */6/**************************************************************************/7/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */8/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */9/* */10/* Permission is hereby granted, free of charge, to any person obtaining */11/* a copy of this software and associated documentation files (the */12/* "Software"), to deal in the Software without restriction, including */13/* without limitation the rights to use, copy, modify, merge, publish, */14/* distribute, sublicense, and/or sell copies of the Software, and to */15/* permit persons to whom the Software is furnished to do so, subject to */16/* the following conditions: */17/* */18/* The above copyright notice and this permission notice shall be */19/* included in all copies or substantial portions of the Software. */20/* */21/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */22/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */23/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */24/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */25/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */26/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */27/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */28/**************************************************************************/2930#pragma once3132#include "core/io/file_access.h"33#include "core/object/script_backtrace.h"34#include "core/string/ustring.h"35#include "core/templates/vector.h"3637#include <cstdarg>3839class RegEx;4041class Logger {42protected:43bool should_log(bool p_err);4445static inline bool _flush_stdout_on_print = true;4647public:48enum ErrorType {49ERR_ERROR,50ERR_WARNING,51ERR_SCRIPT,52ERR_SHADER53};5455static constexpr const char *error_type_string(ErrorType p_type) {56switch (p_type) {57case ERR_ERROR:58return "ERROR";59case ERR_WARNING:60return "WARNING";61case ERR_SCRIPT:62return "SCRIPT ERROR";63case ERR_SHADER:64return "SHADER ERROR";65}66return "UNKNOWN ERROR";67}6869static constexpr const char *error_type_indent(ErrorType p_type) {70switch (p_type) {71case ERR_ERROR:72return " ";73case ERR_WARNING:74return " ";75case ERR_SCRIPT:76return " ";77case ERR_SHADER:78return " ";79}80return " ";81}8283static void set_flush_stdout_on_print(bool value);8485virtual void logv(const char *p_format, va_list p_list, bool p_err) _PRINTF_FORMAT_ATTRIBUTE_2_0 = 0;86virtual void 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 = false, ErrorType p_type = ERR_ERROR, const Vector<Ref<ScriptBacktrace>> &p_script_backtraces = {});8788void logf(const char *p_format, ...) _PRINTF_FORMAT_ATTRIBUTE_2_3;89void logf_error(const char *p_format, ...) _PRINTF_FORMAT_ATTRIBUTE_2_3;9091virtual ~Logger() {}92};9394/**95* Writes messages to stdout/stderr.96*/97class StdLogger : public Logger {98public:99virtual void logv(const char *p_format, va_list p_list, bool p_err) override _PRINTF_FORMAT_ATTRIBUTE_2_0;100virtual ~StdLogger() {}101};102103/**104* Writes messages to the specified file. If the file already exists, creates a copy (backup)105* of it with timestamp appended to the file name. Maximum number of backups is configurable.106* When maximum is reached, the oldest backups are erased. With the maximum being equal to 1,107* it acts as a simple file logger.108*/109class RotatedFileLogger : public Logger {110String base_path;111int max_files;112113Ref<FileAccess> file;114115void clear_old_backups();116void rotate_file();117118Ref<RegEx> strip_ansi_regex;119120public:121explicit RotatedFileLogger(const String &p_base_path, int p_max_files = 10);122123virtual void logv(const char *p_format, va_list p_list, bool p_err) override _PRINTF_FORMAT_ATTRIBUTE_2_0;124};125126class CompositeLogger : public Logger {127Vector<Logger *> loggers;128129public:130explicit CompositeLogger(const Vector<Logger *> &p_loggers);131132virtual void logv(const char *p_format, va_list p_list, bool p_err) override _PRINTF_FORMAT_ATTRIBUTE_2_0;133virtual void 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 = ERR_ERROR, const Vector<Ref<ScriptBacktrace>> &p_script_backtraces = {}) override;134135void add_logger(Logger *p_logger);136137virtual ~CompositeLogger();138};139140141