1 /*
2  * Copyright 2018 Oticon A/S
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #ifndef UTIL_TRACING_H
8 #define UTIL_TRACING_H
9 
10 #ifdef __cplusplus
11 extern "C"{
12 #endif
13 
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <stdarg.h>
17 #include <stddef.h>
18 #include "bs_types.h"
19 
20 typedef uint8_t (*main_cleanup_f)(void);
21 typedef bs_time_t (*time_f)(void);
22 
23 void bs_trace_disable_color(char * argv, int offset);
24 void bs_trace_enable_color(char * argv, int offset);
25 void bs_trace_force_color(char * argv, int offset);
26 int bs_trace_is_tty(int file_number);
27 
28 void bs_trace_set_level(int new_trace_level);
29 int  bs_trace_will_it_be_traced(int this_message_trace_level);
30 void bs_trace_register_cleanup_function(main_cleanup_f cleanup_f);
31 void bs_trace_register_time_function(time_f t_f);
32 void bs_trace_set_prefix(const char* prefix);
33 void bs_trace_set_prefix_phy(const char* prefix);
34 void bs_trace_set_prefix_dev(int device_nbr);
35 void bs_trace_silent_exit(uint8_t code);
36 
37 typedef enum {BS_TRACE_EXIT = 0, /*To Stdout, with EXIT prefix*/
38               BS_TRACE_ERROR,    /*To Stderr, with ERROR prefix*/
39               BS_TRACE_WARNING,  /*To Stderr, with WARNING prefix*/
40               BS_TRACE_INFO,     /*To Stdout, with INFO prefix*/
41               BS_TRACE_DEBUG,    /*To Stdout, with DEBUG prefix*/
42               BS_TRACE_RAW,      /*To Stdout, no extra prefix */
43               } base_trace_type_t;
44 typedef enum { BS_TRACE_NOTIME = 0, BS_TRACE_TIME_PROVIDED, BS_TRACE_AUTOTIME} base_trace_timed_type_t;
45 
46 //This is the underlying function all tracing functions below call:
47 void bs_trace_print(base_trace_type_t type,
48                     const char *caller_filename, unsigned int caller_line,
49                     int this_message_trace_level,
50                     base_trace_timed_type_t time_type, bs_time_t time,
51                     const char *format, ...);
52 void bs_trace_vprint(base_trace_type_t type,
53                      const char *caller_filename, unsigned int caller_line,
54                      int this_message_trace_level,
55                      base_trace_timed_type_t time_type, bs_time_t time,
56                      const char *format, va_list variable_args);
57 /*
58  * We provide a set of shorcuts for convineance,
59  *  all of them follow this convention bs_trace_<A>[_line][_time] , where:
60  *    A = {exit|error|warning|info|debug|raw}
61  *    line => with caller file name and line number automatically inserted
62  *    time => with time automatically inserted
63  */
64 
65 #define bs_trace_exit(...)                  bs_trace_print(BS_TRACE_EXIT   ,NULL,    0,       5,BS_TRACE_NOTIME,       0,__VA_ARGS__)
66 #define bs_trace_exit_line(...)             bs_trace_print(BS_TRACE_EXIT   ,__FILE__,__LINE__,5,BS_TRACE_NOTIME,       0,__VA_ARGS__)
67 #define bs_trace_exit_line_time(...)        bs_trace_print(BS_TRACE_EXIT   ,__FILE__,__LINE__,5,BS_TRACE_AUTOTIME,     0,__VA_ARGS__)
68 #define bs_trace_exit_time_line(...)        bs_trace_print(BS_TRACE_EXIT   ,__FILE__,__LINE__,5,BS_TRACE_AUTOTIME,     0,__VA_ARGS__)
69 #define bs_trace_exit_time(...)             bs_trace_print(BS_TRACE_EXIT   ,NULL,    0,       5,BS_TRACE_AUTOTIME,     0,__VA_ARGS__)
70 
71 #define bs_trace_error(...)                 bs_trace_print(BS_TRACE_ERROR  ,NULL,    0,       0,BS_TRACE_NOTIME,       0,__VA_ARGS__)
72 #define bs_trace_error_line(...)            bs_trace_print(BS_TRACE_ERROR  ,__FILE__,__LINE__,0,BS_TRACE_NOTIME,       0,__VA_ARGS__)
73 #define bs_trace_error_line_time(...)       bs_trace_print(BS_TRACE_ERROR  ,__FILE__,__LINE__,0,BS_TRACE_AUTOTIME,     0,__VA_ARGS__)
74 #define bs_trace_error_time_line(...)       bs_trace_print(BS_TRACE_ERROR  ,__FILE__,__LINE__,0,BS_TRACE_AUTOTIME,     0,__VA_ARGS__)
75 #define bs_trace_error_time(...)            bs_trace_print(BS_TRACE_ERROR  ,NULL,    0,       0,BS_TRACE_AUTOTIME,     0,__VA_ARGS__)
76 #define bs_trace_error_manual_time(t,...)   bs_trace_print(BS_TRACE_ERROR  ,NULL,    0,       0,BS_TRACE_TIME_PROVIDED,t,__VA_ARGS__)
77 
78 #define bs_trace_warning(...)               bs_trace_print(BS_TRACE_WARNING,NULL,    0,       0,BS_TRACE_NOTIME,       0,__VA_ARGS__)
79 #define bs_trace_warning_line(...)          bs_trace_print(BS_TRACE_WARNING,__FILE__,__LINE__,0,BS_TRACE_NOTIME,       0,__VA_ARGS__)
80 #define bs_trace_warning_line_time(...)     bs_trace_print(BS_TRACE_WARNING,__FILE__,__LINE__,0,BS_TRACE_AUTOTIME,     0,__VA_ARGS__)
81 #define bs_trace_warning_time_line(...)     bs_trace_print(BS_TRACE_WARNING,__FILE__,__LINE__,0,BS_TRACE_AUTOTIME,     0,__VA_ARGS__)
82 #define bs_trace_warning_time(...)          bs_trace_print(BS_TRACE_WARNING,NULL,    0,       0,BS_TRACE_AUTOTIME,     0,__VA_ARGS__)
83 #define bs_trace_warning_manual_time(t,...) bs_trace_print(BS_TRACE_WARNING  ,NULL,    0,       0,BS_TRACE_TIME_PROVIDED,t,__VA_ARGS__)
84 #define bs_trace_warning_manual_time_line(t,...) bs_trace_print(BS_TRACE_WARNING  ,__FILE__,__LINE__,       0,BS_TRACE_TIME_PROVIDED,t,__VA_ARGS__)
85 
86 #define bs_trace_info(l,...)                bs_trace_print(BS_TRACE_INFO   ,NULL,    0,       l,BS_TRACE_NOTIME,       0,__VA_ARGS__)
87 #define bs_trace_info_line(l,...)           bs_trace_print(BS_TRACE_INFO   ,__FILE__,__LINE__,l,BS_TRACE_NOTIME,       0,__VA_ARGS__)
88 #define bs_trace_info_line_time(l,...)      bs_trace_print(BS_TRACE_INFO   ,__FILE__,__LINE__,l,BS_TRACE_AUTOTIME,     0,__VA_ARGS__)
89 #define bs_trace_info_time_line(l,...)      bs_trace_print(BS_TRACE_INFO   ,__FILE__,__LINE__,l,BS_TRACE_AUTOTIME,     0,__VA_ARGS__)
90 #define bs_trace_info_time(l,...)           bs_trace_print(BS_TRACE_INFO   ,NULL,    0,       l,BS_TRACE_AUTOTIME,     0,__VA_ARGS__)
91 
92 #define bs_trace_debug(l,...)               bs_trace_print(BS_TRACE_DEBUG  ,NULL,    0,       l,BS_TRACE_NOTIME,       0,__VA_ARGS__)
93 #define bs_trace_debug_line(l,...)          bs_trace_print(BS_TRACE_DEBUG  ,__FILE__,__LINE__,l,BS_TRACE_NOTIME,       0,__VA_ARGS__)
94 #define bs_trace_debug_line_time(l,...)     bs_trace_print(BS_TRACE_DEBUG  ,__FILE__,__LINE__,l,BS_TRACE_AUTOTIME,     0,__VA_ARGS__)
95 #define bs_trace_debug_time_line(l,...)     bs_trace_print(BS_TRACE_DEBUG  ,__FILE__,__LINE__,l,BS_TRACE_AUTOTIME,     0,__VA_ARGS__)
96 #define bs_trace_debug_time(l,...)          bs_trace_print(BS_TRACE_DEBUG  ,NULL,    0,       l,BS_TRACE_AUTOTIME,     0,__VA_ARGS__)
97 
98 #define bs_trace_raw(l,...)                 bs_trace_print(BS_TRACE_RAW    ,NULL,    0,       l,BS_TRACE_NOTIME,       0,__VA_ARGS__)
99 #define bs_trace_raw_line(l,...)            bs_trace_print(BS_TRACE_RAW    ,__FILE__,__LINE__,l,BS_TRACE_NOTIME,       0,__VA_ARGS__)
100 #define bs_trace_raw_line_time(l,...)       bs_trace_print(BS_TRACE_RAW    ,__FILE__,__LINE__,l,BS_TRACE_AUTOTIME,     0,__VA_ARGS__)
101 #define bs_trace_raw_time_line(l,...)       bs_trace_print(BS_TRACE_RAW    ,__FILE__,__LINE__,l,BS_TRACE_AUTOTIME,     0,__VA_ARGS__)
102 #define bs_trace_raw_time(l,...)            bs_trace_print(BS_TRACE_RAW    ,NULL,    0,       l,BS_TRACE_AUTOTIME,     0,__VA_ARGS__)
103 #define bs_trace_raw_manual_time(l,t,...)   bs_trace_print(BS_TRACE_RAW    ,NULL,    0,       l,BS_TRACE_TIME_PROVIDED,t,__VA_ARGS__)
104 //Note: the "manual" versions exists only to support bizarre use cases, better do not use them
105 //there is very few cases in which they would really be needed
106 
107 #ifdef __cplusplus
108 }
109 #endif
110 
111 #endif
112