Skip to content

Commit

Permalink
fix: fix error log bug by redirect fd
Browse files Browse the repository at this point in the history
Signed-off-by: NaturalSelect <[email protected]>
  • Loading branch information
NaturalSelect committed Nov 6, 2023
1 parent c63296b commit 346f119
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 4 deletions.
4 changes: 3 additions & 1 deletion curvefs/src/mds/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

#include "curvefs/src/mds/mds.h"
#include "src/common/configuration.h"
#include "src/common/log_util.h"
#include "curvefs/src/common/dynamic_vlog.h"

using ::curve::common::Configuration;
Expand Down Expand Up @@ -64,7 +65,8 @@ int main(int argc, char **argv) {
}

// initialize logging module
google::InitGoogleLogging(argv[0]);
FLAGS_logtostderr = false;
curve::common::InitGoogleLogging(argv[0]);

conf->PrintConfig();

Expand Down
1 change: 1 addition & 0 deletions curvefs/src/metaserver/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ int main(int argc, char **argv) {
FLAGS_vlog_level = FLAGS_v;

// initialize logging module
FLAGS_logtostderr = false;
google::InitGoogleLogging(argv[0]);

conf->PrintConfig();
Expand Down
3 changes: 2 additions & 1 deletion src/chunkserver/chunkserver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,8 @@ int ChunkServer::Run(int argc, char** argv) {
LoadConfigFromCmdline(&conf);

// 初始化日志模块
google::InitGoogleLogging(argv[0]);
FLAGS_logtostderr = false;
curve::common::InitGoogleLogging(argv[0]);

// 打印参数
conf.PrintConfig();
Expand Down
27 changes: 27 additions & 0 deletions src/common/log_util.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#include "src/common/log_util.h"

#include <gflags/gflags.h>
#include <glog/logging.h>
#include <sys/stat.h>
#include <fcntl.h>

static int DisableStderr() {
// NOTE: use "/dev/stderr" to reopen stderr fd
int fd = ::open("/dev/null",O_APPEND|O_WRONLY);
if (fd == -1) {
return -1;
}
int r = ::dup2(fd,::fileno(stderr));
if (r == -1) {
return r;
}
::close(fd);
return 0;
}

void curve::common::InitGoogleLogging(const char *argv0) {
if (!FLAGS_logtostderr && !FLAGS_alsologtostderr) {
DisableStderr();
}
google::InitGoogleLogging(argv0);
}
11 changes: 11 additions & 0 deletions src/common/log_util.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#ifndef CURVEFS_SRC_COMMON_LOG_UTIL_H
#define CURVEFS_SRC_COMMON_LOG_UTIL_H

namespace curve {
namespace common {

extern void InitGoogleLogging(const char *argv0);

}}

#endif
3 changes: 2 additions & 1 deletion src/mds/main/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,8 @@ int main(int argc, char **argv) {
}

// initialize logging module
google::InitGoogleLogging(argv[0]);
FLAGS_logtostderr = false;
curve::common::InitGoogleLogging(argv[0]);

// reset SIGPIPE handler
// etcdclient register SIGPIPE handler in its initialization progress, which
Expand Down
3 changes: 2 additions & 1 deletion src/snapshotcloneserver/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,8 @@ int main(int argc, char **argv) {
LoadConfigFromCmdline(conf.get());
conf->PrintConfig();
conf->ExposeMetric("snapshot_clone_server_config");
google::InitGoogleLogging(argv[0]);
FLAGS_logtostderr = false;
curve::common::InitGoogleLogging(argv[0]);
snapshotcloneserver_main(conf);
}

0 comments on commit 346f119

Please sign in to comment.