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