]> granicus.if.org Git - postgresql/commitdiff
Add option to pg_ctl to choose event source for logging
authorMagnus Hagander <magnus@hagander.net>
Thu, 17 Jul 2014 10:42:08 +0000 (12:42 +0200)
committerMagnus Hagander <magnus@hagander.net>
Thu, 17 Jul 2014 10:42:08 +0000 (12:42 +0200)
pg_ctl will log to the Windows event log when it is running as a service,
which is the primary way of running PostgreSQL on Windows. This option
makes it possible to specify which event source to use for this, in order
to separate different instances. The server logging itself is still controlled
by the regular logging parameters, including a separate setting for the event
source. The parameter to pg_ctl only controlls the logging from pg_ctl itself.

MauMau, review in many iterations by Amit Kapila and me.

doc/src/sgml/ref/pg_ctl-ref.sgml
src/backend/utils/error/elog.c
src/backend/utils/misc/guc.c
src/bin/pg_ctl/pg_ctl.c
src/bin/pgevent/pgevent.c
src/include/pg_config_manual.h

index 52bcf5e81538ee8085febae0c9f615a7ce3a9248..23681294b87d767f5d085936ac482bec743c26d5 100644 (file)
@@ -419,6 +419,22 @@ PostgreSQL documentation
    <title>Options for Windows</title>
 
    <variablelist>
+    <varlistentry>
+     <term><option>-e <replaceable class="parameter">source</replaceable></option></term>
+     <listitem>
+      <para>
+       Name of the event source for <application>pg_ctl</application> to use
+       for logging to the event log when running as a Windows service.  The
+       default is <literal>PostgreSQL</literal>.  Note that this only controls
+       the logging from <application>pg_ctl</application> itself - once
+       started, the server will use the event source specified
+       by <xref linkend="guc-event-source">.  Should the server fail during
+       early startup, it may also log using the default event
+       source <literal>PostgreSQL</literal>.
+      </para>
+     </listitem>
+    </varlistentry>
+
     <varlistentry>
      <term><option>-N <replaceable class="parameter">servicename</replaceable></option></term>
      <listitem>
index 0d92dcd036c4679818d235b676eb16dd1346c213..32a9663366d9e1479e6108bba8631f5e22cca184 100644 (file)
@@ -1989,7 +1989,8 @@ write_eventlog(int level, const char *line, int len)
 
        if (evtHandle == INVALID_HANDLE_VALUE)
        {
-               evtHandle = RegisterEventSource(NULL, event_source ? event_source : "PostgreSQL");
+               evtHandle = RegisterEventSource(NULL,
+                                                event_source ? event_source : DEFAULT_EVENT_SOURCE);
                if (evtHandle == NULL)
                {
                        evtHandle = INVALID_HANDLE_VALUE;
index 3a31a7519196cae2eef60b130c3c19c43060231e..6c52db859032af33eccb1eb53a890506f4e7b181 100644 (file)
@@ -3019,7 +3019,7 @@ static struct config_string ConfigureNamesString[] =
                        NULL
                },
                &event_source,
-               "PostgreSQL",
+               DEFAULT_EVENT_SOURCE,
                NULL, NULL, NULL
        },
 
index 9a6ade935f8df2fd06b145ddf851593e64208390..edd202b7d7fcc261f1abe4ee8bf9e676c7fde545 100644 (file)
@@ -89,6 +89,7 @@ static char *post_opts = NULL;
 static const char *progname;
 static char *log_file = NULL;
 static char *exec_path = NULL;
+static char *event_source = NULL;
 static char *register_servicename = "PostgreSQL";              /* FIXME: + version ID? */
 static char *register_username = NULL;
 static char *register_password = NULL;
@@ -178,7 +179,8 @@ write_eventlog(int level, const char *line)
 
        if (evtHandle == INVALID_HANDLE_VALUE)
        {
-               evtHandle = RegisterEventSource(NULL, "PostgreSQL");
+               evtHandle = RegisterEventSource(NULL,
+                                               event_source ? event_source : DEFAULT_EVENT_SOURCE);
                if (evtHandle == NULL)
                {
                        evtHandle = INVALID_HANDLE_VALUE;
@@ -1406,6 +1408,9 @@ pgwin32_CommandLine(bool registration)
                free(dataDir);
        }
 
+       if (registration && event_source != NULL)
+               appendPQExpBuffer(cmdLine, " -e \"%s\"", event_source);
+
        if (registration && do_wait)
                appendPQExpBuffer(cmdLine, " -w");
 
@@ -1878,6 +1883,10 @@ do_help(void)
        printf(_("\nCommon options:\n"));
        printf(_("  -D, --pgdata=DATADIR   location of the database storage area\n"));
        printf(_("  -s, --silent           only print errors, no informational messages\n"));
+#if defined(WIN32) || defined(__CYGWIN__)
+       printf(_("  -e SOURCE              event source to use for logging when running\n"
+                        "                         as a service\n"));
+#endif
        printf(_("  -t, --timeout=SECS     seconds to wait when using -w option\n"));
        printf(_("  -V, --version          output version information, then exit\n"));
        printf(_("  -w                     wait until operation completes\n"));
@@ -2140,7 +2149,7 @@ main(int argc, char **argv)
        /* process command-line options */
        while (optind < argc)
        {
-               while ((c = getopt_long(argc, argv, "cD:l:m:N:o:p:P:sS:t:U:wW", long_options, &option_index)) != -1)
+               while ((c = getopt_long(argc, argv, "cD:e:l:m:N:o:p:P:sS:t:U:wW", long_options, &option_index)) != -1)
                {
                        switch (c)
                        {
@@ -2168,6 +2177,9 @@ main(int argc, char **argv)
                                case 'm':
                                        set_mode(optarg);
                                        break;
+                               case 'e':
+                                       event_source = pg_strdup(optarg);
+                                       break;
                                case 'N':
                                        register_servicename = pg_strdup(optarg);
                                        break;
index 6a667812fba98e7a36038dbc0ffdf72ccd4d6eab..83949c2cda234ec8e043b6e29f1604e7693531d2 100644 (file)
@@ -26,7 +26,7 @@ HANDLE                g_module = NULL;        /* hModule of DLL */
  * The maximum length of a registry key is 255 characters.
  * http://msdn.microsoft.com/en-us/library/ms724872(v=vs.85).aspx
  */
-char           event_source[256] = "PostgreSQL";
+char           event_source[256] = DEFAULT_EVENT_SOURCE;
 
 /* Prototypes */
 HRESULT                DllInstall(BOOL bInstall, LPCWSTR pszCmdLine);
index d1f99fbafef3260b60faf386000cfd786e57c55c..16f7ef9bea6d761d2fe19f425df08065c1830fc1 100644 (file)
  */
 #define DEFAULT_PGSOCKET_DIR  "/tmp"
 
+/*
+ * This is the default event source for Windows event log.
+ */
+#define DEFAULT_EVENT_SOURCE  "PostgreSQL"
+
 /*
  * The random() function is expected to yield values between 0 and
  * MAX_RANDOM_VALUE.  Currently, all known implementations yield