Home | History | Annotate | Download | only in src
      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
      3 // found in the LICENSE file.
      4 
      5 #include "debug.h"
      6 
      7 #include <stdlib.h>
      8 #include <iostream>
      9 #include <string>
     10 
     11 namespace relocation_packer {
     12 
     13 // Construct a new message logger.  Prints if level is less than or equal to
     14 // the level set with SetVerbose() and predicate is true.
     15 Logger::Logger(Severity severity, int level, bool predicate) {
     16   severity_ = severity;
     17   level_ = level;
     18   predicate_ = predicate;
     19 }
     20 
     21 // On destruction, flush and print the strings accumulated.  Abort if FATAL.
     22 Logger::~Logger() {
     23   if (predicate_) {
     24     if (level_ <= max_level_) {
     25       std::ostream* log = severity_ == INFO ? info_stream_ : error_stream_;
     26       std::string tag;
     27       switch (severity_) {
     28         case INFO: tag = "INFO"; break;
     29         case WARNING: tag = "WARNING"; break;
     30         case ERROR: tag = "ERROR"; break;
     31         case FATAL: tag = "FATAL"; break;
     32       }
     33       stream_.flush();
     34       *log << tag << ": " << stream_.str() << std::endl;
     35     }
     36     if (severity_ == FATAL)
     37       abort();
     38   }
     39 }
     40 
     41 // Reset to initial state.
     42 void Logger::Reset() {
     43   max_level_ = -1;
     44   info_stream_ = &std::cout;
     45   error_stream_ = &std::cerr;
     46 }
     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 
     55 }  // namespace relocation_packer
     56