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