blob: a5f67001b5d9a58782f5b0ff254b7986f0539d19 [file] [log] [blame]
Andrew Jeffery4fe996c2018-02-27 12:16:48 +10301// SPDX-License-Identifier: Apache-2.0
2// Copyright (C) 2018 IBM Corp.
Cyril Bur314929b2016-10-14 15:55:16 +11003#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 Gupta90b92fe2017-05-05 17:34:00 +053015void (*mbox_vlog)(int p, const char *fmt, va_list args);
16
17enum verbose verbosity;
18
Cyril Bur314929b2016-10-14 15:55:16 +110019void 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 Jefferyca5eda82020-02-06 14:31:04 +103029
30 if (s == stdout)
31 fflush(s);
Cyril Bur314929b2016-10-14 15:55:16 +110032}
33
34__attribute__((format(printf, 2, 3)))
35void mbox_log(int p, const char *fmt, ...)
36{
Andrew Jeffery44ac0782018-02-26 13:03:40 +103037 static bool warned = false;
Cyril Bur314929b2016-10-14 15:55:16 +110038 va_list args;
39
Andrew Jeffery44ac0782018-02-26 13:03:40 +103040 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 Bur314929b2016-10-14 15:55:16 +110050 va_start(args, fmt);
51 mbox_vlog(p, fmt, args);
52 va_end(args);
53}
54
55uint16_t get_u16(uint8_t *ptr)
56{
Andrew Jeffery7a3814b2018-02-23 16:00:43 +103057 return le16toh(*(uint16_t *)ptr);
Cyril Bur314929b2016-10-14 15:55:16 +110058}
59
60void put_u16(uint8_t *ptr, uint16_t val)
61{
Andrew Jeffery7a3814b2018-02-23 16:00:43 +103062 val = htole16(val);
Cyril Bur314929b2016-10-14 15:55:16 +110063 memcpy(ptr, &val, sizeof(val));
64}
65
66uint32_t get_u32(uint8_t *ptr)
67{
Andrew Jeffery7a3814b2018-02-23 16:00:43 +103068 return le32toh(*(uint32_t *)ptr);
Cyril Bur314929b2016-10-14 15:55:16 +110069}
70
71void put_u32(uint8_t *ptr, uint32_t val)
72{
Andrew Jeffery7a3814b2018-02-23 16:00:43 +103073 val = htole32(val);
Cyril Bur314929b2016-10-14 15:55:16 +110074 memcpy(ptr, &val, sizeof(val));
75}
76