blob: acfb2ca5e3f117bf9e630d3943132286a6fd30ea [file] [log] [blame]
Craig Stout3d2098f2019-11-25 15:36:43 -08001// 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
10void __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
16int puts(const char *s)
17{
18 return fputs(s, stdout);
19}
20
21int 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
30int vprintf(const char *format, va_list ap)
31{
32 return vfprintf(stdout, format, ap);
33}
34
35int 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
48static inline fx_log_severity_t severity(FILE *stream)
49{
50 return stream == stdout ? FX_LOG_INFO : FX_LOG_ERROR;
51}
52
53int 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
63int vfprintf(FILE *stream, const char *format, va_list ap)
64{
65 assert(stream == stdout || stream == stderr);
66 if (stream == stdout || stream == stderr)
67 {
Suraj Malhotrad66818a2020-12-21 17:39:27 -080068 _FX_LOGVF(severity(stream), "goldfish", __FILE__, __LINE__,format, ap);
Craig Stout3d2098f2019-11-25 15:36:43 -080069 }
70 return 0;
71}
72
73size_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}