1 //-----------------------------------------------------------------------------
2 // LogFile
3 //-----------------------------------------------------------------------------
4 
5 #include "logfile.h"
6 #include "cake.h"
7 #include "overlay.h"        // for colors
8 #include "system.h"
9 
10 #ifdef WIN32
11   #include <direct.h>         // for _getcwd
12   #include <tchar.h>          // for _vsntprintf
13   #include <stdarg.h>         // for va_start, va_end
14 #else
15   #include <stdio.h>          // for vsnprintf
16   #include <stdlib.h>
17   #include <stdarg.h>         // for va_start, va_end
18   #include <unistd.h>         // for getcwd
19   #define _getcwd getcwd
20   #define _vsntprintf vsnprintf
21 #endif
22 
23 #define LOG_LINELENGTH  32768     // Max line length for a line in logfile (FIXME: is that too long ??)
24 
LogFile(void)25 LogFile::LogFile(void)
26 {
27   // Get output filename
28   _getcwd(outputfile, PATH_LENGTH);
29   strcat(outputfile, "/");
30   strcat(outputfile, OUTPUT_FILENAME);
31 
32   // Initialize logile
33   if ((logfile = fopen(outputfile, "w")))
34   {
35     fprintf(logfile, "<html>\n<head>\n");
36     fprintf(logfile, "\t<title>cake logfile</title>\n");
37     fprintf(logfile, "</head>\n<body text=\"#FFFFFF\" bgcolor=\"#000000\">\n");
38     fprintf(logfile, "\t<tt><font size=\"3\">\n");
39     fclose(logfile);
40   }
41   // FIXME: Should I throw an exception ? The problem is that cake won't start
42   // directly from CD or readonly media.
43   else printf("ERROR: couldn't create logfile !");
44 
45   logfile = NULL;
46   startNewLine = true;
47 }
48 
~LogFile(void)49 LogFile::~LogFile(void)
50 {
51   if ((logfile = fopen(outputfile, "at")))
52   {
53     fprintf(logfile, "\t</font></tt>\n");
54     fprintf(logfile, "\t<hr><a href=\"http://www.calodox.org/morbac/cake\">cake homepage</a>\n");
55     fprintf(logfile, "</body>\n");
56     fclose(logfile);
57   }
58   logfile = NULL;
59 }
60 
Insert(const char * s,...)61 void LogFile::Insert(const char* s, ...)
62 {
63   bool alreadyOpened = logfile?true:false;
64   if (!logfile) logfile = fopen(outputfile, "at");
65   if (!logfile) return;
66 
67   va_list msg;
68   char buffer[LOG_LINELENGTH] = { '\0' };
69   char output[LOG_LINELENGTH] = { '\0' };
70 
71   va_start(msg, s);
72   _vsntprintf(buffer, LOG_LINELENGTH - 1, s, msg);
73   va_end(msg);
74 
75   int l = (int) strlen(buffer);
76   for (int i = 0, j = (int) strlen(output); i < l; ++i)
77   {
78     if (startNewLine)
79     {
80       strcat(output, "\t<font color=\"#FFFFFF\">");
81       j = (int) strlen(output);
82       startNewLine = false;
83     }
84 
85     if (buffer[i] == '\n')
86     {
87       strcat(output, " </font><br>\n");
88       startNewLine = true;
89       j = (int) strlen(output);
90     }
91     else if (buffer[i] == '^')
92     {
93       ++i;
94       if (i >= l) break;
95       char str_color[8] = { '\0' };
96       if (buffer[i] <= '0' || buffer[i] >= '7') sprintf(str_color, "#FFFFFF");
97       else sprintf(str_color, "#%2x%2x%2x", text_colors[buffer[i]-'0'][0], text_colors[buffer[i]-'0'][1], text_colors[buffer[i]-'0'][2]);
98       strcat(output, "</font><font color=\"");
99       strcat(output, str_color);
100       strcat(output, "\">");
101       j = (int) strlen(output);
102     }
103     else if (buffer[i] == '\t')
104     {
105       for (int k = 0; k < TAB_LEN; ++k) strcat(output, "&nbsp;");
106       j = (int) strlen(output);
107     }
108     else output[j++] = buffer[i];
109   }
110 
111   // Adding the line to logfile
112   fprintf(logfile, "%s", output);
113   if (!alreadyOpened)
114   {
115     fclose(logfile);
116     logfile = NULL;
117   }
118 }
119 
OpenFile(void)120 FILE* LogFile::OpenFile(void)
121 {
122   if (logfile) return logfile;
123   logfile = fopen(outputfile, "at");
124   if (!logfile) ThrowException(ERROR_OPENING_FILE, "LogFile::OpenFile.logfile");
125   return logfile;
126 }
127 
CloseFile(void)128 int LogFile::CloseFile(void)
129 {
130   if (!logfile) return 0;
131   int ret = fclose(logfile);
132   logfile = NULL;
133   return ret;
134 }
135