fehQlibs 26
Qlibs
Loading...
Searching...
No Matches
logmsg.c
Go to the documentation of this file.
1#include <unistd.h>
2#include <stdarg.h>
3#include <errno.h>
4#include "buffer.h"
5#include "fmt.h"
6#include "str.h"
7#include "stralloc.h"
8#include "logmsg.h"
9
17#define WHO "logmsg"
18
19char *build_log_msg(const char *x[])
20{
21 stralloc sa = {0};
22 stralloc_copys(&sa,"");
23
24 while(*x) { if (!stralloc_cats(&sa,*x++)) err_sys(WHO,errno); } /* concatenate *x */
25
26 if (!stralloc_0(&sa)) err_sys(WHO,errno);
27 return(sa.s);
28}
29
30void logmsg(const char *who,int ecode,unsigned int class,const char *msg)
31{
32 char strnum[FMT_ULONG];
33 char *codestr = "";
34 char *classstr = "";
35 char *errmsg = "";
36
37 errno = 0; /* re-initialize errno, value is in 'code' now */
38
39/* Part 1: obtain the (error) code -- perhaps received from OS */
40
41 if (ecode != 0) {
42 codestr = "";
43 if (ecode < 0) { // check for negative error codes
44 ecode = (ecode^-1) + 1;
45 codestr = "-";
46 }
47 strnum[fmt_ulong(strnum,ecode)] = 0; /* format for output */
48 char *temp = strnum;
49 codestr = str_cat(codestr,temp);
50 }
51
52/* Part 2: behavioral on error */
53
54 switch (class) {
55 case ERROR: classstr = "error: "; break; // info + exit
56 case FATAL: classstr = "fatal: "; break; // info + exit
57 case DROP: classstr = "drop: "; break; // info + next call/iteration
58 case ALERT: classstr = "alert: "; break; // info + next statement
59 case WARN: classstr = "warning: "; break; // info + next statement
60 case INFO: classstr = "info: "; break; // info + continue
61 case SYNTAX: classstr = "syntax: "; break; // info + exit
62 case USAGE: classstr = "usage: "; break; // info + exit
63 case TEMP: classstr = "temp: "; break; // info + exit
64 case CAT: classstr = ""; break; // info w/o \n
65 default:
66 class = LOG; classstr = ""; break; // custom info + continue
67 }
68
69/* Part 3: get system error message */
70
71 if (class == FATAL || class == DROP)
72 errmsg = error_str(errno);
73
74/* Part 4: construct log message: Source: Class (Ecode) Message: Errmsg */
75
78 buffer_puts(buffer_2,classstr);
79 if (class == FATAL || class == DROP || class == ERROR) {
81 buffer_puts(buffer_2,codestr);
83 }
85 if (errno) {
87 buffer_puts(buffer_2,errmsg);
88 }
89 if (class != CAT) {
92 }
93
94 if (class == USAGE) _exit(USAGE);
95 if (class == SYNTAX) _exit(SYNTAX);
96 if (class == FATAL || class == DROP || class == ERROR) _exit(ecode);
97}
char * build_log_msg(const char *x[])
Definition: logmsg.c:19
void logmsg(const char *who, int ecode, unsigned int class, const char *msg)
Definition: logmsg.c:30
#define WHO
Definition: logmsg.c:17
#define err_sys(w, e)
Definition: logmsg.h:13
void _exit(int)
#define TEMP
Definition: error.h:46
#define LOG
Definition: error.h:44
#define WARN
Definition: error.h:48
#define FATAL
Definition: error.h:54
#define USAGE
Definition: error.h:51
#define ERROR
Definition: error.h:55
#define CAT
Definition: error.h:43
#define error_str(i)
Definition: error.h:10
#define ALERT
Definition: error.h:47
#define SYNTAX
Definition: error.h:52
#define INFO
Definition: error.h:45
#define DROP
Definition: error.h:53
int buffer_puts(buffer *, const char *)
Definition: buffer.c:218
buffer * buffer_2
Definition: buffer.c:48
int buffer_flush(buffer *)
Definition: buffer.c:161
conversion function declarations
unsigned int fmt_ulong(char *, unsigned long)
Definition: fmt.c:44
#define FMT_ULONG
Definition: fmt.h:11
#define str_cat(s, t)
Definition: str.h:23
#define stralloc_0(sa)
Definition: stralloc.h:37
int stralloc_cats(stralloc *, const char *)
Definition: stralloc.c:36
int stralloc_copys(stralloc *, const char *)
Definition: stralloc.c:79
char * s
Definition: stralloc.h:18