Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/core/io/logger.h
9973 views
1
/**************************************************************************/
2
/* logger.h */
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
#pragma once
32
33
#include "core/io/file_access.h"
34
#include "core/object/script_backtrace.h"
35
#include "core/string/ustring.h"
36
#include "core/templates/vector.h"
37
38
#include <cstdarg>
39
40
class RegEx;
41
42
class Logger {
43
protected:
44
bool should_log(bool p_err);
45
46
static inline bool _flush_stdout_on_print = true;
47
48
public:
49
enum ErrorType {
50
ERR_ERROR,
51
ERR_WARNING,
52
ERR_SCRIPT,
53
ERR_SHADER
54
};
55
56
static constexpr const char *error_type_string(ErrorType p_type) {
57
switch (p_type) {
58
case ERR_ERROR:
59
return "ERROR";
60
case ERR_WARNING:
61
return "WARNING";
62
case ERR_SCRIPT:
63
return "SCRIPT ERROR";
64
case ERR_SHADER:
65
return "SHADER ERROR";
66
}
67
return "UNKNOWN ERROR";
68
}
69
70
static constexpr const char *error_type_indent(ErrorType p_type) {
71
switch (p_type) {
72
case ERR_ERROR:
73
return " ";
74
case ERR_WARNING:
75
return " ";
76
case ERR_SCRIPT:
77
return " ";
78
case ERR_SHADER:
79
return " ";
80
}
81
return " ";
82
}
83
84
static void set_flush_stdout_on_print(bool value);
85
86
virtual void logv(const char *p_format, va_list p_list, bool p_err) _PRINTF_FORMAT_ATTRIBUTE_2_0 = 0;
87
virtual 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 = {});
88
89
void logf(const char *p_format, ...) _PRINTF_FORMAT_ATTRIBUTE_2_3;
90
void logf_error(const char *p_format, ...) _PRINTF_FORMAT_ATTRIBUTE_2_3;
91
92
virtual ~Logger() {}
93
};
94
95
/**
96
* Writes messages to stdout/stderr.
97
*/
98
class StdLogger : public Logger {
99
public:
100
virtual void logv(const char *p_format, va_list p_list, bool p_err) override _PRINTF_FORMAT_ATTRIBUTE_2_0;
101
virtual ~StdLogger() {}
102
};
103
104
/**
105
* Writes messages to the specified file. If the file already exists, creates a copy (backup)
106
* of it with timestamp appended to the file name. Maximum number of backups is configurable.
107
* When maximum is reached, the oldest backups are erased. With the maximum being equal to 1,
108
* it acts as a simple file logger.
109
*/
110
class RotatedFileLogger : public Logger {
111
String base_path;
112
int max_files;
113
114
Ref<FileAccess> file;
115
116
void clear_old_backups();
117
void rotate_file();
118
119
Ref<RegEx> strip_ansi_regex;
120
121
public:
122
explicit RotatedFileLogger(const String &p_base_path, int p_max_files = 10);
123
124
virtual void logv(const char *p_format, va_list p_list, bool p_err) override _PRINTF_FORMAT_ATTRIBUTE_2_0;
125
};
126
127
class CompositeLogger : public Logger {
128
Vector<Logger *> loggers;
129
130
public:
131
explicit CompositeLogger(const Vector<Logger *> &p_loggers);
132
133
virtual void logv(const char *p_format, va_list p_list, bool p_err) override _PRINTF_FORMAT_ATTRIBUTE_2_0;
134
virtual 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;
135
136
void add_logger(Logger *p_logger);
137
138
virtual ~CompositeLogger();
139
};
140
141