1 /*************************************************************************/
2 /*  logger.h                                                             */
3 /*************************************************************************/
4 /*                       This file is part of:                           */
5 /*                           GODOT ENGINE                                */
6 /*                      https://godotengine.org                          */
7 /*************************************************************************/
8 /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur.                 */
9 /* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md).   */
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 #ifndef LOGGER_H
32 #define LOGGER_H
33 
34 #include "core/os/file_access.h"
35 #include "core/ustring.h"
36 #include "core/vector.h"
37 
38 #include <stdarg.h>
39 
40 class Logger {
41 protected:
42 	bool should_log(bool p_err);
43 
44 public:
45 	enum ErrorType {
46 		ERR_ERROR,
47 		ERR_WARNING,
48 		ERR_SCRIPT,
49 		ERR_SHADER
50 	};
51 
52 	virtual void logv(const char *p_format, va_list p_list, bool p_err) _PRINTF_FORMAT_ATTRIBUTE_2_0 = 0;
53 	virtual void log_error(const char *p_function, const char *p_file, int p_line, const char *p_code, const char *p_rationale, ErrorType p_type = ERR_ERROR);
54 
55 	void logf(const char *p_format, ...) _PRINTF_FORMAT_ATTRIBUTE_2_3;
56 	void logf_error(const char *p_format, ...) _PRINTF_FORMAT_ATTRIBUTE_2_3;
57 
58 	virtual ~Logger();
59 };
60 
61 /**
62  * Writes messages to stdout/stderr.
63  */
64 class StdLogger : public Logger {
65 
66 public:
67 	virtual void logv(const char *p_format, va_list p_list, bool p_err) _PRINTF_FORMAT_ATTRIBUTE_2_0;
68 	virtual ~StdLogger();
69 };
70 
71 /**
72  * Writes messages to the specified file. If the file already exists, creates a copy (backup)
73  * of it with timestamp appended to the file name. Maximum number of backups is configurable.
74  * When maximum is reached, the oldest backups are erased. With the maximum being equal to 1,
75  * it acts as a simple file logger.
76  */
77 class RotatedFileLogger : public Logger {
78 	String base_path;
79 	int max_files;
80 
81 	FileAccess *file;
82 
83 	void rotate_file_without_closing();
84 	void close_file();
85 	void clear_old_backups();
86 	void rotate_file();
87 
88 public:
89 	RotatedFileLogger(const String &p_base_path, int p_max_files = 10);
90 
91 	virtual void logv(const char *p_format, va_list p_list, bool p_err) _PRINTF_FORMAT_ATTRIBUTE_2_0;
92 
93 	virtual ~RotatedFileLogger();
94 };
95 
96 class CompositeLogger : public Logger {
97 	Vector<Logger *> loggers;
98 
99 public:
100 	CompositeLogger(Vector<Logger *> p_loggers);
101 
102 	virtual void logv(const char *p_format, va_list p_list, bool p_err) _PRINTF_FORMAT_ATTRIBUTE_2_0;
103 	virtual void log_error(const char *p_function, const char *p_file, int p_line, const char *p_code, const char *p_rationale, ErrorType p_type = ERR_ERROR);
104 
105 	void add_logger(Logger *p_logger);
106 
107 	virtual ~CompositeLogger();
108 };
109 
110 #endif
111