1 // SPDX-License-Identifier: Apache-2.0 2 // Copyright (C) 2018 IBM Corp. 3 #define _GNU_SOURCE 4 #include <stdarg.h> 5 #include <stdbool.h> 6 #include <stdio.h> 7 #include <stdint.h> 8 #include <string.h> 9 #include <sys/types.h> 10 #include <syslog.h> 11 #include <time.h> 12 13 #include "common.h" 14 15 void (*mbox_vlog)(int p, const char *fmt, va_list args); 16 17 enum verbose verbosity; 18 19 void mbox_log_console(int p, const char *fmt, va_list args) 20 { 21 struct timespec time; 22 FILE *s = (p < LOG_WARNING) ? stdout : stderr; 23 24 clock_gettime(CLOCK_REALTIME, &time); 25 26 fprintf(s, "[%s %ld.%.9ld] ", PREFIX, time.tv_sec, time.tv_nsec); 27 28 vfprintf(s, fmt, args); 29 30 if (s == stdout) 31 fflush(s); 32 } 33 34 __attribute__((format(printf, 2, 3))) 35 void mbox_log(int p, const char *fmt, ...) 36 { 37 static bool warned = false; 38 va_list args; 39 40 if (!mbox_vlog) { 41 if (!warned) { 42 fprintf(stderr, "Logging backend not configured, " 43 "log output disabled\n"); 44 warned = true; 45 } 46 47 return; 48 } 49 50 va_start(args, fmt); 51 mbox_vlog(p, fmt, args); 52 va_end(args); 53 } 54 55 uint16_t get_u16(uint8_t *ptr) 56 { 57 return le16toh(*(uint16_t *)ptr); 58 } 59 60 void put_u16(uint8_t *ptr, uint16_t val) 61 { 62 val = htole16(val); 63 memcpy(ptr, &val, sizeof(val)); 64 } 65 66 uint32_t get_u32(uint8_t *ptr) 67 { 68 return le32toh(*(uint32_t *)ptr); 69 } 70 71 void put_u32(uint8_t *ptr, uint32_t val) 72 { 73 val = htole32(val); 74 memcpy(ptr, &val, sizeof(val)); 75 } 76 77