1 /*	$NetBSD: printf.c,v 1.3 2014/12/10 04:37:56 christos Exp $	*/
2 
3 /*
4  * printf.c - printf like debug print function
5  */
6 
7 /*
8  * Copyright (c) 2000,2002 Japan Network Information Center.
9  * All rights reserved.
10  *
11  * By using this file, you agree to the terms and conditions set forth bellow.
12  *
13  * 			LICENSE TERMS AND CONDITIONS
14  *
15  * The following License Terms and Conditions apply, unless a different
16  * license is obtained from Japan Network Information Center ("JPNIC"),
17  * a Japanese association, Kokusai-Kougyou-Kanda Bldg 6F, 2-3-4 Uchi-Kanda,
18  * Chiyoda-ku, Tokyo 101-0047, Japan.
19  *
20  * 1. Use, Modification and Redistribution (including distribution of any
21  *    modified or derived work) in source and/or binary forms is permitted
22  *    under this License Terms and Conditions.
23  *
24  * 2. Redistribution of source code must retain the copyright notices as they
25  *    appear in each source code file, this License Terms and Conditions.
26  *
27  * 3. Redistribution in binary form must reproduce the Copyright Notice,
28  *    this License Terms and Conditions, in the documentation and/or other
29  *    materials provided with the distribution.  For the purposes of binary
30  *    distribution the "Copyright Notice" refers to the following language:
31  *    "Copyright (c) 2000-2002 Japan Network Information Center.  All rights reserved."
32  *
33  * 4. The name of JPNIC may not be used to endorse or promote products
34  *    derived from this Software without specific prior written approval of
35  *    JPNIC.
36  *
37  * 5. Disclaimer/Limitation of Liability: THIS SOFTWARE IS PROVIDED BY JPNIC
38  *    "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
39  *    LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
40  *    PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL JPNIC BE LIABLE
41  *    FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
42  *    CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
43  *    SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
44  *    BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
45  *    WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
46  *    OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
47  *    ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
48  */
49 
50 #include <windows.h>
51 #include <stdio.h>
52 #include <stdlib.h>
53 #include <string.h>
54 #include <process.h>
55 
56 #include "wrapcommon.h"
57 
58 /*
59  * Debug Tracer for DLL
60  */
61 
62 static char	logfile_name[256];
63 static int	log_level = -1;
64 static char	log_header[30];
65 
66 void
67 idnPrintf(char *fmt, ...) {
68 	va_list arg_ptr;
69 	FILE *fp;
70 	char msg[512];
71 
72 	if (log_level < 0 || logfile_name[0] == '\0')
73 		return;
74 
75 	va_start(arg_ptr, fmt);
76 	vsprintf(msg, fmt, arg_ptr);
77 	va_end(arg_ptr);
78 
79 	if ((fp = fopen(logfile_name, "a")) != NULL) {
80 		fputs(log_header, fp);
81 		fputs(msg, fp);
82 		fclose(fp);
83 	}
84 }
85 
86 void
87 idnLogPrintf(int level, char *fmt, ...) {
88 	va_list arg_ptr;
89 	FILE *fp;
90 	char msg[512];
91 
92 	if (level > log_level || logfile_name[0] == '\0')
93 		return;
94 
95 	va_start(arg_ptr, fmt);
96 	vsprintf(msg, fmt, arg_ptr);
97 	va_end(arg_ptr);
98 
99 	if ((fp = fopen(logfile_name, "a")) != NULL) {
100 		fputs(log_header, fp);
101 		fputs(msg, fp);
102 		fclose(fp);
103 	}
104 }
105 
106 static void
107 log_proc(int level, const char *msg) {
108 	FILE *fp;
109 
110 	if (log_level < 0 || logfile_name[0] == '\0')
111 		return;
112 
113 	if ((fp = fopen(logfile_name, "a")) != NULL) {
114 		fputs(msg, fp);
115 		fclose(fp);
116 	}
117 }
118 
119 void
120 idnLogInit(const char *title) {
121 	log_level = idnGetLogLevel();
122 	/* If log file is not stored in the registry, don't do logging. */
123 	if (idnGetLogFile(logfile_name, sizeof(logfile_name)) == FALSE) {
124 		log_level = -1;
125 	}
126 	sprintf(log_header, "%08x %-.16s: ", getpid(), title);
127 	idn_log_setproc(log_proc);
128 	idn_log_setlevel(log_level < 0 ? 0 : log_level);
129 }
130 
131 void
132 idnLogReset(void) {
133 	idn_log_setproc(log_proc);
134 }
135 
136 void
137 idnLogFinish(void) {
138 	idn_log_setproc(NULL);
139 	/* idn_log_setlevel(0); */
140 }
141