1 // Copyright 2018 The Fuchsia Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include <stdlib.h>
6 
7 #include <atomic>
8 #include <cassert>
9 #include <cstdarg>
10 #include <cstdint>
11 #include <cstdio>
12 #include <thread>
13 #include <cstring>
14 
15 #if defined(__Fuchsia__)
16 #include <lib/syslog/structured_backend/cpp/fuchsia_syslog.h>
17 #include <log/log.h>
18 #else
19 #include <libgen.h>
20 #endif
21 
22 #include "cutils/log.h"
23 #include "cutils/properties.h"
24 
25 extern "C" {
26 
27 #if !defined(__Fuchsia__)
linux_log_prefix(const char * prefix,const char * file,int line,const char * format,va_list ap,...)28 static void linux_log_prefix(const char *prefix, const char *file, int line, const char *format,
29                              va_list ap, ...)
30 {
31   char buf[50];
32   char *dup = strdup(file);
33   if (!dup)
34     return;
35 
36   snprintf(buf, sizeof(buf), "[%s(%d)]", basename(dup), line);
37   fprintf(stderr, "%s", buf);
38   vfprintf(stderr, format, ap);
39   fprintf(stderr, "\n");
40 
41   free(dup);
42 }
43 #endif
44 
property_get(const char * key,char * value,const char * default_value)45 int property_get(const char* key, char* value, const char* default_value) {
46   return 0;
47 }
48 
49 // According to https://developer.android.com/ndk/reference/group/logging,
50 // some log levels "Should typically be disabled for a release apk."
include_debug_logging()51 static constexpr bool include_debug_logging() {
52 #if defined(DEBUG)
53     return true;
54 #else
55     return false;
56 #endif
57 }
58 
__android_log_print(int priority,const char * tag,const char * file,int line,const char * format,...)59 int __android_log_print(int priority, const char* tag, const char* file, int line,
60                         const char* format, ...) {
61     const char* local_tag = tag;
62     if (!local_tag) {
63         local_tag = "<NO_TAG>";
64     }
65 
66     va_list ap;
67     va_start(ap, format);
68 #if defined(__Fuchsia__)
69     switch (priority) {
70         case ANDROID_LOG_VERBOSE:
71         case ANDROID_LOG_DEBUG:
72             if (include_debug_logging()) {
73                 gfxstream_fuchsia_log(FUCHSIA_LOG_DEBUG, local_tag, file, line, format, ap);
74             }
75             break;
76         case ANDROID_LOG_INFO:
77             if (include_debug_logging()) {
78                 gfxstream_fuchsia_log(FUCHSIA_LOG_INFO, local_tag, file, line, format, ap);
79             }
80             break;
81         case ANDROID_LOG_WARN:
82             gfxstream_fuchsia_log(FUCHSIA_LOG_WARNING, local_tag, file, line, format, ap);
83             break;
84         case ANDROID_LOG_ERROR:
85             gfxstream_fuchsia_log(FUCHSIA_LOG_ERROR, local_tag, file, line, format, ap);
86             break;
87         case ANDROID_LOG_FATAL:
88             gfxstream_fuchsia_log(FUCHSIA_LOG_FATAL, local_tag, file, line, format, ap);
89             break;
90         default:
91             gfxstream_fuchsia_log(FUCHSIA_LOG_INFO, local_tag, file, line, format, ap);
92             break;
93     }
94 #else
95     // Should we check include_debug_logging() here?
96     linux_log_prefix(local_tag, file, line, format, ap);
97 #endif
98 
99     return 1;
100 }
101 
__android_log_assert(const char * condition,const char * tag,const char * file,int line,const char * format,...)102 void __android_log_assert(const char* condition, const char* tag, const char* file, int line,
103                           const char* format, ...) {
104     const char* local_tag = tag;
105     if (!local_tag) {
106         local_tag = "<NO_TAG>";
107     }
108     va_list ap;
109     va_start(ap, format);
110 #if defined(__Fuchsia__)
111     gfxstream_fuchsia_log(FUCHSIA_LOG_ERROR, local_tag, file, line, format, ap);
112 #else
113     linux_log_prefix(local_tag, file, line, format, ap);
114 #endif
115 
116     va_end(ap);
117 
118     abort();
119 }
120 
sync_wait(int fd,int timeout)121 int sync_wait(int fd, int timeout) { return -1; }
122 }
123