Marat Dukhan | 547fa36 | 2017-03-03 02:47:26 -0500 | [diff] [blame] | 1 | #include <stdarg.h> |
Marat Dukhan | cf70aee | 2018-03-24 23:21:02 -0700 | [diff] [blame] | 2 | #include <stdlib.h> |
Marat Dukhan | 547fa36 | 2017-03-03 02:47:26 -0500 | [diff] [blame] | 3 | #include <stdio.h> |
Marat Dukhan | b2fc4ab | 2018-02-19 22:43:26 -0800 | [diff] [blame] | 4 | #ifndef _WIN32 |
| 5 | #include <unistd.h> |
| 6 | #endif |
Marat Dukhan | cf70aee | 2018-03-24 23:21:02 -0700 | [diff] [blame] | 7 | #ifdef __ANDROID__ |
| 8 | #include <android/log.h> |
| 9 | #define CPUINFO_LOG_TAG "cpuinfo" |
| 10 | #endif |
Marat Dukhan | 547fa36 | 2017-03-03 02:47:26 -0500 | [diff] [blame] | 11 | |
| 12 | #include <log.h> |
| 13 | |
Marat Dukhan | cf70aee | 2018-03-24 23:21:02 -0700 | [diff] [blame] | 14 | #ifndef CPUINFO_LOG_TO_STDIO |
| 15 | #ifdef __ANDROID__ |
| 16 | #define CPUINFO_LOG_TO_STDIO 0 |
| 17 | #else |
| 18 | #define CPUINFO_LOG_TO_STDIO 1 |
| 19 | #endif |
| 20 | #endif |
| 21 | |
| 22 | void cpuinfo_log_fatal(const char* format, ...) { |
| 23 | va_list args; |
| 24 | va_start(args, format); |
| 25 | |
| 26 | #if defined(__ANDROID__) && !CPUINFO_LOG_TO_STDIO |
| 27 | __android_log_vprint(ANDROID_LOG_FATAL, CPUINFO_LOG_TAG, format, args); |
| 28 | #elif defined(__ANDROID__) || defined(_WIN32) |
| 29 | fprintf(stderr, "Fatal error: "); |
| 30 | vfprintf(stderr, format, args); |
| 31 | fprintf(stderr, "\n"); |
| 32 | fflush(stderr); |
| 33 | #else |
| 34 | dprintf(STDERR_FILENO, "Error: "); |
| 35 | vdprintf(STDERR_FILENO, format, args); |
| 36 | dprintf(STDERR_FILENO, "\n"); |
| 37 | #endif |
| 38 | |
| 39 | va_end(args); |
| 40 | abort(); |
| 41 | } |
| 42 | |
Marat Dukhan | 547fa36 | 2017-03-03 02:47:26 -0500 | [diff] [blame] | 43 | #if CPUINFO_LOG_LEVEL >= CPUINFO_LOG_ERROR |
| 44 | void cpuinfo_log_error(const char* format, ...) { |
| 45 | va_list args; |
| 46 | va_start(args, format); |
| 47 | |
Marat Dukhan | cf70aee | 2018-03-24 23:21:02 -0700 | [diff] [blame] | 48 | #if defined(__ANDROID__) && !CPUINFO_LOG_TO_STDIO |
| 49 | __android_log_vprint(ANDROID_LOG_ERROR, CPUINFO_LOG_TAG, format, args); |
| 50 | #elif defined(__ANDROID__) || defined(_WIN32) |
Marat Dukhan | 59d3071 | 2017-05-08 04:43:52 -0400 | [diff] [blame] | 51 | fprintf(stderr, "Error: "); |
| 52 | vfprintf(stderr, format, args); |
| 53 | fprintf(stderr, "\n"); |
| 54 | fflush(stderr); |
| 55 | #else |
| 56 | dprintf(STDERR_FILENO, "Error: "); |
| 57 | vdprintf(STDERR_FILENO, format, args); |
| 58 | dprintf(STDERR_FILENO, "\n"); |
| 59 | #endif |
Marat Dukhan | 547fa36 | 2017-03-03 02:47:26 -0500 | [diff] [blame] | 60 | |
| 61 | va_end(args); |
| 62 | } |
| 63 | #endif |
| 64 | |
| 65 | #if CPUINFO_LOG_LEVEL >= CPUINFO_LOG_WARNING |
| 66 | void cpuinfo_log_warning(const char* format, ...) { |
| 67 | va_list args; |
| 68 | va_start(args, format); |
| 69 | |
Marat Dukhan | cf70aee | 2018-03-24 23:21:02 -0700 | [diff] [blame] | 70 | #if defined(__ANDROID__) && !CPUINFO_LOG_TO_STDIO |
| 71 | __android_log_vprint(ANDROID_LOG_WARN, CPUINFO_LOG_TAG, format, args); |
| 72 | #elif defined(__ANDROID__) || defined(_WIN32) |
Marat Dukhan | 59d3071 | 2017-05-08 04:43:52 -0400 | [diff] [blame] | 73 | fprintf(stderr, "Warning: "); |
| 74 | vfprintf(stderr, format, args); |
| 75 | fprintf(stderr, "\n"); |
| 76 | fflush(stderr); |
| 77 | #else |
| 78 | dprintf(STDERR_FILENO, "Warning: "); |
| 79 | vdprintf(STDERR_FILENO, format, args); |
| 80 | dprintf(STDERR_FILENO, "\n"); |
| 81 | #endif |
Marat Dukhan | 547fa36 | 2017-03-03 02:47:26 -0500 | [diff] [blame] | 82 | |
| 83 | va_end(args); |
| 84 | } |
| 85 | #endif |
| 86 | |
| 87 | #if CPUINFO_LOG_LEVEL >= CPUINFO_LOG_INFO |
| 88 | void cpuinfo_log_info(const char* format, ...) { |
| 89 | va_list args; |
| 90 | va_start(args, format); |
| 91 | |
Marat Dukhan | cf70aee | 2018-03-24 23:21:02 -0700 | [diff] [blame] | 92 | #if defined(__ANDROID__) && !CPUINFO_LOG_TO_STDIO |
| 93 | __android_log_vprint(ANDROID_LOG_INFO, CPUINFO_LOG_TAG, format, args); |
| 94 | #elif defined(__ANDROID__) || defined(_WIN32) |
Marat Dukhan | a8fb3dd | 2017-08-09 13:49:39 -0700 | [diff] [blame] | 95 | printf("Note: "); |
Marat Dukhan | 59d3071 | 2017-05-08 04:43:52 -0400 | [diff] [blame] | 96 | vprintf(format, args); |
| 97 | printf("\n"); |
| 98 | fflush(stdout); |
| 99 | #else |
| 100 | vdprintf(STDOUT_FILENO, format, args); |
| 101 | dprintf(STDOUT_FILENO, "\n"); |
| 102 | #endif |
Marat Dukhan | 547fa36 | 2017-03-03 02:47:26 -0500 | [diff] [blame] | 103 | |
| 104 | va_end(args); |
| 105 | } |
| 106 | #endif |
| 107 | |
| 108 | #if CPUINFO_LOG_LEVEL >= CPUINFO_LOG_DEBUG |
| 109 | void cpuinfo_log_debug(const char* format, ...) { |
| 110 | va_list args; |
| 111 | va_start(args, format); |
| 112 | |
Marat Dukhan | cf70aee | 2018-03-24 23:21:02 -0700 | [diff] [blame] | 113 | #if defined(__ANDROID__) && !CPUINFO_LOG_TO_STDIO |
| 114 | __android_log_vprint(ANDROID_LOG_DEBUG, CPUINFO_LOG_TAG, format, args); |
| 115 | #elif defined(__ANDROID__) || defined(_WIN32) |
Marat Dukhan | a8fb3dd | 2017-08-09 13:49:39 -0700 | [diff] [blame] | 116 | printf("Debug: "); |
Marat Dukhan | 59d3071 | 2017-05-08 04:43:52 -0400 | [diff] [blame] | 117 | vprintf(format, args); |
| 118 | printf("\n"); |
| 119 | fflush(stdout); |
| 120 | #else |
| 121 | vdprintf(STDOUT_FILENO, format, args); |
| 122 | dprintf(STDOUT_FILENO, "\n"); |
| 123 | #endif |
Marat Dukhan | 547fa36 | 2017-03-03 02:47:26 -0500 | [diff] [blame] | 124 | |
| 125 | va_end(args); |
| 126 | } |
| 127 | #endif |