Create a branch for header work.
[reactos.git] / 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 "tnerror.h"
46 #include "ttelhndl.h" // Paul Brannan 5/25/98
47 #include "tnconfig.h" // Paul Brannan 5/25/98
48 #include <time.h>
49 #include <stdio.h>
50 #include <stdarg.h>
51 #include <stdlib.h>
52
53 #ifndef LANG_USER_DEFAULT
54 #define LANG_USER_DEFAULT 400
55 #endif
56
57 // This has been moved to tnconfig.cpp
58 // int Telnet_Redir = 0;
59 // Telnet_Redir is set to the value of the environment variable TELNET_REDIR
60 // in main.
61
62 int printit(const char * it){
63 DWORD numwritten;
64 if (!ini.get_output_redir()) {
65 if (!WriteConsole(
66 GetStdHandle(STD_OUTPUT_HANDLE), // handle of a console screen buffer
67 it, // address of buffer to write from
68 strlen(it), // number of characters to write
69 &numwritten, // address of number of characters written
70 0 // reserved
71 )) return -1;
72 // FIX ME!!! We need to tell the console that the cursor has moved.
73 // Does this mean making Console global?
74 // Paul Brannan 6/14/98
75 // Console.sync();
76 }else{
77 if (!WriteFile(
78 GetStdHandle(STD_OUTPUT_HANDLE), // handle of a console screen buffer
79 it, // address of buffer to write from
80 strlen(it), // number of characters to write
81 &numwritten, // address of number of characters written
82 NULL // no overlapped I/O
83 )) return -1;
84 }
85 return 0;
86 }
87
88 int printm(LPTSTR szModule, BOOL fSystem, DWORD dwMessageId, ...)
89 {
90 int Result = 0;
91
92 HMODULE hModule = 0;
93 if (szModule)
94 hModule = LoadLibrary(szModule);
95
96 va_list Ellipsis;
97 va_start(Ellipsis, dwMessageId);
98
99 LPTSTR pszMessage = 0;
100 DWORD dwMessage = 0;
101 if(fSystem) {
102 dwMessage = FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
103 FORMAT_MESSAGE_FROM_SYSTEM, hModule, dwMessageId,
104 LANG_USER_DEFAULT, (LPTSTR)&pszMessage, 128, &Ellipsis);
105 } else {
106 // we will use a string table.
107 char szString[256];
108 if(LoadString(0, dwMessageId, szString, sizeof(szString)))
109 dwMessage = FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
110 FORMAT_MESSAGE_FROM_STRING, szString, dwMessageId,
111 LANG_USER_DEFAULT, (LPTSTR)&pszMessage, 256, &Ellipsis);
112 }
113
114 va_end(Ellipsis);
115
116 if (szModule)
117 FreeLibrary(hModule);
118
119 if (dwMessage) {
120
121 Result = printit(pszMessage);
122 LocalFree(pszMessage);
123 }
124
125 return Result;
126 }
127
128
129 void LogErrorConsole(LPTSTR szError)
130 {
131 DWORD dwLastError = GetLastError();
132
133 const int cbLastError = 1024;
134 TCHAR szLastError[cbLastError];
135 FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, 0, dwLastError, LANG_USER_DEFAULT,
136 szLastError, cbLastError, 0);
137
138 LPTSTR lpszStrings[2];
139 lpszStrings[0] = szError;
140 lpszStrings[1] = szLastError;
141
142 const int cbErrorString = 1024;
143 TCHAR szErrorString[cbErrorString];
144 FormatMessage(FORMAT_MESSAGE_FROM_HMODULE| FORMAT_MESSAGE_ARGUMENT_ARRAY,
145 0, MSG_ERROR, LANG_USER_DEFAULT,
146 szErrorString, cbErrorString, (va_list*)lpszStrings);
147
148 time_t dwTime;
149 time(&dwTime);
150 char* szTime = ctime(&dwTime);
151 szTime[19] = 0;
152
153 // printf("E %s %s", szTime + 11, szErrorString);
154 char * buf;
155 buf = new char [ 3 + strlen(szTime) - 11 + strlen(szErrorString) + 5 ];
156 sprintf( buf,"E %s %s", szTime + 11, szErrorString);
157 printit(buf);
158 delete [] buf;
159 }
160
161
162 void LogWarningConsole(DWORD dwEvent, LPTSTR szWarning)
163 {
164 DWORD dwLastError = GetLastError();
165
166 const int cbLastError = 1024;
167 TCHAR szLastError[cbLastError];
168 FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, 0, dwLastError, LANG_USER_DEFAULT,
169 szLastError, cbLastError, 0);
170
171 LPTSTR lpszStrings[2];
172 lpszStrings[0] = szWarning;
173 lpszStrings[1] = szLastError;
174
175 const int cbWarningString = 1024;
176 TCHAR szWarningString[cbWarningString];
177 FormatMessage(FORMAT_MESSAGE_FROM_HMODULE| FORMAT_MESSAGE_ARGUMENT_ARRAY,
178 0, dwEvent, LANG_USER_DEFAULT,
179 szWarningString, cbWarningString, (va_list*)lpszStrings);
180
181 time_t dwTime;
182 time(&dwTime);
183 char* szTime = ctime(&dwTime);
184 szTime[19] = 0;
185
186 // printf("W %s %s", szTime + 11, szWarningString);
187 char * buf;
188 buf = new char [ 3 + strlen(szTime) - 11 + strlen(szWarningString) + 5 ];
189 sprintf(buf ,"W %s %s", szTime + 11, szWarningString);
190 printit(buf);
191 delete [] buf;
192
193 }
194
195
196 void LogInfoConsole(DWORD dwEvent, LPTSTR szInformation)
197 {
198 LPTSTR lpszStrings[1];
199 lpszStrings[0] = szInformation;
200
201 const int cbInfoString = 1024;
202 TCHAR szInfoString[cbInfoString];
203 FormatMessage(FORMAT_MESSAGE_FROM_HMODULE| FORMAT_MESSAGE_ARGUMENT_ARRAY,
204 0, dwEvent, LANG_USER_DEFAULT,
205 szInfoString, cbInfoString, (va_list*)lpszStrings);
206
207 time_t dwTime;
208 time(&dwTime);
209 char* szTime = ctime(&dwTime);
210 szTime[19] = 0;
211
212 // printf("I %s %s", szTime + 11, szInfoString);
213 char * buf;
214 buf = new char [ 3 + strlen(szTime) - 11 + strlen(szInfoString) + 5 ];
215 sprintf(buf,"I %s %s", szTime + 11, szInfoString);
216 printit(buf);
217 delete [] buf;
218
219 }
220