bad char
[vg.git] / vg_log.c
1 #include <stdarg.h>
2 #include <string.h>
3 #include <malloc.h>
4 #include "vg_platform.h"
5 #include "vg_log.h"
6 #include "vg_string.h"
7
8 #ifndef _WIN32
9 #include <execinfo.h>
10 #endif
11
12 struct vg_log vg_log;
13
14 static void _vg_log_append_line( const char *str )
15 {
16 if( vg_log.log_line_count < vg_list_size( vg_log.log ) )
17 vg_log.log_line_count ++;
18
19 char *dest = vg_log.log[ vg_log.log_line_current ++ ];
20 vg_strncpy( str, dest, vg_list_size(vg_log.log[0]), k_strncpy_allow_cutoff );
21
22 if( vg_log.log_line_current >= vg_list_size( vg_log.log ) )
23 vg_log.log_line_current = 0;
24 }
25
26 void _vg_logx_va( FILE *file,
27 const char *location, const char *prefix,
28 const char *colour,
29 const char *fmt, va_list args )
30 {
31
32 /* @model.h:2000 info| dwahjdiawdjaiwdwadwa djaidjwa\n
33 * | djwaidwaj waodawh a\n
34 * | dwajdkiawjdiw
35 */
36
37 #ifdef VG_ENGINE
38 SDL_AtomicLock( &vg_log.print_sl );
39 #endif
40
41 char buffer[4096];
42
43 vsnprintf( buffer, vg_list_size(buffer), fmt, args );
44
45 const char *line = buffer;
46 char logline[96];
47
48 for( u32 i=0; i<vg_list_size(buffer); i++ ){
49 char c = buffer[i];
50
51 if( c == '\0' || c == '\n' ){
52 buffer[i] = '\0';
53
54 const char *line_prefix = "",
55 *line_location = "";
56
57 if( line == buffer ) {
58 line_prefix = prefix;
59 line_location = location;
60 }
61
62 snprintf( logline, 96, "%s%7s" KNRM "|%s %s",
63 colour, line_prefix, colour, line );
64 _vg_log_append_line( logline );
65
66 if( location ){
67 #ifdef VG_ENGINE
68 const char *thread_colours[] = {
69 KGRN, KMAG, KCYN, KYEL, KBLU
70 };
71
72 const char *colour = thread_colours[
73 (vg_thread_purpose() % vg_list_size( thread_colours ))];
74
75 fprintf( file, "%s[%u]"KNRM"%.32s",
76 colour, vg_thread_purpose(), line_location );
77 #else
78 fprintf( file, KNRM "%.32s", line_location );
79 #endif
80 }
81
82 fputs( logline, file );
83 fputc( '\n', file );
84 fputs( KNRM, file );
85
86 if( c == '\0' ) break;
87 if( buffer[i+1] == '\0' ) break;
88 line = buffer+i+1;
89 }
90 }
91
92 #ifdef VG_ENGINE
93 SDL_AtomicUnlock( &vg_log.print_sl );
94 #endif
95 }
96
97 void vg_logx( FILE *file,
98 const char *location, const char *prefix,
99 const char *colour,
100 const char *fmt, ... )
101 {
102
103 va_list args;
104 va_start( args, fmt );
105 _vg_logx_va( file,
106 #ifdef VG_LOG_SOURCE_INFO
107 location,
108 #else
109 NULL,
110 #endif
111 prefix, colour, fmt, args );
112 va_end( args );
113 }
114
115 void vg_print_backtrace(void)
116 {
117 #ifndef _WIN32
118 void *array[20];
119 char **strings;
120 int size, i;
121
122 size = backtrace( array, 20 );
123 strings = backtrace_symbols( array, size );
124
125 if( strings != NULL )
126 {
127 vg_error( "---------------- gnu backtrace -------------\n" );
128
129 for( int i=0; i<size; i++ )
130 vg_info( "%s\n", strings[i] );
131
132 vg_error( "---------------- gnu backtrace -------------\n" );
133 }
134
135 free( strings );
136
137 #endif
138 }