Logging.h (2275B)
1 /* This Source Code Form is subject to the terms of the Mozilla Public 2 * License, v. 2.0. If a copy of the MPL was not distributed with this file, 3 * You can obtain one at http://mozilla.org/MPL/2.0/. */ 4 5 #ifndef Logging_h 6 #define Logging_h 7 8 #include <cstdlib> 9 #include "mozilla/Likely.h" 10 #include "mozilla/MacroArgs.h" 11 12 #ifdef ANDROID 13 # include <android/log.h> 14 # define LOG(...) \ 15 __android_log_print(ANDROID_LOG_INFO, "GeckoLinker", __VA_ARGS__) 16 # define WARN(...) \ 17 __android_log_print(ANDROID_LOG_WARN, "GeckoLinker", __VA_ARGS__) 18 # define ERROR(...) \ 19 __android_log_print(ANDROID_LOG_ERROR, "GeckoLinker", __VA_ARGS__) 20 #else 21 # include <cstdio> 22 23 /* Expand to 1 or m depending on whether there is one argument or more 24 * given. */ 25 # define MOZ_ONE_OR_MORE_ARGS_IMPL2(_1, _2, _3, _4, _5, _6, _7, _8, _9, N, \ 26 ...) \ 27 N 28 # define MOZ_ONE_OR_MORE_ARGS_IMPL(args) MOZ_ONE_OR_MORE_ARGS_IMPL2 args 29 # define MOZ_ONE_OR_MORE_ARGS(...) \ 30 MOZ_ONE_OR_MORE_ARGS_IMPL((__VA_ARGS__, m, m, m, m, m, m, m, m, 1, 0)) 31 32 # define MOZ_MACRO_GLUE(a, b) a b 33 34 /* Some magic to choose between LOG1 and LOGm depending on the number of 35 * arguments */ 36 # define MOZ_CHOOSE_LOG(...) \ 37 MOZ_MACRO_GLUE(MOZ_CONCAT(LOG, MOZ_ONE_OR_MORE_ARGS(__VA_ARGS__)), \ 38 (__VA_ARGS__)) 39 40 # define LOG1(format) fprintf(stderr, format "\n") 41 # define LOGm(format, ...) fprintf(stderr, format "\n", __VA_ARGS__) 42 # define LOG(...) MOZ_CHOOSE_LOG(__VA_ARGS__) 43 # define WARN(...) MOZ_CHOOSE_LOG("Warning: " __VA_ARGS__) 44 # define ERROR(...) MOZ_CHOOSE_LOG("Error: " __VA_ARGS__) 45 46 #endif 47 48 class Logging { 49 public: 50 static bool isVerbose() { return Singleton.verbose; } 51 52 private: 53 bool verbose; 54 55 public: 56 static void Init() { 57 const char* env = getenv("MOZ_DEBUG_LINKER"); 58 if (env && *env == '1') Singleton.verbose = true; 59 } 60 61 private: 62 static Logging Singleton; 63 }; 64 65 #define DEBUG_LOG(...) \ 66 do { \ 67 if (MOZ_UNLIKELY(Logging::isVerbose())) { \ 68 LOG(__VA_ARGS__); \ 69 } \ 70 } while (0) 71 72 #endif /* Logging_h */