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.
15 #ifndef STRESSAPPTEST_LOGGER_H_
16 #define STRESSAPPTEST_LOGGER_H_
24 // This file must work with autoconf on its public version,
25 // so these includes are correct.
28 // Attempts to log additional lines will block when the queue reaches this size.
29 // Due to how the logging thread works, up to twice this many log lines may be
30 // outstanding at any point.
31 static const size_t kMaxQueueSize = 250;
34 // This is only for use by the Logger class, do not use it elsewhere!
36 // All Logger assertions should use this macro instead of sat_assert().
38 // This is like sat_assert() from sattypes.h, but whereas sat_assert() tries to
39 // log the assertion after printing it to stderr, this only prints it to stderr.
40 // Logging from within the wrong part of the logger would trigger a deadlock,
41 // and even in places where it wouldn't there's a very good chance that the
42 // logger is in no condition to handle new log lines.
43 #define LOGGER_ASSERT(x) \
46 fprintf(stderr, "Assertion failed at %s:%d\n", __FILE__, __LINE__);\
52 // This class handles logging in SAT. It is a singleton accessed via
55 // By default log lines are written in the calling thread. Call StartThread()
56 // to launch a dedicated thread for the writes.
59 // Returns a pointer to the single global Logger instance. Will not return
61 static Logger *GlobalLogger();
63 // Lines with a priority numerically greater than this will not be logged.
64 // May not be called while multiple threads are running.
65 virtual void SetVerbosity(int verbosity) {
66 verbosity_ = verbosity;
69 // Sets a file to log to, in addition to stdout. May not be called while
70 // multiple threads are running.
73 // log_fd: The file descriptor to write to. Will not be closed by this
75 virtual void SetLogFd(int log_fd) {
76 LOGGER_ASSERT(log_fd >= 0);
80 // Set output to be written to stdout only. This is the default mode. May
81 // not be called while multiple threads are running.
82 virtual void SetStdoutOnly() {
86 // Enable or disable logging of timestamps.
87 void SetTimestampLogging(bool log_ts_enabled) {
88 log_timestamps_ = log_ts_enabled;
91 // Logs a line, with a vprintf(3)-like interface. This will block on writing
92 // the line to stdout/disk iff the dedicated logging thread is not running.
93 // This will block on adding the line to the queue if doing so would exceed
97 // priority: If this is numerically greater than the verbosity, the line
98 // will not be logged.
99 // format: see vprintf(3)
100 // args: see vprintf(3)
101 void VLogF(int priority, const char *format, va_list args);
103 // Starts the dedicated logging thread. May not be called while multiple
104 // threads are already running.
107 // Stops the dedicated logging thread. May only be called when the logging
108 // thread is the only other thread running. Any queued lines will be logged
109 // before this returns. Waits for the thread to finish before returning.
119 // line: Must be non-NULL. This function takes ownership of it.
120 void QueueLogLine(string *line);
123 // line: Must be non-NULL. This function takes ownership of it.
124 void WriteAndDeleteLogLine(string *line);
126 // Callback for pthread_create(3).
127 static void *StartRoutine(void *ptr);
129 // Processes the log queue.
135 bool thread_running_;
136 bool log_timestamps_;
137 vector<string*> queued_lines_;
138 // This doubles as a mutex for log_fd_ when the logging thread is not running.
139 pthread_mutex_t queued_lines_mutex_;
140 // Lets the logging thread know that the queue is no longer empty.
141 pthread_cond_t queued_lines_cond_;
142 // Lets the threads blocked on the queue having reached kMaxQueueSize know
143 // that the queue has been emptied.
144 pthread_cond_t full_queue_cond_;
146 DISALLOW_COPY_AND_ASSIGN(Logger);
149 #endif // STRESSAPPTEST_LOGGER_H_