• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <stdlib.h>
33 #include <string.h>
34 #include "util/detect_os.h"
35 #include "util/log.h"
36 #include "util/ralloc.h"
37 
38 #ifdef ANDROID
39 static inline android_LogPriority
level_to_android(enum mesa_log_level l)40 level_to_android(enum mesa_log_level l)
41 {
42    switch (l) {
43    case MESA_LOG_ERROR: return ANDROID_LOG_ERROR;
44    case MESA_LOG_WARN: return ANDROID_LOG_WARN;
45    case MESA_LOG_INFO: return ANDROID_LOG_INFO;
46    case MESA_LOG_DEBUG: return ANDROID_LOG_DEBUG;
47    }
48 
49    unreachable("bad mesa_log_level");
50 }
51 #endif
52 
53 #ifndef ANDROID
54 static inline const char *
level_to_str(enum mesa_log_level l)55 level_to_str(enum mesa_log_level l)
56 {
57    switch (l) {
58    case MESA_LOG_ERROR: return "error";
59    case MESA_LOG_WARN: return "warning";
60    case MESA_LOG_INFO: return "info";
61    case MESA_LOG_DEBUG: return "debug";
62    }
63 
64    unreachable("bad mesa_log_level");
65 }
66 #endif
67 
68 void
mesa_log(enum mesa_log_level level,const char * tag,const char * format,...)69 mesa_log(enum mesa_log_level level, const char *tag, const char *format, ...)
70 {
71    va_list va;
72 
73    va_start(va, format);
74    mesa_log_v(level, tag, format, va);
75    va_end(va);
76 }
77 
78 void
mesa_log_v(enum mesa_log_level level,const char * tag,const char * format,va_list va)79 mesa_log_v(enum mesa_log_level level, const char *tag, const char *format,
80             va_list va)
81 {
82 #ifdef ANDROID
83    __android_log_vprint(level_to_android(level), tag, format, va);
84 #else
85 #if !DETECT_OS_WINDOWS
86    flockfile(stderr);
87 #endif
88    fprintf(stderr, "%s: %s: ", tag, level_to_str(level));
89    vfprintf(stderr, format, va);
90    if (format[strlen(format) - 1] != '\n')
91       fprintf(stderr, "\n");
92 #if !DETECT_OS_WINDOWS
93    funlockfile(stderr);
94 #endif
95 #endif
96 }
97 
98 struct log_stream *
_mesa_log_stream_create(enum mesa_log_level level,char * tag)99 _mesa_log_stream_create(enum mesa_log_level level, char *tag)
100 {
101    struct log_stream *stream = ralloc(NULL, struct log_stream);
102    stream->level = level;
103    stream->tag = tag;
104    stream->msg = ralloc_strdup(stream, "");
105    stream->pos = 0;
106    return stream;
107 }
108 
109 void
mesa_log_stream_destroy(struct log_stream * stream)110 mesa_log_stream_destroy(struct log_stream *stream)
111 {
112    /* If you left trailing stuff in the log stream, flush it out as a line. */
113    if (stream->pos != 0)
114       mesa_log(stream->level, stream->tag, "%s", stream->msg);
115 
116    ralloc_free(stream);
117 }
118 
119 static void
mesa_log_stream_flush(struct log_stream * stream,size_t scan_offset)120 mesa_log_stream_flush(struct log_stream *stream, size_t scan_offset)
121 {
122    char *end;
123    char *next = stream->msg;
124    while ((end = strchr(stream->msg + scan_offset, '\n'))) {
125       *end = 0;
126       mesa_log(stream->level, stream->tag, "%s", next);
127       next = end + 1;
128       scan_offset = next - stream->msg;
129    }
130    if (next != stream->msg) {
131       /* Clear out the lines we printed and move any trailing chars to the start. */
132       size_t remaining = stream->msg + stream->pos - next;
133       memmove(stream->msg, next, remaining);
134       stream->pos = remaining;
135    }
136 }
137 
mesa_log_stream_printf(struct log_stream * stream,const char * format,...)138 void mesa_log_stream_printf(struct log_stream *stream, const char *format, ...)
139 {
140    size_t old_pos = stream->pos;
141 
142    va_list va;
143    va_start(va, format);
144    ralloc_vasprintf_rewrite_tail(&stream->msg, &stream->pos, format, va);
145    va_end(va);
146 
147    mesa_log_stream_flush(stream, old_pos);
148 }
149 
150 void
_mesa_log_multiline(enum mesa_log_level level,const char * tag,const char * lines)151 _mesa_log_multiline(enum mesa_log_level level, const char *tag, const char *lines)
152 {
153    struct log_stream tmp = {
154       .level = level,
155       .tag = tag,
156       .msg = strdup(lines),
157       .pos = strlen(lines),
158    };
159    mesa_log_stream_flush(&tmp, 0);
160    free(tmp.msg);
161 }
162