blob: 4564b98819ce5e1b46672fb49e3701c94724229f [file] [log] [blame]
Anton Moberg07cf70b2021-07-07 11:08:17 +02001/*
2 * Copyright (c) 2021 Arm Limited. All rights reserved.
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 *
6 * Licensed under the Apache License, Version 2.0 (the License); you may
7 * not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an AS IS BASIS, WITHOUT
14 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18
19#ifndef ETHOSU_LOG_H
20#define ETHOSU_LOG_H
21
22/******************************************************************************
23 * Includes
24 ******************************************************************************/
25
26#include <stdio.h>
27#include <string.h>
28
29/******************************************************************************
30 * Defines
31 ******************************************************************************/
32
33// Log severity levels
34#define ETHOSU_LOG_ERR 0
35#define ETHOSU_LOG_WARN 1
36#define ETHOSU_LOG_INFO 2
37#define ETHOSU_LOG_DEBUG 3
38
39// Define default log severity
40#ifndef ETHOSU_LOG_SEVERITY
41#define ETHOSU_LOG_SEVERITY ETHOSU_LOG_WARN
42#endif
43
44// Log formatting
45
46#define LOG(f, ...) fprintf(stdout, f, ##__VA_ARGS__)
47
48#if ETHOSU_LOG_SEVERITY >= ETHOSU_LOG_ERR
49#define LOG_ERR_N(f, ...) fprintf(stderr, f, ##__VA_ARGS__)
50#define LOG_ERR(f, ...) LOG_ERR_N("E: " f " (%s:%d)", ##__VA_ARGS__, strrchr("/" __FILE__, '/') + 1, __LINE__)
51#else
52#define LOG_ERR(f, ...)
53#define LOG_ERR_N(f, ...)
54#endif
55
56#if ETHOSU_LOG_SEVERITY >= ETHOSU_LOG_WARN
57#define LOG_WARN_N(f, ...) fprintf(stdout, f, ##__VA_ARGS__)
58#define LOG_WARN(f, ...) LOG_WARN_N("W: " f, ##__VA_ARGS__)
59#else
60#define LOG_WARN(f, ...)
61#define LOG_WARN_N(f, ...)
62#endif
63
64#if ETHOSU_LOG_SEVERITY >= ETHOSU_LOG_INFO
65#define LOG_INFO_N(f, ...) fprintf(stdout, f, ##__VA_ARGS__)
66#define LOG_INFO(f, ...) LOG_INFO_N("I: " f, ##__VA_ARGS__)
67#else
68#define LOG_INFO(f, ...)
69#define LOG_INFO_N(f, ...)
70#endif
71
72#if ETHOSU_LOG_SEVERITY >= ETHOSU_LOG_DEBUG
73#define LOG_DEBUG_N(f, ...) fprintf(stdout, f, ##__VA_ARGS__)
74#define LOG_DEBUG(f, ...) LOG_DEBUG_N("D: %s(): " f, __FUNCTION__, ##__VA_ARGS__)
75#else
76#define LOG_DEBUG(f, ...)
77#define LOG_DEBUG_N(f, ...)
78#endif
79
80#endif