1 /*
2  *  ReactOS Win32 Applications
3  *  Copyright (C) 2007 ReactOS Team
4  *
5  *  This program is free software; you can redistribute it and/or modify
6  *  it under the terms of the GNU General Public License as published by
7  *  the Free Software Foundation; either version 2 of the License, or
8  *  (at your option) any later version.
9  *
10  *  This program is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  *  GNU General Public License for more details.
14  *
15  *  You should have received a copy of the GNU General Public License
16  *  along with this program; if not, write to the Free Software
17  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */
19 /*
20  * COPYRIGHT : See COPYING in the top level directory
21  * PROJECT   : Event Logging Utility
22  * FILE      : logevent.c
23  * PROGRAMMER: Marc Piulachs (marc.piulachs at codexchange [dot] net)
24  */
25 
26 #include <windows.h>
27 #include <stdlib.h>
28 #include <stdio.h>
29 #include <string.h>
30 #include <malloc.h>
31 #include <tchar.h>
32 #include <stdarg.h>
33 
34 TCHAR* m_MachineName    = NULL;
35 TCHAR* m_Text           = "No User Event Text";
36 TCHAR* m_Source         = "User Event";
37 WORD m_Severity         = EVENTLOG_INFORMATION_TYPE;
38 WORD m_Category         = 0;
39 DWORD m_EventID         = 1;
40 
41 void
42 Usage(VOID)
43 {
44     fputs("Usage: logevent.exe [-m \\MachineName] [options] \"Event Text\"", stderr);
45     fputs("\n\n", stderr);
46     fputs("Options:\n", stderr);
47     fputs("  -s  Severity one of:\n", stderr);
48     fputs("  \t(S)uccess\n", stderr);
49     fputs("  \t(I)nformation\n", stderr);
50     fputs("  \t(W)arning\n", stderr);
51     fputs("  \t(E)rror\n", stderr);
52     fputs("  \t(F)ailure\n", stderr);
53     fputs("  -r  Source\n", stderr);
54     fputs("  -c  Category number\n", stderr);
55     fputs("  -e  Event ID\n", stderr);
56     fputs("  /?  Help\n", stderr);
57 }
58 
59 void
60 WriteEvent (VOID)
61 {
62     HANDLE hAppLog;
63     BOOL bSuccess;
64     LPCTSTR arrLogEntry[] = { m_Text }; //writing just one entry
65 
66     /* Get a handle to the Application event log */
67     hAppLog = RegisterEventSource(
68         (LPCSTR)m_MachineName,    /* machine  */
69         (LPCSTR)m_Source);        /* source name */
70 
71     /* Now report the event, which will add this event to the event log */
72     bSuccess = ReportEvent(
73         hAppLog,                 /* event-log handle                */
74         m_Severity,              /* event type                      */
75         m_Category,              /* category                        */
76         m_EventID,               /* event ID                        */
77         NULL,                    /* no user SID                     */
78         1,                       /* number of substitution strings  */
79         0,                       /* no binary data                  */
80         arrLogEntry,             /* string array                    */
81         NULL);                   /* address of data                 */
82 
83     DeregisterEventSource(hAppLog);
84 
85     return;
86 }
87 
88 /* Parse command line parameters */
89 static BOOL ParseCmdline(int argc, TCHAR **argv)
90 {
91     INT i;
92     BOOL ShowUsage;
93     BOOL FoundEventText;
94     BOOL InvalidOption;
95 
96     if (argc < 2) {
97         ShowUsage = TRUE;
98     } else {
99         ShowUsage = FALSE;
100     }
101 
102     FoundEventText = FALSE;
103     InvalidOption = FALSE;
104 
105     for (i = 1; i < argc; i++) {
106         if (argv[i][0] == '-' || argv[i][0] == '/') {
107             switch (argv[i][1]) {
108                 case 's':
109                 case 'S':
110                     switch (argv[i + 1][0])
111                     {
112                         case 's':
113                         case 'S':
114                             m_Severity = EVENTLOG_SUCCESS;
115                             i++;
116                             break;
117                         case 'i':
118                         case 'I':
119                             m_Severity = EVENTLOG_INFORMATION_TYPE;
120                             i++;
121                             break;
122                         case 'w':
123                         case 'W':
124                             m_Severity = EVENTLOG_WARNING_TYPE;
125                             i++;
126                             break;
127                         case 'e':
128                         case 'E':
129                             m_Severity = EVENTLOG_ERROR_TYPE;
130                             i++;
131                             break;
132                         case 'f':
133                         case 'F':
134                             m_Severity = EVENTLOG_ERROR_TYPE;
135                             i++;
136                             break;
137                         default:
138                             printf("Bad option %s.\n", argv[i]);
139                             Usage();
140                             return FALSE;
141                     }
142                     break;
143                 case 'm':
144                 case 'M':
145                     m_MachineName = argv[i + 1];
146                     i++;
147                     break;
148                 case 'r':
149                 case 'R':
150                     m_Source = argv[i + 1];
151                     i++;
152                     break;
153                 case 'c':
154                 case 'C':
155                     m_Category = atoi(argv[i + 1]);
156                     i++;
157                     break;
158                 case 'e':
159                 case 'E':
160                     m_EventID  = atoi(argv[i + 1]);
161                     i++;
162                     break;
163                 case '?':
164                     ShowUsage = TRUE;
165                     break;
166                 default:
167                     printf("Bad option %s.\n", argv[i]);
168                     Usage();
169                     return FALSE;
170             }
171             if (InvalidOption) {
172                 printf("Bad option format %s.\n", argv[i]);
173                 return FALSE;
174             }
175         } else {
176             if (FoundEventText) {
177                 printf("Bad parameter %s.\n", argv[i]);
178                 return FALSE;
179             } else {
180                 m_Text = argv[i];
181                 FoundEventText = TRUE;
182             }
183         }
184     }
185 
186     if ((!ShowUsage) && (!FoundEventText)) {
187         printf("The event text must be specified.\n");
188         return FALSE;
189     }
190 
191     if (ShowUsage) {
192         Usage();
193         return FALSE;
194     }
195 
196     return TRUE;
197 }
198 
199 int main(int argc, char **argv)
200 {
201     if (ParseCmdline(argc, argv))
202         WriteEvent ();
203 
204     return 0;
205 }
206