33 namespace logging_internal {
39 static std::function<void()> s_break_handler_;
41 static const std::function<void()> GetBreakHandler() {
42 static std::atomic<int> has_been_initialized_(0);
43 if (has_been_initialized_.exchange(1) == 0) {
46 return s_break_handler_;
54 static std::set<std::string>& GetSingleLoggerMessageSet() {
56 return *logged_messages;
67 std::function<void()> break_handler = GetBreakHandler();
74 static bool HasLoggedMessageSince(
const char* file_name,
int line_number,
77 const port::Timer::steady_clock::time_point now =
78 port::Timer::steady_clock::now();
79 const port::Timer::steady_clock::time_point when =
80 now - std::chrono::duration_cast<port::Timer::steady_clock::duration>(
81 std::chrono::duration<float>(past_seconds));
82 static std::map<std::string, port::Timer::steady_clock::time_point>
84 std::stringstream
str;
85 str << file_name <<
":" << line_number;
86 const std::string key = str.str();
89 const auto& insert_pair = logged_messages.insert(std::make_pair(key, now));
90 if (insert_pair.second) {
94 if (insert_pair.first->second >= when) {
100 insert_pair.first->second = now;
107 : severity_(severity) {
108 stream_ <<
"[" << filename <<
":" << line_number <<
"] ";
120 BreakOnFatalSeverity(severity_);
124 BreakOnFatalSeverity(severity);
128 static std::ostream null_stream(NULL);
135 const char* expr_string) {
136 return std::string(check_string) +
" failed: expression='" + expr_string +
140 static std::ostream& GetNullStream() {
141 static std::ostream null_stream(NULL);
147 : logger_(HasLoggedMessageAt(file_name, line_number)
149 : new
Logger(file_name, line_number, severity)) {}
155 GetSingleLoggerMessageSet().clear();
159 return logger_ ? logger_->GetStream() : GetNullStream();
162 bool SingleLogger::HasLoggedMessageAt(
const char* file_name,
int line_number) {
164 std::set<std::string>& logged_messages = GetSingleLoggerMessageSet();
165 std::stringstream
str;
166 str << file_name <<
":" << line_number;
167 return !(logged_messages.insert(str.str()).second);
172 : logger_(HasLoggedMessageSince(file_name, line_number, seconds)
174 : new
Logger(file_name, line_number, severity)) {}
179 return logger_ ? logger_->GetStream() : GetNullStream();
189 logging_internal::s_writer_ = w;
193 return logging_internal::s_writer_ ? logging_internal::s_writer_
202 return default_writer;
207 logging_internal::GetBreakHandler();
208 logging_internal::s_break_handler_ = break_handler;
Logger(const char *file_name, int line_number, port::LogSeverity severity)
Abstract class which can be overridden to integrate Ion logging with other logging systems...
#define ION_DECLARE_SAFE_STATIC_POINTER(type, variable)
Declare a static non-array pointer and calls a default constructor.
#define ION_DECLARE_SAFE_STATIC_POINTER_WITH_CONSTRUCTOR(type, variable, constructor)
Declare a static non-array pointer and calls a non-default constructor.
static void ClearMessages()
Clears the set of messages that have been logged.
std::ostream & GetStream()
Returns the stream to which output is sent.
static const std::string CheckMessage(const char *check_string, const char *expr_string)
Returns a message that can be used in CHECK or DCHECK output.
port::LogEntryWriter * GetDefaultLogEntryWriter()
Returns the log-writer that messages will be logged to if if another is not explicitly specified via ...
std::ostream & GetStream()
Return the stream to which output is sent (or accumulated).
ThrottledLogger(const char *file_name, int line_number, port::LogSeverity severity, float seconds)
NullLogger()
Constructs a NullLogger that does nothing.
A LockGuard locks a mutex when created, and unlocks it when destroyed.
port::LogEntryWriter * GetLogEntryWriter()
Returns the log-writer that messages are currently logged to.
std::ostream & GetStream()
Return the stream to which output is sent (or accumulated).
SingleLogger(const char *file_name, int line_number, port::LogSeverity severity)
virtual void Write(LogSeverity severity, const std::string &message)=0
Copyright 2016 Google Inc.
std::ostream & GetStream()
Return the stream to which output is sent (or accumulated).
void BreakOrAbort()
Calls Break() if running in a debugger, abort() otherwise.
void RestoreDefaultBreakHandler()
Restores the default break handler (port::BreakAndAbort).
void SetLogEntryWriter(port::LogEntryWriter *w)
Public functions.
ION_API LogEntryWriter * CreateDefaultLogEntryWriter()
Instantiate a new LogEntryWriter of the default type for the current platform...
A Mutex is used to ensure that only one thread or process can access a block of code at one time...
void SetBreakHandler(const std::function< void()> &break_handler)
Sets a custom break handler that gets invoked by Logger::CheckMessage.
This class is used for regular logging.