]> granicus.if.org Git - postgresql/commitdiff
Add pg_logger to /contrib.
authorBruce Momjian <bruce@momjian.us>
Sun, 11 Feb 2001 02:18:27 +0000 (02:18 +0000)
committerBruce Momjian <bruce@momjian.us>
Sun, 11 Feb 2001 02:18:27 +0000 (02:18 +0000)
contrib/README
contrib/pg_logger/Makefile [new file with mode: 0644]
contrib/pg_logger/README.pg_logger [new file with mode: 0644]
contrib/pg_logger/pg_logger.c [new file with mode: 0644]

index 108fb0d380acbf844a362588272b7d916d5bd949..1988c068abe6e0849dd633b609b52cce22f139e2 100644 (file)
@@ -89,6 +89,10 @@ pg_dumplo -
        Dump large objects
        by Karel Zak <zakkr@zf.jcu.cz>
 
+pg_logger -
+       Stdin-to-syslog gateway for PostgreSQL
+       by Nathan Myers <ncm@nospam.cantrip.org>
+
 pgbench -
        TPC-B like benchmarking tool
        by Tatsuo Ishii <t-ishii@sra.co.jp>
diff --git a/contrib/pg_logger/Makefile b/contrib/pg_logger/Makefile
new file mode 100644 (file)
index 0000000..4a3f180
--- /dev/null
@@ -0,0 +1,34 @@
+#
+# $Header: /cvsroot/pgsql/contrib/pg_logger/Attic/Makefile,v 1.1 2001/02/11 02:18:27 momjian Exp $
+#
+
+subdir = contrib/pg_logger
+top_builddir = ../..
+include $(top_builddir)/src/Makefile.global
+
+OBJS   = pg_logger.o
+
+all: pg_logger
+
+pg_logger: $(OBJS)
+       $(CC) $(CFLAGS) $(LDFLAGS) $(OBJS) $(LIBS) -o $@
+
+install: all installdirs
+       $(INSTALL_PROGRAM) pg_logger$(X)        $(bindir)
+       $(INSTALL_DATA) README.pg_logger        $(docdir)/contrib
+
+installdirs:
+       $(mkinstalldirs) $(bindir) $(docdir)/contrib
+
+uninstall:
+       rm -f $(bindir)/pg_logger$(X) $(docdir)/contrib/README.pg_logger
+
+clean distclean maintainer-clean:
+       rm -f pg_logger$(X) $(OBJS)
+
+depend dep:
+       $(CC) -MM -MG $(CFLAGS) *.c > depend
+
+ifeq (depend,$(wildcard depend))
+include depend
+endif
diff --git a/contrib/pg_logger/README.pg_logger b/contrib/pg_logger/README.pg_logger
new file mode 100644 (file)
index 0000000..aaad73f
--- /dev/null
@@ -0,0 +1 @@
+Stdin-to-syslog gateway for PostgreSQL
diff --git a/contrib/pg_logger/pg_logger.c b/contrib/pg_logger/pg_logger.c
new file mode 100644 (file)
index 0000000..af56a24
--- /dev/null
@@ -0,0 +1,85 @@
+/* pg_logger: stdin-to-syslog gateway for postgresql.
+ *
+ * Copyright 2001 by Nathan Myers <ncm@nospam.cantrip.org>
+ * This software is distributed free of charge with no warranty of any kind.
+ * You have permission to make copies for any purpose, provided that (1) 
+ * this copyright notice is retained unchanged, and (2) you agree to 
+ * absolve the author of all responsibility for all consequences arising 
+ * from any use.  
+ */
+
+#include <stdio.h>
+#include <stddef.h>
+#include <syslog.h>
+#include <string.h>
+
+struct {
+    const char *tag;
+    int size;
+    int priority;
+} tags[] = {
+    { "",         0,                 LOG_NOTICE },
+    { "emerg:",   sizeof("emerg"),   LOG_EMERG },
+    { "alert:",   sizeof("alert"),   LOG_ALERT },
+    { "crit:",    sizeof("crit"),    LOG_CRIT },
+    { "err:",     sizeof("err"),     LOG_ERR },
+    { "error:",   sizeof("error"),   LOG_ERR },
+    { "warning:", sizeof("warning"), LOG_WARNING },
+    { "notice:",  sizeof("notice"),  LOG_NOTICE },
+    { "info:",    sizeof("info"),    LOG_INFO },
+    { "debug:",   sizeof("debug"),   LOG_DEBUG }
+};
+
+int main()
+{
+    char buf[301];
+    int c;
+    char *pos = buf;
+    const char *colon = 0;
+
+#ifndef DEBUG
+    openlog("postgresql", LOG_CONS, LOG_LOCAL1);
+#endif
+    while ( (c = getchar()) != EOF) {
+        if (c == '\r') {
+          continue;
+        }
+        if (c == '\n') {
+            int level = sizeof(tags)/sizeof(*tags);
+            char *bol;
+
+            if (colon == 0 || (size_t)(colon - buf) > sizeof("warning")) {
+                level = 1;
+            }
+            *pos = 0;
+            while (--level) {
+                if (pos - buf >= tags[level].size
+                    && strncmp(buf, tags[level].tag, tags[level].size) == 0) {
+                    break; 
+                }
+            }
+            bol = buf + tags[level].size;
+            if (bol > buf && *bol == ' ') {
+                ++bol;
+            }
+            if (pos - bol > 0) {
+#ifndef DEBUG
+                syslog(tags[level].priority, "%s", bol);
+#else
+                printf("%d/%s\n", tags[level].priority, bol);
+#endif
+            }
+            pos = buf;
+            colon = (char const *)0;
+            continue;
+        }
+        if (c == ':' && !colon) {
+            colon = pos;
+        }
+        if ((size_t)(pos - buf) < sizeof(buf)-1) {
+            *pos++ = c;
+        }
+    }
+    return 0;
+}
+