1 // Copyright 2009 Google Inc. All Rights Reserved.
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
7 // http://www.apache.org/licenses/LICENSE-2.0
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.
25 // This file must work with autoconf on its public version,
26 // so these includes are correct.
30 Logger *Logger::GlobalLogger() {
35 void Logger::VLogF(int priority, const char *format, va_list args) {
36 if (priority > verbosity_) {
40 int length = vsnprintf(buffer, sizeof buffer, format, args);
41 if (static_cast<size_t>(length) >= sizeof buffer) {
42 length = sizeof buffer;
43 buffer[sizeof buffer - 1] = '\n';
45 QueueLogLine(new string(buffer, length));
48 void Logger::StartThread() {
49 LOGGER_ASSERT(!thread_running_);
50 thread_running_ = true;
51 LOGGER_ASSERT(0 == pthread_create(&thread_, NULL, &StartRoutine, this));
54 void Logger::StopThread() {
55 LOGGER_ASSERT(thread_running_);
56 thread_running_ = false;
57 LOGGER_ASSERT(0 == pthread_mutex_lock(&queued_lines_mutex_));
58 bool need_cond_signal = queued_lines_.empty();
59 queued_lines_.push_back(NULL);
60 LOGGER_ASSERT(0 == pthread_mutex_unlock(&queued_lines_mutex_));
61 if (need_cond_signal) {
62 LOGGER_ASSERT(0 == pthread_cond_signal(&queued_lines_cond_));
64 LOGGER_ASSERT(0 == pthread_join(thread_, NULL));
67 Logger::Logger() : verbosity_(20), log_fd_(-1), thread_running_(false) {
68 LOGGER_ASSERT(0 == pthread_mutex_init(&queued_lines_mutex_, NULL));
69 LOGGER_ASSERT(0 == pthread_cond_init(&queued_lines_cond_, NULL));
70 LOGGER_ASSERT(0 == pthread_cond_init(&full_queue_cond_, NULL));
74 LOGGER_ASSERT(0 == pthread_mutex_destroy(&queued_lines_mutex_));
75 LOGGER_ASSERT(0 == pthread_cond_destroy(&queued_lines_cond_));
76 LOGGER_ASSERT(0 == pthread_cond_destroy(&full_queue_cond_));
79 void Logger::QueueLogLine(string *line) {
80 LOGGER_ASSERT(line != NULL);
81 LOGGER_ASSERT(0 == pthread_mutex_lock(&queued_lines_mutex_));
82 if (thread_running_) {
83 if (queued_lines_.size() >= kMaxQueueSize) {
84 LOGGER_ASSERT(0 == pthread_cond_wait(&full_queue_cond_,
85 &queued_lines_mutex_));
87 if (queued_lines_.empty()) {
88 LOGGER_ASSERT(0 == pthread_cond_signal(&queued_lines_cond_));
90 queued_lines_.push_back(line);
92 WriteAndDeleteLogLine(line);
94 LOGGER_ASSERT(0 == pthread_mutex_unlock(&queued_lines_mutex_));
98 void WriteToFile(const string& line, int fd) {
99 LOGGER_ASSERT(write(fd, line.data(), line.size()) ==
100 static_cast<ssize_t>(line.size()));
104 void Logger::WriteAndDeleteLogLine(string *line) {
105 LOGGER_ASSERT(line != NULL);
107 WriteToFile(*line, log_fd_);
109 WriteToFile(*line, 1);
113 void *Logger::StartRoutine(void *ptr) {
114 Logger *self = static_cast<Logger*>(ptr);
119 void Logger::ThreadMain() {
120 vector<string*> local_queue;
121 LOGGER_ASSERT(0 == pthread_mutex_lock(&queued_lines_mutex_));
124 if (queued_lines_.empty()) {
125 LOGGER_ASSERT(0 == pthread_cond_wait(&queued_lines_cond_,
126 &queued_lines_mutex_));
130 // We move the log lines into a local queue so we can release the lock
131 // while writing them to disk, preventing other threads from blocking on
133 local_queue.swap(queued_lines_);
134 if (local_queue.size() >= kMaxQueueSize) {
135 LOGGER_ASSERT(0 == pthread_cond_broadcast(&full_queue_cond_));
138 // Unlock while we process our local queue.
139 LOGGER_ASSERT(0 == pthread_mutex_unlock(&queued_lines_mutex_));
140 for (vector<string*>::const_iterator it = local_queue.begin();
141 it != local_queue.end(); ++it) {
143 // NULL is guaranteed to be at the end.
146 WriteAndDeleteLogLine(*it);
149 // We must hold the lock at the start of each iteration of this for loop.
150 LOGGER_ASSERT(0 == pthread_mutex_lock(&queued_lines_mutex_));