|
| 1 | +/* pg_logger: stdin-to-syslog gateway for postgresql. |
| 2 | + * |
| 3 | + * Copyright 2001 by Nathan Myers <ncm@nospam.cantrip.org> |
| 4 | + * This software is distributed free of charge with no warranty of any kind. |
| 5 | + * You have permission to make copies for any purpose, provided that (1) |
| 6 | + * this copyright notice is retained unchanged, and (2) you agree to |
| 7 | + * absolve the author of all responsibility for all consequences arising |
| 8 | + * from any use. |
| 9 | + */ |
| 10 | + |
| 11 | +#include <stdio.h> |
| 12 | +#include <stddef.h> |
| 13 | +#include <syslog.h> |
| 14 | +#include <string.h> |
| 15 | + |
| 16 | +struct { |
| 17 | + const char *tag; |
| 18 | + int size; |
| 19 | + int priority; |
| 20 | +} tags[] = { |
| 21 | + { "", 0, LOG_NOTICE }, |
| 22 | + { "emerg:", sizeof("emerg"), LOG_EMERG }, |
| 23 | + { "alert:", sizeof("alert"), LOG_ALERT }, |
| 24 | + { "crit:", sizeof("crit"), LOG_CRIT }, |
| 25 | + { "err:", sizeof("err"), LOG_ERR }, |
| 26 | + { "error:", sizeof("error"), LOG_ERR }, |
| 27 | + { "warning:", sizeof("warning"), LOG_WARNING }, |
| 28 | + { "notice:", sizeof("notice"), LOG_NOTICE }, |
| 29 | + { "info:", sizeof("info"), LOG_INFO }, |
| 30 | + { "debug:", sizeof("debug"), LOG_DEBUG } |
| 31 | +}; |
| 32 | + |
| 33 | +int main() |
| 34 | +{ |
| 35 | + char buf[301]; |
| 36 | + int c; |
| 37 | + char *pos = buf; |
| 38 | + const char *colon = 0; |
| 39 | + |
| 40 | +#ifndef DEBUG |
| 41 | + openlog("postgresql", LOG_CONS, LOG_LOCAL1); |
| 42 | +#endif |
| 43 | + while ( (c = getchar()) != EOF) { |
| 44 | + if (c == '\r') { |
| 45 | + continue; |
| 46 | + } |
| 47 | + if (c == '\n') { |
| 48 | + int level = sizeof(tags)/sizeof(*tags); |
| 49 | + char *bol; |
| 50 | + |
| 51 | + if (colon == 0 || (size_t)(colon - buf) > sizeof("warning")) { |
| 52 | + level = 1; |
| 53 | + } |
| 54 | + *pos = 0; |
| 55 | + while (--level) { |
| 56 | + if (pos - buf >= tags[level].size |
| 57 | + && strncmp(buf, tags[level].tag, tags[level].size) == 0) { |
| 58 | + break; |
| 59 | + } |
| 60 | + } |
| 61 | + bol = buf + tags[level].size; |
| 62 | + if (bol > buf && *bol == ' ') { |
| 63 | + ++bol; |
| 64 | + } |
| 65 | + if (pos - bol > 0) { |
| 66 | +#ifndef DEBUG |
| 67 | + syslog(tags[level].priority, "%s", bol); |
| 68 | +#else |
| 69 | + printf("%d/%s\n", tags[level].priority, bol); |
| 70 | +#endif |
| 71 | + } |
| 72 | + pos = buf; |
| 73 | + colon = (char const *)0; |
| 74 | + continue; |
| 75 | + } |
| 76 | + if (c == ':' && !colon) { |
| 77 | + colon = pos; |
| 78 | + } |
| 79 | + if ((size_t)(pos - buf) < sizeof(buf)-1) { |
| 80 | + *pos++ = c; |
| 81 | + } |
| 82 | + } |
| 83 | + return 0; |
| 84 | +} |
| 85 | + |
0 commit comments