LogInit: migrate from class Error to C++ exceptions
This commit is contained in:
		| @@ -27,9 +27,10 @@ | |||||||
| #include "system/FatalError.hxx" | #include "system/FatalError.hxx" | ||||||
| #include "fs/AllocatedPath.hxx" | #include "fs/AllocatedPath.hxx" | ||||||
| #include "fs/FileSystem.hxx" | #include "fs/FileSystem.hxx" | ||||||
| #include "util/Error.hxx" |  | ||||||
| #include "util/Domain.hxx" | #include "util/Domain.hxx" | ||||||
|  | #include "util/RuntimeError.hxx" | ||||||
| #include "system/FatalError.hxx" | #include "system/FatalError.hxx" | ||||||
|  | #include "system/Error.hxx" | ||||||
|  |  | ||||||
| #include <assert.h> | #include <assert.h> | ||||||
| #include <string.h> | #include <string.h> | ||||||
| @@ -66,21 +67,26 @@ open_log_file(void) | |||||||
| 	return OpenFile(out_path, O_CREAT | O_WRONLY | O_APPEND, 0666); | 	return OpenFile(out_path, O_CREAT | O_WRONLY | O_APPEND, 0666); | ||||||
| } | } | ||||||
|  |  | ||||||
| static bool | static void | ||||||
| log_init_file(int line, Error &error) | log_init_file(int line) | ||||||
| { | { | ||||||
| 	assert(!out_path.IsNull()); | 	assert(!out_path.IsNull()); | ||||||
|  |  | ||||||
| 	out_fd = open_log_file(); | 	out_fd = open_log_file(); | ||||||
| 	if (out_fd < 0) { | 	if (out_fd < 0) { | ||||||
|  | #ifdef WIN32 | ||||||
| 		const std::string out_path_utf8 = out_path.ToUTF8(); | 		const std::string out_path_utf8 = out_path.ToUTF8(); | ||||||
| 		error.FormatErrno("failed to open log file \"%s\" (config line %d)", | 		throw FormatRuntimeError("failed to open log file \"%s\" (config line %d)", | ||||||
|  | 					 out_path_utf8.c_str(), line); | ||||||
|  | #else | ||||||
|  | 		int e = errno; | ||||||
|  | 		const std::string out_path_utf8 = out_path.ToUTF8(); | ||||||
|  | 		throw FormatErrno(e, "failed to open log file \"%s\" (config line %d)", | ||||||
| 				  out_path_utf8.c_str(), line); | 				  out_path_utf8.c_str(), line); | ||||||
| 		return false; | #endif | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	EnableLogTimestamp(); | 	EnableLogTimestamp(); | ||||||
| 	return true; |  | ||||||
| } | } | ||||||
|  |  | ||||||
| static inline LogLevel | static inline LogLevel | ||||||
| @@ -114,15 +120,12 @@ log_early_init(bool verbose) | |||||||
| #endif | #endif | ||||||
| } | } | ||||||
|  |  | ||||||
| bool | void | ||||||
| log_init(bool verbose, bool use_stdout, Error &error) | log_init(bool verbose, bool use_stdout) | ||||||
| { | { | ||||||
| #ifdef ANDROID | #ifdef ANDROID | ||||||
| 	(void)verbose; | 	(void)verbose; | ||||||
| 	(void)use_stdout; | 	(void)use_stdout; | ||||||
| 	(void)error; |  | ||||||
|  |  | ||||||
| 	return true; |  | ||||||
| #else | #else | ||||||
| 	if (verbose) | 	if (verbose) | ||||||
| 		SetLogThreshold(LogLevel::DEBUG); | 		SetLogThreshold(LogLevel::DEBUG); | ||||||
| @@ -130,29 +133,21 @@ log_init(bool verbose, bool use_stdout, Error &error) | |||||||
| 		SetLogThreshold(parse_log_level(param->value.c_str(), | 		SetLogThreshold(parse_log_level(param->value.c_str(), | ||||||
| 						param->line)); | 						param->line)); | ||||||
|  |  | ||||||
| 	if (use_stdout) { | 	if (!use_stdout) { | ||||||
| 		return true; |  | ||||||
| 	} else { |  | ||||||
| 		const auto *param = config_get_param(ConfigOption::LOG_FILE); | 		const auto *param = config_get_param(ConfigOption::LOG_FILE); | ||||||
| 		if (param == nullptr) { | 		if (param == nullptr) { | ||||||
| #ifdef HAVE_SYSLOG |  | ||||||
| 			/* no configuration: default to syslog (if | 			/* no configuration: default to syslog (if | ||||||
| 			   available) */ | 			   available) */ | ||||||
| 			LogInitSysLog(); | #ifndef HAVE_SYSLOG | ||||||
| 			return true; | 			throw std::runtime_error("config parameter 'log_file' not found"); | ||||||
| #else |  | ||||||
| 			error.Set(log_domain, |  | ||||||
| 				  "config parameter 'log_file' not found"); |  | ||||||
| 			return false; |  | ||||||
| #endif | #endif | ||||||
| #ifdef HAVE_SYSLOG | #ifdef HAVE_SYSLOG | ||||||
| 		} else if (strcmp(param->value.c_str(), "syslog") == 0) { | 		} else if (strcmp(param->value.c_str(), "syslog") == 0) { | ||||||
| 			LogInitSysLog(); | 			LogInitSysLog(); | ||||||
| 			return true; |  | ||||||
| #endif | #endif | ||||||
| 		} else { | 		} else { | ||||||
| 			out_path = param->GetPath(); | 			out_path = param->GetPath(); | ||||||
| 			return log_init_file(param->line, error); | 			log_init_file(param->line); | ||||||
| 		} | 		} | ||||||
| 	} | 	} | ||||||
| #endif | #endif | ||||||
|   | |||||||
| @@ -20,8 +20,6 @@ | |||||||
| #ifndef MPD_LOG_INIT_HXX | #ifndef MPD_LOG_INIT_HXX | ||||||
| #define MPD_LOG_INIT_HXX | #define MPD_LOG_INIT_HXX | ||||||
|  |  | ||||||
| class Error; |  | ||||||
|  |  | ||||||
| /** | /** | ||||||
|  * Configure a logging destination for daemon startup, before the |  * Configure a logging destination for daemon startup, before the | ||||||
|  * configuration file is read.  This allows the daemon to use the |  * configuration file is read.  This allows the daemon to use the | ||||||
| @@ -33,8 +31,11 @@ class Error; | |||||||
| void | void | ||||||
| log_early_init(bool verbose); | log_early_init(bool verbose); | ||||||
|  |  | ||||||
| bool | /** | ||||||
| log_init(bool verbose, bool use_stdout, Error &error); |  * Throws #std::runtime_error on error. | ||||||
|  |  */ | ||||||
|  | void | ||||||
|  | log_init(bool verbose, bool use_stdout); | ||||||
|  |  | ||||||
| void | void | ||||||
| log_deinit(); | log_deinit(); | ||||||
|   | |||||||
| @@ -412,10 +412,7 @@ try { | |||||||
| 	stats_global_init(); | 	stats_global_init(); | ||||||
| 	TagLoadConfig(); | 	TagLoadConfig(); | ||||||
|  |  | ||||||
| 	if (!log_init(options.verbose, options.log_stderr, error)) { | 	log_init(options.verbose, options.log_stderr); | ||||||
| 		LogError(error); |  | ||||||
| 		return EXIT_FAILURE; |  | ||||||
| 	} |  | ||||||
|  |  | ||||||
| 	instance = new Instance(); | 	instance = new Instance(); | ||||||
|  |  | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user
	 Max Kellermann
					Max Kellermann