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/global.h>
17 #else
18 #include <libgen.h>
19 #endif
20
21 #include "cutils/log.h"
22 #include "cutils/properties.h"
23 #include "cutils/threads.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\n", buf);
38 vfprintf(stderr, format, ap);
39
40 free(dup);
41 }
42 #endif
43
property_get(const char * key,char * value,const char * default_value)44 int property_get(const char* key, char* value, const char* default_value) {
45 return 0;
46 }
47
__android_log_print(int priority,const char * tag,const char * file,int line,const char * format,...)48 int __android_log_print(int priority, const char* tag, const char* file,
49 int line, const char* format, ...) {
50 const char* local_tag = tag;
51 if (!local_tag) {
52 local_tag = "<NO_TAG>";
53 }
54
55 va_list ap;
56 va_start(ap, format);
57 #if defined(__Fuchsia__)
58 switch (priority) {
59 case ANDROID_LOG_VERBOSE:
60 case ANDROID_LOG_DEBUG:
61 FX_LOGVF(DEBUG, local_tag, file, line, format, ap);
62 break;
63 case ANDROID_LOG_WARN:
64 FX_LOGVF(WARNING, local_tag, file, line, format, ap);
65 break;
66 case ANDROID_LOG_ERROR:
67 FX_LOGVF(ERROR, local_tag, file, line, format, ap);
68 break;
69 case ANDROID_LOG_FATAL:
70 FX_LOGVF(FATAL, local_tag, file, line, format, ap);
71 break;
72 case ANDROID_LOG_INFO:
73 default:
74 FX_LOGVF(INFO, local_tag, file, line, format, ap);
75 break;
76 }
77 #else
78 linux_log_prefix(local_tag, file, line, format, ap);
79 #endif
80
81 return 1;
82 }
83
__android_log_assert(const char * condition,const char * tag,const char * file,int line,const char * format,...)84 void __android_log_assert(const char* condition, const char* tag,
85 const char* file, int line, const char* format, ...) {
86 const char* local_tag = tag;
87 if (!local_tag) {
88 local_tag = "<NO_TAG>";
89 }
90 va_list ap;
91 va_start(ap, format);
92 #if defined(__Fuchsia__)
93 FX_LOGVF(ERROR, local_tag, file, line, format, ap);
94 #else
95 linux_log_prefix(local_tag, file, line, format, ap);
96 #endif
97
98 va_end(ap);
99
100 abort();
101 }
102
sync_wait(int fd,int timeout)103 int sync_wait(int fd, int timeout) {
104 return -1;
105 }
106
gettid()107 pid_t gettid() {
108 static thread_local pid_t id = 0;
109 if (!id) {
110 static std::atomic<pid_t> next_thread_id{1};
111 id = next_thread_id++;
112 }
113 return id;
114 }
115
116 }
117