:: Re: [DNG] Dng Digest, Vol 13, Issue…
Top Page
Delete this message
Reply to this message
Author: karl
Date:  
To: dng
Subject: Re: [DNG] Dng Digest, Vol 13, Issue 53
Katola2:
> On Mon, Oct 19, 2015 at 09:09:03PM +0200, aitor_czr wrote:
> > I use the '==' operator. For example, in the case of a file:
> >
> > FILE *fp;
> >
> > if ( fp = fopen ( "file_name", "w") == NULL) { ... error message,
> > and exit... }
> >
>
> That's surely legal, but I usually find
>
>    if ( !(fp = fopen(whatever, "r")){ error_message();}

>
> a little more readable. Anyway, I always prefer to disentangle
> assignments and conditional expressions, just and only for the sake of
> improving readability:
>
> fp = fopen(whatever, "r");
> if (!fp){error_message();}
>
> Debugging C code is already a pain in the ass, so there is no reason
> to make it even more painful with syntax back somersaults....

...

To make error handling in c less cluttered, you can do something like:
(see http://turkos.aspodata.se/git/c/libaspoutil/log_util.h for details)

/* syslog() or whatever logging function you want */
#define LOG_PRINTF(  lvl, format, ... ) \
 syslog( lvl, "%s %s() %d: " format, __FILE__, __func__, __LINE__, \
              __VA_ARGS__ )


#define LOG_IF( f, lvl ) \
if ( (f) && ( LOG_PRINTF(lvl, "<%s>", #f), 1 ) )

... and similar

which gives you the possibiblity to do:

///////
int ipio_recv(int fd, uint8_t const *obuf, int const olen, uint8_t *ibuf,
              int const ilen) {
  int retstat = 0;


errno = 0;
LOG_IF(fd == -1, LOG_CRIT) return -1;
LOG_IF(obuf == NULL, LOG_CRIT) return -1;
LOG_IF(olen < 4, LOG_CRIT) return -1;
LOG_IF(ibuf == NULL, LOG_CRIT) return -1;
// though STATUS requires 10 bytes, 4 is a minimum
LOG_IF(ilen < 5, LOG_CRIT) return -1;

  errno = 0;
  retstat = recv(fd, ibuf, ilen, 0);
  if (retstat == -1) {
    if (errno != EAGAIN)
      CRIT("recv(fd, ibuf, ilen, 0): %s (%d)", strerror(errno), errno);
    return errno;
  }


  if      (retstat == 0)
    NOTICE("%s", "peer has shutdown");
  else if (retstat < 5)
    NOTICE("too short (%d) response", retstat);
  else if (ibuf[1] != obuf[1])
    NOTICE("id (%d) didn't match response (%d)", obuf[1], ibuf[1]);
  else if (ibuf[2] != obuf[0])
    NOTICE("command (%x) didn't match response (%x)", obuf[0], ibuf[2]);
  else if (ibuf[3] != obuf[2])
    NOTICE("address (%x) didn't match response (%x)", obuf[2], ibuf[3]);


return retstat;
}
///////

and get logs (depending on your logging function) like:

2015-10-20 15:44:48.31: NOTI ipio.c ipio_recv() 94: id (75) didn't match response (74)

////

My point is that I can write

LOG_IF(fd == -1, LOG_CRIT) return -1; // maybe I should use (fd < 0)

instead of

  if(fd == -1) {
    //your logging thing
    return -1;
  }


bonus points I get a copy the code (fd == -1), source file, line no,
severety of the error, and optional message in one go.
And depending on logging function, ability to filter out messages of
too low severety.

Regards,
/Karl Hammar

-----------------------------------------------------------------------
Aspö Data
Lilla Aspö 148
S-742 94 Östhammar
Sweden
+46 173 140 57