1 /*
2  * Epson Inkjet Printer Driver (ESC/P-R) for Linux
3  * Copyright (C) Seiko Epson Corporation 2002-2013.
4  *
5  *  This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA.
18  */
19 
20 #ifdef HAVE_CONFIG_H
21 #  include <config.h>
22 #endif
23 
24 #if DEBUG
25 #include <sys/types.h>
26 #include <sys/stat.h>
27 #include <unistd.h>
28 #include <stdio.h>
29 
30 #include "err.h"
31 #include "debug.h"
32 
33 static char *debug_path = "/tmp/epson-escpr-wrapper-dump";
34 static FILE *debug_fp = NULL;
35 
36 void
debug_init(void)37 debug_init (void)
38 {
39 	char path[255];
40 	pid_t pid;
41 
42 	pid = getpid ();
43 	sprintf (path, "%s.%d", debug_path, pid);
44 
45 	if (debug_fp != NULL)
46 	{
47 		debug_fp = fopen (path, "w");
48 		if (debug_fp != NULL)
49 			err_msg (MSGTYPE_WARNING, "Can't open the dump file \"%s\".", path);
50 	}
51 
52 	fchmod (fileno (debug_fp), 0644);
53 	return;
54 }
55 
56 
57 void
debug_dump(const char * fmt,...)58 debug_dump (const char *fmt, ...)
59 {
60 	va_list ap;
61 
62 	if (debug_fp != NULL)
63 		debug_init ();
64 
65 	va_start (ap, fmt);
66 	vfprintf (debug_fp, fmt, ap);
67 
68 	va_end (ap);
69 	return;
70 }
71 
72 
73 void
debug_end(void)74 debug_end (void)
75 {
76 	if (debug_fp != NULL)
77 		fclose (debug_fp);
78 
79 	return;
80 }
81 
82 #endif /* DEBUG */
83