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/structured_backend/cpp/fuchsia_syslog.h>
7 #include <log/log.h>
8 #include <stdio.h>
9 #include <stdlib.h>
10
11 #include <cstdarg>
12
log_vararg(int8_t severity,const char * tag,const char * file,int line,const char * format,...)13 static void log_vararg(int8_t severity, const char* tag, const char* file, int line,
14 const char* format, ...) {
15 va_list args;
16 va_start(args, format);
17 gfxstream_fuchsia_log(severity, tag, file, line, format, args);
18 va_end(args);
19 }
20
__assert_fail(const char * expr,const char * file,int line,const char * func)21 void __assert_fail(const char* expr, const char* file, int line, const char* func) {
22 log_vararg(FUCHSIA_LOG_ERROR, "gfxstream", file, line,
23 "Assertion failed: %s (%s: %s: %d)", expr, file, func, line);
24 abort();
25 }
26
puts(const char * s)27 int puts(const char *s)
28 {
29 return fputs(s, stdout);
30 }
31
printf(const char * format,...)32 int printf(const char *format, ...)
33 {
34 va_list args;
35 va_start(args, format);
36 vfprintf(stdout, format, args);
37 va_end(args);
38 return 0;
39 }
40
vprintf(const char * format,va_list ap)41 int vprintf(const char *format, va_list ap)
42 {
43 return vfprintf(stdout, format, ap);
44 }
45
fprintf(FILE * stream,const char * format,...)46 int fprintf(FILE *stream, const char *format, ...)
47 {
48 assert(stream == stdout || stream == stderr);
49 if (stream == stdout || stream == stderr)
50 {
51 va_list args;
52 va_start(args, format);
53 vfprintf(stream, format, args);
54 va_end(args);
55 }
56 return 0;
57 }
58
severity(FILE * stream)59 static inline FuchsiaLogSeverity severity(FILE* stream) {
60 return stream == stdout ? FUCHSIA_LOG_INFO : FUCHSIA_LOG_ERROR;
61 }
62
fputs(const char * s,FILE * stream)63 int fputs(const char* s, FILE* stream) {
64 assert(stream == stdout || stream == stderr);
65 if (stream == stdout || stream == stderr) {
66 // File is set to nullptr as that information isn't available here.
67 log_vararg(severity(stream), "gfxstream", nullptr, 0, s);
68 }
69 return 0;
70 }
71
vfprintf(FILE * stream,const char * format,va_list ap)72 int vfprintf(FILE* stream, const char* format, va_list ap) {
73 assert(stream == stdout || stream == stderr);
74 if (stream == stdout || stream == stderr) {
75 gfxstream_fuchsia_log(severity(stream), "gfxstream", __FILE__, __LINE__, format, ap);
76 }
77 return 0;
78 }
79
fwrite(const void * ptr,size_t size,size_t nitems,FILE * stream)80 size_t fwrite(const void *ptr, size_t size, size_t nitems, FILE *stream)
81 {
82 assert(stream == stdout || stream == stderr);
83 char buffer[512];
84 size_t offset = 0;
85 size_t count = 0;
86 for (; count < nitems; count++)
87 {
88 snprintf(buffer + offset, sizeof(buffer) - offset, reinterpret_cast<const char *>(ptr) + offset, size);
89 offset += size;
90 if (offset > sizeof(buffer))
91 break;
92 }
93 buffer[sizeof(buffer) - 1] = 0;
94 fputs(buffer, stream);
95 return count;
96 }
97