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); |
| 29 | } |
| 30 | |
| 31 | __attribute__((format(printf, 2, 3))) |
| 32 | void mbox_log(int p, const char *fmt, ...) |
| 33 | { |
Andrew Jeffery | 44ac078 | 2018-02-26 13:03:40 +1030 | [diff] [blame] | 34 | static bool warned = false; |
Cyril Bur | 314929b | 2016-10-14 15:55:16 +1100 | [diff] [blame] | 35 | va_list args; |
| 36 | |
Andrew Jeffery | 44ac078 | 2018-02-26 13:03:40 +1030 | [diff] [blame] | 37 | if (!mbox_vlog) { |
| 38 | if (!warned) { |
| 39 | fprintf(stderr, "Logging backend not configured, " |
| 40 | "log output disabled\n"); |
| 41 | warned = true; |
| 42 | } |
| 43 | |
| 44 | return; |
| 45 | } |
| 46 | |
Cyril Bur | 314929b | 2016-10-14 15:55:16 +1100 | [diff] [blame] | 47 | va_start(args, fmt); |
| 48 | mbox_vlog(p, fmt, args); |
| 49 | va_end(args); |
| 50 | } |
| 51 | |
| 52 | uint16_t get_u16(uint8_t *ptr) |
| 53 | { |
Andrew Jeffery | 7a3814b | 2018-02-23 16:00:43 +1030 | [diff] [blame] | 54 | return le16toh(*(uint16_t *)ptr); |
Cyril Bur | 314929b | 2016-10-14 15:55:16 +1100 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | void put_u16(uint8_t *ptr, uint16_t val) |
| 58 | { |
Andrew Jeffery | 7a3814b | 2018-02-23 16:00:43 +1030 | [diff] [blame] | 59 | val = htole16(val); |
Cyril Bur | 314929b | 2016-10-14 15:55:16 +1100 | [diff] [blame] | 60 | memcpy(ptr, &val, sizeof(val)); |
| 61 | } |
| 62 | |
| 63 | uint32_t get_u32(uint8_t *ptr) |
| 64 | { |
Andrew Jeffery | 7a3814b | 2018-02-23 16:00:43 +1030 | [diff] [blame] | 65 | return le32toh(*(uint32_t *)ptr); |
Cyril Bur | 314929b | 2016-10-14 15:55:16 +1100 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | void put_u32(uint8_t *ptr, uint32_t val) |
| 69 | { |
Andrew Jeffery | 7a3814b | 2018-02-23 16:00:43 +1030 | [diff] [blame] | 70 | val = htole32(val); |
Cyril Bur | 314929b | 2016-10-14 15:55:16 +1100 | [diff] [blame] | 71 | memcpy(ptr, &val, sizeof(val)); |
| 72 | } |
| 73 | |