1 | // |
---|
2 | // Cforall Version 1.0.0 Copyright (C) 2016 University of Waterloo |
---|
3 | // |
---|
4 | // The contents of this file are covered under the licence agreement in the |
---|
5 | // file "LICENCE" distributed with Cforall. |
---|
6 | // |
---|
7 | // libdebug.c -- |
---|
8 | // |
---|
9 | // Author : Thierry Delisle |
---|
10 | // Created On : Thu Mar 30 12:30:01 2017 |
---|
11 | // Last Modified By : |
---|
12 | // Last Modified On : |
---|
13 | // Update Count : 0 |
---|
14 | // |
---|
15 | |
---|
16 | extern "C" { |
---|
17 | #include <stdio.h> |
---|
18 | #include <stdlib.h> |
---|
19 | #include <string.h> |
---|
20 | #include <errno.h> |
---|
21 | #include <stdarg.h> |
---|
22 | #include <unistd.h> |
---|
23 | } |
---|
24 | |
---|
25 | enum { buffer_size = 512 }; |
---|
26 | static char buffer[ buffer_size ]; |
---|
27 | |
---|
28 | extern "C" { |
---|
29 | |
---|
30 | void __lib_debug_write( int fd, const char *in_buffer, int len ) { |
---|
31 | // ensure all data is written |
---|
32 | for ( int count = 0, retcode; count < len; count += retcode ) { |
---|
33 | in_buffer += count; |
---|
34 | |
---|
35 | for ( ;; ) { |
---|
36 | retcode = write( fd, in_buffer, len - count ); |
---|
37 | |
---|
38 | // not a timer interrupt ? |
---|
39 | if ( retcode != -1 || errno != EINTR ) break; |
---|
40 | } |
---|
41 | |
---|
42 | if ( retcode == -1 ) _exit( EXIT_FAILURE ); |
---|
43 | } |
---|
44 | } |
---|
45 | |
---|
46 | void __lib_debug_acquire() __attribute__((__weak__)) {} |
---|
47 | void __lib_debug_release() __attribute__((__weak__)) {} |
---|
48 | |
---|
49 | void __lib_debug_print_safe ( const char fmt[], ... ) __attribute__(( format (printf, 1, 2) )) { |
---|
50 | va_list args; |
---|
51 | |
---|
52 | va_start( args, fmt ); |
---|
53 | __lib_debug_acquire(); |
---|
54 | |
---|
55 | int len = vsnprintf( buffer, buffer_size, fmt, args ); |
---|
56 | __lib_debug_write( STDERR_FILENO, buffer, len ); |
---|
57 | |
---|
58 | __lib_debug_release(); |
---|
59 | va_end( args ); |
---|
60 | } |
---|
61 | |
---|
62 | void __lib_debug_print_nolock( const char fmt[], ... ) __attribute__(( format (printf, 1, 2) )) { |
---|
63 | va_list args; |
---|
64 | |
---|
65 | va_start( args, fmt ); |
---|
66 | |
---|
67 | int len = vsnprintf( buffer, buffer_size, fmt, args ); |
---|
68 | __lib_debug_write( STDERR_FILENO, buffer, len ); |
---|
69 | |
---|
70 | va_end( args ); |
---|
71 | } |
---|
72 | |
---|
73 | void __lib_debug_print_vararg( const char fmt[], va_list args ) { |
---|
74 | int len = vsnprintf( buffer, buffer_size, fmt, args ); |
---|
75 | __lib_debug_write( STDERR_FILENO, buffer, len ); |
---|
76 | } |
---|
77 | |
---|
78 | void __lib_debug_print_buffer( char in_buffer[], int in_buffer_size, const char fmt[], ... ) __attribute__(( format (printf, 3, 4) )) { |
---|
79 | va_list args; |
---|
80 | |
---|
81 | va_start( args, fmt ); |
---|
82 | |
---|
83 | int len = vsnprintf( in_buffer, in_buffer_size, fmt, args ); |
---|
84 | __lib_debug_write( STDERR_FILENO, in_buffer, len ); |
---|
85 | |
---|
86 | va_end( args ); |
---|
87 | } |
---|
88 | } |
---|