17 #include <boost/foreach.hpp>
18 #include <boost/thread/locks.hpp>
19 #include <boost/thread/mutex.hpp>
20 #include <boost/uuid/uuid.hpp>
21 #include <boost/uuid/uuid_generators.hpp>
22 #include <boost/uuid/uuid_io.hpp>
27 #include <gutil/strings/substitute.h>
42 "Prefix of log filename - "
43 "full path is <log_dir>/<log_filename>.[INFO|WARN|ERROR|FATAL]");
45 "If true, redirects stdout/stderr to INFO/ERROR log.");
49 using boost::uuids::random_generator;
57 if (logging_initialized)
return;
58 if (!FLAGS_log_filename.empty()) {
59 for (
int severity = google::INFO; severity <= google::FATAL; ++severity) {
60 google::SetLogSymlink(severity, FLAGS_log_filename.c_str());
68 if (FLAGS_log_dir.empty()) {
69 FLAGS_log_dir =
"/tmp";
73 FLAGS_stderrthreshold = google::FATAL + 1;
75 if (FLAGS_redirect_stdout_stderr && !TestInfo::is_test()) {
78 FLAGS_logtostderr =
false;
79 FLAGS_alsologtostderr =
false;
82 if (!FLAGS_logtostderr) {
85 random_generator uuid_generator;
86 ss << FLAGS_log_dir <<
"/" <<
"impala_test_log." << uuid_generator();
87 const string file_name = ss.str();
88 ofstream test_file(file_name.c_str());
89 if (!test_file.is_open()) {
90 stringstream error_msg;
91 error_msg <<
"Could not open file in log_dir " << FLAGS_log_dir;
92 perror(error_msg.str().c_str());
97 remove(file_name.c_str());
100 google::InitGoogleLogging(arg);
101 if (!FLAGS_redaction_rules_file.empty()) {
107 if (FLAGS_log_filename.empty()) {
108 FLAGS_log_filename = google::ProgramInvocationShortName();
111 if (FLAGS_redirect_stdout_stderr && !TestInfo::is_test()) {
114 string info_log_path, error_log_path;
119 LOG(INFO) <<
"stdout will be logged to this file.";
120 LOG(ERROR) <<
"stderr will be logged to this file.";
124 cout <<
"Redirecting stdout to " << info_log_path << endl;
125 cerr <<
"Redirecting stderr to " << error_log_path << endl;
128 if (freopen(info_log_path.c_str(),
"a", stdout) == NULL) {
129 cout <<
"Could not redirect stdout: " <<
GetStrErrMsg();
131 if (freopen(error_log_path.c_str(),
"a", stderr) == NULL) {
132 cerr <<
"Could not redirect stderr: " <<
GetStrErrMsg();
136 logging_initialized =
true;
141 ss << FLAGS_log_dir <<
"/" << FLAGS_log_filename <<
"."
142 << google::GetLogSeverityName(severity);
143 *filename = ss.str();
151 google::ShutdownGoogleLogging();
155 LOG(INFO) <<
"Flags (see also /varz are on debug webserver):" << endl
156 << google::CommandlineFlagsIntoString();
161 typedef map<time_t, string> LogFileMap;
163 if (max_log_files <= 1)
return;
165 for (
int severity = 0; severity < google::NUM_SEVERITIES; ++severity) {
168 string fname = strings::Substitute(
"$0/$1.*.$2*", FLAGS_log_dir, FLAGS_log_filename,
169 google::GetLogSeverityName(severity));
171 LogFileMap log_file_mtime;
173 glob(fname.c_str(), GLOB_TILDE, NULL, &result);
174 for (
size_t i = 0; i < result.gl_pathc; ++i) {
176 struct stat stat_val;
177 if (stat(result.gl_pathv[i], &stat_val) != 0) {
178 LOG(ERROR) <<
"Could not read last-modified-timestamp for log file "
179 << result.gl_pathv[i] <<
", will not delete (error was: "
180 << strerror(errno) <<
")";
183 log_file_mtime[stat_val.st_mtime] = result.gl_pathv[i];
189 if (log_file_mtime.size() <= max_log_files)
return;
190 int files_to_delete = log_file_mtime.size() - max_log_files;
191 DCHECK_GT(files_to_delete, 0);
192 BOOST_FOREACH(
const LogFileMap::reference val, log_file_mtime) {
193 if (unlink(val.second.c_str()) == 0) {
194 LOG(INFO) <<
"Old log file deleted during log rotation: " << val.second;
196 LOG(ERROR) <<
"Failed to delete old log file: "
197 << val.second <<
"(error was: " << strerror(errno) <<
")";
199 if (--files_to_delete == 0)
break;
void Redact(string *value, bool *changed)
void GetFullLogFilename(google::LogSeverity severity, std::string *filename)
void InitGoogleLoggingSafe(const char *arg)
void CheckAndRotateLogFiles(int max_log_files)
DEFINE_bool(redirect_stdout_stderr, true,"If true, redirects stdout/stderr to INFO/ERROR log.")
void LogCommandLineFlags()
Writes all command-line flags to the log at level INFO.
DECLARE_string(redaction_rules_file)
DEFINE_string(log_filename,"","Prefix of log filename - ""full path is <log_dir>/<log_filename>.[INFO|WARN|ERROR|FATAL]")