UNIX Environment advanced Programming-Error handling function __unix

Source: Internet
Author: User
Tags error handling sprintf syslog

UNIX Environment Advanced Programming book the error-handling functions that are often used by the sample programs.

Save these functions in a file for easy learning and use.

My_err.h

#include <errno.h> #include <stdarg.h> static void Err_doit (int, const char*, va_list);

char *pname = NULL;
void Err_ret (const char *FMT, ...)
    {va_list ap;
    Va_start (AP, FMT);
    Err_doit (1, FMT, AP);
    Va_end (AP);
return;
} void Err_sys (const char *FMT, ...)
    {va_list ap;
    Va_start (AP, FMT);
    Err_doit (1, FMT, AP);
    Va_end (AP);
Exit (1);
} void Err_dump (const char *FMT, ...)
    {va_list ap;
    Va_start (AP, FMT);
    Err_doit (1, FMT, AP);
    Va_end (AP);
    Abort ();
Exit (1);
} void Err_msg (const char *FMT, ...)
    {va_list ap;
    Va_start (AP, FMT);
    Err_doit (0, FMT, AP);
    Va_end (AP);
return;
} void Err_quit (const char *FMT, ...)
    {va_list ap;
    Va_start (AP, FMT);
    Err_doit (0, FMT, AP);
    Va_end (AP);
Exit (1);
    static void Err_doit (int errnoflag, const char *FMT, va_list ap) {int errno_save;
    
    Char Buf[maxline];
    Errno_save = errno;
    vsprintf (BUF, FMT, AP);
        if (Errnoflag)sprintf (Buf+strlen (BUF), ":%s", Strerror (Errno_save));
    strcat (buf, "\ n");
    Fflush (stdout);
    Fputs (buf, stderr);
    Fflush (NULL);
return; }


My_log.h

#include <errno.h> #include <stdarg.h> #include <syslog.h> #include "ourhdr.h" static void Log_doit (in

t, int, const char*, va_list AP);

extern int debug; 

void Log_open (const char *ident, int option, int facility) {if (debug==0) openlog (Ident, option, facility);
void Log_ret (const char *FMT, ...)
    {va_list ap;
    Va_start (AP, FMT);
    Log_doit (1, Log_err, FMT, AP);
    Va_end (AP);
return;
} void Log_sys (const char *FMT, ...)
    {va_list ap;
    Va_start (AP, FMT);
    Log_doit (1, Log_err, FMT, AP);
    Va_end (AP);
Exit (2);
} void Log_msg (const char *FMT, ...)
    {va_list ap;
    Va_start (AP, FMT);
    Log_doit (0, Log_err, FMT, AP);
    Va_end (AP);
return;
} void Log_quit (const char *FMT, ...)
    {va_list ap;
    Va_start (AP, FMT);
    Log_doit (0, Log_err, FMT, AP);
    Va_end (AP);
Exit (2);
    static void Log_doit (int errnoflag, int priority, const char *FMT, va_list ap) {int errno_save;
    
Char Buf[maxline];    Errno_save = errno;
    vsprintf (BUF, FMT, AP);
    if (Errnoflag) sprintf (Buf+strlen (BUF), ":%s", Strerror (Errno_save));
    strcat (buf, "\ n");
        if (Debug) {fflush (stdout);
        Fputs (buf, stderr);
    Fflush (stderr);
    else Syslog (priority, BUF);
return; }


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.