Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 // Logging and checks. | |
| 6 // | |
| 7 // Log messages to stdout. LOG() prints normal user messages, VLOG) | |
|
rmcilroy
2014/06/02 15:16:35
VLOG()
simonb (inactive)
2014/06/04 16:40:35
Done.
| |
| 8 // is verbose, for tracing and debugging. SetVerbose() enables/disables | |
| 9 // VLOG() output. | |
| 10 // | |
| 11 // LOG() and VLOG() are printf-like, and arguments are checked by gcc. | |
| 12 // LOG_IF() and VLOG_IF() call LOG/VLOG if their predicate is true. | |
| 13 // CHECK() aborts if its predicate is false. NOTREACHED() always aborts. | |
| 14 // | |
| 15 | |
| 16 #ifndef RELOCATION_PACKER_DEBUG_H | |
| 17 #define RELOCATION_PACKER_DEBUG_H | |
| 18 | |
| 19 #undef NDEBUG | |
|
rmcilroy
2014/06/02 15:16:35
I don't think you should be undef'ing NDEBUG in a
simonb (inactive)
2014/06/04 16:40:35
Done.
| |
| 20 #include <assert.h> | |
| 21 #include <stdarg.h> | |
| 22 #include <string.h> | |
| 23 | |
| 24 namespace relocation_packer { | |
| 25 | |
| 26 // If gcc, define PRINTF_ATTRIBUTE so that gcc checks Log() as printf-like. | |
| 27 #if defined(__GNUC__) && (__GNUC__ >= 3) | |
| 28 #define PRINTF_ATTRIBUTE(string_index, first_to_check) \ | |
| 29 __attribute__((__format__(__printf__, string_index, first_to_check))) | |
| 30 #else | |
| 31 #define PRINTF_ATTRIBUTE(string_index, first_to_check) | |
| 32 #endif | |
| 33 | |
| 34 // Logging and checking macros. | |
| 35 #define LOG(...) ::relocation_packer::Logger::Log(__VA_ARGS__) | |
| 36 #define VLOG(...) ::relocation_packer::Logger::VLog(__VA_ARGS__) | |
| 37 #define LOG_IF(cond, ...) \ | |
| 38 do { \ | |
| 39 if ((cond)) \ | |
| 40 LOG(__VA_ARGS__); \ | |
| 41 } while (0) | |
| 42 #define VLOG_IF(cond, ...) \ | |
| 43 do { \ | |
| 44 if ((cond)) \ | |
| 45 VLOG(__VA_ARGS__); \ | |
| 46 } while (0) | |
| 47 | |
| 48 #define CHECK(expr) assert((expr)) | |
| 49 #define NOTREACHED(_) assert(false) | |
| 50 | |
| 51 class Logger { | |
| 52 public: | |
| 53 // Log and verbose log to stdout. | |
| 54 // |format| is a printf format string. | |
| 55 static void Log(const char* format, ...) PRINTF_ATTRIBUTE(1, 2); | |
| 56 static void VLog(const char* format, ...) PRINTF_ATTRIBUTE(1, 2); | |
| 57 | |
| 58 // Set verbose mode. | |
| 59 // |flag| is true to enable verbose logging, false to disable it. | |
| 60 static void SetVerbose(bool flag); | |
| 61 | |
| 62 private: | |
| 63 Logger() { ::memset(this, 0, sizeof(*this)); } | |
|
rmcilroy
2014/06/02 15:16:35
no need for "::" global namespace prefix (througho
simonb (inactive)
2014/06/04 16:40:35
Done.
| |
| 64 ~Logger() {} | |
| 65 | |
| 66 // Implementation of log to stdout. | |
| 67 // |format| is a printf format string. | |
| 68 // |args| is a varargs list of printf arguments. | |
| 69 void Log(const char* format, va_list args); | |
| 70 | |
| 71 // If set, VLOG is enabled, otherwise it is a no-op. | |
| 72 bool is_verbose_; | |
| 73 | |
| 74 // Singleton support. | |
| 75 static Logger* GetInstance(); | |
| 76 static Logger* instance_; | |
| 77 }; | |
| 78 | |
| 79 } // namespace relocation_packer | |
| 80 | |
| 81 #endif // RELOCATION_PACKER_DEBUG_H | |
| OLD | NEW |