完善代码

This commit is contained in:
xiongziliang 2020-06-08 16:41:49 +08:00
parent a3089f9a7e
commit ab847ff575

View File

@ -38,15 +38,14 @@ void Process::run(const string &cmd, const string &log_file_tmp) {
if (log_file_tmp.empty()) { if (log_file_tmp.empty()) {
//未指定子进程日志文件时,重定向至/dev/null //未指定子进程日志文件时,重定向至/dev/null
log_file = "NUL"; log_file = "NUL";
} } else {
else {
log_file = StrPrinter << log_file_tmp << "." << getCurrentMillisecond(); log_file = StrPrinter << log_file_tmp << "." << getCurrentMillisecond();
} }
//重定向shell日志至文件 //重定向shell日志至文件
auto fp = File::create_file(log_file.data(), "ab"); auto fp = File::create_file(log_file.data(), "ab");
if (!fp) { if (!fp) {
fprintf(stderr, "open log file %s failed:%d(%s)\r\n", log_file.data(), errno, strerror(errno)); fprintf(stderr, "open log file %s failed:%d(%s)\r\n", log_file.data(), get_uv_error(), get_uv_errmsg());
} else { } else {
auto log_fd = (HANDLE)(_get_osfhandle(fileno(fp))); auto log_fd = (HANDLE)(_get_osfhandle(fileno(fp)));
// dup to stdout and stderr. // dup to stdout and stderr.
@ -65,15 +64,23 @@ void Process::run(const string &cmd, const string &log_file_tmp) {
_pid = pi.dwProcessId; _pid = pi.dwProcessId;
_handle = pi.hProcess; _handle = pi.hProcess;
fprintf(fp, "\r\n\r\n#### pid=%d,cmd=%s #####\r\n\r\n", _pid, cmd.data()); fprintf(fp, "\r\n\r\n#### pid=%d,cmd=%s #####\r\n\r\n", _pid, cmd.data());
InfoL << "start child proces " << _pid << ", log file:" << log_file; InfoL << "start child process " << _pid << ", log file:" << log_file;
} else { } else {
WarnL << "start child proces fail: " << get_uv_errmsg(); WarnL << "start child process fail: " << get_uv_errmsg();
} }
fclose(fp); fclose(fp);
#else #else
string log_file;
if (log_file_tmp.empty()) {
//未指定子进程日志文件时,重定向至/dev/null
log_file = "/dev/null";
} else {
log_file = StrPrinter << log_file_tmp << "." << getpid();
}
_pid = fork(); _pid = fork();
if (_pid < 0) { if (_pid < 0) {
throw std::runtime_error(StrPrinter << "fork child process falied,err:" << get_uv_errmsg()); throw std::runtime_error(StrPrinter << "fork child process failed,err:" << get_uv_errmsg());
} }
if (_pid == 0) { if (_pid == 0) {
//子进程关闭core文件生成 //子进程关闭core文件生成
@ -84,26 +91,18 @@ void Process::run(const string &cmd, const string &log_file_tmp) {
signal(SIGINT, SIG_IGN); signal(SIGINT, SIG_IGN);
signal(SIGTERM, SIG_IGN); signal(SIGTERM, SIG_IGN);
string log_file;
if (log_file_tmp.empty()) {
//未指定子进程日志文件时,重定向至/dev/null
log_file = "/dev/null";
} else {
log_file = StrPrinter << log_file_tmp << "." << getpid();
}
//重定向shell日志至文件 //重定向shell日志至文件
auto fp = File::create_file(log_file.data(), "ab"); auto fp = File::create_file(log_file.data(), "ab");
if (!fp) { if (!fp) {
fprintf(stderr, "open log file %s failed:%d(%s)\r\n", log_file.data(), errno, strerror(errno)); fprintf(stderr, "open log file %s failed:%d(%s)\r\n", log_file.data(), get_uv_error(), get_uv_errmsg());
} else { } else {
auto log_fd = fileno(fp); auto log_fd = fileno(fp);
// dup to stdout and stderr. // dup to stdout and stderr.
if (dup2(log_fd, STDOUT_FILENO) < 0) { if (dup2(log_fd, STDOUT_FILENO) < 0) {
fprintf(stderr, "dup2 stdout file %s failed:%d(%s)\r\n", log_file.data(), errno, strerror(errno)); fprintf(stderr, "dup2 stdout file %s failed:%d(%s)\r\n", log_file.data(), get_uv_error(), get_uv_errmsg());
} }
if (dup2(log_fd, STDERR_FILENO) < 0) { if (dup2(log_fd, STDERR_FILENO) < 0) {
fprintf(stderr, "dup2 stderr file %s failed:%d(%s)\r\n", log_file.data(), errno, strerror(errno)); fprintf(stderr, "dup2 stderr file %s failed:%d(%s)\r\n", log_file.data(), get_uv_error(), get_uv_errmsg());
} }
// 关闭日志文件 // 关闭日志文件
::fclose(fp); ::fclose(fp);
@ -125,15 +124,15 @@ void Process::run(const string &cmd, const string &log_file_tmp) {
// EOF: NULL // EOF: NULL
charpv_params[params.size()] = NULL; charpv_params[params.size()] = NULL;
InfoL << "start child proces " << _pid << ", log file:" << log_file;
// TODO: execv or execvp // TODO: execv or execvp
auto ret = execv(params[0].c_str(), charpv_params); auto ret = execv(params[0].c_str(), charpv_params);
if (ret < 0) { if (ret < 0) {
fprintf(stderr, "fork process failed, errno=%d(%s)\r\n", errno, strerror(errno)); fprintf(stderr, "fork process failed:%d(%s)\r\n", get_uv_error(), get_uv_errmsg());
} }
exit(ret); exit(ret);
} }
InfoL << "start child process " << _pid << ", log file:" << log_file;
#endif // _WIN32 #endif // _WIN32
} }