wvcrashbase.cc

00001 /*
00002  * Worldvisions Weaver Software:
00003  *   Copyright (C) 2005 Net Integration Technologies, Inc.
00004  *
00005  * Routines to save messages that can be logged when a program crashes.
00006  */
00007 #include "wvcrash.h"
00008 
00009 #include <errno.h>
00010 #include <stdio.h>
00011 #include <stdlib.h>
00012 #include <string.h>
00013 
00014 // FIXME: this file mostly only works in Linux
00015 #ifdef __linux
00016 
00017 #ifdef __USE_GNU
00018 static const char *argv0 = program_invocation_short_name;
00019 #else
00020 static const char *argv0 = "UNKNOWN";
00021 #endif // __USE_GNU
00022 
00023 // Reserve enough buffer for a screenful of programme.
00024 static const int buffer_size = 2048;
00025 static char will_msg[buffer_size];
00026 static char assert_msg[buffer_size];
00027 
00028 extern "C"
00029 {
00030     // Support assert().
00031     void __assert_fail(const char *__assertion, const char *__file,
00032                        unsigned int __line, const char *__function)
00033     {
00034         // Set the assert message that WvCrash will dump.
00035         snprintf(assert_msg, buffer_size,
00036                  "%s: %s:%u: %s: Assertion `%s' failed.\n",
00037                  argv0, __file, __line, __function, __assertion);
00038         assert_msg[buffer_size - 1] = '\0';
00039 
00040         // Emulate the GNU C library's __assert_fail().
00041         fprintf(stderr, "%s: %s:%u: %s: Assertion `%s' failed.\n",
00042                 argv0, __file, __line, __function, __assertion);
00043         abort();
00044     }
00045 
00046 
00047     // Wrapper for standards compliance.
00048     void __assert(const char *__assertion, const char *__file,
00049                   unsigned int __line, const char *__function)
00050     {
00051         __assert_fail(__assertion, __file, __line, __function);
00052     }
00053 
00054 
00055     // Support the GNU assert_perror() extension.
00056     void __assert_perror_fail(int __errnum, const char *__file,
00057                               unsigned int __line, const char *__function)
00058     {
00059         // Set the assert message that WvCrash will dump.
00060         snprintf(assert_msg, buffer_size,
00061                  "%s: %s:%u: %s: Unexpected error: %s.\n",
00062                  argv0, __file, __line, __function, strerror(__errnum));
00063         assert_msg[buffer_size - 1] = '\0';
00064 
00065         // Emulate the GNU C library's __assert_perror_fail().
00066         fprintf(stderr, "%s: %s:%u: %s: Unexpected error: %s.\n",
00067                 argv0, __file, __line, __function, strerror(__errnum));
00068         abort();
00069     }
00070 } // extern "C"
00071 
00072 
00073 // This function is meant to support people who wish to leave a last will
00074 // and testament in the WvCrash.
00075 void wvcrash_leave_will(const char *will)
00076 {
00077     if (will)
00078     {
00079         strncpy(will_msg, will, buffer_size);
00080         will_msg[buffer_size - 1] = '\0';
00081     }
00082     else
00083         will_msg[0] = '\0';
00084 }
00085 
00086 
00087 const char *wvcrash_read_will()
00088 {
00089     return will_msg;
00090 }
00091 
00092 
00093 const char *wvcrash_read_assert()
00094 {
00095     return assert_msg;
00096 }
00097 
00098 
00099 void __wvcrash_init_buffers(const char *program_name)
00100 {
00101     if (program_name)
00102         argv0 = program_name;
00103     will_msg[0] = '\0';
00104     assert_msg[0] = '\0';
00105 }
00106 
00107 
00108 #else // __linux
00109 
00110 void wvcrash_leave_will(const char *will) {}
00111 const char *wvcrash_read_will() { return NULL; }
00112 
00113 #endif // __linux

Generated on Wed Jul 12 17:53:20 2006 for WvStreams by  doxygen 1.4.7