Post Scarcity
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 * @brief print this debug `message` to stderr, if `verbosity` matches `level`.
37 *
38 * `verbosity` is a set of flags, see debug_print.h; so you can
39 * turn debugging on for only one part of the system.
40 */
41void debug_print( wchar_t *message, int level ) {
42#ifdef DEBUG
43 if ( level & verbosity ) {
44 fwide( stderr, 1 );
45 fputws( message, stderr );
46 }
47#endif
48}
49
50/**
51 * @brief print a 128 bit integer value to stderr, if `verbosity` matches `level`.
52 *
53 * `verbosity` is a set of flags, see debug_print.h; so you can
54 * turn debugging on for only one part of the system.
55 *
56 * stolen from https://stackoverflow.com/questions/11656241/how-to-print-uint128-t-number-using-gcc
57 */
58void debug_print_128bit( __int128_t n, int level ) {
59#ifdef DEBUG
60 if ( level & verbosity ) {
61 if ( n == 0 ) {
62 fwprintf( stderr, L"0" );
63 } else {
64 char str[40] = { 0 }; // log10(1 << 128) + '\0'
65 char *s = str + sizeof( str ) - 1; // start at the end
66 while ( n != 0 ) {
67 if ( s == str )
68 return; // never happens
69
70 *--s = "0123456789"[n % 10]; // save last digit
71 n /= 10; // drop it
72 }
73 fwprintf( stderr, L"%s", s );
74 }
75 }
76#endif
77}
78
79/**
80 * @brief print a line feed to stderr, if `verbosity` matches `level`.
81 *
82 * `verbosity` is a set of flags, see debug_print.h; so you can
83 * turn debugging on for only one part of the system.
84 */
85void debug_println( int level ) {
86#ifdef DEBUG
87 if ( level & verbosity ) {
88 fwide( stderr, 1 );
89 fputws( L"\n", stderr );
90 }
91#endif
92}
93
94
95/**
96 * @brief `wprintf` adapted for the debug logging system.
97 *
98 * Print to stderr only if `verbosity` matches `level`. All other arguments
99 * as for `wprintf`.
100 */
101void debug_printf( int level, wchar_t *format, ... ) {
102#ifdef DEBUG
103 if ( level & verbosity ) {
104 fwide( stderr, 1 );
105 va_list( args );
106 va_start( args, format );
107 vfwprintf( stderr, format, args );
108 }
109#endif
110}
111
112/**
113 * @brief print the object indicated by this `pointer` to stderr, if `verbosity`
114 * matches `level`.
115 *
116 * `verbosity` is a set of flags, see debug_print.h; so you can
117 * turn debugging on for only one part of the system.
118 */
119void debug_print_object( struct cons_pointer pointer, int level ) {
120#ifdef DEBUG
121 if ( level & verbosity ) {
122 URL_FILE *ustderr = file_to_url_file( stderr );
123 fwide( stderr, 1 );
124 print( ustderr, pointer );
125 free( ustderr );
126 }
127#endif
128}
129
130/**
131 * @brief Like `dump_object`, q.v., but protected by the verbosity mechanism.
132 *
133 * `verbosity` is a set of flags, see debug_print.h; so you can
134 * turn debugging on for only one part of the system.
135 */
136void debug_dump_object( struct cons_pointer pointer, int level ) {
137#ifdef DEBUG
138 if ( level & verbosity ) {
139 URL_FILE *ustderr = file_to_url_file( stderr );
140 fwide( stderr, 1 );
141 dump_object( ustderr, pointer );
142 free( ustderr );
143 }
144#endif
145}
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_128bit(__int128_t n, int level)
print a 128 bit integer value to stderr, if verbosity matches level.
Definition debug.c:58
void debug_println(int level)
print a line feed to stderr, if verbosity matches level.
Definition debug.c:85
void debug_dump_object(struct cons_pointer pointer, int level)
Like dump_object, q.v., but protected by the verbosity mechanism.
Definition debug.c:136
void debug_printf(int level, wchar_t *format,...)
wprintf adapted for the debug logging system.
Definition debug.c:101
void debug_print(wchar_t *message, int level)
print this debug message to stderr, if verbosity matches level.
Definition debug.c:41
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:119
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:151