aboutsummaryrefslogtreecommitdiffstats
path: root/generate_debug_h.sh
blob: 209495ccb9cc42b7735999e715949a2217737353 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
#!/bin/sh
#
# Script to generate debug.h
# Output goes to stdout, this should be redirected by the caller

# Configuration
DEBUG_LEVELS=4
DEBUG_PARTS="MAIN ARGPARSE SYSFS CPUFREQ LOG"
DEBUG_HELPER_DEF=`grep -o "void .*()" debug.c | head -n 1`
DEBUG_HELPER=${DEBUG_HELPER_DEF##* }
HEXDUMP_HELPER_DEF=`grep -o "void .*(unsigned char \*data,int len)" debug.c`
HEXDUMP_HELPER=${HEXDUMP_HELPER_DEF#* }
HEXDUMP_HELPER=${HEXDUMP_HELPER%%(*}

# Generate the configuration part
cat << EOF
#ifndef HAVE_DEBUG_H
#define HAVE_DEBUG_H

// Configuration
EOF

for PART in ALL $DEBUG_PARTS
do
 VALUE=0
 if [ -r "debug.h" ]
 then
  VALUE=`grep "^#define SET_DEBUG_$PART " debug.h | head -n 1 | awk '{ print $NF }' | tr -cd "[0-9]"`
 fi
 if [ -n "$VALUE" ]
 then
  echo "#define SET_DEBUG_$PART $VALUE"
 else
  echo "#define SET_DEBUG_$PART 0"
 fi
done

for SETTING in SET_DEBUG_PRINT_LEVEL SET_DEBUG_PRINT_TIME SET_DEBUG_PRINT_LINE SET_DEBUG_PRINT_FUNCTION
do
 if [ -r "debug.h" ]
 then
  if grep -q "^#define $SETTING" debug.h
  then
   echo "#define $SETTING"
  else
   echo "//#define $SETTING"
  fi
 else
  echo "#define $SETTING"
 fi
done
echo "// End of configuration"
echo ""

# Configure prefix
echo "// Set prefix"
if [ -n "$DEBUG_PREFIX_CONDITION" ]
then
 echo "$DEBUG_PREFIX_CONDITION"
fi
echo "#define SET_DEBUG_PRINT_PREFIX \"$DEBUG_PREFIX\""
if [ -n "$DEBUG_PREFIX_CONDITION" ]
then
 echo "#endif"
fi
echo ""

# Generate the part to make ALL work
echo "// Make ALL work"
for PART in $DEBUG_PARTS
do
 echo "#if (SET_DEBUG_ALL>SET_DEBUG_$PART)"
 echo " #undef SET_DEBUG_$PART"
 echo " #define SET_DEBUG_$PART SET_DEBUG_ALL"
 echo "#endif"
 echo ""
done

# Check if we need stdio pr the helper
echo "// Check if we need stdio.h or the helpers"
for PART in $DEBUG_PARTS
do
 echo "#if (SET_DEBUG_$PART>0)"
 echo " #ifndef NEED_STDIO_H"
 echo "  #define NEED_STDIO_H"
 echo " #endif"
 echo " #ifdef SET_DEBUG_PRINT_TIME"
# echo "  #ifdef NEED_DEBUG_HELPER"
# echo "   #undef NEED_DEBUG_HELPER"
# echo "  #endif"
 echo "  #ifndef NEED_DEBUG_HELPER"
 echo "   #define NEED_DEBUG_HELPER"
 echo "  #endif"
 echo " #endif"
 echo " #ifndef NEED_HEXDUMP_HELPER"
 echo "  #define NEED_HEXDUMP_HELPER"
 echo " #endif"
 echo "#endif"
 echo ""
done

# Print the format definitions
# Define printing of prefixes
cat <<EOF
// Define the printing of the debug prefix
#ifdef SET_DEBUG_PRINT_PREFIX
 #define DEBUG_PRINT_PREFIX fprintf(stderr,"%s",SET_DEBUG_PRINT_PREFIX);
#else
 #define DEBUG_PRINT_PREFIX
#endif

EOF
# Generate the ammount of levels requested
echo "// Define the printing of debuglevels"
echo "#ifdef SET_DEBUG_PRINT_LEVEL"
for((COUNT=1;COUNT<=$DEBUG_LEVELS;COUNT++))
do
 echo " #define DEBUG_PRINT_LEVEL$COUNT fprintf(stderr,\"$COUNT: \");"
done
echo "#else"
for((COUNT=1;COUNT<=$DEBUG_LEVELS;COUNT++))
do
 echo " #define DEBUG_PRINT_LEVEL$COUNT"
done
echo "#endif"
echo ""

echo "// Debug timestamps definition"
echo "#ifdef SET_DEBUG_PRINT_TIME"
#echo "#define NEED_DEBUG_HELPER"
echo " #define DEBUG_PRINT_TIME $DEBUG_HELPER;"
echo "#else"
echo " #define DEBUG_PRINT_TIME"
echo "#endif"
echo ""

cat << EOF
// Define the printing of file names and line numbers
#ifdef SET_DEBUG_PRINT_LINE
 #define DEBUG_PRINT_LINE fprintf(stderr,"%s:%d: ",__FILE__,__LINE__);
#else
 #define DEBUG_PRINT_LINE
#endif

// Define the printing of function names
#ifdef SET_DEBUG_PRINT_FUNCTION
 #define DEBUG_PRINT_FUNCTION fprintf(stderr,"%s: ",__func__);
#else
 #define DEBUG_PRINT_FUNCTION
#endif

EOF

echo "// Define debugging format"
for((COUNT=1;COUNT<=$DEBUG_LEVELS;COUNT++))
do
 echo "#define DEBUG${COUNT}_FORMAT(...) \\"
 echo " DEBUG_PRINT_PREFIX \\"
 echo " DEBUG_PRINT_LEVEL${COUNT} \\"
 echo " DEBUG_PRINT_TIME \\"
 echo " DEBUG_PRINT_LINE \\"
 echo " DEBUG_PRINT_FUNCTION \\"
 echo " fprintf(stderr,__VA_ARGS__);"
 echo ""
done

# Generate the debug functions
echo "// Define the functions"
for PART in $DEBUG_PARTS
do
 for((COUNT=1;COUNT<=$DEBUG_LEVELS;COUNT++))
 do
  echo "#if (SET_DEBUG_$PART>$((COUNT-1)))"
  echo " #define DEBUG${COUNT}_$PART(...) DEBUG${COUNT}_FORMAT(__VA_ARGS__)"
  echo " #define HEXDUMP${COUNT}_$PART(data,datalen) $HEXDUMP_HELPER(data,datalen);"
  echo "#else"
  echo " #define DEBUG${COUNT}_$PART(...)"
  echo " #define HEXDUMP${COUNT}_$PART(data,datalen)"
  echo "#endif"
  echo ""
 done
done

# Include stdio when needed
echo "#ifdef NEED_STDIO_H"
echo " #include <stdio.h>"
echo "#endif"

# Define helper function
echo "#ifdef NEED_DEBUG_HELPER"
echo " $DEBUG_HELPER_DEF;"
echo "#endif"

# Define hexdump function
echo "#ifdef NEED_HEXDUMP_HELPER"
echo " $HEXDUMP_HELPER_DEF;"
echo "#endif"

# Finish the ifdef HAVE_DEBUG_H
echo ""
echo "#endif"