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 
14 #include <lib/syslog/global.h>
15 
16 #include "cutils/log.h"
17 #include "cutils/properties.h"
18 #include "cutils/threads.h"
19 
20 extern "C" {
21 
property_get(const char * key,char * value,const char * default_value)22 int property_get(const char* key, char* value, const char* default_value) {
23   return 0;
24 }
25 
__android_log_print(int priority,const char * tag,const char * format,...)26 int __android_log_print(int priority, const char* tag, const char* format,
27                         ...) {
28   const char* local_tag = tag;
29   if (!local_tag) {
30     local_tag = "<NO_TAG>";
31   }
32   va_list ap;
33   va_start(ap, format);
34   switch (priority) {
35     case ANDROID_LOG_VERBOSE:
36     case ANDROID_LOG_DEBUG:
37       FX_LOGVF(DEBUG, local_tag, format, ap);
38       break;
39     case ANDROID_LOG_WARN:
40       FX_LOGVF(WARNING, local_tag, format, ap);
41       break;
42     case ANDROID_LOG_ERROR:
43       FX_LOGVF(ERROR, local_tag, format, ap);
44       break;
45     case ANDROID_LOG_FATAL:
46       FX_LOGVF(FATAL, local_tag, format, ap);
47       break;
48     case ANDROID_LOG_INFO:
49     default:
50       FX_LOGVF(INFO, local_tag, format, ap);
51       break;
52   }
53   return 1;
54 }
55 
__android_log_assert(const char * condition,const char * tag,const char * format,...)56 void __android_log_assert(const char* condition, const char* tag,
57                           const char* format, ...) {
58   const char* local_tag = tag;
59   if (!local_tag) {
60     local_tag = "<NO_TAG>";
61   }
62   va_list ap;
63   va_start(ap, format);
64   FX_LOGVF(ERROR, local_tag, format, ap);
65   va_end(ap);
66 
67   abort();
68 }
69 
sync_wait(int fd,int timeout)70 int sync_wait(int fd, int timeout) {
71   return -1;
72 }
73 
thread_store_get(thread_store_t * store)74 void* thread_store_get(thread_store_t* store) {
75   return store->has_tls ? pthread_getspecific(store->tls) : nullptr;
76 }
77 
thread_store_set(thread_store_t * store,void * value,thread_store_destruct_t destroy)78 void thread_store_set(thread_store_t* store,
79                       void* value,
80                       thread_store_destruct_t destroy) {
81     pthread_mutex_lock(&store->lock);
82     if (!store->has_tls) {
83         if (pthread_key_create(&store->tls, destroy) != 0) {
84             pthread_mutex_unlock(&store->lock);
85             return;
86         }
87         store->has_tls = 1;
88     }
89     pthread_mutex_unlock(&store->lock);
90     pthread_setspecific(store->tls, value);
91 }
92 
gettid()93 pid_t gettid() {
94   static thread_local pid_t id = 0;
95   if (!id) {
96     static std::atomic<pid_t> next_thread_id{1};
97     id = next_thread_id++;
98   }
99   return id;
100 }
101 
102 }
103