1 /*
2  * Copyright (C) 2018 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #pragma once
18 
19 #include <lk/compiler.h>
20 #include <stdio.h>
21 
22 #define TLOG_LVL_NONE 0
23 #define TLOG_LVL_CRIT 1
24 #define TLOG_LVL_ERROR 2
25 #define TLOG_LVL_WARN 3
26 #define TLOG_LVL_INFO 4
27 #define TLOG_LVL_DEBUG 5
28 
29 #ifndef TLOG_LVL
30 #ifdef TLOG_LVL_DEFAULT
31 #define TLOG_LVL TLOG_LVL_DEFAULT
32 #else
33 #define TLOG_LVL TLOG_LVL_INFO
34 #endif
35 #endif
36 
37 __BEGIN_CDECLS
38 
39 #ifdef TRUSTY_USERSPACE
40 
41 /* Defined in libc and libunittest, whichever is statically linked first will be
42  * used, so libunittest must always come before libc in the link order. */
43 int _tlog(const char* fmt, ...) __PRINTFLIKE(1, 2);
44 int _vtlog(const char* fmt, va_list args);
45 
46 #else
47 
48 /* TLOG is also called from host code, where we don't provide a definition of
49  * _tlog. In this case, just printf */
50 #define _tlog(fmt, ...) fprintf(stderr, fmt, ##__VA_ARGS__)
51 #define _vtlog(fmt, args) vfprintf(stderr, fmt, args)
52 #endif
53 
54 __END_CDECLS
55 
56 #define TLOG(fmt, ...)                                            \
57     do {                                                          \
58         _tlog("%s: %d: " fmt, TLOG_TAG, __LINE__, ##__VA_ARGS__); \
59     } while (0)
60 
61 /* debug  */
62 #define TLOGD(x...)                       \
63     do {                                  \
64         if (TLOG_LVL >= TLOG_LVL_DEBUG) { \
65             TLOG(x);                      \
66         }                                 \
67     } while (0)
68 
69 /* info */
70 #define TLOGI(x...)                      \
71     do {                                 \
72         if (TLOG_LVL >= TLOG_LVL_INFO) { \
73             TLOG(x);                     \
74         }                                \
75     } while (0)
76 
77 /* warning */
78 #define TLOGW(x...)                      \
79     do {                                 \
80         if (TLOG_LVL >= TLOG_LVL_WARN) { \
81             TLOG(x);                     \
82         }                                \
83     } while (0)
84 
85 /* error */
86 #define TLOGE(x...)                       \
87     do {                                  \
88         if (TLOG_LVL >= TLOG_LVL_ERROR) { \
89             TLOG(x);                      \
90         }                                 \
91     } while (0)
92 
93 /* critical */
94 #define TLOGC(x...)                      \
95     do {                                 \
96         if (TLOG_LVL >= TLOG_LVL_CRIT) { \
97             TLOG(x);                     \
98         }                                \
99     } while (0)
100