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