summaryrefslogtreecommitdiff
path: root/src/win32ctl/eventlog/pcp-eventlog.c
blob: e8f8fe303c09ee2549bf213eb52010756b15a2d6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/*
 * Copyright (C) 2008 Aconex.  All Rights Reserved.
 * 
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2 of the License, or (at your
 * option) any later version.
 * 
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * for more details.
 */
#include "pmapi.h"
#include "impl.h"
#include <string.h>
#include <winbase.h>

int
posix2win32(char *pri)
{
    if (strcmp(pri, "alert") == 0 || strcmp(pri, "warning") == 0 ||
	strcmp(pri, "warn") == 0)
	return EVENTLOG_WARNING_TYPE;
    if (strcmp(pri, "crit") == 0 || strcmp(pri, "emerg") == 0 ||
	strcmp(pri, "err") == 0 || strcmp(pri, "error") == 0 ||
	strcmp(pri, "panic") == 0)
	return EVENTLOG_ERROR_TYPE;
    if (strcmp(pri, "info") == 0 || strcmp(pri, "notice") == 0 ||
	strcmp(pri, "debug") == 0)
	return EVENTLOG_INFORMATION_TYPE;
    return -1;
}

void
append(char *buffer, int bsize, char *string)
{
    static int spaced;	/* first argument needs no whitespace-prefix */
    static int offset;

    if (spaced)
	offset += snprintf(buffer + offset, bsize - offset, " %s", string);
    else {
	offset += snprintf(buffer + offset, bsize - offset, "%s", string);
	spaced = 1;	/* remainder will all be space-prefixed */
    }
}

int
main(int argc, char **argv)
{
    HANDLE sink;
    LPCSTR msgptr;
    char buffer[256];
    char msg[32*1024];
    char *pri = NULL;
    char *tag = NULL;
    int error = 0;
    int iflag = 0;
    int sflag = 0;
    int priority;
    int c;

    __pmSetProgname(argv[0]);

    while ((c = getopt(argc, argv, "ip:st:?")) != EOF) {
	switch (c) {
	case 'i':	/* process ID */
	    iflag = 1;
	    break;
	case 'p':	/* pri (facility.level) */
	    pri = optarg;
	    break;
	case 's':	/* stderr too */
	    sflag = 1;
	    break;
	case 't':	/* tag (prefix) */
	    tag = optarg;
	    break;
	default:
	    error++;
	}
    }

    if (error) {
	fprintf(stderr, "Usage: %s [ options ] message\n\n"
			"Options:\n"
			"  -i          log process identifier with each line\n"
			"  -s          log message to standard error as well\n"
			"  -p pri      enter message with specified priority\n"
			"  -t tag      mark the line with the specified tag\n",
		pmProgname);
	return 2;
    }

    /*
     * Parse priority.  Discard facility, pick out valid level names.
     */
    if (!pri)
	priority = EVENTLOG_INFORMATION_TYPE;	/* default event type */
    else {
	char *p = strrchr(pri, '.');
	if (p)
	    pri = p;
	priority = posix2win32(pri);
	if (!priority)
	    priority = EVENTLOG_INFORMATION_TYPE;	/* default event type */
    }

    /*
     * Construct the message from all contributing components.
     */
    if (iflag) {
	snprintf(buffer, sizeof(buffer), "[%" FMT_PID "]", getpid());
	append(msg, sizeof(msg), buffer);
    }
    if (tag) {
	snprintf(buffer, sizeof(buffer), "%s:", tag);
	append(msg, sizeof(msg), buffer);
    }
    for (c = optind; c < argc; c++)	/* insert the remaining text */
	append(msg, sizeof(msg), argv[c]);

    /*
     * Optionally write to the standard error stream (as well).
     */
    if (sflag) {
	fputs(msg, stderr);
	fputc('\n', stderr);
    }

    sink = RegisterEventSource(NULL, "Application");
    if (!sink) {
	fprintf(stderr, "%s: RegisterEventSource failed (%ld)\n",
			pmProgname, GetLastError());
	return 1;
    }
    msgptr = msg;
    if (!ReportEvent(sink, priority, 0, 0, NULL, 1, 0, &msgptr, NULL))
	fprintf(stderr, "%s: ReportEvent failed (%ld)\n",
			pmProgname, GetLastError());
    DeregisterEventSource(sink);
    return 0;
}