Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(583)

Side by Side Diff: tools/relocation_packer/src/debug.cc

Issue 392653002: Upgrade logging to resemble base/logging.h. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Update for review feedback Created 6 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « tools/relocation_packer/src/debug.h ('k') | tools/relocation_packer/src/debug_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 "debug.h"
6 #include <stdio.h>
7 6
8 #include "debug.h" 7 #include <stdlib.h>
8 #include <iostream>
9 #include <string>
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
OLDNEW
« no previous file with comments | « tools/relocation_packer/src/debug.h ('k') | tools/relocation_packer/src/debug_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698