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 "util/detect_os.h"
33 #include "util/log.h"
34 
35 #ifdef ANDROID
36 static inline android_LogPriority
level_to_android(enum mesa_log_level l)37 level_to_android(enum mesa_log_level l)
38 {
39    switch (l) {
40    case MESA_LOG_ERROR: return ANDROID_LOG_ERROR;
41    case MESA_LOG_WARN: return ANDROID_LOG_WARN;
42    case MESA_LOG_INFO: return ANDROID_LOG_INFO;
43    case MESA_LOG_DEBUG: return ANDROID_LOG_DEBUG;
44    }
45 
46    unreachable("bad mesa_log_level");
47 }
48 #endif
49 
50 #ifndef ANDROID
51 static inline const char *
level_to_str(enum mesa_log_level l)52 level_to_str(enum mesa_log_level l)
53 {
54    switch (l) {
55    case MESA_LOG_ERROR: return "error";
56    case MESA_LOG_WARN: return "warning";
57    case MESA_LOG_INFO: return "info";
58    case MESA_LOG_DEBUG: return "debug";
59    }
60 
61    unreachable("bad mesa_log_level");
62 }
63 #endif
64 
65 void
mesa_log(enum mesa_log_level level,const char * tag,const char * format,...)66 mesa_log(enum mesa_log_level level, const char *tag, const char *format, ...)
67 {
68    va_list va;
69 
70    va_start(va, format);
71    mesa_log_v(level, tag, format, va);
72    va_end(va);
73 }
74 
75 void
mesa_log_v(enum mesa_log_level level,const char * tag,const char * format,va_list va)76 mesa_log_v(enum mesa_log_level level, const char *tag, const char *format,
77             va_list va)
78 {
79 #ifdef ANDROID
80    __android_log_vprint(level_to_android(level), tag, format, va);
81 #else
82 #if !DETECT_OS_WINDOWS
83    flockfile(stderr);
84 #endif
85    fprintf(stderr, "%s: %s: ", tag, level_to_str(level));
86    vfprintf(stderr, format, va);
87    fprintf(stderr, "\n");
88 #if !DETECT_OS_WINDOWS
89    funlockfile(stderr);
90 #endif
91 #endif
92 }
93