blob: fdbf58352593ba50d91c5069d9a9859626c05aab [file] [log] [blame]
David Reveman74e99bb2019-02-15 18:47:25 -05001// 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>
Gurchetan Singh8fb06422022-01-13 13:30:24 -080013#include <cstring>
David Reveman74e99bb2019-02-15 18:47:25 -050014
Gurchetan Singhff76c162021-10-25 14:58:00 -070015#if defined(__Fuchsia__)
Craig Stout3d2098f2019-11-25 15:36:43 -080016#include <lib/syslog/global.h>
Gurchetan Singh8fb06422022-01-13 13:30:24 -080017#else
18#include <libgen.h>
Gurchetan Singhff76c162021-10-25 14:58:00 -070019#endif
Craig Stout3d2098f2019-11-25 15:36:43 -080020
David Reveman74e99bb2019-02-15 18:47:25 -050021#include "cutils/log.h"
22#include "cutils/properties.h"
23#include "cutils/threads.h"
24
25extern "C" {
26
Gurchetan Singhff76c162021-10-25 14:58:00 -070027#if !defined(__Fuchsia__)
28static void linux_log_prefix(const char *prefix, const char *file, int line, const char *format,
29 va_list ap, ...)
30{
31 char buf[50];
Gurchetan Singh8fb06422022-01-13 13:30:24 -080032 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);
Gurchetan Singhff76c162021-10-25 14:58:00 -070038 vfprintf(stderr, format, ap);
Gurchetan Singh8fb06422022-01-13 13:30:24 -080039
40 free(dup);
Gurchetan Singhff76c162021-10-25 14:58:00 -070041}
42#endif
43
David Reveman74e99bb2019-02-15 18:47:25 -050044int property_get(const char* key, char* value, const char* default_value) {
45 return 0;
46}
47
Suraj Malhotra1cf30162020-11-25 17:31:11 -080048int __android_log_print(int priority, const char* tag, const char* file,
49 int line, const char* format, ...) {
David Reveman74e99bb2019-02-15 18:47:25 -050050 const char* local_tag = tag;
51 if (!local_tag) {
52 local_tag = "<NO_TAG>";
53 }
Gurchetan Singhff76c162021-10-25 14:58:00 -070054
David Reveman74e99bb2019-02-15 18:47:25 -050055 va_list ap;
56 va_start(ap, format);
Gurchetan Singhff76c162021-10-25 14:58:00 -070057#if defined(__Fuchsia__)
Craig Stout3d2098f2019-11-25 15:36:43 -080058 switch (priority) {
Yilong Li0289f1c2020-06-24 22:21:07 -070059 case ANDROID_LOG_VERBOSE:
60 case ANDROID_LOG_DEBUG:
Suraj Malhotrad66818a2020-12-21 17:39:27 -080061 FX_LOGVF(DEBUG, local_tag, file, line, format, ap);
Yilong Li0289f1c2020-06-24 22:21:07 -070062 break;
Craig Stout3d2098f2019-11-25 15:36:43 -080063 case ANDROID_LOG_WARN:
Suraj Malhotrad66818a2020-12-21 17:39:27 -080064 FX_LOGVF(WARNING, local_tag, file, line, format, ap);
Craig Stout3d2098f2019-11-25 15:36:43 -080065 break;
66 case ANDROID_LOG_ERROR:
Suraj Malhotrad66818a2020-12-21 17:39:27 -080067 FX_LOGVF(ERROR, local_tag, file, line, format, ap);
Craig Stout3d2098f2019-11-25 15:36:43 -080068 break;
Yilong Li0289f1c2020-06-24 22:21:07 -070069 case ANDROID_LOG_FATAL:
Suraj Malhotrad66818a2020-12-21 17:39:27 -080070 FX_LOGVF(FATAL, local_tag, file, line, format, ap);
Yilong Li0289f1c2020-06-24 22:21:07 -070071 break;
Craig Stout3d2098f2019-11-25 15:36:43 -080072 case ANDROID_LOG_INFO:
73 default:
Suraj Malhotrad66818a2020-12-21 17:39:27 -080074 FX_LOGVF(INFO, local_tag, file, line, format, ap);
Craig Stout3d2098f2019-11-25 15:36:43 -080075 break;
76 }
Gurchetan Singhff76c162021-10-25 14:58:00 -070077#else
78 linux_log_prefix(local_tag, file, line, format, ap);
79#endif
80
David Reveman74e99bb2019-02-15 18:47:25 -050081 return 1;
82}
83
Yilong Lic2867e22020-11-26 21:28:20 -080084void __android_log_assert(const char* condition, const char* tag,
85 const char* file, int line, const char* format, ...) {
David Reveman74e99bb2019-02-15 18:47:25 -050086 const char* local_tag = tag;
87 if (!local_tag) {
88 local_tag = "<NO_TAG>";
89 }
Craig Stout3d2098f2019-11-25 15:36:43 -080090 va_list ap;
91 va_start(ap, format);
Gurchetan Singhff76c162021-10-25 14:58:00 -070092#if defined(__Fuchsia__)
Suraj Malhotrad66818a2020-12-21 17:39:27 -080093 FX_LOGVF(ERROR, local_tag, file, line, format, ap);
Gurchetan Singhff76c162021-10-25 14:58:00 -070094#else
95 linux_log_prefix(local_tag, file, line, format, ap);
96#endif
97
Craig Stout3d2098f2019-11-25 15:36:43 -080098 va_end(ap);
David Reveman74e99bb2019-02-15 18:47:25 -050099
Craig Stout3d2098f2019-11-25 15:36:43 -0800100 abort();
David Reveman74e99bb2019-02-15 18:47:25 -0500101}
102
103int sync_wait(int fd, int timeout) {
104 return -1;
105}
106
David Reveman74e99bb2019-02-15 18:47:25 -0500107pid_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}