Impala
Impalaistheopensource,nativeanalyticdatabaseforApacheHadoop.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
simple-logger.cc
Go to the documentation of this file.
1 // Copyright 2012 Cloudera Inc.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include "util/simple-logger.h"
16 
17 #include <boost/date_time/posix_time/posix_time.hpp>
18 #include <boost/date_time/posix_time/posix_time_types.hpp>
19 #include <boost/filesystem.hpp>
20 
21 #include "common/names.h"
22 
23 using boost::filesystem::create_directory;
24 using boost::filesystem::exists;
25 using boost::filesystem::is_directory;
26 using boost::posix_time::microsec_clock;
27 using boost::posix_time::ptime;
28 using boost::posix_time::time_from_string;
29 using namespace impala;
30 
31 const ptime EPOCH = time_from_string("1970-01-01 00:00:00.000");
32 
33 Status InitLoggingDir(const string& log_dir) {
34  if (!exists(log_dir)) {
35  LOG(INFO) << "Log directory does not exist, creating: " << log_dir;
36  try {
37  create_directory(log_dir);
38  } catch (const std::exception& e) { // Explicit std:: to distinguish from boost::
39  LOG(ERROR) << "Could not create log directory: "
40  << log_dir << ", " << e.what();
41  return Status("Failed to create log directory");
42  }
43  }
44 
45  if (!is_directory(log_dir)) {
46  LOG(ERROR) << "Log path is not a directory ("
47  << log_dir << ")";
48  return Status("Log path is not a directory");
49  }
50  return Status::OK;
51 }
52 
54  stringstream ss;
55  int64_t ms_since_epoch =
56  (microsec_clock::universal_time() - EPOCH).total_milliseconds();
57  ss << log_dir_ << "/" << log_file_name_prefix_ << ms_since_epoch;
58  log_file_name_ = ss.str();
59 }
60 
61 SimpleLogger::SimpleLogger(const string& log_dir, const string& log_file_name_prefix,
62  uint64_t max_entries_per_file)
63  : log_dir_(log_dir),
64  log_file_name_prefix_(log_file_name_prefix),
65  num_log_file_entries_(0),
66  max_entries_per_file_(max_entries_per_file) {
67 }
68 
70  // Check that Init hasn't already been called by verifying the log_file_name_ is still
71  // empty.
72  DCHECK(log_file_name_.empty());
76  LOG(INFO) << "Logging to: " << log_file_name_;
77  return Status::OK;
78 }
79 
80 Status SimpleLogger::AppendEntry(const std::string& entry) {
81  lock_guard<mutex> l(log_file_lock_);
86  }
87  if (!log_file_.is_open()) return Status("Log file is not open: " + log_file_name_);
88  // Not std::endl, since that causes an implicit flush
89  log_file_ << entry << "\n";
91  return Status::OK;
92 }
93 
95  lock_guard<mutex> l(log_file_lock_);
96  return FlushInternal();
97 }
98 
100  if (log_file_.is_open()) {
101  // flush() alone does not apparently fsync, but we actually want
102  // the results to become visible, hence the close / reopen
103  log_file_.flush();
104  log_file_.close();
105  }
106  log_file_.open(log_file_name_.c_str(), std::ios_base::app | std::ios_base::out);
107  if (!log_file_.is_open()) return Status("Could not open log file: " + log_file_name_);
108  return Status::OK;
109 }
std::ofstream log_file_
Log files are written to this stream.
Definition: simple-logger.h:66
boost::mutex log_file_lock_
Protects log_file_, num_log_file_entries_ and log_file_name_.
Definition: simple-logger.h:49
#define RETURN_IF_ERROR(stmt)
some generally useful macros
Definition: status.h:242
std::string log_file_name_
Current log file name.
Definition: simple-logger.h:69
const ptime EPOCH
uint64_t num_log_file_entries_
Definition: simple-logger.h:59
Status AppendEntry(const std::string &entry)
std::string log_dir_
Directory to log to.
Definition: simple-logger.h:52
static const Status OK
Definition: status.h:87
Status InitLoggingDir(const string &log_dir)
SimpleLogger(const std::string &log_dir_, const std::string &log_file_name_prefix_, uint64_t max_entries_per_file)
std::string log_file_name_prefix_
Prefix for all log files.
Definition: simple-logger.h:55
uint64_t max_entries_per_file_
Definition: simple-logger.h:63