| 197 | } |
| 198 | |
| 199 | void impala::InitGoogleLoggingSafe(const char* arg) { |
| 200 | lock_guard<mutex> logging_lock(logging_mutex); |
| 201 | if (logging_initialized) return; |
| 202 | if (!FLAGS_log_filename.empty()) { |
| 203 | for (int severity = google::INFO; severity <= google::FATAL; ++severity) { |
| 204 | google::SetLogSymlink(severity, FLAGS_log_filename.c_str()); |
| 205 | } |
| 206 | } |
| 207 | |
| 208 | // This forces our logging to use /tmp rather than looking for a |
| 209 | // temporary directory if none is specified. This is done so that we |
| 210 | // can reliably construct the log file name without duplicating the |
| 211 | // complex logic that glog uses to guess at a temporary dir. |
| 212 | if (FLAGS_log_dir.empty()) { |
| 213 | FLAGS_log_dir = "/tmp"; |
| 214 | } |
| 215 | |
| 216 | // Don't double log to stderr on any threshold. |
| 217 | FLAGS_stderrthreshold = google::FATAL + 1; |
| 218 | |
| 219 | if (RedirectStdoutStderr()) { |
| 220 | // We will be redirecting stdout/stderr to INFO/LOG so override any glog settings |
| 221 | // that log to stdout/stderr... |
| 222 | FLAGS_logtostderr = false; |
| 223 | FLAGS_alsologtostderr = false; |
| 224 | } |
| 225 | |
| 226 | if (!FLAGS_logtostderr) { |
| 227 | // Verify that a log file can be created in log_dir by creating a tmp file. |
| 228 | stringstream ss; |
| 229 | random_generator uuid_generator; |
| 230 | ss << FLAGS_log_dir << "/" << "impala_test_log." << uuid_generator(); |
| 231 | const string file_name = ss.str(); |
| 232 | ofstream test_file(file_name.c_str()); |
| 233 | if (!test_file.is_open()) { |
| 234 | stringstream error_msg; |
| 235 | error_msg << "Could not open file in log_dir " << FLAGS_log_dir; |
| 236 | perror(error_msg.str().c_str()); |
| 237 | // Unlock the mutex before exiting the program to avoid mutex d'tor assert. |
| 238 | logging_mutex.unlock(); |
| 239 | exit(1); |
| 240 | } |
| 241 | remove(file_name.c_str()); |
| 242 | } |
| 243 | |
| 244 | google::InitGoogleLogging(arg); |
| 245 | google::InstallLogMessageListenerFunction(MessageListener); |
| 246 | |
| 247 | // Needs to be done after InitGoogleLogging |
| 248 | if (FLAGS_log_filename.empty()) { |
| 249 | FLAGS_log_filename = google::ProgramInvocationShortName(); |
| 250 | } |
| 251 | |
| 252 | if (RedirectStdoutStderr()) { |
| 253 | // The log files are created on first use, log something to each and flush before |
| 254 | // redirecting. |
| 255 | LOG(INFO) << "stdout will be logged to this file."; |
| 256 | LOG(ERROR) << "stderr will be logged to this file."; |