2 * Worldvisions Weaver Software:
3 * Copyright (C) 2005 Net Integration Technologies, Inc.
5 * Routines to save messages that can be logged when a program crashes.
14 IWvStream
*WvCrashInfo::in_stream
= NULL
;
15 const char *WvCrashInfo::in_stream_id
= NULL
;
16 enum WvCrashInfo::InStreamState
WvCrashInfo::in_stream_state
= UNUSED
;
17 static const int ring_buffer_order
= wvcrash_ring_buffer_order
;
18 static const int ring_buffer_size
= wvcrash_ring_buffer_size
;
19 static const int ring_buffer_mask
= ring_buffer_size
- 1;
20 static char ring_buffer
[ring_buffer_size
+1];
21 static int ring_buffer_start
= 0, ring_buffer_used
= 0;
23 void wvcrash_ring_buffer_put(const char *str
)
25 wvcrash_ring_buffer_put(str
, strlen(str
));
29 void wvcrash_ring_buffer_put(const char *str
, size_t len
)
33 int pos
= (ring_buffer_start
+ ring_buffer_used
) & ring_buffer_mask
;
34 ring_buffer
[pos
] = *str
++;
36 if (ring_buffer_used
== ring_buffer_size
)
37 ring_buffer_start
= (ring_buffer_start
+ 1) & ring_buffer_mask
;
44 const char *wvcrash_ring_buffer_get()
46 if (ring_buffer_used
== 0)
49 if (ring_buffer_start
+ ring_buffer_used
>= ring_buffer_size
)
51 ring_buffer
[ring_buffer_size
] = '\0';
52 result
= &ring_buffer
[ring_buffer_start
];
53 ring_buffer_used
-= ring_buffer_size
- ring_buffer_start
;
54 ring_buffer_start
= 0;
58 ring_buffer
[ring_buffer_start
+ ring_buffer_used
] = '\0';
59 result
= &ring_buffer
[ring_buffer_start
];
60 ring_buffer_start
+= ring_buffer_used
;
68 // FIXME: leaving of a will and catching asserts mostly only works in Linux
72 static const char *argv0
= program_invocation_short_name
;
74 static const char *argv0
= "UNKNOWN";
77 // Reserve enough buffer for a screenful of programme.
78 static const int buffer_size
= 2048;
79 static char will_msg
[buffer_size
];
80 static char assert_msg
[buffer_size
];
86 void __assert_fail(const char *__assertion
, const char *__file
,
87 unsigned int __line
, const char *__function
)
89 // Set the assert message that WvCrash will dump.
90 snprintf(assert_msg
, buffer_size
,
91 "%s: %s:%u: %s: Assertion `%s' failed.\n",
92 argv0
, __file
, __line
, __function
, __assertion
);
93 assert_msg
[buffer_size
- 1] = '\0';
95 // Emulate the GNU C library's __assert_fail().
96 fprintf(stderr
, "%s: %s:%u: %s: Assertion `%s' failed.\n",
97 argv0
, __file
, __line
, __function
, __assertion
);
102 // Wrapper for standards compliance.
103 void __assert(const char *__assertion
, const char *__file
,
104 unsigned int __line
, const char *__function
)
106 __assert_fail(__assertion
, __file
, __line
, __function
);
110 // Support the GNU assert_perror() extension.
111 void __assert_perror_fail(int __errnum
, const char *__file
,
112 unsigned int __line
, const char *__function
)
114 // Set the assert message that WvCrash will dump.
115 snprintf(assert_msg
, buffer_size
,
116 "%s: %s:%u: %s: Unexpected error: %s.\n",
117 argv0
, __file
, __line
, __function
, strerror(__errnum
));
118 assert_msg
[buffer_size
- 1] = '\0';
120 // Emulate the GNU C library's __assert_perror_fail().
121 fprintf(stderr
, "%s: %s:%u: %s: Unexpected error: %s.\n",
122 argv0
, __file
, __line
, __function
, strerror(__errnum
));
128 // This function is meant to support people who wish to leave a last will
129 // and testament in the WvCrash.
130 void wvcrash_leave_will(const char *will
)
134 strncpy(will_msg
, will
, buffer_size
);
135 will_msg
[buffer_size
- 1] = '\0';
142 const char *wvcrash_read_will()
148 const char *wvcrash_read_assert()
154 void __wvcrash_init_buffers(const char *program_name
)
157 argv0
= program_name
;
159 assert_msg
[0] = '\0';
163 #else // this is NOT __linux
165 void wvcrash_leave_will(const char *will
) {}
166 const char *wvcrash_read_will() { return NULL
; }