OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2013 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 "crazy_linker_debug.h" |
| 6 |
| 7 #include <errno.h> |
| 8 #include <string.h> |
| 9 |
| 10 #ifdef __ANDROID__ |
| 11 #include <android/log.h> |
| 12 #endif |
| 13 #include <stdarg.h> |
| 14 #include <stdio.h> |
| 15 |
| 16 namespace crazy { |
| 17 |
| 18 #if CRAZY_DEBUG |
| 19 |
| 20 namespace { |
| 21 |
| 22 void LogArgs(const char* fmt, va_list args, bool print_error, int error) { |
| 23 char buffer[4096]; |
| 24 int ret; |
| 25 |
| 26 ret = vsnprintf(buffer, sizeof(buffer), fmt, args); |
| 27 if (ret >= static_cast<int>(sizeof(buffer))) |
| 28 ret = static_cast<int>(sizeof(buffer)) - 1; |
| 29 |
| 30 if (print_error) { |
| 31 strlcat(buffer, ": ", sizeof(buffer)); |
| 32 strlcat(buffer, strerror(error), sizeof(buffer)); |
| 33 } |
| 34 |
| 35 // First, send to stderr. |
| 36 fprintf(stderr, "%.*s", ret, buffer); |
| 37 |
| 38 #ifdef __ANDROID__ |
| 39 // Then to the Android log. |
| 40 __android_log_write(ANDROID_LOG_INFO, "crazy_linker", buffer); |
| 41 #endif |
| 42 } |
| 43 |
| 44 } // namespace |
| 45 |
| 46 void Log(const char* fmt, ...) { |
| 47 int old_errno = errno; |
| 48 va_list args; |
| 49 va_start(args, fmt); |
| 50 LogArgs(fmt, args, false, -1); |
| 51 va_end(args); |
| 52 errno = old_errno; |
| 53 } |
| 54 |
| 55 void LogErrno(const char* fmt, ...) { |
| 56 int old_errno = errno; |
| 57 va_list args; |
| 58 va_start(args, fmt); |
| 59 LogArgs(fmt, args, true, old_errno); |
| 60 va_end(args); |
| 61 errno = old_errno; |
| 62 } |
| 63 |
| 64 #endif // CRAZY_DEBUG |
| 65 |
| 66 } // namespace crazy |
OLD | NEW |