Skip to content

Commit

Permalink
Improvements to the libUseful RaiseError system
Browse files Browse the repository at this point in the history
  • Loading branch information
ColumPaget committed Jan 30, 2020
1 parent 05f5169 commit d5a8841
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 25 deletions.
62 changes: 42 additions & 20 deletions Errors.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,33 @@ ListNode *ErrorsGet()
}


static bool ErrorOutputWanted(int flags)
{
bool result=TRUE;

//if stderr isn't a tty then don't print, unless ERRFLAG_NOTTY is set
//(this is used to disable this check when we consider syslog debugging too)
if (
(! (flags & ERRFLAG_NOTTY)) &&
(! isatty(2))
)
{
if (! LibUsefulGetBool("Error:notty")) result=FALSE;
}

//if this error is a debug error, then don't print unless either LIBUSEFUL_DEBUG environment variable is set
//or the libUseful:Debug internal value is set
if (flags & ERRFLAG_DEBUG)
{
result=FALSE;
if (StrValid(getenv("LIBUSEFUL_DEBUG"))) result=TRUE;
if (LibUsefulGetValue("libUseful:Debug")) result=TRUE;
}

return(result);
}


void InternalRaiseError(int flags, const char *where, const char *file, int line, const char *FmtStr, ...)
{
char *Tempstr=NULL;
Expand All @@ -47,28 +74,23 @@ void InternalRaiseError(int flags, const char *where, const char *file, int line
if (! Errors) Errors=ListCreate();
if (flags & ERRFLAG_ERRNO) ptr=strerror(errno_save);

if (flags & ERRFLAG_DEBUG)
{
ptr=getenv("LIBUSEFUL_DEBUG");
if (StrEnd(ptr))
{
if (LibUsefulGetBool("libUseful:Debug")) ptr="y";
}

if (StrValid(ptr))
{
if (! LibUsefulGetBool("Error:Silent")) fprintf(stderr,"DEBUG: %s %s:%d %s. %s\n", where, file, line, Tempstr, ptr);
if (LibUsefulGetBool("Error:Syslog")) syslog(LOG_ERR,"DEBUG: %s %s:%d %s. %s", where, file, line, Tempstr, ptr);
}
}
else
//first consider if we want to print out a message on stderr
if (ErrorOutputWanted(flags))
{
if (! LibUsefulGetBool("Error:Silent")) fprintf(stderr,"DEBUG: %s %s:%d %s. %s\n", where, file, line, Tempstr, ptr);
}

//now consider if we want to syslog an error message. Pass ERRFLAG_NOTTY as it doesn't matter if stderr is not a
//tty for this case
if (ErrorOutputWanted(flags | ERRFLAG_NOTTY))
{
if (LibUsefulGetBool("Error:Syslog")) syslog(LOG_ERR,"DEBUG: %s %s:%d %s. %s", where, file, line, Tempstr, ptr);
}

//if this Error is just debugging, then never add it to the list of errors
if (! (flags & ERRFLAG_DEBUG))
{
Now=GetTime(TIME_MILLISECS);

if (! LibUsefulGetBool("Error:Silent")) fprintf(stderr,"ERROR: %s %s:%d %s. %s\n", where, file, line, Tempstr, ptr);
if (LibUsefulGetBool("Error:Syslog")) syslog(LOG_ERR,"ERROR: %s %s:%d %s. %s", where, file, line, Tempstr, ptr);


Err=(TError *) calloc(1,sizeof(TError));
Err->errval=errno_save;
Err->when=Now;
Expand Down
31 changes: 26 additions & 5 deletions Errors.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ Copyright (c) 2015 Colum Paget <[email protected]>
#define ERRFLAG_ERRNO 1
#define ERRFLAG_DEBUG 2
#define ERRFLAG_CLEAR 4
#define ERRFLAG_NOTTY 8


/*
Expand Down Expand Up @@ -41,14 +42,34 @@ Errors are injected into the list with RaiseError like this:
the system when RaiseError was called. Also, if RaiseError prints out an error, then the string description of
errno, as obtained from strerror will be appended to the output message.
If the flag ERRFLAG_DEBUG is set then RaiseError won't print anything out unless the environment variable
'LIBUSEFUL_DEBUG' is set (to any value) or the libUseful internal value 'Error:Debug' has been set to 'true' (or
'y' or 'yes') with LibUsefulSetValue. If ERRFLAG_DEBUG is passed then nothing is EVER added to the ErrorList.
Bye default RaiseError will not print out to stderr if stderr is not a tty, in order to prevent sending errors
up pipes and sockets to applications that aren't expecting them. If you want raise error to print even when
stderr is not a tty then either pass the ERRFLAG_NOTTY value, like this
RaiseError(ERRFLAG_ERRNO|ERRFLAG_NOTTY, "ServiceConnect", "host=%s port=%d",Host, Port);
or, if you don't want to have to do this every time you call RaiseError, then you can achieve the same by
setting the libUseful internal boolean value 'Error:notty', like this:
ListUsefulSetValue("Error:notty","Y")
If the flag ERRFLAG_DEBUG is set then RaiseError won't print anything out by default. So this line:
RaiseError(ERRFLAG_DEBUG, "ServiceConnect", "host=%s port=%d",Host, Port);
Wouldn't produce any output normally. This allows you to have debugging lines that can be switched on as
needed. There's two ways to activate debugging. Firstly there's the environment variable 'LIBUSEFUL_DEBUG'.
When this environment variable exists and is non-empty then RaiseError calls will print out, even if they're
passed ERRFLAG_DEBUG. Secondly there's the libuseful internal value 'Error:Debug'. When this has been set to
'true' (or 'y' or 'yes') with LibUsefulSetValue, then again all debugging lines will print out.
If ERRFLAG_DEBUG is passed then nothing is EVER added to the ErrorList, the 'error' is just a debugging line
and it's assumed that the program continues normally.
If the libUseful internal value 'Error:Silent' is set true/yes/y with ListUsefulSetValue then RaiseError never
prints errors to stderr. If the value 'Error:Syslog' is set true/yes/y then error and debug messages are sent
to the system logger via syslog
prints errors to stderr, regardless of any other config.
If the value 'Error:Syslog' is set true/yes/y then error and debug messages are sent to the system logger via syslog
*/


Expand Down

0 comments on commit d5a8841

Please sign in to comment.