Is there a C equivalent for Perl's Carp module? -
In some projects I have done in C, I like to use the following macros which are similar to Pearl's warning and dying work Sub-Routine:
#include & lt; Stdio.h & gt; # Include & lt; Stdlib.h & gt; #define Warning (...) \ fprintf (stderr, __VA_ARGS__); \ Fprintf (stderr, "% s line% d \ n", __FILE__, __LINE__) #define die (...) \ warnings (__VA_ARGS__); Excuse (0xFF)
Do some like Perl's carp, accept crisp, clack, and sub-heroine? I have to do something to report errors from a user's perspective. If not, I know that the backtas () and backtraces_smoles () functions have glibc which can supply me with K-Dination GCC option but with the backcases of function names and code addresses but I Want a little better; With the access to the file, line and function names in the call stack of Pearl's collar sub-routine, I can write my own deburs for use in my programs.
Edit: 2009-10-19
I am thinking of doing something that uses GDB when available at Basnem (argv [0]), then I I want to process stack traces to create different types of messages. It should be able to determine whether the debugable executable is not in a system without GDB, in which case, the carp and click alert and croke will become and will be accepted.
Ive never used like gdb before (Ive only run it with my program at the beginning, not while its already running). But I found some functions in Gib (g_on_error_stack_trace and stack_trace) that looks closer to what I want to do: This argument creates a GDB process with the basename (argv [0]) and process id, then its stdin ( ), Which has been redirected to a pipe) "leave" after the command "backtrace" then reads it from its results and parses the way it likes. This is almost exactly what I need to do.
Well, I never tried to show the call stack, but for the sake of my programs I did the following .
First of all, I define a function that performs actual logging. This is just an example ; Please note that this function is very vulnerable (buffer can overlap anybody?)
invalid strLog (char * file, char * function, int line, char * fmt, .. .) {Char buf [1024]; Va_list args; Va_start (Args, FMT); Vsprintf (buff, fmt, args); Va_end (args); Fprintf (stadder, "% s:% s:% d:% s \ n", file, function, line, buff); }
However, this is not very practical. What is the practical to use macros to call this function?
#define die (...) \ strLog (__FILE__, __PRETTY_FUNCTION__, \ __LINE__, __VA_ARGS__)
Then you printf () < / Code>. Like you can call.
If (Answer == 42) dies ("Oh,% d Absolutely.", Answer);
And you'll get something like this:
main.c: 10: somefunc: oh, exactly 42
Well, there is no backtrace but there is something.
Comments
Post a Comment