Cyril Bur | 314929b | 2016-10-14 15:55:16 +1100 | [diff] [blame] | 1 | /* Copyright 2016 IBM |
| 2 | * |
| 3 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | * you may not use this file except in compliance with the License. |
| 5 | * You may obtain a copy of the License at |
| 6 | * |
| 7 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | * |
| 9 | * Unless required by applicable law or agreed to in writing, software |
| 10 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | * See the License for the specific language governing permissions and |
| 13 | * limitations under the License. |
| 14 | * |
| 15 | */ |
| 16 | |
| 17 | #define _GNU_SOURCE |
| 18 | #include <stdarg.h> |
| 19 | #include <stdbool.h> |
| 20 | #include <stdio.h> |
| 21 | #include <stdint.h> |
| 22 | #include <string.h> |
| 23 | #include <sys/types.h> |
| 24 | #include <syslog.h> |
| 25 | #include <time.h> |
| 26 | |
| 27 | #include "common.h" |
| 28 | |
Ratan Gupta | 90b92fe | 2017-05-05 17:34:00 +0530 | [diff] [blame] | 29 | void (*mbox_vlog)(int p, const char *fmt, va_list args); |
| 30 | |
| 31 | enum verbose verbosity; |
| 32 | |
Cyril Bur | 314929b | 2016-10-14 15:55:16 +1100 | [diff] [blame] | 33 | void mbox_log_console(int p, const char *fmt, va_list args) |
| 34 | { |
| 35 | struct timespec time; |
| 36 | FILE *s = (p < LOG_WARNING) ? stdout : stderr; |
| 37 | |
| 38 | clock_gettime(CLOCK_REALTIME, &time); |
| 39 | |
| 40 | fprintf(s, "[%s %ld.%.9ld] ", PREFIX, time.tv_sec, time.tv_nsec); |
| 41 | |
| 42 | vfprintf(s, fmt, args); |
| 43 | } |
| 44 | |
| 45 | __attribute__((format(printf, 2, 3))) |
| 46 | void mbox_log(int p, const char *fmt, ...) |
| 47 | { |
Andrew Jeffery | 44ac078 | 2018-02-26 13:03:40 +1030 | [diff] [blame] | 48 | static bool warned = false; |
Cyril Bur | 314929b | 2016-10-14 15:55:16 +1100 | [diff] [blame] | 49 | va_list args; |
| 50 | |
Andrew Jeffery | 44ac078 | 2018-02-26 13:03:40 +1030 | [diff] [blame] | 51 | if (!mbox_vlog) { |
| 52 | if (!warned) { |
| 53 | fprintf(stderr, "Logging backend not configured, " |
| 54 | "log output disabled\n"); |
| 55 | warned = true; |
| 56 | } |
| 57 | |
| 58 | return; |
| 59 | } |
| 60 | |
Cyril Bur | 314929b | 2016-10-14 15:55:16 +1100 | [diff] [blame] | 61 | va_start(args, fmt); |
| 62 | mbox_vlog(p, fmt, args); |
| 63 | va_end(args); |
| 64 | } |
| 65 | |
| 66 | uint16_t get_u16(uint8_t *ptr) |
| 67 | { |
Andrew Jeffery | 7a3814b | 2018-02-23 16:00:43 +1030 | [diff] [blame] | 68 | return le16toh(*(uint16_t *)ptr); |
Cyril Bur | 314929b | 2016-10-14 15:55:16 +1100 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | void put_u16(uint8_t *ptr, uint16_t val) |
| 72 | { |
Andrew Jeffery | 7a3814b | 2018-02-23 16:00:43 +1030 | [diff] [blame] | 73 | val = htole16(val); |
Cyril Bur | 314929b | 2016-10-14 15:55:16 +1100 | [diff] [blame] | 74 | memcpy(ptr, &val, sizeof(val)); |
| 75 | } |
| 76 | |
| 77 | uint32_t get_u32(uint8_t *ptr) |
| 78 | { |
Andrew Jeffery | 7a3814b | 2018-02-23 16:00:43 +1030 | [diff] [blame] | 79 | return le32toh(*(uint32_t *)ptr); |
Cyril Bur | 314929b | 2016-10-14 15:55:16 +1100 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | void put_u32(uint8_t *ptr, uint32_t val) |
| 83 | { |
Andrew Jeffery | 7a3814b | 2018-02-23 16:00:43 +1030 | [diff] [blame] | 84 | val = htole32(val); |
Cyril Bur | 314929b | 2016-10-14 15:55:16 +1100 | [diff] [blame] | 85 | memcpy(ptr, &val, sizeof(val)); |
| 86 | } |
| 87 | |