Post Scarcity 0.0.6
A prototype for a post scarcity programming environment
Loading...
Searching...
No Matches
debug.c
Go to the documentation of this file.
1/*
2 * debug.c
3 *
4 * Better debug log messages.
5 *
6 * (c) 2017 Simon Brooke <simon@journeyman.cc>
7 * Licensed under GPL version 2.0, or, at your option, any later version.
8 */
9
10#include <ctype.h>
11#include <stdarg.h>
12#include <stdio.h>
13#include <stdlib.h>
14#include <string.h>
15/*
16 * wide characters
17 */
18#include <wchar.h>
19#include <wctype.h>
20
22#include "debug.h"
23#include "memory/dump.h"
24#include "io/io.h"
25#include "io/print.h"
26
27/**
28 * @brief the controlling flags for `debug_print`; set in `init.c`, q.v.
29 *
30 * Interpreted as a set o binary flags. The values are controlled by macros
31 * with names 'DEBUG_[A_Z]*' in `debug.h`, q.v.
32 */
33int verbosity = 0;
34
35/**
36 * When debugging, we want to see exceptions as they happen, because they may
37 * not make their way back down the stack to whatever is expected to handle
38 * them.
39 */
40void debug_print_exception( struct cons_pointer ex_ptr ) {
41#ifdef DEBUG
42 if ( ( verbosity != 0 ) && exceptionp( ex_ptr ) ) {
43 fwide( stderr, 1 );
44 fputws( L"EXCEPTION: ", stderr );
45
46 URL_FILE *ustderr = file_to_url_file( stderr );
47 fwide( stderr, 1 );
48 print( ustderr, ex_ptr );
49 free( ustderr );
50 }
51#endif
52}
53
54/**
55 * @brief print this debug `message` to stderr, if `verbosity` matches `level`.
56 *
57 * `verbosity` is a set of flags, see debug_print.h; so you can
58 * turn debugging on for only one part of the system.
59 */
60void debug_print( wchar_t *message, int level ) {
61#ifdef DEBUG
62 if ( level & verbosity ) {
63 fwide( stderr, 1 );
64 fputws( message, stderr );
65 }
66#endif
67}
68
69/**
70 * @brief print a 128 bit integer value to stderr, if `verbosity` matches `level`.
71 *
72 * `verbosity` is a set of flags, see debug_print.h; so you can
73 * turn debugging on for only one part of the system.
74 *
75 * stolen from https://stackoverflow.com/questions/11656241/how-to-print-uint128-t-number-using-gcc
76 */
77void debug_print_128bit( __int128_t n, int level ) {
78#ifdef DEBUG
79 if ( level & verbosity ) {
80 if ( n == 0 ) {
81 fwprintf( stderr, L"0" );
82 } else {
83 char str[40] = { 0 }; // log10(1 << 128) + '\0'
84 char *s = str + sizeof( str ) - 1; // start at the end
85 while ( n != 0 ) {
86 if ( s == str )
87 return; // never happens
88
89 *--s = "0123456789"[n % 10]; // save last digit
90 n /= 10; // drop it
91 }
92 fwprintf( stderr, L"%s", s );
93 }
94 }
95#endif
96}
97
98/**
99 * @brief print a line feed to stderr, if `verbosity` matches `level`.
100 *
101 * `verbosity` is a set of flags, see debug_print.h; so you can
102 * turn debugging on for only one part of the system.
103 */
104void debug_println( int level ) {
105#ifdef DEBUG
106 if ( level & verbosity ) {
107 fwide( stderr, 1 );
108 fputws( L"\n", stderr );
109 }
110#endif
111}
112
113
114/**
115 * @brief `wprintf` adapted for the debug logging system.
116 *
117 * Print to stderr only if `verbosity` matches `level`. All other arguments
118 * as for `wprintf`.
119 */
120void debug_printf( int level, wchar_t *format, ... ) {
121#ifdef DEBUG
122 if ( level & verbosity ) {
123 fwide( stderr, 1 );
124 va_list( args );
125 va_start( args, format );
126 vfwprintf( stderr, format, args );
127 }
128#endif
129}
130
131/**
132 * @brief print the object indicated by this `pointer` to stderr, if `verbosity`
133 * matches `level`.
134 *
135 * `verbosity` is a set of flags, see debug_print.h; so you can
136 * turn debugging on for only one part of the system.
137 */
138void debug_print_object( struct cons_pointer pointer, int level ) {
139#ifdef DEBUG
140 if ( level & verbosity ) {
141 URL_FILE *ustderr = file_to_url_file( stderr );
142 fwide( stderr, 1 );
143 print( ustderr, pointer );
144 free( ustderr );
145 }
146#endif
147}
148
149/**
150 * @brief Like `dump_object`, q.v., but protected by the verbosity mechanism.
151 *
152 * `verbosity` is a set of flags, see debug_print.h; so you can
153 * turn debugging on for only one part of the system.
154 */
155void debug_dump_object( struct cons_pointer pointer, int level ) {
156#ifdef DEBUG
157 if ( level & verbosity ) {
158 URL_FILE *ustderr = file_to_url_file( stderr );
159 fwide( stderr, 1 );
160 dump_object( ustderr, pointer );
161 free( ustderr );
162 }
163#endif
164}
165
166/**
167 * Standardise printing of binding trace messages.
168 */
169void debug_print_binding( struct cons_pointer key, struct cons_pointer val,
170 bool deep, int level ) {
171#ifdef DEBUG
172 // wchar_t * depth = (deep ? L"Deep" : L"Shallow");
173
174 debug_print( ( deep ? L"Deep" : L"Shallow" ), level );
175 debug_print( L" binding `", level );
176 debug_print_object( key, level );
177 debug_print( L"` to `", level );
178 debug_print_object( val, level );
179 debug_print( L"`\n", level );
180#endif
181}
#define exceptionp(conspoint)
true if conspoint points to an exception, else false
An indirect pointer to a cons cell.
int verbosity
the controlling flags for debug_print; set in init.c, q.v.
Definition debug.c:33
void debug_print_binding(struct cons_pointer key, struct cons_pointer val, bool deep, int level)
Standardise printing of binding trace messages.
Definition debug.c:169
void debug_print_128bit(__int128_t n, int level)
print a 128 bit integer value to stderr, if verbosity matches level.
Definition debug.c:77
void debug_println(int level)
print a line feed to stderr, if verbosity matches level.
Definition debug.c:104
void debug_dump_object(struct cons_pointer pointer, int level)
Like dump_object, q.v., but protected by the verbosity mechanism.
Definition debug.c:155
void debug_printf(int level, wchar_t *format,...)
wprintf adapted for the debug logging system.
Definition debug.c:120
void debug_print(wchar_t *message, int level)
print this debug message to stderr, if verbosity matches level.
Definition debug.c:60
void debug_print_object(struct cons_pointer pointer, int level)
print the object indicated by this pointer to stderr, if verbosity matches level.
Definition debug.c:138
void debug_print_exception(struct cons_pointer ex_ptr)
When debugging, we want to see exceptions as they happen, because they may not make their way back do...
Definition debug.c:40
void dump_object(URL_FILE *output, struct cons_pointer pointer)
dump the object at this cons_pointer to this output stream.
Definition dump.c:59
URL_FILE * file_to_url_file(FILE *f)
given this file handle f, return a new url_file handle wrapping it.
Definition io.c:134
struct cons_pointer print(URL_FILE *output, struct cons_pointer pointer)
Print the cons-space object indicated by pointer to the stream indicated by output.
Definition print.c:156