1 1.6 cegger /* $NetBSD: print.c,v 1.6 2009/03/18 10:22:29 cegger Exp $ */ 2 1.3 takemura 3 1.3 takemura /*- 4 1.3 takemura * Copyright (c) 1999 Shin Takemura. 5 1.3 takemura * All rights reserved. 6 1.3 takemura * 7 1.3 takemura * This software is part of the PocketBSD. 8 1.3 takemura * 9 1.3 takemura * Redistribution and use in source and binary forms, with or without 10 1.3 takemura * modification, are permitted provided that the following conditions 11 1.3 takemura * are met: 12 1.3 takemura * 1. Redistributions of source code must retain the above copyright 13 1.3 takemura * notice, this list of conditions and the following disclaimer. 14 1.3 takemura * 2. Redistributions in binary form must reproduce the above copyright 15 1.3 takemura * notice, this list of conditions and the following disclaimer in the 16 1.3 takemura * documentation and/or other materials provided with the distribution. 17 1.3 takemura * 3. All advertising materials mentioning features or use of this software 18 1.3 takemura * must display the following acknowledgement: 19 1.3 takemura * This product includes software developed by the PocketBSD project 20 1.3 takemura * and its contributors. 21 1.3 takemura * 4. Neither the name of the project nor the names of its contributors 22 1.3 takemura * may be used to endorse or promote products derived from this software 23 1.3 takemura * without specific prior written permission. 24 1.3 takemura * 25 1.3 takemura * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 26 1.3 takemura * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 27 1.3 takemura * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 28 1.3 takemura * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 29 1.3 takemura * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 30 1.3 takemura * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 31 1.3 takemura * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 32 1.3 takemura * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 33 1.3 takemura * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 34 1.3 takemura * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 35 1.3 takemura * SUCH DAMAGE. 36 1.3 takemura * 37 1.3 takemura */ 38 1.3 takemura #include <pbsdboot.h> 39 1.4 takemura #include <res/resource.h> /* for IDC_STATUS, status area ID */ 40 1.4 takemura 41 1.4 takemura static HANDLE debug_log = INVALID_HANDLE_VALUE; 42 1.3 takemura 43 1.3 takemura int 44 1.3 takemura debug_printf(LPWSTR lpszFmt, ...) 45 1.3 takemura { 46 1.3 takemura int count; 47 1.3 takemura va_list ap; 48 1.3 takemura wchar_t buffer[1024]; 49 1.4 takemura char ascbuf[2048]; 50 1.3 takemura 51 1.3 takemura va_start(ap, lpszFmt); 52 1.3 takemura count = wvsprintf(buffer, lpszFmt, ap); 53 1.3 takemura va_end(ap); 54 1.3 takemura if (count > 0) { 55 1.4 takemura DWORD n; 56 1.3 takemura OutputDebugStringW(buffer); 57 1.4 takemura 58 1.4 takemura if (debug_log != INVALID_HANDLE_VALUE) { 59 1.4 takemura /* convert wide char string into multibyte 60 1.4 takemura string (ascii) */ 61 1.4 takemura n = wcstombs(ascbuf, buffer, sizeof(ascbuf)); 62 1.4 takemura /* convert into DOS style new line code */ 63 1.4 takemura if (ascbuf[n - 1] == 0x0a) { 64 1.4 takemura ascbuf[n - 1] = 0x0d; 65 1.4 takemura ascbuf[n + 0] = 0x0a; 66 1.4 takemura ascbuf[n + 1] = 0x00; 67 1.4 takemura n += 1; 68 1.4 takemura } 69 1.4 takemura WriteFile(debug_log, ascbuf, n, &n, NULL); 70 1.4 takemura } 71 1.3 takemura } 72 1.3 takemura return count; 73 1.3 takemura } 74 1.3 takemura 75 1.3 takemura int 76 1.3 takemura msg_printf(UINT type, LPWSTR caption, LPWSTR lpszFmt, ...) 77 1.3 takemura { 78 1.3 takemura int count; 79 1.3 takemura va_list ap; 80 1.3 takemura TCHAR buffer[1024]; 81 1.3 takemura 82 1.3 takemura va_start(ap, lpszFmt); 83 1.3 takemura count = wvsprintf(buffer, lpszFmt, ap); 84 1.3 takemura va_end(ap); 85 1.5 takemura return MessageBox(hDlgMain, buffer, caption, type); 86 1.4 takemura } 87 1.4 takemura 88 1.4 takemura int 89 1.4 takemura stat_printf(LPWSTR lpszFmt, ...) 90 1.4 takemura { 91 1.4 takemura int count; 92 1.4 takemura va_list ap; 93 1.4 takemura wchar_t buffer[1024]; 94 1.4 takemura 95 1.4 takemura va_start(ap, lpszFmt); 96 1.4 takemura count = wvsprintf(buffer, lpszFmt, ap); 97 1.4 takemura va_end(ap); 98 1.4 takemura if (count > 0) { 99 1.5 takemura SetDlgItemText(hDlgMain, IDC_STATUS, buffer); 100 1.4 takemura } 101 1.4 takemura return count; 102 1.4 takemura } 103 1.4 takemura 104 1.4 takemura int 105 1.4 takemura set_debug_log(TCHAR* filename) 106 1.4 takemura { 107 1.4 takemura 108 1.4 takemura /* 109 1.4 takemura * Logging into file is dangerous. It may cause file system clash, 110 1.4 takemura * because it try to write file until the last moment to boot and 111 1.4 takemura * Windows can't flush file cache properly. 112 1.4 takemura * And therefore the logging will be disanable unless you put a 113 1.4 takemura * dummy log file on a directory. 114 1.4 takemura */ 115 1.4 takemura debug_log = CreateFile( 116 1.4 takemura filename, /* file name */ 117 1.4 takemura GENERIC_READ, /* access (read-write) mode */ 118 1.4 takemura FILE_SHARE_READ,/* share mode */ 119 1.4 takemura NULL, /* pointer to security attributes */ 120 1.4 takemura OPEN_EXISTING, /* how to create */ 121 1.4 takemura FILE_ATTRIBUTE_NORMAL, /* file attributes*/ 122 1.4 takemura NULL /* handle to file with attributes to */ 123 1.4 takemura ); 124 1.4 takemura if (debug_log == INVALID_HANDLE_VALUE) { 125 1.4 takemura return (-1); 126 1.4 takemura } 127 1.4 takemura CloseHandle(debug_log); 128 1.4 takemura 129 1.4 takemura debug_log = CreateFile( 130 1.4 takemura filename, /* file name */ 131 1.4 takemura GENERIC_WRITE, /* access (read-write) mode */ 132 1.4 takemura FILE_SHARE_WRITE,/* share mode */ 133 1.4 takemura NULL, /* pointer to security attributes */ 134 1.4 takemura CREATE_ALWAYS, /* how to create */ 135 1.4 takemura FILE_ATTRIBUTE_NORMAL, /* file attributes*/ 136 1.4 takemura NULL /* handle to file with attributes to */ 137 1.4 takemura ); 138 1.4 takemura 139 1.4 takemura if (debug_log == INVALID_HANDLE_VALUE) { 140 1.4 takemura return (-1); 141 1.4 takemura } 142 1.4 takemura 143 1.4 takemura return (0); 144 1.4 takemura } 145 1.4 takemura 146 1.4 takemura void 147 1.6 cegger close_debug_log(void) 148 1.4 takemura { 149 1.4 takemura if (debug_log != INVALID_HANDLE_VALUE) { 150 1.4 takemura CloseHandle(debug_log); 151 1.4 takemura debug_log = INVALID_HANDLE_VALUE; 152 1.4 takemura /* 153 1.4 takemura * I hope Windows flush file buffer properly... 154 1.4 takemura */ 155 1.4 takemura Sleep(2000); 156 1.4 takemura } 157 1.3 takemura } 158