Andrew Jeffery | 4fe996c | 2018-02-27 12:16:48 +1030 | [diff] [blame] | 1 | // SPDX-License-Identifier: Apache-2.0 |
| 2 | // Copyright (C) 2018 IBM Corp. |
Cyril Bur | 314929b | 2016-10-14 15:55:16 +1100 | [diff] [blame] | 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 | |
Ratan Gupta | 90b92fe | 2017-05-05 17:34:00 +0530 | [diff] [blame] | 15 | void (*mbox_vlog)(int p, const char *fmt, va_list args); |
| 16 | |
| 17 | enum verbose verbosity; |
| 18 | |
Cyril Bur | 314929b | 2016-10-14 15:55:16 +1100 | [diff] [blame] | 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); |
Andrew Jeffery | ca5eda8 | 2020-02-06 14:31:04 +1030 | [diff] [blame^] | 29 | |
| 30 | if (s == stdout) |
| 31 | fflush(s); |
Cyril Bur | 314929b | 2016-10-14 15:55:16 +1100 | [diff] [blame] | 32 | } |
| 33 | |
| 34 | __attribute__((format(printf, 2, 3))) |
| 35 | void mbox_log(int p, const char *fmt, ...) |
| 36 | { |
Andrew Jeffery | 44ac078 | 2018-02-26 13:03:40 +1030 | [diff] [blame] | 37 | static bool warned = false; |
Cyril Bur | 314929b | 2016-10-14 15:55:16 +1100 | [diff] [blame] | 38 | va_list args; |
| 39 | |
Andrew Jeffery | 44ac078 | 2018-02-26 13:03:40 +1030 | [diff] [blame] | 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 | |
Cyril Bur | 314929b | 2016-10-14 15:55:16 +1100 | [diff] [blame] | 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 | { |
Andrew Jeffery | 7a3814b | 2018-02-23 16:00:43 +1030 | [diff] [blame] | 57 | return le16toh(*(uint16_t *)ptr); |
Cyril Bur | 314929b | 2016-10-14 15:55:16 +1100 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | void put_u16(uint8_t *ptr, uint16_t val) |
| 61 | { |
Andrew Jeffery | 7a3814b | 2018-02-23 16:00:43 +1030 | [diff] [blame] | 62 | val = htole16(val); |
Cyril Bur | 314929b | 2016-10-14 15:55:16 +1100 | [diff] [blame] | 63 | memcpy(ptr, &val, sizeof(val)); |
| 64 | } |
| 65 | |
| 66 | uint32_t get_u32(uint8_t *ptr) |
| 67 | { |
Andrew Jeffery | 7a3814b | 2018-02-23 16:00:43 +1030 | [diff] [blame] | 68 | return le32toh(*(uint32_t *)ptr); |
Cyril Bur | 314929b | 2016-10-14 15:55:16 +1100 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | void put_u32(uint8_t *ptr, uint32_t val) |
| 72 | { |
Andrew Jeffery | 7a3814b | 2018-02-23 16:00:43 +1030 | [diff] [blame] | 73 | val = htole32(val); |
Cyril Bur | 314929b | 2016-10-14 15:55:16 +1100 | [diff] [blame] | 74 | memcpy(ptr, &val, sizeof(val)); |
| 75 | } |
| 76 | |