Craig Stout | 3d2098f | 2019-11-25 15:36:43 -0800 | [diff] [blame] | 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 | |
| 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 | |
| 16 | int puts(const char *s) |
| 17 | { |
| 18 | return fputs(s, stdout); |
| 19 | } |
| 20 | |
| 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 | |
| 30 | int vprintf(const char *format, va_list ap) |
| 31 | { |
| 32 | return vfprintf(stdout, format, ap); |
| 33 | } |
| 34 | |
| 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 | |
| 48 | static inline fx_log_severity_t severity(FILE *stream) |
| 49 | { |
| 50 | return stream == stdout ? FX_LOG_INFO : FX_LOG_ERROR; |
| 51 | } |
| 52 | |
| 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 | |
| 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 | { |
Suraj Malhotra | d66818a | 2020-12-21 17:39:27 -0800 | [diff] [blame] | 68 | _FX_LOGVF(severity(stream), "goldfish", __FILE__, __LINE__,format, ap); |
Craig Stout | 3d2098f | 2019-11-25 15:36:43 -0800 | [diff] [blame] | 69 | } |
| 70 | return 0; |
| 71 | } |
| 72 | |
| 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 | } |