1 /* 2 * Copyright 2017 Google, Inc. 3 * 4 * Permission is hereby granted, free of charge, to any person obtaining a 5 * copy of this software and associated documentation files (the "Software"), 6 * to deal in the Software without restriction, including without limitation 7 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 8 * and/or sell copies of the Software, and to permit persons to whom the 9 * Software is furnished to do so, subject to the following conditions: 10 * 11 * The above copyright notice and this permission notice (including the next 12 * paragraph) shall be included in all copies or substantial portions of the 13 * Software. 14 * 15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 21 * IN THE SOFTWARE. 22 */ 23 24 #include <stdarg.h> 25 26 #ifdef ANDROID 27 #include <android/log.h> 28 #else 29 #include <stdio.h> 30 #endif 31 32 #include "intel_log.h" 33 34 #ifdef ANDROID 35 static inline android_LogPriority 36 level_to_android(enum intel_log_level l) 37 { 38 switch (l) { 39 case INTEL_LOG_ERROR: return ANDROID_LOG_ERROR; 40 case INTEL_LOG_WARN: return ANDROID_LOG_WARN; 41 case INTEL_LOG_INFO: return ANDROID_LOG_INFO; 42 case INTEL_LOG_DEBUG: return ANDROID_LOG_DEBUG; 43 } 44 45 unreachable("bad intel_log_level"); 46 } 47 #endif 48 49 #ifndef ANDROID 50 static inline const char * 51 level_to_str(enum intel_log_level l) 52 { 53 switch (l) { 54 case INTEL_LOG_ERROR: return "error"; 55 case INTEL_LOG_WARN: return "warning"; 56 case INTEL_LOG_INFO: return "info"; 57 case INTEL_LOG_DEBUG: return "debug"; 58 } 59 60 unreachable("bad intel_log_level"); 61 } 62 #endif 63 64 void 65 intel_log(enum intel_log_level level, const char *tag, const char *format, ...) 66 { 67 va_list va; 68 69 va_start(va, format); 70 intel_log_v(level, tag, format, va); 71 va_end(va); 72 } 73 74 void 75 intel_log_v(enum intel_log_level level, const char *tag, const char *format, 76 va_list va) 77 { 78 #ifdef ANDROID 79 __android_log_vprint(level_to_android(level), tag, format, va); 80 #else 81 flockfile(stderr); 82 fprintf(stderr, "%s: %s: ", tag, level_to_str(level)); 83 vfprintf(stderr, format, va); 84 fprintf(stderr, "\n"); 85 funlockfile(stderr); 86 #endif 87 } 88