• 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    fprintf(stderr, "\n");
91 #if !DETECT_OS_WINDOWS
92    funlockfile(stderr);
93 #endif
94 #endif
95 }
96 
97 struct log_stream *
_mesa_log_stream_create(enum mesa_log_level level,char * tag)98 _mesa_log_stream_create(enum mesa_log_level level, char *tag)
99 {
100    struct log_stream *stream = ralloc(NULL, struct log_stream);
101    stream->level = level;
102    stream->tag = tag;
103    stream->msg = ralloc_strdup(stream, "");
104    stream->pos = 0;
105    return stream;
106 }
107 
108 void
mesa_log_stream_destroy(struct log_stream * stream)109 mesa_log_stream_destroy(struct log_stream *stream)
110 {
111    /* If you left trailing stuff in the log stream, flush it out as a line. */
112    if (stream->pos != 0)
113       mesa_log(stream->level, stream->tag, "%s", stream->msg);
114 
115    ralloc_free(stream);
116 }
117 
118 static void
mesa_log_stream_flush(struct log_stream * stream,size_t scan_offset)119 mesa_log_stream_flush(struct log_stream *stream, size_t scan_offset)
120 {
121    char *end;
122    char *next = stream->msg;
123    while ((end = strchr(stream->msg + scan_offset, '\n'))) {
124       *end = 0;
125       mesa_log(stream->level, stream->tag, "%s", next);
126       next = end + 1;
127       scan_offset = next - stream->msg;
128    }
129    if (next != stream->msg) {
130       /* Clear out the lines we printed and move any trailing chars to the start. */
131       size_t remaining = stream->msg + stream->pos - next;
132       memmove(stream->msg, next, remaining);
133       stream->pos = remaining;
134    }
135 }
136 
mesa_log_stream_printf(struct log_stream * stream,const char * format,...)137 void mesa_log_stream_printf(struct log_stream *stream, const char *format, ...)
138 {
139    size_t old_pos = stream->pos;
140 
141    va_list va;
142    va_start(va, format);
143    ralloc_vasprintf_rewrite_tail(&stream->msg, &stream->pos, format, va);
144    va_end(va);
145 
146    mesa_log_stream_flush(stream, old_pos);
147 }
148 
149 void
_mesa_log_multiline(enum mesa_log_level level,const char * tag,const char * lines)150 _mesa_log_multiline(enum mesa_log_level level, const char *tag, const char *lines)
151 {
152    struct log_stream tmp = {
153       .level = level,
154       .tag = tag,
155       .msg = strdup(lines),
156       .pos = strlen(lines),
157    };
158    mesa_log_stream_flush(&tmp, 0);
159    free(tmp.msg);
160 }
161