lolsyslog.c (1897B)
1 /* ISC license. */ 2 3 #undef INTERNAL_MARK 4 #ifndef SYSLOG_NAMES 5 #define SYSLOG_NAMES 6 #endif 7 8 #include <skalibs/nonposix.h> 9 10 #include <string.h> 11 #include <syslog.h> 12 13 #include <skalibs/types.h> 14 15 #include "lolsyslog.h" 16 17 #ifndef INTERNAL_MARK 18 19 typedef struct CODE_s CODE, *CODE_ref ; 20 struct CODE_s 21 { 22 char *c_name ; 23 unsigned int c_val ; 24 } ; 25 26 #define LOG_PRI(p) ((p) & LOG_PRIMASK) 27 #define LOG_FAC(p) (((p) & LOG_FACMASK) / (LOG_PRIMASK + 1)) 28 29 static CODE const facilitynames[] = 30 { 31 { "kern", LOG_KERN }, 32 { "user", LOG_USER }, 33 { "mail", LOG_MAIL }, 34 { "news", LOG_NEWS }, 35 { "uucp", LOG_UUCP }, 36 { "daemon", LOG_DAEMON }, 37 { "auth", LOG_AUTH }, 38 { "cron", LOG_CRON }, 39 { "lpr", LOG_LPR }, 40 #ifdef LOG_SYSLOG 41 { "syslog", LOG_SYSLOG }, 42 #endif 43 #ifdef LOG_AUDIT 44 { "audit", LOG_AUDIT }, 45 #endif 46 { "local0", LOG_LOCAL0 }, 47 { "local1", LOG_LOCAL1 }, 48 { "local2", LOG_LOCAL2 }, 49 { "local3", LOG_LOCAL3 }, 50 { "local4", LOG_LOCAL4 }, 51 { "local5", LOG_LOCAL5 }, 52 { "local6", LOG_LOCAL6 }, 53 { "local7", LOG_LOCAL7 }, 54 { 0, -1 } 55 } ; 56 57 static CODE const prioritynames[] = 58 { 59 { "emerg", LOG_EMERG }, 60 { "alert", LOG_ALERT }, 61 { "crit", LOG_CRIT }, 62 { "err", LOG_ERR }, 63 { "warning", LOG_WARNING }, 64 { "notice", LOG_NOTICE }, 65 { "info", LOG_INFO }, 66 { "debug", LOG_DEBUG }, 67 { 0, -1 } 68 } ; 69 70 #endif 71 72 size_t lolsyslog_string (char *out, char const *in) 73 { 74 size_t i ; 75 unsigned int fpr ; 76 int fp ; 77 CODE const *p = facilitynames ; 78 79 if (in[0] != '<' || !(i = uint_scan(in+1, &fpr)) || in[1+i] != '>') return 0 ; 80 fp = LOG_FAC(fpr) << 3 ; 81 for (; p->c_name ; p++) if (p->c_val == fp) break ; 82 out = stpcpy(out, p->c_name ? p->c_name : "unknown") ; 83 *out++ = '.' ; 84 85 p = prioritynames ; 86 fp = LOG_PRI(fpr) ; 87 for (; p->c_name ; p++) if (p->c_val == fp) break ; 88 out = stpcpy(out, p->c_name ? p->c_name : "unknown") ; 89 *out++ = ':' ; *out++ = ' ' ; *out++ = 0 ; 90 return i+2 ; 91 }