Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include <stdarg.h> | 5 #include <stdlib.h> |
| 6 #include <stdio.h> | 6 #include <iostream> |
| 7 #include <string> | |
| 7 | 8 |
| 8 #include "debug.h" | 9 #include "debug.h" |
|
rmcilroy
2014/07/16 11:38:48
nit - debug.h at the top
simonb (inactive)
2014/07/16 12:51:03
Done.
| |
| 9 | 10 |
| 10 namespace relocation_packer { | 11 namespace relocation_packer { |
| 11 | 12 |
| 12 Logger* Logger::instance_ = NULL; | 13 // Construct a new message logger. Prints if level is less than or equal to |
| 13 | 14 // the level set with SetVerbose() and predicate is true. |
| 14 Logger* Logger::GetInstance() { | 15 Logger::Logger(Severity severity, int level, bool predicate) { |
| 15 if (instance_ == NULL) | 16 severity_ = severity; |
| 16 instance_ = new Logger; | 17 level_ = level; |
| 17 return instance_; | 18 predicate_ = predicate; |
| 18 } | 19 } |
| 19 | 20 |
| 20 void Logger::Log(const char* format, va_list args) { | 21 // On destruction, flush and print the strings accumulated. Abort if FATAL. |
| 21 vfprintf(stdout, format, args); | 22 Logger::~Logger() { |
| 22 } | 23 if (predicate_) { |
| 23 | 24 if (level_ <= max_level_) { |
| 24 void Logger::Log(const char* format, ...) { | 25 std::ostream* log = severity_ == INFO ? info_stream_ : error_stream_; |
| 25 va_list args; | 26 std::string tag; |
| 26 va_start(args, format); | 27 switch (severity_) { |
| 27 GetInstance()->Log(format, args); | 28 case INFO: tag = "INFO"; break; |
| 28 va_end(args); | 29 case WARNING: tag = "WARNING"; break; |
| 29 } | 30 case ERROR: tag = "ERROR"; break; |
| 30 | 31 case FATAL: tag = "FATAL"; break; |
| 31 void Logger::VLog(const char* format, ...) { | 32 } |
| 32 if (GetInstance()->is_verbose_) { | 33 stream_.flush(); |
| 33 va_list args; | 34 *log << tag << ": " << stream_.str() << std::endl; |
| 34 va_start(args, format); | 35 } |
| 35 GetInstance()->Log(format, args); | 36 if (severity_ == FATAL) |
| 36 va_end(args); | 37 abort(); |
| 37 } | 38 } |
| 38 } | 39 } |
| 39 | 40 |
| 40 void Logger::SetVerbose(bool flag) { | 41 // Reset to initial state. |
| 41 GetInstance()->is_verbose_ = flag; | 42 void Logger::Reset() { |
| 43 max_level_ = -1; | |
| 44 info_stream_ = &std::cout; | |
| 45 error_stream_ = &std::cerr; | |
| 42 } | 46 } |
| 43 | 47 |
| 48 // Verbosity. Not thread-safe. | |
| 49 int Logger::max_level_ = -1; | |
| 50 | |
| 51 // Logging streams. Not thread-safe. | |
| 52 std::ostream* Logger::info_stream_ = &std::cout; | |
| 53 std::ostream* Logger::error_stream_ = &std::cerr; | |
| 54 | |
| 44 } // namespace relocation_packer | 55 } // namespace relocation_packer |
| OLD | NEW |