1 /*********************************************************/
2 /* TAUCS */
3 /* Author: Sivan Toledo */
4 /*********************************************************/
5
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <string.h>
9 #include <float.h>
10 #include <math.h>
11 #include <stdarg.h>
12
13 #include "taucs.h"
14
15 #ifndef OSTYPE_win32
16 #include <unistd.h>
17 #endif
18
19 /*********************************************************/
20 /* logging */
21 /*********************************************************/
22
23 #define LOG_NONE 0
24 #define LOG_STDERR 1
25 #define LOG_STDOUT 2
26 #define LOG_FILE 3
27
28 static char log_file_prefix[256];
29 static int log_file_type = LOG_NONE;
30 static int first_time = 0;
31
32 void
taucs_logfile(char * file_prefix)33 taucs_logfile(char* file_prefix)
34 {
35 if (!strcmp(file_prefix,"stderr")) {
36 log_file_type = LOG_STDERR;
37 } else if (!strcmp(file_prefix,"stdout")) {
38 log_file_type = LOG_STDOUT;
39 } else if (!strcmp(file_prefix,"none")) {
40 log_file_type = LOG_NONE;
41 } else {
42 strcpy(log_file_prefix,file_prefix);
43 log_file_type = LOG_FILE;
44 first_time = 1;
45 }
46 }
47
48 int
taucs_printf(char * fmt,...)49 taucs_printf(char *fmt, ...)
50 {
51 static FILE* logf;
52 va_list ap;
53
54 if (log_file_type == LOG_NONE) return 0;
55
56 if (first_time && log_file_type == LOG_FILE) {
57 char filename[256];
58
59 sprintf(filename,"%s",log_file_prefix);
60
61 if ((logf = fopen(filename,"w")) == NULL) {
62 fprintf(stderr,"could not open log file %s, exiting\n",filename);
63 exit(1);
64 }
65 first_time = 0;
66 }
67
68 if (log_file_type == LOG_STDERR) logf = stderr;
69 if (log_file_type == LOG_STDOUT) logf = stdout;
70
71 va_start(ap, fmt);
72
73 vfprintf(logf, fmt, ap);
74
75 fflush(logf);
76
77 va_end(ap);
78
79 return 0;
80 }
81
82 /*********************************************************/
83 /* */
84 /*********************************************************/
85