2 * Copyright 2000, International Business Machines Corporation and others.
5 * This software has been released under the terms of the IBM Public
6 * License. For details, see the LICENSE file in the top-level source
7 * directory or online at http://www.openafs.org/dl/license10.html
14 #include <afsconfig.h>
15 #include <afs/param.h>
21 #include "svc_viewlog.h"
22 #include "svc_general.h"
25 #define cchMAXLOG 20480 // show at most 20k worth of log file
27 #define cxMIN_VIEWLOG 250 // minimum size of View Log File dialog
28 #define cyMIN_VIEWLOG 200 // minimum size of View Log File dialog
32 * RESIZING WINDOWS ___________________________________________________________
36 rwWindowData awdShowLog[] = {
37 { IDC_SVC_VIEWLOG_DESC, raSizeX | raRepaint, 0, 0 },
38 { IDC_SVC_VIEWLOG_FILENAME, raSizeX | raRepaint, 0, 0 },
39 { IDC_VIEWLOG_TEXT, raSizeX | raSizeY | raRepaint, MAKELONG(cxMIN_VIEWLOG,cyMIN_VIEWLOG), 0 },
40 { IDC_SVC_VIEWLOG_CONTENTS, raSizeX, 0, 0 },
41 { IDOK, raMoveX | raMoveY, 0, 0 },
42 { IDC_VIEWLOG_SAVEAS, raMoveX | raMoveY, 0, 0 },
43 { idENDLIST, 0, 0, 0 }
48 * PROTOTYPES _________________________________________________________________
52 BOOL CALLBACK Services_ShowLog_DlgProc (HWND hDlg, UINT msg, WPARAM wp, LPARAM lp);
53 void Services_ShowLog_TakeNextStep (HWND hDlg, LPSVC_VIEWLOG_PACKET lpp);
54 void Services_ShowLog_OnEndTask (HWND hDlg, LPSVC_VIEWLOG_PACKET lpp, LPTASKPACKET ptp);
55 void Services_ShowLog_OnInitDialog (HWND hDlg, LPSVC_VIEWLOG_PACKET lpp);
56 void Services_ShowLog_OnSaveAs (HWND hDlg, LPSVC_VIEWLOG_PACKET lpp);
57 BOOL Services_ShowLog_Pick (HWND hDlg, LPSVC_VIEWLOG_PACKET lpp);
59 BOOL CALLBACK Services_PickLog_DlgProc (HWND hDlg, UINT msg, WPARAM wp, LPARAM lp);
60 void Services_PickLog_OnInitDialog (HWND hDlg, LPSVC_VIEWLOG_PACKET lpp);
64 * ROUTINES ___________________________________________________________________
68 void Services_ShowServiceLog (LPIDENT lpi)
70 LPSVC_VIEWLOG_PACKET lpp = New (SVC_VIEWLOG_PACKET);
71 lpp->lpiServer = lpi->GetServer();
72 lpp->lpiService = (lpi && lpi->fIsService()) ? lpi : NULL;
73 lpp->szLocal[0] = TEXT('\0');
74 lpp->szRemote[0] = TEXT('\0');
75 lpp->nDownloadAttempts = 0;
77 HWND hDlg = ModelessDialogParam (IDD_SVC_VIEWLOG, NULL, (DLGPROC)Services_ShowLog_DlgProc, (LPARAM)lpp);
78 AfsAppLib_RegisterModelessDialog (hDlg);
82 void Services_ShowServerLog (LPIDENT lpiServer, LPTSTR pszRemote)
84 LPSVC_VIEWLOG_PACKET lpp = New (SVC_VIEWLOG_PACKET);
85 lpp->lpiService = NULL;
86 lpp->lpiServer = lpiServer;
87 lpp->szLocal[0] = TEXT('\0');
88 lpp->nDownloadAttempts = 0;
91 lstrcpy (lpp->szRemote, pszRemote);
93 lpp->szRemote[0] = TEXT('\0');
95 HWND hDlg = ModelessDialogParam (IDD_SVC_VIEWLOG, NULL, (DLGPROC)Services_ShowLog_DlgProc, (LPARAM)lpp);
96 AfsAppLib_RegisterModelessDialog (hDlg);
100 BOOL CALLBACK Services_ShowLog_DlgProc (HWND hDlg, UINT msg, WPARAM wp, LPARAM lp)
102 if (AfsAppLib_HandleHelp (IDD_SVC_VIEWLOG, hDlg, msg, wp, lp))
105 if (msg == WM_INITDIALOG)
106 SetWindowLongPtr (hDlg, DWLP_USER, lp);
108 LPSVC_VIEWLOG_PACKET lpp;
109 if ((lpp = (LPSVC_VIEWLOG_PACKET)GetWindowLongPtr (hDlg, DWLP_USER)) != NULL)
114 if (gr.rViewLog.right == 0)
115 GetWindowRect (hDlg, &gr.rViewLog);
116 ResizeWindow (hDlg, awdShowLog, rwaMoveToHere, &gr.rViewLog);
118 Services_ShowLog_TakeNextStep (hDlg, lpp);
122 if (lpp->szLocal[0] != TEXT('\0'))
123 DeleteFile (lpp->szLocal);
124 GetWindowRect (hDlg, &gr.rViewLog);
125 SetWindowLongPtr (hDlg, DWLP_USER, 0);
130 // if (lp==0), we're minimizing--don't call ResizeWindow().
133 ResizeWindow (hDlg, awdShowLog, rwaFixupGuts);
138 if ((ptp = (LPTASKPACKET)lp) == NULL)
139 Services_ShowLog_TakeNextStep (hDlg, lpp);
142 Services_ShowLog_OnEndTask (hDlg, lpp, ptp);
143 FreeTaskPacket (ptp);
152 DestroyWindow (hDlg);
155 case IDC_VIEWLOG_SAVEAS:
156 Services_ShowLog_OnSaveAs (hDlg, lpp);
161 case WM_CTLCOLOREDIT:
162 if ((HWND)lp == GetDlgItem (hDlg, IDC_VIEWLOG_TEXT))
164 SetBkColor ((HDC)wp, GetSysColor (COLOR_WINDOW));
165 return CreateSolidBrush (GetSysColor (COLOR_WINDOW))?TRUE:FALSE;
175 void Services_ShowLog_TakeNextStep (HWND hDlg, LPSVC_VIEWLOG_PACKET lpp)
177 if (lpp->szLocal[0] != TEXT('\0'))
179 Services_ShowLog_OnInitDialog (hDlg, lpp);
180 ShowWindow (hDlg, SW_SHOW);
182 else if (lpp->szRemote[0] != TEXT('\0'))
184 LPSVC_VIEWLOG_PACKET lppNew = New (SVC_VIEWLOG_PACKET);
186 lpp->nDownloadAttempts++;
187 StartTask (taskSVC_VIEWLOG, hDlg, lppNew); // downloads log file
189 else if (lpp->lpiService)
191 StartTask (taskSVC_FINDLOG, hDlg, lpp->lpiService); // guesses log name
195 if (!Services_ShowLog_Pick (hDlg, lpp))
196 DestroyWindow (hDlg);
198 PostMessage (hDlg, WM_ENDTASK, 0, 0); // Services_ShowLog_TakeNextStep
203 void Services_ShowLog_OnEndTask (HWND hDlg, LPSVC_VIEWLOG_PACKET lpp, LPTASKPACKET ptp)
205 if (ptp->idTask == taskSVC_FINDLOG) // tried to guess log file name?
209 lstrcpy (lpp->szRemote, TASKDATA(ptp)->pszText1);
210 PostMessage (hDlg, WM_ENDTASK, 0, 0); // Services_ShowLog_TakeNextStep
214 if (!Services_ShowLog_Pick (hDlg, lpp))
215 DestroyWindow (hDlg);
217 PostMessage (hDlg, WM_ENDTASK, 0, 0); // Services_ShowLog_TakeNextStep
220 else if (ptp->idTask == taskSVC_VIEWLOG) // tried to download log file?
224 lstrcpy (lpp->szLocal, TASKDATA(ptp)->pszText1);
225 PostMessage (hDlg, WM_ENDTASK, 0, 0); // Services_ShowLog_TakeNextStep
229 if (lpp->lpiService && lpp->nDownloadAttempts==1)
231 if (!Services_ShowLog_Pick (hDlg, lpp))
232 DestroyWindow (hDlg);
234 PostMessage (hDlg, WM_ENDTASK, 0, 0); // Services_ShowLog_TakeNextStep
238 TCHAR szServer[ cchNAME ];
239 lpp->lpiServer->GetServerName (szServer);
240 ErrorDialog (ptp->status, IDS_ERROR_VIEW_LOGFILE, TEXT("%s%s"), szServer, lpp->szRemote);
241 DestroyWindow (hDlg);
248 void Services_ShowLog_OnInitDialog (HWND hDlg, LPSVC_VIEWLOG_PACKET lpp)
250 // If we're viewing the log file for a service, record the remote
251 // filename so the user won't ever have to enter it again.
256 if ((lpcp = (LPSERVICE_PREF)lpp->lpiService->GetUserParam()) != NULL)
258 lstrcpy (lpcp->szLogFile, lpp->szRemote);
259 Services_SavePreferences (lpp->lpiService);
263 // Prepare the ShowLog dialog's static text controls
268 TCHAR szServer[ cchNAME ];
269 TCHAR szService[ cchNAME ];
270 lpp->lpiService->GetServerName (szServer);
271 lpp->lpiService->GetServiceName (szService);
272 psz = FormatString (IDS_VIEWLOG_FROMSERVICE, TEXT("%s%s"), szServer, szService);
276 TCHAR szServer[ cchNAME ];
277 lpp->lpiServer->GetServerName (szServer);
278 psz = FormatString (IDS_VIEWLOG_FROMSERVER, TEXT("%s"), szServer);
280 SetDlgItemText (hDlg, IDC_SVC_VIEWLOG_DESC, psz);
283 TCHAR szText[ cchRESOURCE ];
284 GetDlgItemText (hDlg, IDC_SVC_VIEWLOG_FILENAME, szText, cchRESOURCE);
285 psz = FormatString (szText, TEXT("%s"), lpp->szRemote);
286 SetDlgItemText (hDlg, IDC_SVC_VIEWLOG_FILENAME, psz);
289 // Read the log file into memory, and chunk its text into the
290 // edit control on the dialog. If the file is over cchMAXLOG chars
291 // long, only read cchMAXLOG bytes from the end (make sure we start
292 // the read after a carriage return).
294 HANDLE hFile = CreateFile (lpp->szLocal, GENERIC_READ, 0, NULL,
295 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
296 if (hFile != INVALID_HANDLE_VALUE)
298 BOOL fTruncated = FALSE;
299 DWORD dwSize = GetFileSize (hFile, NULL);
300 if (dwSize > cchMAXLOG)
302 // find the first \r\n within the final cchMAXLOG chars of the file
304 SetFilePointer (hFile, 0-cchMAXLOG, NULL, FILE_END);
308 while (ReadFile (hFile, &ch, 1, &cbRead, NULL) && cbRead)
310 if (ch == TEXT('\n'))
314 dwSize -= SetFilePointer (hFile, 0, 0, FILE_CURRENT);
319 if ((pszLog = AllocateString (dwSize)) != NULL)
322 (void)ReadFile (hFile, pszLog, dwSize, &cbRead, NULL);
323 pszLog[ cbRead ] = TEXT('\0');
327 (pszLog[ cch-1 ] == TEXT('\r')) ||
328 (pszLog[ cch-1 ] == TEXT('\n')) )
330 pszLog[ --cch ] = TEXT('\0');
333 SetFocus (GetDlgItem (hDlg, IDC_VIEWLOG_TEXT));
334 SetDlgItemText (hDlg, IDC_VIEWLOG_TEXT, pszLog);
335 SendDlgItemMessage (hDlg, IDC_VIEWLOG_TEXT, EM_LINESCROLL, 0, 0xFFFF);
336 SendDlgItemMessage (hDlg, IDC_VIEWLOG_TEXT, EM_SETSEL, (WPARAM)-1, 0);
341 for (size_t ich = 0; ich < dwSize; ++ich)
343 if (pszLog[ich] == TEXT('\n'))
347 psz = FormatString (IDS_VIEWLOG_TRUNCATED, TEXT("%lu"), nLines);
348 SetDlgItemText (hDlg, IDC_SVC_VIEWLOG_CONTENTS, psz);
360 void Services_ShowLog_OnSaveAs (HWND hDlg, LPSVC_VIEWLOG_PACKET lpp)
362 TCHAR szFilter[ cchRESOURCE ];
363 GetString (szFilter, IDS_SAVELOG_FILTER);
364 TCHAR chFilter = szFilter[ lstrlen(szFilter)-1 ];
365 for (LPTSTR pszFilter = szFilter;
366 (*pszFilter) && ((pszFilter = (LPTSTR)lstrchr (pszFilter, chFilter)) != NULL);
369 *pszFilter = TEXT('\0');
372 TCHAR szSaveAs[ MAX_PATH ] = TEXT("");
375 memset (&sfn, 0x00, sizeof(sfn));
376 sfn.lStructSize = sizeof(sfn);
377 sfn.hwndOwner = hDlg;
378 sfn.hInstance = THIS_HINST;
379 sfn.lpstrFilter = szFilter;
380 sfn.nFilterIndex = 1;
381 sfn.lpstrFile = szSaveAs;
382 sfn.nMaxFile = MAX_PATH;
383 sfn.Flags = OFN_HIDEREADONLY | OFN_NOREADONLYRETURN |
384 OFN_OVERWRITEPROMPT | OFN_PATHMUSTEXIST;
385 sfn.lpstrDefExt = TEXT("txt");
387 if (GetSaveFileName (&sfn))
389 TCHAR szzSource[ MAX_PATH+1 ];
390 lstrcpy (szzSource, lpp->szLocal);
391 szzSource[ lstrlen(szzSource)+1 ] = TEXT('\0');
393 TCHAR szzTarget[ MAX_PATH+1 ];
394 lstrcpy (szzTarget, szSaveAs);
395 szzTarget[ lstrlen(szzTarget)+1 ] = TEXT('\0');
398 memset (&op, 0x00, sizeof(op));
401 op.pFrom = szzSource;
403 op.fFlags = FOF_ALLOWUNDO | FOF_NOCONFIRMATION;
404 SHFileOperation (&op);
409 BOOL Services_ShowLog_Pick (HWND hDlg, LPSVC_VIEWLOG_PACKET lpp)
411 if (ModalDialogParam (IDD_SVC_LOGNAME, NULL, (DLGPROC)Services_PickLog_DlgProc, (LPARAM)lpp) != IDOK)
416 if (lpp->szRemote[0] == TEXT('\0'))
425 BOOL CALLBACK Services_PickLog_DlgProc (HWND hDlg, UINT msg, WPARAM wp, LPARAM lp)
427 if (AfsAppLib_HandleHelp (IDD_SVC_LOGNAME, hDlg, msg, wp, lp))
430 if (msg == WM_INITDIALOG)
431 SetWindowLongPtr (hDlg, DWLP_USER, lp);
433 LPSVC_VIEWLOG_PACKET lpp;
434 if ((lpp = (LPSVC_VIEWLOG_PACKET)GetWindowLongPtr (hDlg, DWLP_USER)) != NULL)
439 Services_PickLog_OnInitDialog (hDlg, lpp);
446 GetDlgItemText (hDlg, IDC_VIEWLOG_FILENAME, lpp->szRemote, MAX_PATH);
448 EndDialog (hDlg, LOWORD(wp));
455 if ((ptp = (LPTASKPACKET)lp) != NULL)
457 if (ptp->idTask == taskSVR_ENUM_TO_COMBOBOX)
458 EnableWindow (GetDlgItem (hDlg, IDC_VIEWLOG_SERVER), TRUE);
459 FreeTaskPacket (ptp);
464 switch (((LPNMHDR)lp)->code)
467 if (IsWindowEnabled (GetDlgItem (hDlg, IDC_VIEWLOG_SERVER)))
470 if ((lpi = (LPIDENT)FL_GetSelectedData (GetDlgItem (hDlg, IDC_VIEWLOG_SERVER))) != NULL)
471 lpp->lpiServer = lpi;
483 void Services_PickLog_OnInitDialog (HWND hDlg, LPSVC_VIEWLOG_PACKET lpp)
485 SetDlgItemText (hDlg, IDC_VIEWLOG_FILENAME, lpp->szRemote);
486 EnableWindow (GetDlgItem (hDlg, IDC_VIEWLOG_SERVER), FALSE);
490 TCHAR szServer[ cchNAME ];
491 TCHAR szService[ cchNAME ];
492 lpp->lpiService->GetServerName (szServer);
493 lpp->lpiService->GetServiceName (szService);
495 LPTSTR pszText = FormatString (IDS_VIEWLOG_DESC_NOFILE, TEXT("%s%s"), szServer, szService);
496 SetDlgItemText (hDlg, IDC_VIEWLOG_DESC, pszText);
497 FreeString (pszText);
499 CB_StartChange (GetDlgItem (hDlg, IDC_VIEWLOG_SERVER), TRUE);
500 CB_AddItem (GetDlgItem (hDlg, IDC_VIEWLOG_SERVER), szServer, (LPARAM)(lpp->lpiServer));
501 CB_EndChange (GetDlgItem (hDlg, IDC_VIEWLOG_SERVER), (LPARAM)(lpp->lpiServer));
505 LPSVR_ENUM_TO_COMBOBOX_PACKET lpEnum = New (SVR_ENUM_TO_COMBOBOX_PACKET);
506 lpEnum->hCombo = GetDlgItem (hDlg, IDC_VIEWLOG_SERVER);
507 lpEnum->lpiSelect = lpp->lpiServer;
508 StartTask (taskSVR_ENUM_TO_COMBOBOX, hDlg, lpEnum);
511 PostMessage (hDlg, WM_NEXTDLGCTL, (WPARAM)GetDlgItem (hDlg, IDC_VIEWLOG_FILENAME), TRUE);