[SHELL32]
[reactos.git] / reactos / base / applications / network / telnet / src / tnerror.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 //Telnet Win32 : an ANSI telnet client.
3 //Copyright (C) 1998-2000 Paul Brannan
4 //Copyright (C) 1998 I.Ioannou
5 //Copyright (C) 1997 Brad Johnson
6 //
7 //This program is free software; you can redistribute it and/or
8 //modify it under the terms of the GNU General Public License
9 //as published by the Free Software Foundation; either version 2
10 //of the License, or (at your option) any later version.
11 //
12 //This program is distributed in the hope that it will be useful,
13 //but WITHOUT ANY WARRANTY; without even the implied warranty of
14 //MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 //GNU General Public License for more details.
16 //
17 //You should have received a copy of the GNU General Public License
18 //along with this program; if not, write to the Free Software
19 //Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 //
21 //I.Ioannou
22 //roryt@hol.gr
23 //
24 ///////////////////////////////////////////////////////////////////////////
25
26 ///////////////////////////////////////////////////////////////////////////////
27 //
28 // Module: tnerror.cpp
29 //
30 // Contents: error reporting
31 //
32 // Product: telnet
33 //
34 // Revisions: June 15, 1998 Paul Brannan <pbranna@clemson.edu>
35 // May 15, 1998 Paul Brannan
36 // 5.April.1997 jbj@nounname.com
37 // 5.Dec.1996 jbj@nounname.com
38 // Version 2.0
39 //
40 // 02.Apr.1995 igor.milavec@uni-lj.si
41 // Original code
42 //
43 ///////////////////////////////////////////////////////////////////////////////
44
45 #include "precomp.h"
46
47 #ifndef LANG_USER_DEFAULT
48 #define LANG_USER_DEFAULT 400
49 #endif
50
51 // This has been moved to tnconfig.cpp
52 // int Telnet_Redir = 0;
53 // Telnet_Redir is set to the value of the environment variable TELNET_REDIR
54 // in main.
55
56 int printit(const char * it){
57 DWORD numwritten;
58 if (!ini.get_output_redir()) {
59 if (!WriteConsole(
60 GetStdHandle(STD_OUTPUT_HANDLE), // handle of a console screen buffer
61 it, // address of buffer to write from
62 strlen(it), // number of characters to write
63 &numwritten, // address of number of characters written
64 0 // reserved
65 )) return -1;
66 // FIX ME!!! We need to tell the console that the cursor has moved.
67 // Does this mean making Console global?
68 // Paul Brannan 6/14/98
69 // Console.sync();
70 }else{
71 if (!WriteFile(
72 GetStdHandle(STD_OUTPUT_HANDLE), // handle of a console screen buffer
73 it, // address of buffer to write from
74 strlen(it), // number of characters to write
75 &numwritten, // address of number of characters written
76 NULL // no overlapped I/O
77 )) return -1;
78 }
79 return 0;
80 }
81
82 int printm(LPTSTR szModule, BOOL fSystem, DWORD dwMessageId, ...)
83 {
84 int Result = 0;
85
86 HMODULE hModule = 0;
87 if (szModule)
88 hModule = LoadLibrary(szModule);
89
90 va_list Ellipsis;
91 va_start(Ellipsis, dwMessageId);
92
93 LPTSTR pszMessage = 0;
94 DWORD dwMessage = 0;
95 if(fSystem) {
96 dwMessage = FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
97 FORMAT_MESSAGE_FROM_SYSTEM, hModule, dwMessageId,
98 LANG_USER_DEFAULT, (LPTSTR)&pszMessage, 128, &Ellipsis);
99 } else {
100 // we will use a string table.
101 char szString[256];
102 if(LoadString(0, dwMessageId, szString, sizeof(szString)))
103 dwMessage = FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
104 FORMAT_MESSAGE_FROM_STRING, szString, dwMessageId,
105 LANG_USER_DEFAULT, (LPTSTR)&pszMessage, 256, &Ellipsis);
106 }
107
108 va_end(Ellipsis);
109
110 if (szModule)
111 FreeLibrary(hModule);
112
113 if (dwMessage) {
114
115 Result = printit(pszMessage);
116 LocalFree(pszMessage);
117 }
118
119 return Result;
120 }
121
122
123 void LogErrorConsole(LPTSTR szError)
124 {
125 DWORD dwLastError = GetLastError();
126
127 const int cbLastError = 1024;
128 TCHAR szLastError[cbLastError];
129 FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, 0, dwLastError, LANG_USER_DEFAULT,
130 szLastError, cbLastError, 0);
131
132 LPTSTR lpszStrings[2];
133 lpszStrings[0] = szError;
134 lpszStrings[1] = szLastError;
135
136 const int cbErrorString = 1024;
137 TCHAR szErrorString[cbErrorString];
138 FormatMessage(FORMAT_MESSAGE_FROM_HMODULE| FORMAT_MESSAGE_ARGUMENT_ARRAY,
139 0, MSG_ERROR, LANG_USER_DEFAULT,
140 szErrorString, cbErrorString, (va_list*)lpszStrings);
141
142 time_t dwTime;
143 time(&dwTime);
144 char* szTime = ctime(&dwTime);
145 szTime[19] = 0;
146
147 // printf("E %s %s", szTime + 11, szErrorString);
148 char * buf;
149 buf = new char [ 3 + strlen(szTime) - 11 + strlen(szErrorString) + 5 ];
150 sprintf( buf,"E %s %s", szTime + 11, szErrorString);
151 printit(buf);
152 delete [] buf;
153 }
154
155
156 void LogWarningConsole(DWORD dwEvent, LPTSTR szWarning)
157 {
158 DWORD dwLastError = GetLastError();
159
160 const int cbLastError = 1024;
161 TCHAR szLastError[cbLastError];
162 FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, 0, dwLastError, LANG_USER_DEFAULT,
163 szLastError, cbLastError, 0);
164
165 LPTSTR lpszStrings[2];
166 lpszStrings[0] = szWarning;
167 lpszStrings[1] = szLastError;
168
169 const int cbWarningString = 1024;
170 TCHAR szWarningString[cbWarningString];
171 FormatMessage(FORMAT_MESSAGE_FROM_HMODULE| FORMAT_MESSAGE_ARGUMENT_ARRAY,
172 0, dwEvent, LANG_USER_DEFAULT,
173 szWarningString, cbWarningString, (va_list*)lpszStrings);
174
175 time_t dwTime;
176 time(&dwTime);
177 char* szTime = ctime(&dwTime);
178 szTime[19] = 0;
179
180 // printf("W %s %s", szTime + 11, szWarningString);
181 char * buf;
182 buf = new char [ 3 + strlen(szTime) - 11 + strlen(szWarningString) + 5 ];
183 sprintf(buf ,"W %s %s", szTime + 11, szWarningString);
184 printit(buf);
185 delete [] buf;
186
187 }
188
189
190 void LogInfoConsole(DWORD dwEvent, LPTSTR szInformation)
191 {
192 LPTSTR lpszStrings[1];
193 lpszStrings[0] = szInformation;
194
195 const int cbInfoString = 1024;
196 TCHAR szInfoString[cbInfoString];
197 FormatMessage(FORMAT_MESSAGE_FROM_HMODULE| FORMAT_MESSAGE_ARGUMENT_ARRAY,
198 0, dwEvent, LANG_USER_DEFAULT,
199 szInfoString, cbInfoString, (va_list*)lpszStrings);
200
201 time_t dwTime;
202 time(&dwTime);
203 char* szTime = ctime(&dwTime);
204 szTime[19] = 0;
205
206 // printf("I %s %s", szTime + 11, szInfoString);
207 char * buf;
208 buf = new char [ 3 + strlen(szTime) - 11 + strlen(szInfoString) + 5 ];
209 sprintf(buf,"I %s %s", szTime + 11, szInfoString);
210 printit(buf);
211 delete [] buf;
212
213 }
214