1 // Copyright 2019 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 <assert.h>
6 #include <lib/syslog/global.h>
7 #include <stdio.h>
8 #include <stdlib.h>
9
__assert_fail(const char * expr,const char * file,int line,const char * func)10 void __assert_fail(const char *expr, const char *file, int line, const char *func)
11 {
12 FX_LOGF(ERROR, "goldfish", "Assertion failed: %s (%s: %s: %d)", expr, file, func, line);
13 abort();
14 }
15
puts(const char * s)16 int puts(const char *s)
17 {
18 return fputs(s, stdout);
19 }
20
printf(const char * format,...)21 int printf(const char *format, ...)
22 {
23 va_list args;
24 va_start(args, format);
25 vfprintf(stdout, format, args);
26 va_end(args);
27 return 0;
28 }
29
vprintf(const char * format,va_list ap)30 int vprintf(const char *format, va_list ap)
31 {
32 return vfprintf(stdout, format, ap);
33 }
34
fprintf(FILE * stream,const char * format,...)35 int fprintf(FILE *stream, const char *format, ...)
36 {
37 assert(stream == stdout || stream == stderr);
38 if (stream == stdout || stream == stderr)
39 {
40 va_list args;
41 va_start(args, format);
42 vfprintf(stream, format, args);
43 va_end(args);
44 }
45 return 0;
46 }
47
severity(FILE * stream)48 static inline fx_log_severity_t severity(FILE *stream)
49 {
50 return stream == stdout ? FX_LOG_INFO : FX_LOG_ERROR;
51 }
52
fputs(const char * s,FILE * stream)53 int fputs(const char *s, FILE *stream)
54 {
55 assert(stream == stdout || stream == stderr);
56 if (stream == stdout || stream == stderr)
57 {
58 _FX_LOG(severity(stream), "goldfish", s);
59 }
60 return 0;
61 }
62
vfprintf(FILE * stream,const char * format,va_list ap)63 int vfprintf(FILE *stream, const char *format, va_list ap)
64 {
65 assert(stream == stdout || stream == stderr);
66 if (stream == stdout || stream == stderr)
67 {
68 _FX_LOGVF(severity(stream), "goldfish", format, ap);
69 }
70 return 0;
71 }
72
fwrite(const void * ptr,size_t size,size_t nitems,FILE * stream)73 size_t fwrite(const void *ptr, size_t size, size_t nitems, FILE *stream)
74 {
75 assert(stream == stdout || stream == stderr);
76 char buffer[512];
77 size_t offset = 0;
78 size_t count = 0;
79 for (; count < nitems; count++)
80 {
81 snprintf(buffer + offset, sizeof(buffer) - offset, reinterpret_cast<const char *>(ptr) + offset, size);
82 offset += size;
83 if (offset > sizeof(buffer))
84 break;
85 }
86 buffer[sizeof(buffer) - 1] = 0;
87 fputs(buffer, stream);
88 return count;
89 }
90