| 
									
										
										
										
											2017-09-22 12:56:02 +07:00
										 |  |  | /*************************************************************************/ | 
					
						
							|  |  |  | /*  logger.cpp                                                           */ | 
					
						
							|  |  |  | /*************************************************************************/ | 
					
						
							|  |  |  | /*                       This file is part of:                           */ | 
					
						
							|  |  |  | /*                           GODOT ENGINE                                */ | 
					
						
							|  |  |  | /*                      https://godotengine.org                          */ | 
					
						
							|  |  |  | /*************************************************************************/ | 
					
						
							| 
									
										
										
										
											2019-01-01 12:53:14 +01:00
										 |  |  | /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur.                 */ | 
					
						
							|  |  |  | /* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md)    */ | 
					
						
							| 
									
										
										
										
											2017-09-22 12:56:02 +07:00
										 |  |  | /*                                                                       */ | 
					
						
							|  |  |  | /* Permission is hereby granted, free of charge, to any person obtaining */ | 
					
						
							|  |  |  | /* a copy of this software and associated documentation files (the       */ | 
					
						
							|  |  |  | /* "Software"), to deal in the Software without restriction, including   */ | 
					
						
							|  |  |  | /* without limitation the rights to use, copy, modify, merge, publish,   */ | 
					
						
							|  |  |  | /* distribute, sublicense, and/or sell copies of the Software, and to    */ | 
					
						
							|  |  |  | /* permit persons to whom the Software is furnished to do so, subject to */ | 
					
						
							|  |  |  | /* the following conditions:                                             */ | 
					
						
							|  |  |  | /*                                                                       */ | 
					
						
							|  |  |  | /* The above copyright notice and this permission notice shall be        */ | 
					
						
							|  |  |  | /* included in all copies or substantial portions of the Software.       */ | 
					
						
							|  |  |  | /*                                                                       */ | 
					
						
							|  |  |  | /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,       */ | 
					
						
							|  |  |  | /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF    */ | 
					
						
							|  |  |  | /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ | 
					
						
							|  |  |  | /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY  */ | 
					
						
							|  |  |  | /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,  */ | 
					
						
							|  |  |  | /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE     */ | 
					
						
							|  |  |  | /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.                */ | 
					
						
							|  |  |  | /*************************************************************************/ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include "logger.h"
 | 
					
						
							| 
									
										
										
										
											2017-11-21 16:35:01 +07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-11 18:13:45 +02:00
										 |  |  | #include "core/os/dir_access.h"
 | 
					
						
							|  |  |  | #include "core/os/os.h"
 | 
					
						
							|  |  |  | #include "core/print_string.h"
 | 
					
						
							| 
									
										
										
										
											2017-09-22 12:56:02 +07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-10 12:08:36 +07:00
										 |  |  | // va_copy was defined in the C99, but not in C++ standards before C++11.
 | 
					
						
							|  |  |  | // When you compile C++ without --std=c++<XX> option, compilers still define
 | 
					
						
							|  |  |  | // va_copy, otherwise you have to use the internal version (__va_copy).
 | 
					
						
							|  |  |  | #if !defined(va_copy)
 | 
					
						
							|  |  |  | #if defined(__GNUC__)
 | 
					
						
							| 
									
										
										
										
											2019-01-31 01:08:57 +05:30
										 |  |  | #define va_copy(d, s) __va_copy((d), (s))
 | 
					
						
							| 
									
										
										
										
											2017-10-10 12:08:36 +07:00
										 |  |  | #else
 | 
					
						
							|  |  |  | #define va_copy(d, s) ((d) = (s))
 | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-04 14:38:52 +01:00
										 |  |  | #if defined(MINGW_ENABLED) || defined(_MSC_VER)
 | 
					
						
							|  |  |  | #define sprintf sprintf_s
 | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-22 12:56:02 +07:00
										 |  |  | bool Logger::should_log(bool p_err) { | 
					
						
							|  |  |  | 	return (!p_err || _print_error_enabled) && (p_err || _print_line_enabled); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void Logger::log_error(const char *p_function, const char *p_file, int p_line, const char *p_code, const char *p_rationale, ErrorType p_type) { | 
					
						
							|  |  |  | 	if (!should_log(true)) { | 
					
						
							|  |  |  | 		return; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	const char *err_type = "**ERROR**"; | 
					
						
							|  |  |  | 	switch (p_type) { | 
					
						
							|  |  |  | 		case ERR_ERROR: err_type = "**ERROR**"; break; | 
					
						
							|  |  |  | 		case ERR_WARNING: err_type = "**WARNING**"; break; | 
					
						
							|  |  |  | 		case ERR_SCRIPT: err_type = "**SCRIPT ERROR**"; break; | 
					
						
							|  |  |  | 		case ERR_SHADER: err_type = "**SHADER ERROR**"; break; | 
					
						
							|  |  |  | 		default: ERR_PRINT("Unknown error type"); break; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	const char *err_details; | 
					
						
							|  |  |  | 	if (p_rationale && *p_rationale) | 
					
						
							|  |  |  | 		err_details = p_rationale; | 
					
						
							|  |  |  | 	else | 
					
						
							|  |  |  | 		err_details = p_code; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	logf_error("%s: %s\n", err_type, err_details); | 
					
						
							|  |  |  | 	logf_error("   At: %s:%i:%s() - %s\n", p_file, p_line, p_function, p_code); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void Logger::logf(const char *p_format, ...) { | 
					
						
							|  |  |  | 	if (!should_log(false)) { | 
					
						
							|  |  |  | 		return; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	va_list argp; | 
					
						
							|  |  |  | 	va_start(argp, p_format); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	logv(p_format, argp, false); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	va_end(argp); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void Logger::logf_error(const char *p_format, ...) { | 
					
						
							|  |  |  | 	if (!should_log(true)) { | 
					
						
							|  |  |  | 		return; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	va_list argp; | 
					
						
							|  |  |  | 	va_start(argp, p_format); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	logv(p_format, argp, true); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	va_end(argp); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Logger::~Logger() {} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void RotatedFileLogger::close_file() { | 
					
						
							|  |  |  | 	if (file) { | 
					
						
							|  |  |  | 		memdelete(file); | 
					
						
							|  |  |  | 		file = NULL; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void RotatedFileLogger::clear_old_backups() { | 
					
						
							|  |  |  | 	int max_backups = max_files - 1; // -1 for the current file
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-20 10:34:46 +07:00
										 |  |  | 	String basename = base_path.get_file().get_basename(); | 
					
						
							| 
									
										
										
										
											2018-06-12 04:52:34 -03:00
										 |  |  | 	String extension = base_path.get_extension(); | 
					
						
							| 
									
										
										
										
											2017-09-22 12:56:02 +07:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	DirAccess *da = DirAccess::open(base_path.get_base_dir()); | 
					
						
							|  |  |  | 	if (!da) { | 
					
						
							|  |  |  | 		return; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	da->list_dir_begin(); | 
					
						
							|  |  |  | 	String f = da->get_next(); | 
					
						
							|  |  |  | 	Set<String> backups; | 
					
						
							|  |  |  | 	while (f != String()) { | 
					
						
							| 
									
										
										
										
											2018-06-12 04:52:34 -03:00
										 |  |  | 		if (!da->current_is_dir() && f.begins_with(basename) && f.get_extension() == extension && f != base_path.get_file()) { | 
					
						
							| 
									
										
										
										
											2017-09-22 12:56:02 +07:00
										 |  |  | 			backups.insert(f); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		f = da->get_next(); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	da->list_dir_end(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (backups.size() > max_backups) { | 
					
						
							|  |  |  | 		// since backups are appended with timestamp and Set iterates them in sorted order,
 | 
					
						
							|  |  |  | 		// first backups are the oldest
 | 
					
						
							|  |  |  | 		int to_delete = backups.size() - max_backups; | 
					
						
							|  |  |  | 		for (Set<String>::Element *E = backups.front(); E && to_delete > 0; E = E->next(), --to_delete) { | 
					
						
							|  |  |  | 			da->remove(E->get()); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	memdelete(da); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void RotatedFileLogger::rotate_file() { | 
					
						
							|  |  |  | 	close_file(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (FileAccess::exists(base_path)) { | 
					
						
							|  |  |  | 		if (max_files > 1) { | 
					
						
							|  |  |  | 			char timestamp[21]; | 
					
						
							|  |  |  | 			OS::Date date = OS::get_singleton()->get_date(); | 
					
						
							|  |  |  | 			OS::Time time = OS::get_singleton()->get_time(); | 
					
						
							| 
									
										
										
										
											2017-10-20 10:34:46 +07:00
										 |  |  | 			sprintf(timestamp, "-%04d-%02d-%02d-%02d-%02d-%02d", date.year, date.month, date.day, time.hour, time.min, time.sec); | 
					
						
							| 
									
										
										
										
											2017-09-22 12:56:02 +07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-29 22:36:50 -03:00
										 |  |  | 			String backup_name = base_path.get_basename() + timestamp; | 
					
						
							|  |  |  | 			if (base_path.get_extension() != String()) { | 
					
						
							|  |  |  | 				backup_name += "." + base_path.get_extension(); | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2017-09-22 12:56:02 +07:00
										 |  |  | 
 | 
					
						
							|  |  |  | 			DirAccess *da = DirAccess::open(base_path.get_base_dir()); | 
					
						
							|  |  |  | 			if (da) { | 
					
						
							|  |  |  | 				da->copy(base_path, backup_name); | 
					
						
							|  |  |  | 				memdelete(da); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			clear_old_backups(); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} else { | 
					
						
							|  |  |  | 		DirAccess *da = DirAccess::create(DirAccess::ACCESS_USERDATA); | 
					
						
							|  |  |  | 		if (da) { | 
					
						
							|  |  |  | 			da->make_dir_recursive(base_path.get_base_dir()); | 
					
						
							|  |  |  | 			memdelete(da); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	file = FileAccess::open(base_path, FileAccess::WRITE); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-12-08 15:07:33 -05:00
										 |  |  | RotatedFileLogger::RotatedFileLogger(const String &p_base_path, int p_max_files) : | 
					
						
							|  |  |  | 		base_path(p_base_path.simplify_path()), | 
					
						
							|  |  |  | 		max_files(p_max_files > 0 ? p_max_files : 1), | 
					
						
							|  |  |  | 		file(NULL) { | 
					
						
							| 
									
										
										
										
											2017-09-22 12:56:02 +07:00
										 |  |  | 	rotate_file(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void RotatedFileLogger::logv(const char *p_format, va_list p_list, bool p_err) { | 
					
						
							|  |  |  | 	if (!should_log(p_err)) { | 
					
						
							|  |  |  | 		return; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (file) { | 
					
						
							|  |  |  | 		const int static_buf_size = 512; | 
					
						
							|  |  |  | 		char static_buf[static_buf_size]; | 
					
						
							|  |  |  | 		char *buf = static_buf; | 
					
						
							| 
									
										
										
										
											2017-10-05 16:16:20 +07:00
										 |  |  | 		va_list list_copy; | 
					
						
							|  |  |  | 		va_copy(list_copy, p_list); | 
					
						
							| 
									
										
										
										
											2017-09-22 12:56:02 +07:00
										 |  |  | 		int len = vsnprintf(buf, static_buf_size, p_format, p_list); | 
					
						
							|  |  |  | 		if (len >= static_buf_size) { | 
					
						
							|  |  |  | 			buf = (char *)Memory::alloc_static(len + 1); | 
					
						
							| 
									
										
										
										
											2017-10-05 16:16:20 +07:00
										 |  |  | 			vsnprintf(buf, len + 1, p_format, list_copy); | 
					
						
							| 
									
										
										
										
											2017-09-22 12:56:02 +07:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2017-10-05 16:16:20 +07:00
										 |  |  | 		va_end(list_copy); | 
					
						
							| 
									
										
										
										
											2017-09-22 12:56:02 +07:00
										 |  |  | 		file->store_buffer((uint8_t *)buf, len); | 
					
						
							|  |  |  | 		if (len >= static_buf_size) { | 
					
						
							|  |  |  | 			Memory::free_static(buf); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | #ifdef DEBUG_ENABLED
 | 
					
						
							|  |  |  | 		const bool need_flush = true; | 
					
						
							|  |  |  | #else
 | 
					
						
							|  |  |  | 		bool need_flush = p_err; | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 		if (need_flush) { | 
					
						
							|  |  |  | 			file->flush(); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | RotatedFileLogger::~RotatedFileLogger() { | 
					
						
							|  |  |  | 	close_file(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void StdLogger::logv(const char *p_format, va_list p_list, bool p_err) { | 
					
						
							|  |  |  | 	if (!should_log(p_err)) { | 
					
						
							|  |  |  | 		return; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (p_err) { | 
					
						
							|  |  |  | 		vfprintf(stderr, p_format, p_list); | 
					
						
							|  |  |  | 	} else { | 
					
						
							|  |  |  | 		vprintf(p_format, p_list); | 
					
						
							|  |  |  | #ifdef DEBUG_ENABLED
 | 
					
						
							|  |  |  | 		fflush(stdout); | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | StdLogger::~StdLogger() {} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-12-08 15:07:33 -05:00
										 |  |  | CompositeLogger::CompositeLogger(Vector<Logger *> p_loggers) : | 
					
						
							|  |  |  | 		loggers(p_loggers) { | 
					
						
							| 
									
										
										
										
											2017-09-22 12:56:02 +07:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void CompositeLogger::logv(const char *p_format, va_list p_list, bool p_err) { | 
					
						
							|  |  |  | 	if (!should_log(p_err)) { | 
					
						
							|  |  |  | 		return; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	for (int i = 0; i < loggers.size(); ++i) { | 
					
						
							|  |  |  | 		va_list list_copy; | 
					
						
							|  |  |  | 		va_copy(list_copy, p_list); | 
					
						
							|  |  |  | 		loggers[i]->logv(p_format, list_copy, p_err); | 
					
						
							|  |  |  | 		va_end(list_copy); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void CompositeLogger::log_error(const char *p_function, const char *p_file, int p_line, const char *p_code, const char *p_rationale, ErrorType p_type) { | 
					
						
							|  |  |  | 	if (!should_log(true)) { | 
					
						
							|  |  |  | 		return; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	for (int i = 0; i < loggers.size(); ++i) { | 
					
						
							|  |  |  | 		loggers[i]->log_error(p_function, p_file, p_line, p_code, p_rationale, p_type); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-21 16:35:01 +07:00
										 |  |  | void CompositeLogger::add_logger(Logger *p_logger) { | 
					
						
							|  |  |  | 	loggers.push_back(p_logger); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-22 12:56:02 +07:00
										 |  |  | CompositeLogger::~CompositeLogger() { | 
					
						
							|  |  |  | 	for (int i = 0; i < loggers.size(); ++i) { | 
					
						
							|  |  |  | 		memdelete(loggers[i]); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } |